vigicorp / php-mcpdf
基于mcpdf的PDF转换和表单工具。
Requires
- php: >=5.3.0
- mikehaertl/php-shellcommand: ^1.5.0
- mikehaertl/php-tmpfile: ^1.1.0
Requires (Dev)
- phpunit/phpunit: >4.0 <8
- dev-master
- 0.9.1
- 0.9.0
- 0.8.1
- 0.8.0
- 0.7.0
- 0.6.1
- 0.6.0
- 0.5.0
- 0.4.5
- 0.4.4
- 0.4.3
- 0.4.2
- 0.4.1
- 0.4.0
- 0.3.2
- 0.3.1
- 0.3.0
- 0.2.4
- 0.2.3
- 0.2.2
- 0.2.1
- 0.2.0
- 0.1.2
- 0.1.1
- 0.1.0
- dev-develop
- dev-150-tmp-directory-option
- dev-refine-test-config
- dev-refactor-info-fields
- dev-push-dependency-constraints
- dev-fix-coding-style
- dev-131-add-to-string-method
- dev-fix-travis
This package is auto-updated.
Last update: 2024-08-29 05:30:18 UTC
README
README需要更新。
### 简述: pdftk 不允许将Unicode字体渲染到PDF表单字段中。
mcpdf https://github.com/m-click/mcpdf 可以做到。
基于pdftk的PDF转换和表单工具。
特性
php-pdftk 将 pdftk
的全部功能带到PHP中 - 更多。
- 填充表单,从XFDF/FDF文件或数据数组中(UTF-8安全,未平整的表单需要pdftk 2.x !)
- 从PHP数组创建XFDF或FDF文件(UTF-8安全!)
- 从已填充的PDF表单创建FDF文件
- 将多个PDF文件中的页面合并到新的PDF文件中
- 将PDF分割成每个页面一个文件
- 添加背景或叠加PDF
- 读取PDF和表单字段的元数据
- 设置密码和权限
- 删除密码
要求
- 必须在您的系统上安装并配置好
pdftk
命令 - 此库是为pdftk 2.x版本编写的。您应该能够使用pdftk 1.x版本,但并非所有方法都将在那里工作。有关详细信息,请参阅系统上pdftk的手册页。
注意 如果您在Ubuntu上,您可能想安装来自
ppa:malteworld/ppa
的版本。默认包似乎使用snap,并且有关于此版本文件权限问题的报告。以下提供了一种解决方案。
安装
您应使用 composer 来安装此库。
composer require mikehaertl/php-pdftk
示例
操作
请查阅 pdftk
手册页以了解每个操作的详细工作原理和可用选项。
注意:某些命令允许您使用句柄别名您的文件(见以下示例)。在pdftk 2.x版本中,句柄可以是一个或多个大写字母。
对于所有操作,您可以通过 saveAs($name)
将PDF本地保存,或通过 send()
发送到浏览器。如果传递文件名到 send($name)
,则客户端浏览器将打开下载对话框;如果没有文件名,它通常以内联方式显示PDF。
重要:您始终只能在单个PDF实例上执行以下操作之一。以下提供了一种解决方案,如果您需要多个操作。
填充表单
使用PHP数组或XFDF/FDF文件中的数据填充PDF表单。
use mikehaertl\pdftk\Pdf; // Fill form with data array $pdf = new Pdf('/full/path/to/form.pdf'); $pdf->fillForm([ 'name'=>'ÄÜÖ äüö мирано čárka', 'nested.name' => 'valX', ]) ->needAppearances() ->saveAs('filled.pdf'); // Fill form from FDF $pdf = new Pdf('form.pdf'); $pdf->fillForm('data.xfdf') ->saveAs('filled.pdf'); // Check for errors if (!$pdf->saveAs('my.pdf')) { $error = $pdf->getError(); }
注意:当填充UTF-8数据时,您应该始终添加 needAppearances()
选项。这将确保PDF阅读器会负责使用正确的字体进行渲染,这是pdftk无法为您做的。另外请注意,如果您的数据中有特殊字符,则 flatten()
可能无法很好地工作。
从PHP数组创建XFDF/FDF文件
这是一个额外的功能,在 pdftk
中不可用。
use mikehaertl\pdftk\XfdfFile; use mikehaertl\pdftk\FdfFile; $xfdf = new XfdfFile(['name' => 'Jürgen мирано']); $xfdf->saveAs('/path/to/data.xfdf'); $fdf = new FdfFile(['name' => 'Jürgen мирано']); $fdf->saveAs('/path/to/data.fdf');
合并
从一个或多个PDF文件中的页面组装PDF。
use mikehaertl\pdftk\Pdf; // Extract pages 1-5 and 7,4,9 into a new file $pdf = new Pdf('/path/to/my.pdf'); $pdf->cat(1, 5) ->cat([7, 4, 9]) ->saveAs('/path/to/new.pdf'); // Combine pages from several files, demonstrating several ways how to add files $pdf = new Pdf([ 'A' => '/path/file1.pdf', // A is alias for file1.pdf 'B' => ['/path/file2.pdf','pass**word'], // B is alias for file2.pdf ]); $pdf->addFile('/path/file3.pdf','C','**secret**pw'); // C is alias file3.pdf $pdf->cat(1, 5, 'A') // pages 1-5 from A ->cat(3, null, 'B') // page 3 from B ->cat(7, 'end', 'B', null, 'east') // pages 7-end from B, rotated East ->cat('end',3,'A','even') // even pages 3-end in reverse order from A ->cat([2,3,7], 'C') // pages 2,3 and 7 from C ->saveAs('/path/new.pdf');
洗牌
类似于 cat()
,但创建 "流" 并一次填充新PDF中的一个页面。
use mikehaertl\pdftk\Pdf; $pdf = new Pdf([ 'A' => '/path/file1.pdf', // A is alias for file1.pdf 'B' => '/path/file2.pdf', // B is alias for file2.pdf ]); // new.pdf will have pages A1, B3, A2, B4, A3, B5, ... $pdf->shuffle(1, 5, 'A') // pages 1-5 from A ->shuffle(3, 8, 'B') // pages 3-8 from B ->saveAs('/path/new.pdf');
拆分
将PDF文件拆分成每个页面一个文件。
use mikehaertl\pdftk\Pdf; $pdf = new Pdf('/path/my.pdf'); $pdf->burst('/path/page_%d.pdf'); // Supply a printf() pattern
添加背景PDF
添加另一个PDF文件作为背景。
use mikehaertl\pdftk\Pdf; // Set background from another PDF (first page repeated) $pdf = new Pdf('/path/my.pdf'); $pdf->background('/path/back.pdf') ->saveAs('/path/watermarked.pdf'); // Set background from another PDF (one page each) $pdf = new Pdf('/path/my.pdf'); $pdf->multiBackground('/path/back_pages.pdf') ->saveAs('/path/watermarked.pdf');
添加叠加PDF
添加另一个PDF文件作为叠加。
use mikehaertl\pdftk\Pdf; // Stamp with another PDF (first page repeated) $pdf = new Pdf('/path/my.pdf'); $pdf->stamp('/path/overlay.pdf') ->saveAs('/path/stamped.pdf'); // Stamp with another PDF (one page each) $pdf = new Pdf('/path/my.pdf'); $pdf->multiStamp('/path/overlay_pages.pdf') ->saveAs('/path/stamped.pdf');
生成FDF
从给定的填充PDF表单创建FDF文件。
use mikehaertl\pdftk\Pdf; // Create FDF from PDF $pdf = new Pdf('/path/form.pdf'); $pdf->generateFdfFile('/path/data.fdf');
获取PDF数据
从PDF文件中读取元数据或表单字段信息。
use mikehaertl\pdftk\Pdf; // Get data $pdf = new Pdf('/path/my.pdf'); $data = $pdf->getData(); // Get form data fields $pdf = new Pdf('/path/my.pdf'); $data = $pdf->getDataFields(); // Get data as string echo $data; $txt = (string) $data; $txt = $data->__toString(); // Get data as array $arr = (array) $data; $arr = $data->__toArray(); $field1 = $data[0]['Field1'];
如何对PDF执行多个操作
如上所述,您只能对单个PDF实例执行上述操作之一。如果您需要执行多个操作,可以将一个Pdf
实例传递给另一个实例。
use mikehaertl\pdftk\Pdf; // Extract pages 1-5 and 7,4,9 into a new file $pdf = new Pdf('/path/my.pdf'); $pdf->cat(1, 5) ->cat([7, 4, 9]); // We now use the above PDF as source file for a new PDF $pdf2 = new Pdf($pdf); $pdf2->fillForm(['name' => 'ÄÜÖ äüö мирано čárka']) ->needAppearances() ->saveAs('/path/filled.pdf');
选项
您可以将上述操作与以下一个或多个选项结合使用。
use mikehaertl\pdftk\Pdf; $pdf = new Pdf('/path/my.pdf'); $pdf->allow('AllFeatures') // Change permissions ->flatten() // Merge form data into document (doesn't work well with UTF-8!) ->compress($value) // Compress/Uncompress ->keepId('first') // Keep first/last Id of combined files ->dropXfa() // Drop newer XFA form from PDF ->dropXmp() // Drop newer XMP data from PDF ->needAppearances() // Make clients create appearance for form fields ->setPassword($pw) // Set owner password ->setUserPassword($pw) // Set user password ->passwordEncryption(128) // Set password encryption strength ->saveAs('new.pdf'); // Example: Fill PDF form and merge form data into PDF // Fill form with data array $pdf = new Pdf('/path/form.pdf'); $pdf->fillForm(['name' => 'My Name']) ->flatten() ->saveAs('/path/filled.pdf'); // Example: Remove password from a PDF $pdf = new Pdf; $pdf->addFile('/path/my.pdf', null, 'some**password') ->saveAs('/path/new.pdf');
Shell命令
该类使用php-shellcommand来执行pdftk
。您可以将$options
作为其Command
类的第二个参数传递给构造函数。
use mikehaertl\pdftk\Pdf; $pdf = new Pdf('/path/my.pdf', [ 'command' => '/some/other/path/to/pdftk', // or on most Windows systems: // 'command' => 'C:\Program Files (x86)\PDFtk\bin\pdftk.exe', 'useExec' => true, // May help on Windows systems if execution fails ]);
临时文件
内部通过php-tmpfile创建一个临时文件。您也可以直接访问该文件,例如,如果您既不希望发送也不希望保存文件,但只需要二进制PDF内容。
use mikehaertl\pdftk\Pdf; $pdf = new Pdf('/path/my.pdf'); $pdf->fillForm(['name' => 'My Name']) ->execute(); $content = file_get_contents( (string) $pdf->getTmpFile() );
如果您有权限问题,您可能需要设置一个目录,以便您的pdftk
命令可以写入。
use mikehaertl\pdftk\Pdf; $pdf = new Pdf('/path/my.pdf'); $pdf->tempDir = '/home/john/temp';
API
请查阅源文件以获取每个方法的完整文档。