【问题标题】:how to save DOMPDF generated content to file? [closed]如何将 DOMPDF 生成的内容保存到文件中? [关闭]
【发布时间】:2012-02-02 00:03:03
【问题描述】:

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

有什么想法吗?

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 的版本?任何 PHP 错误?
  • dompdf 0.5.2, php 5.2.13
  • 我没有看到任何会阻止您保存的内容,所以我猜是服务器配置错误。也许 PHP 无法写入该目录?如果是这种情况,PHP 将报告错误。检查您的 PHP 错误日志或启用错误显示。

标签: php file pdf-generation save dompdf


【解决方案1】:
<?php
$content='<table width="100%" border="1">';
$content.='<tr><th>name</th><th>email</th><th>contact</th><th>address</th><th>city</th><th>country</th><th>postcode</th></tr>';
for ($index = 0; $index < 10; $index++) { 
$content.='<tr><td>nadim</td><td>nadim.sheikh.07@gmail.com</td><td>7737033665</td><td>247 dehligate</td><td>udaipur</td><td>india</td><td>313001</td></tr>';
}
$content.='</table>';
//$html = file_get_contents('pdf.php');
if(isset($_POST['pdf'])){
    require_once('./dompdf/dompdf_config.inc.php');
    $dompdf = new DOMPDF;                        
    $dompdf->load_html($content);
    $dompdf->render();
    $dompdf->stream("hello.pdf");
}
?>
<html>
    <body>
        <form action="#" method="post">        
            <button name="pdf" type="submit">export</button>
        <table width="100%" border="1">
           <tr><th>name</th><th>email</th><th>contact</th><th>address</th><th>city</th><th>country</th><th>postcode</th></tr>         
            <?php for ($index = 0; $index < 10; $index++) { ?>
            <tr><td>nadim</td><td>nadim.sheikh.07@gmail.com</td><td>7737033665</td><td>247 dehligate</td><td>udaipur</td><td>india</td><td>313001</td></tr>
            <?php } ?>            
        </table>        
        </form>        
    </body>
</html>

【讨论】:

    【解决方案2】:

    我确实测试了您的代码,我能看到的唯一问题是您尝试将文件写入到的目录缺少权限。

    为您需要放置文件的目录授予“写入”权限。在您的情况下,它是当前目录。

    在 linux 中使用“chmod”。

    如果您在 Windows 中,请将启用“写入”的“所有人”添加到目录的安全选项卡中。

    【讨论】:

      【解决方案3】:

      我刚刚使用过 dompdf,代码有点不同,但它可以工作。

      这里是:

      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);
      

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

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-07-20
        • 2012-01-06
        • 2023-02-01
        • 1970-01-01
        • 2015-02-05
        • 1970-01-01
        相关资源
        最近更新 更多