Wkhtmltopdf conversion - php

My html to pdf conversion using wkhtmlpdf working fine without the <style> tag
but when I include the style tag it's not working that means not generating the pdf some one help me to solve this issue or let me know how to include my style.css in this format.

The way I handle this is as follows--
<?php
// create some HTML content
$html = '<!DOCTYPE html><html><head>
<style>
body {
margin: 0;
padding: 0;
}
...
</style></head><body>
<!-- Your body content -->
</body></html>';
$options = array(
"no-outline",
"binary" => "/usr/local/bin/wkhtmltopdf",
"margin-top" => ".25in",
"margin-right" => ".25in",
"margin-bottom" => ".55in",
"margin-left" => ".25in",
"zoom" => 1
);
$footer = '<!DOCTYPE html><html><head></head><body>...</body></html>';
$page_options = array('footer-html' => $footer);
$pdf->addPage($html, $page_options);
$pdf->saveAs('MY_PDF_NAME.pdf');
Hope this helps!
Best,
-Rush

Related

dompdf local language(gujarati) text not display properly

I am using DOMPDF version 2
i want to print Gujarati text in my pdf and gujarati font is displayed but it changed some text like "ટેસ્ટ ડેટા ચેક" is changed in pdf. not all character is changed but some merraged and special character is changed. I had tried multiple different gujarati fonts but same thing is happen in all.
<?php
use Dompdf\Dompdf;
require 'vendor/autoload.php';
$html = '
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<style>
#font-face {
font-family: "notoSerifGujarati";
src:url(fonts/NotoSerifGujarati-VariableFont_wght.ttf)
}
.m {
font-family: "notoSerifGujarati";
}
</style>
</head>
<body>
<p class="m">
ટેસ્ટ ડેટા ચેક
</p>
</body>
</html>';
$tmp = sys_get_temp_dir();
$dompdf = new Dompdf([
'logOutputFile' => '',
// authorize DomPdf to download fonts and other Internet assets
'isRemoteEnabled' => true,
// all directories must exist and not end with /
'fontDir' => $tmp,
'fontCache' => $tmp,
'tempDir' => $tmp,
'chroot' => $tmp,
]);
$dompdf->loadHtml($html); //load an html
$dompdf->render();
$dompdf->stream('hello.pdf', [
'compress' => true,
'Attachment' => false,
]);
how text displayed in pdf

Google Unicode Fonts not rendered properly in mpdf for PHP

I'm trying to use Baloo2 Google Font in mpdf to render Hindi Language.
I'm using the Latest Version.
The PDF is being Rendered but the complex scripts are not being rendered properly.
I'm trying to render the following,
चातुर्यकला त्रिवेदी But it is rendered asचातुर् यकला त् रविदी
Although when inspected in browser, the rendering is proper in inspect source code. but not displayed properly in the PDF.
The Code in my file is
$html = ' आवेदक का नाम : चातुर्यकला त्रिवेदी';
$filename = 'demofile' . time() . '.pdf';
$mpdf = new \Mpdf\Mpdf([
'mode' => 'utf-8',
'default_font' => 'baloo',
]);
$mpdf->debug = true;
$mpdf->WriteHTML("p, td {font-family: baloo; color: transparent; white-space: pre; cursor: text; transform-origin: 0% 0%;}", 1);
$mpdf->WriteHTML($html);
$mpdf->Output($filename, 'D');
I've also copied the fonts in the ttfonts directory and also made changes to the fontVariables.php files as follows
'fontdata' => [
"baloo" => [
'R' => "BalooRegular.ttf",
'useOTL' => 0xFF,
'useKashida' => 75,
]...
];
When done in this format, an error is thrown
GPOS Lookup Type 5, Format 3 not supported (ttfontsuni.php)
Pls. let me know what I'm doing wrong...

pdf document - setting the window title (html header)

I'm using this to output a pdf document :
$response = new \Symfony\Component\HttpFoundation\Response(file_get_contents($thefile), 200, [
'Content-Description' => 'File transfer',
'Content-Disposition' => 'filename="' . $filename . '"',
'Content-Transfer-Encoding' => 'binary',
'Content-Type' => 'application/pdf',
]);
unlink($thefile);
$response->send();
In the generated page, the header title is the link to the page.
How can I customize the title ?
You have to include the title inside the pdf.
I just have to generate the pdf from a script as src of an iframe !
This way the head title is kept !
<head>
<title>THE TITLE</title>
</head>
<body>
<iframe src="process.php"></iframe>
</body>
</html>
Or I could ajax the php script, and return the result in a container element (eg div)

Wkhtmltopdf pagination without header or footer

