1

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!

Bidou
  • 7,378
  • 9
  • 47
  • 70

4 Answers4

1

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();
?>
Sukhwinder Sodhi
  • 455
  • 1
  • 4
  • 18
  • i tried the above but i got following error: Warning: file_put_contents(/var/folders/w3/tq3q0srx2tg596sr34vy2d840000gn/T/3b5e8e58b68f3e27c7632fdcb3ceb0f5): failed to open stream: Permission denied in /Applications/XAMPP/xamppfiles/htdocs/engloto/dompdf/src/FontMetrics.php on line 243 Fatal error: Class 'FontLib\Font' not found in /Applications/XAMPP/xamppfiles/htdocs/engloto/dompdf/src/FontMetrics.php on line 245 –  Jun 02 '16 at 11:23
  • I uploaded on main server to test this i got following:` Fatal error: Call to undefined function Dompdf\mb_internal_encoding() in /home/loto/public_html/dompdf/src/Dompdf.php on line 282` –  Jun 02 '16 at 11:30
  • this on 282 line: mb_internal_encoding('UTF-8'); –  Jun 02 '16 at 11:32
  • Try updating the dompdf library through composer – Sukhwinder Sodhi Jun 02 '16 at 11:43
  • i m new into this. can u help me out? –  Jun 02 '16 at 11:44
  • i m close to this & will be very very helpful if you help me a bit more into this. I added composer.json into server it contains: { "require": { "dompdf/dompdf": "dev-master" } } Correct me if i m wrong please. still same error. You can find updated page here for testing : https://www.lotomanager.in/pdf.php –  Jun 02 '16 at 11:52
  • Did you installed composer on your server? – Sukhwinder Sodhi Jun 02 '16 at 11:54
  • i m not sure. i have dedicated server from godaddy. i just uploaded that page composer.json –  Jun 02 '16 at 11:55
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/113630/discussion-between-sukhwinder-sodhi-and-webgen). – Sukhwinder Sodhi Jun 02 '16 at 11:58
0

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

$dompdf->loadHtml('hello world ' . $_GET['ID'] );
beingalex
  • 2,416
  • 4
  • 32
  • 71
  • print-pdf is a php page. This is a complete dynamic page. So how can we get print-pdf results on pdf using the same as above? –  Jun 02 '16 at 08:51
0

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();
?>
beingalex
  • 2,416
  • 4
  • 32
  • 71
0

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.

johannes
  • 15,807
  • 3
  • 44
  • 57