drwj4061 2016-12-08 12:23
浏览 43
已采纳

(内联)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 2016-12-08 13:11
    关注

    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?

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器