user6230357
user6230357

Reputation:

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!

Upvotes: 1

Views: 3691

Answers (4)

johannes
johannes

Reputation: 15969

You do this by refactoring print-job.php in a way that you get some function generating the HTML which can either print the HTML to the user or feed it into the PDF library.

The most simple way might be

ob_start();
include('print-job.php');
$html = ob_end_clean();

But this can lead to trouble with different global variables or something in both files and you have to be careful for future changes to pront-job.php. So as said better clean up the code in there not to print directly.

An even better approach would be not using dompdf + html at all, but create PDF specifically with a proper layout. This is more work but should give a way cleaner result.

Upvotes: 0

beingalex
beingalex

Reputation: 2476

DOMPDF has a load_html_file method that you can use. I have also cast the $_GET as an integer for security.

<?php
require_once 'dompdf/autoload.inc.php';
?>
<?php

// reference the Dompdf namespace

use Dompdf\Dompdf;

// instantiate and use the dompdf class
$dompdf = new Dompdf();
$dompdf->load_html_file('https://www.lotomanager.in/en/print-job.php?ID='.(int)$_GET['ID']);

// (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();
?>

Upvotes: 0

Sukhwinder Sodhi
Sukhwinder Sodhi

Reputation: 475

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();
?>

Upvotes: 1

beingalex
beingalex

Reputation: 2476

Not sure what you mean. If you meant to display the ID in the document:

$dompdf->loadHtml('hello world ' . $_GET['ID'] );

Upvotes: 0

Related Questions