2012-01-04 47 views
37

我正在使用Dompdf来创建PDF文件,但我不知道为什么它不会将创建的PDF保存到服务器。如何将DOMPDF生成的内容保存到文件?

任何想法?

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

dompdf的版本?任何PHP错误? – BrianS 2012-01-04 03:02:01

+0

dompdf 0.5.2,php 5.2.13 – user1079810 2012-01-05 02:27:37

+0

我没有看到任何会阻止你保存的东西,所以我猜测服务器配置错误。也许PHP无法写入该目录?如果是这样的话,PHP会报告一个错误。检查你的PHP错误日志或启用错误显示。 – BrianS 2012-01-05 18:20:41

回答

58

我刚刚使用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); 

唯一的区别就是在这里,所有的包括目录中的文件都包括在内。

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

1

我测试了你的代码,我唯一可以看到的问题是缺少给你试图写入文件的目录的权限。

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

在linux中使用“chmod”。

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

-2
<?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> 
相关问题