如何将 DOMPDF 生成的内容保存到文件中?

2022-01-24 00:00:00 file pdf-generation save php dompdf

我正在使用 Dompdf 创建 PDF 文件,但我不知道为什么它没有将创建的 PDF 保存到服务器.

I am using Dompdf to create PDF file but I don't know why it doesn't save the created PDF to server.

有什么想法吗?

require_once("./pdf/dompdf_config.inc.php");
    $html =
      '<html><body>'.
      '<p>Put your html here, or generate it with your favourite '.
      'templating system.</p>'.
      '</body></html>';

    $dompdf = new DOMPDF();
    $dompdf->load_html($html);
    $dompdf->render();
    file_put_contents('Brochure.pdf', $dompdf->output());

推荐答案

我刚用过 dompdf,代码有点不一样,但是能用.

I have just used dompdf and the code was a little different but it worked.

这里是:

require_once("./pdf/dompdf_config.inc.php");
$files = glob("./pdf/include/*.php");
foreach($files as $file) include_once($file);

$html =
      '<html><body>'.
      '<p>Put your html here, or generate it with your favourite '.
      'templating system.</p>'.
      '</body></html>';

    $dompdf = new DOMPDF();
    $dompdf->load_html($html);
    $dompdf->render();
    $output = $dompdf->output();
    file_put_contents('Brochure.pdf', $output);

这里唯一的区别是包含目录中的所有文件.

Only difference here is that all of the files in the include directory are included.

除此之外,我唯一的建议是指定用于写入文件的完整目录路径,而不仅仅是文件名.

Other than that my only suggestion would be to specify a full directory path for writing the file rather than just the filename.

相关文章