drwj4061
drwj4061
2016-12-08 12:23

(内联)PHP in domPDF 7.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!

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享
  • 邀请回答

2条回答

  • douou0977 douou0977 5年前

    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?

    点赞 评论 复制链接分享
  • dscqrkvr9562034621 dscqrkvr9562034621 5年前

    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');
    
    点赞 评论 复制链接分享

为你推荐