来源:http://trac.seagullproject.org/wiki/Integration/PDF/DOMPDF

使用DOMPDF生成PDF文件

下面是在Seagull内使用DOMPDF将HTML生成pdf文件的步骤(http://www.digitaljunkies.ca/dompdf/

  • 创建目录: lib/other/Dompdf
  • 从dompdf复制文件到Dompdf:
lib/other/Dompdf/include
lib/other/Dompdf/dompdf_config.inc.php
lib/other/Dompdf/lib
  • 设置Seagull使用”lib/other” 作为额外库目录:

General → Configuration → Additional include paths

  • 示例. 修改publisher/classes的FileMgr
  • 包含文件(在类声明之前):
require_once SGL_CORE_DIR . '/Item.php';
require_once 'Dompdf/dompdf_config.inc.php';
  • 修改”validate”函数 (放在函数的结尾):

$input→articleID = (int)$req→get('frmArticleID');

  • 添加新的方法:
 function _cmd_downloadPdf(&$input, &$output)
 {
     SGL::logMessage(null, PEAR_LOG_DEBUG);
     $aArticleDetail = SGL_Item::getItemDetail($input->articleID);
     $dompdf = new DOMPDF();
     $dompdf->load_html($aArticleDetail['bodyHtml']);
     $dompdf->render();
     $dompdf->stream($aArticleDetail['title'].".pdf");
     exit;
 }
  • 在模板中使用{makeUrl()}, 如:
{makeUrl(#downloadPdf#,#file#,#publisher#,aPagedData[data],#frmArticleID|item_id#,key)}

或者在浏览器地址栏中输出下面的URL做一个快速测试:

http://your_sgl_website/index.php/publisher/file/action/downloadPdf/frmArticleID/1/

这样就可以和CMS或其它模块一起使用,不过事实上还有另外一种方法可以将html转换成PDF。

Autoload问题

Fatal error: require_once() [function.require]: Failed opening required '/trunk/lib/other/Dompdf/include/html_template_flexy_token_doctype.cls.php'

解决方法 - Add a check for the include file in 在包含文件dompdf_config.inc.php之前,先做一下检查,即将下面代码:

function DOMPDF_autoload($class) {
  $filename = mb_strtolower($class) . ".cls.php";
  require_once(DOMPDF_INC_DIR . "/$filename");
}

改为

function DOMPDF_autoload($class) {
  $filename = mb_strtolower($class) . ".cls.php";
  if(file_exists(DOMPDF_INC_DIR . "/$filename")) {
      require_once(DOMPDF_INC_DIR . "/$filename");
  }    
}
 
integration/pdf/dompdf.txt · 最后更改: 2010/05/30 00:21 (外部编辑)
 
Except where otherwise noted, content on this wiki is licensed under the following license:GNU Free Documentation License 1.2