0

I switched from TCPDF to domPDF because it seems more convenient to handle when creating invoices from html to pdf (I am rather a low pro on PHP :)). Now that I created the html file as a PDF file I recognized it does not output any PHP in the PDF - since the data from my sql databanks should fill the PDF it is kinda a problem.

I saw that you can enable PHP in the options.php included in the src-folder and I tried to do like it is written in the manual (and also tried various other code lines) but it just doesn't want to work:

$root = realpath($_SERVER["DOCUMENT_ROOT"]);

require_once ("$root/../xxx/dompdf/autoload.inc.php");

use Dompdf\Dompdf;
use Dompdf\Options;

$options = new Options();
$options->setIsPhpEnabled('true');
$dompdf = new Dompdf($options);

$dompdf->loadHtml(file_get_contents("testdomhtml.php"));

$dompdf->setPaper('A4', 'portrait');

$dompdf->render();

$dompdf->stream("bla",array("Attachment"=>0));

The PDF is shown but without the input from any PHP code.

If someone would be so kind, I would also be interested in knowing why and in how far enabling PHP is a security risk since I actually want to use that for my business. Would it be more advisable to wrap it all up in the main php file without loading external html and css files?

Thanks a lot in advance!

Franky2207
  • 163
  • 2
  • 19

2 Answers2

0

Your file_get_contents("testdomhtml.php") will get actual content of file and will not execute any code inside it. Instead make it web accessible and pass URL to this page:

$dompdf->load_html_file('http://yourdomain.ext/testdomhtml.php');
Justinas
  • 41,402
  • 5
  • 66
  • 96
  • thanks for help! This throws: `Fatal error: Uncaught exception 'Dompdf\Exception' with message 'Remote file requested, but remote file download is disabled.' in /var/www/web1159/files/dompdf/src/Dompdf.php:364` - I guess I have to enable the remote file requests first. Would you suggest this is a better method than the one from above? For now I am using the solution that is put forward by @ziah75 – Franky2207 Dec 08 '16 at 15:30
0

You could do something like this (not tested the code). Replace

$dompdf->loadHtml(file_get_contents("testdomhtml.php"));

With

ob_start();
include 'testdomhtml.php';
$output = ob_get_clean();
$dompdf->loadHtml($output);

More options How to execute and get content of a .php file in a variable?

Community
  • 1
  • 1
ziah75
  • 73
  • 5