Reputation: 1586
I am using dompdf library to convert my php page to pdf, below is the code
<?php
require_once 'dompdf/autoload.inc.php';
// reference the Dompdf namespace
use Dompdf\Dompdf;
if (isset($_GET['action']) && $_GET['action'] == 'download') {
// instantiate and use the dompdf class
$dompdf = new Dompdf();
//to put other html file
$html = file_get_contents('challan-form.php');
//hide download pdf link in generated output pdf
$html .= '<style type="text/css">.hideforpdf { display: none; }</style>';
//load html
$dompdf->loadHtml($html);
// (Optional) Setup the paper size and orientation
$dompdf->setPaper('Legal', 'Landscape');
// Render the HTML as PDF
$dompdf->render();
// Output the generated PDF (1 = download and 0 = preview)
$dompdf->stream("sample",array("Attachment"=>0));
}
?>
When I see the php file in browser it is showing the echoed php variables properly, but when I generate pdf from that php file, php code is coming in pdf file. How to make it work?
I am getting data from my postgresql table and displaying it in php
My example php code below
<div style="display:block; width:98%; padding: 2px 0 0 5px; font-size:10px;">
<span style="width:25%; display:inline-block;">Employee ID</span>
<Span style="width:65%; display:inline-block;" class="bold-text"><?php echo $row['empcode'];?></SPan>
</div>
<div style="display:block; width:98%; padding: 2px 0 0 5px; font-size:10px;">
<span style="width:25%; display:inline-block;">PRAN No</span>
<Span style="width:65%;" class="bold-text dInline"><?php echo $row['pranno'];?></SPan>
</div>
Update : I have used dompdf 0.6 and enabled inline php also, still not working
Upvotes: 0
Views: 2209
Reputation: 156
I know this is an old question but anyhow. Have just figured this out myself: The trick is simply to render your html as a string in php, concatenated with the values you wish to display in your PDF. You have to be careful with quotes, as there will be double quotes in your markup. So it helps to use single quotes as wrappers if you know what I mean.
So something like:
$html = '<div class = "some class" >' . $variable . '</div>';
You can then load this string into your $dompdf instance. $variable could be pulling it's value from anywhere, a database for example, or you could be iterating through a loop to build elements of your layout.
Upvotes: 1