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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8720897/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 05:21:14  来源:igfitidea点击:

how to save DOMPDF generated content to file?

phpfilepdf-generationsavedompdf

提问by TomTom

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

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

Any ideas?

有任何想法吗?

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

回答by startupsmith

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

我刚刚使用了 dompdf,代码有点不同,但它有效。

Here it is:

这里是:

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.

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

回答by Charlie

I did test your code and the only problem I could see was the lack of permission given to the directory you try to write the file in to.

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

Give "write" permission to the directory you need to put the file. In your case it is the current directory.

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

Use "chmod" in linux.

在 linux 中使用“chmod”。

Add "Everyone" with "write" enabled to the security tab of the directory if you are in Windows.

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

回答by Nadim Sheikh

<?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>[email protected]</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>[email protected]</td><td>7737033665</td><td>247 dehligate</td><td>udaipur</td><td>india</td><td>313001</td></tr>
            <?php } ?>            
        </table>        
        </form>        
    </body>
</html>