Search code examples
phppdfhtml2pdf

php to pdf using dompdf


I have following codes for start.

<?
require('html2fpdf.php');
$pdf=new HTML2FPDF();
$pdf->AddPage();
$fp = fopen("en/print-job.php?ID=12","r");
$strContent = fread($fp, filesize("en/print-job.php?ID=12"));
fclose($fp);
$pdf->WriteHTML($strContent);
$pdf->Output("home.pdf");
echo "PDF file is generated successfully!";
?>

I have a page called print-pdf.php which is built on bootstrap & output is something like this:

https://www.lotomanager.in/en/print-job.php?ID=12

so how can this page be converted as it is to pdf?

I get following result from above codes:

Warning: fopen(en/print-job.php?ID=12): failed to open stream: No such file or directory in /home/loto/public_html/pdf.php on line 5

Warning: filesize(): stat failed for en/print-job.php?ID=12 in /home/loto/public_html/pdf.php on line 6

Warning: fread() expects parameter 1 to be resource, boolean given in /home/loto/public_html/pdf.php on line 6

Warning: fclose() expects parameter 1 to be resource, boolean given in /home/loto/public_html/pdf.php on line 7 PDF file is generated successfully!


Solution

  • Try this

    <?php
    require_once 'dompdf/autoload.inc.php';
    ?>
    <?php
    
    // reference the Dompdf namespace
    
    use Dompdf\Dompdf;
    
    // instantiate and use the dompdf class
    $html = file_get_contents('http://www.lotomanager.in/en/print-job.php?ID=12');
    $dompdf = new Dompdf();
    $dompdf->loadHtml($html);
    
     // (Optional) Setup the paper size and orientation
    $dompdf->setPaper('A4', 'landscape');
    
    // Render the HTML as PDF
    $dompdf->render();
    
    // Output the generated PDF to Browser
    $dompdf->stream();
    ?>