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 word样式右侧翻页键消失
  • ¥15 springboot+vue 集成keycloak sso到阿里云
  • ¥15 win7系统进入桌面过一秒后突然黑屏
  • ¥30 backtrader对于期货交易的现金和资产计算的问题
  • ¥15 求C# .net4.8小报表工具
  • ¥15 安装虚拟机时出现问题
  • ¥15 Selenium+docker Chrome不能运行
  • ¥15 mac电脑,安装charles后无法正常抓包
  • ¥18 visio打开文件一直显示文件未找到
  • ¥15 请教一下,openwrt如何让同一usb储存设备拔插后设备符号不变?