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

时间:2021-11-23 01:04:20

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

3 个解决方案

#1


65  

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.

这里唯一不同的是包含了include目录中的所有文件。

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

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

#2


1  

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中,请将“Everyone”添加到目录的安全选项卡中并启用“write”。

#3


-3  

<?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>

#1


65  

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.

这里唯一不同的是包含了include目录中的所有文件。

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

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

#2


1  

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中,请将“Everyone”添加到目录的安全选项卡中并启用“write”。

#3


-3  

<?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>