如何在php中将Html转换为Pdf


How to convert Html to Pdf in php

我在使用 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');
?>

我收到的错误消息是:

未捕获的异常"LogicException",在 dom-pdf''dom-pdf1.php:3 中带有消息"找不到函数'DOMPDF_autoload'(找不到函数'

DOMPDF_autoload'或无效的函数名称(" 堆栈跟踪: #0 dom-pdf''dom-pdf1.php(3(: spl_autoload_register('DOMPDF_autoload'( #1 {main} 在第 3 行抛出 dom-pdf''dom-pdf1.php

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

spl_autoload_register('DOMPDF_autoload');

实际上,我正在尝试从此代码生成和设计pdf,因此请帮助我更正此问题。

如果你看一下 git (https://github.com/dompdf/dompdf( 中的自述文件,根据你安装代码的方式,你要么需要包含自动加载器,要么包括供应商/自动加载:

如果使用作曲家安装:

require 'vendor/autoload.php';

如果从 git 存储库手动安装:

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

它工作正常任何人都可以使用它...

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