vincentvete / tcpdi
基于FPDI的TCPDF PDF导入器(支持优化后的PDF 1.5+)
1.0.2
2017-12-16 09:52 UTC
This package is not auto-updated.
Last update: 2024-09-20 02:37:29 UTC
README
注意:这是一个为Composer复制的
TCPDI
如果你在使用FPDI时无法解析1.5+版本的PDF,那么请尝试这个适用于PDF合并、组合或层叠重叠的完美解决方案
安装
克隆此git,并复制包含TCPDF的目录
git clone https://github.com/lthh89vt/tcpdi
通过Composer
composer require lthh89vt/tcpdi
用法
用法基本上与FPDI相同,只是导入TCPDI而不是FPDI。它还提供了一个"setSourceData()"函数,该函数接受原始PDF数据,适用于文件不在磁盘上或无法由TCPDI读取的情况。
// Create new PDF document. $pdf = new \TCPDI(PDF_PAGE_ORIENTATION, PDF_UNIT, PDF_PAGE_FORMAT, true, 'UTF-8', false); // Add a page from a PDF by file path. $pdf->AddPage(); $pdf->setSourceFile('/path/to/file-to-import.pdf'); $idx = $pdf->importPage(1); $pdf->useTemplate($idx); $pdfdata = file_get_contents('/path/to/other-file.pdf'); // Simulate only having raw data available. $pagecount = $pdf->setSourceData($pdfdata); for ($i = 1; $i <= $pagecount; $i++) { $tplidx = $pdf->importPage($i); $pdf->AddPage(); $pdf->useTemplate($tplidx); }
截至版本1.1,TCPDI还包括处理PDF注释的附加功能。由于注释是基于出血框而不是裁剪框定位的,因此你需要确保导入的是完整的出血框;还引入了一个新功能来设置页面格式(包括裁剪框在内的各种框)来自导入的页面,以便导入的页面与原始页面更好地匹配。以下示例演示了这一点
// Create new PDF document. $pdf = new \TCPDI(PDF_PAGE_ORIENTATION, PDF_UNIT, PDF_PAGE_FORMAT, true, 'UTF-8', false); // Add a page from a PDF by file path. $pdf->setSourceFile('/path/to/file-to-import.pdf'); // Import the bleed box (default is crop box) for page 1. $tplidx = $pdf->importPage(1, '/BleedBox'); $size = $pdf->getTemplatesize($tplidx); $orientation = ($size['w'] > $size['h']) ? 'L' : 'P'; $pdf->AddPage($orientation); // Set page boxes from imported page 1. $pdf->setPageFormatFromTemplatePage(1, $orientation); // Import the content for page 1. $pdf->useTemplate($tplidx); // Import the annotations for page 1. $pdf->importAnnotations(1);
注意:要导入注释,需要tcpdi_parser v1.1,尽管setPageFormatFromTemplatePage()应与较旧的tcpdi_parser版本兼容。