I'm using this php wkhtmltopdf library to generate pdf's. I'm not using the most recent version yet, but there doesn't seem to be any improvements to this problem.
Using a full page background-image, I run into the following problems:
Footer (and header) overlap pages (white background)
Cannot show pagination without header/footer (footer pagination won't work on pages)
Questions:
Is there a way to make the footer background transparent?
Is there a way to show pagination without header/footer? (page/pages format)
Screenshot:
PHP:
$pdf_options = array(
'binPath' => 'wkhtmltopdf-amd64',
'encoding' => 'UTF-8',
'dpi' => 96,
'margin-top' => '0mm',
'margin-right' => '0mm',
'margin-bottom' => '0mm',
'margin-left' => '0mm',
'footer-html' => $tmpDir.'/footer.html',
'header-spacing' => 0,
'footer-spacing' => -20,
'no-outline'
);
$page_options = array(
'disable-smart-shrinking'
);
$pdf = new WkHtmlToPdf($pdf_options);
$pdf->setPageOptions($page_options);
$pdf->addPage($tmpDir.'/body.html');
$renderPdf = $pdf->send();
Footer pagination:
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<script>
function pagination()
{
var vars = {};
var x = document.location.search.substring(1).split('&');
for (var i in x)
{
var z = x[i].split('=', 2);
vars[z[0]] = unescape(z[1]);
}
var x = ['frompage','topage','page','webpage','section','subsection','subsubsection'];
for (var i in x)
{
var y = document.getElementsByClassName(x[i]);
for (var j = 0; j < y.length; ++j)
{
y[j].textContent = vars[x[i]];
}
}
}
</script>
</head>
<body onload="pagination()">
<div id="pagination">
<span class="page"></span>/<span class="topage"></span>
</div>
</body>
</html>

Formatting tables generated with PHP/PEAR Structures_DataGrid & HTML_Table

I've been working through some examples in a book using pear for the first time... so far it seems pretty cool, but I'm having a small problem where I need to tweak the formatting of one particular column of the table generated from Structures_DataGrid & HTML_Table - specifically I need it to not wrap the text in a cell at the hyphen - which I think I could do using 'nowrap' and html/css - but so far I'm not seeing how to pass that sort of formatting to just one column in the table...
Any suggestions?
Added source code below:
<?php
// Include the DB access credentials
require 'dbcred.php';
// Include the PEAR Structures_DataGrid class
require 'Structures/DataGrid.php';
// instantiate grid for 10 records per page
$datagrid = new Structures_DataGrid(10);
// Define our Datasource options, in this case PDO MySQL
$options = array('dsn' => "mysql://$user:$password#$db_host/$db_name");
// Define the Query
$sql = "SELECT * FROM contact_info";
// Bind the Query to our Datagrid
$bind = $datagrid->bind($sql, $options);
// Test for Errors
if (PEAR::isError($bind))
{
/*error_log('DataGrid Error: '. $bind->getMessage());
$gridsource = 'Foo';*/
echo $bind -> getMessage();
}
else
{
// Define our Column labels, using a 'column' => 'Label' format
$columns = array(
'title' => 'Title',
'first_name' => 'First Name',
'mid_init' => 'Mid. Init.',
'last_name' => 'Last Name',
'suffix' => 'Suffix',
'street_address' => 'Street Address',
'city' => 'City',
'state_prov' => 'State/Province',
'post_code' => 'Postal Code',
'phone_pri' => 'Phone (Pri)',
'phone_alt' => 'Phone (Alt)',
'email' => 'E-Mail',
);
$datagrid->generateColumns($columns);
// Some more options, for our renderer
$renderer_options = array(
'sortIconASC' => '⇑',
'sortIconDESC' => '⇓',
'headerAttributes' => array('bgcolor' => '#E3E3E3'),
'evenRowAttributes' => array('bgcolor' => '#A6A6A6'),
);
$datagrid->setRendererOptions($renderer_options);
// Add some final attributes to our table
$renderer = $datagrid->getRenderer();
$renderer->setTableAttribute('cellspacing', 0);
$renderer->setTableAttribute('cellpadding', 5);
$renderer->setTableAttribute('border', 1);
// Render the table, be sure to check for errors
$gridbody = $datagrid->getOutput();
if (PEAR::isError($gridbody))
{
error_log('DataGrid render error: ' . $gridbody->getMessage());
$gridbody = '';
}
// Finally, render the pager, again checking for errors
$gridpager = $datagrid->getOutput(DATAGRID_RENDER_PAGER);
if (PEAR::isError($gridpager))
{
error_log('DataGrid render error: ' . $gridpager->getMessage());
$gridpager = '';
}
$gridsource = $gridbody . $gridpager;
}
?>
<!DOCTYPE html public "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>PEAR::Structures_DataGrid</title>
<meta http-equiv="Content-type"
content="text/html; charset=iso-8859-1" />
<style type="text/css">
body {
font-family: Tahoma, Arial, Helvetica, sans-serif;
font-size: 11px;
}
h1 {
font-size: 1.2em;
color: navy
}
th {
white-space: no-wrap;
}
</style>
</head>
<body>
<h1>PEAR::Structures_DataGrid</h1>
<?php echo $gridsource ?>
</body>
</html>
Don't know what pear is but:
You can format columns like this in html:
<table>
<col style="background: green;" />
<col />
<tr>
<td>First TD of first TR</td>
<td>Second TD of first TR</td>
</tr>
<tr>
<td>First TD of second TR</td>
<td>Second TD of second TR</td>
</tr>
</table>
That makes the background of the first column green.

Categories