如何在PHP

时间:2016-02-15 11:19:00

标签: php pdf dom dompdf

我在使用Php将HTML页面内容转换为PDF时遇到了一些问题。 请参阅下面的代码..

<?php
    require_once("../dompdf/dompdf_config.custom.inc.php");
    require_once("../dompdf/include/autoload.inc.php");
    spl_autoload_register('DOMPDF_autoload');
    function pdf_create($html, $filename, $paper, $orientation, $stream=TRUE){    
            $dompdf= new DOMPDF();
            $dompdf->set_paper($paper, $orientation);
            $dompdf->load_html($html);
            $dompdf->render();
            $dompdf->stream($filename.".pdf");
    }


    $filename='file_name';
    $dompdf=new DOMPDF();
    $html=file_get_contents('file_html.php');
    pdf_create($html, $filename, 'A4', 'portrait');

?>

我得到的错误信息是:

  

未捕获的异常&#39; LogicException&#39;消息&#39;功能&#39; DOMPDF_autoload&#39;找不到(功能&#39; DOMPDF_autoload&#39;未找到或无效的功能名称)&#39;在dom-pdf \ dom-pdf1.php:3堆栈跟踪:#0 dom-pdf \ dom-pdf1.php(3):spl_autoload_register(&#39; DOMPDF_autoload&#39;)#1 {main}抛出dom-第3行的pdf \ dom-pdf1.php

请告诉我如何更正此处显示的错误

spl_autoload_register('DOMPDF_autoload');

其实我正在尝试从这段代码生成和设计pdf,所以请帮我纠正这个。

2 个答案:

答案 0 :(得分:1)

如果您查看git(https://github.com/dompdf/dompdf)中的自述文件,根据您安装代码的方式,您需要包含自动加载器,或者包含供应商/自动加载:

如果使用composer安装:

require 'vendor/autoload.php';

如果您是从git repo手动安装的:

// include autoloader
require_once '../dompdf/autoload.inc.php';

答案 1 :(得分:0)

它适用于任何人都可以使用它......

<?php
  require_once ("Dompdf/dompdf_config.inc.php");


  $pdf_content='
                <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  <html xmlns="http://www.w3.org/1999/xhtml">
  <head>
  <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  </head>
  <style type="text/css">                           
  #pdf_header, #pdf_container{ border: 1px solid #CCCCCC; padding:10px; }               
  #pdf_header{ margin:10px auto 0px; border-bottom:none; }              
  table{ width:580px; }
  #pdf_container{margin:0px auto; }
  .rpt_title{ background:#99CCFF; }                                                         
  </style>
 <body>
 <div id="pdf_header" >
 <div id="maindiv1" style="border:1px solid blue;width:500px;height:230px">            
    <div  style="border:0px solid red;width:100px;height:80px;float:left;">
     <img src="<img src="http://incroyablefuture.com/blog/img/small-icon.jpg"/>" />
    </div>
    <h1 style="color:blue;font-family:sans-     serif;float:left;width:390px;margin-top:10;text-align:center;border:0px solid red;">enterployee.com</h1>
   <div style="border:0px solid red;width:100%;height:120px;float:left;">
    <p style="font-family:sans-serif;font-size:15px;">
       Enterployee.com is a employee relationship management tool for enterprise. It’s a fully managed notification platform for employees.
       We have a vision to make organizations more professional and organized. After getting an enterployee account organizations can notify their employees easily and employees can connect with their company in better way.
       </p>

    </div>

    <div style="border:0px solid red;width:80px;height:30px;float:right;">
    </div>
</div> 
</div></body></html>'
;

$name = date("Ymd").rand().'.pdf';
$reportPDF=createPDF(12, $pdf_content, 'example', $name );
function createPDF($pdf_userid, $pdf_content, $pdf_For, $filename){
$path='pdf-lib/';    
$dompdf=new DOMPDF();
$dompdf->load_html($pdf_content);
$dompdf->render();
$output = $dompdf->output();
file_put_contents($path.$filename, $output);
return $filename;       
}   
echo '<a href="pdf-lib/'.$name.'" > Download as PDF </a>';
  ?>

您可以从html格式创建pdf ..