承接 interactively/php-pdftk 相关项目开发

从需求分析到上线部署,全程专人跟进,保证项目质量与交付效率

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

interactively/php-pdftk

Composer 安装命令:

composer require interactively/php-pdftk

包简介

A PDF conversion and form utility based on pdftk.

关键字:

README 文档

README

Build Status Latest Stable Version Total Downloads Latest Unstable Version License

A PDF conversion and form utility based on pdftk.

Features

php-pdftk brings the full power of pdftk to PHP - and more.

  • Fill forms, either from a XFDF/FDF file or from a data array (UTF-8 safe for unflattened forms, requires pdftk 2.x !)
  • Create XFDF or FDF files from PHP arrays (UTF-8 safe!)
  • Create FDF files from filled PDF forms
  • Combine pages from several PDF files into a new PDF file
  • Split a PDF into one file per page
  • Add background or overlay PDFs
  • Read out meta data about PDF and form fields
  • Set passwords and permissions
  • Remove passwords

Requirements

  • The pdftk command must be installed and working on your system
  • This library is written for pdftk 2.x versions. You should be able to use it with pdftk 1.x but not all methods will work there. For details consult the man page of pdftk on your system.

Note If you're on Ubuntu you may want to install the version from ppa:malteworld/ppa. The default packages seems to use snap an there have been reports about file permission issues with this version.

Installation

You should use composer to install this library.

composer require mikehaertl/php-pdftk

Examples

Operations

Please consult the pdftk man page for each operation to find out how each operation works in detail and which options are available.

Note: Some commands allow to alias your files with a handle (see examples below). In version 2.x of pdftk a handle can be one or more upper case letters.

For all operations you can either save the PDF locally through saveAs($name) or send it to the browser with send(). If you pass a filename to send($name) the client browser will open a download dialogue whereas without a filename it will usually display the PDF inline.

IMPORTANT: You can always only perform one of the following operations on a single PDF instance. Below you can find a workaround if you need multiple operations.

Fill Form

Fill a PDF form with data from a PHP array or an XFDF/FDF file.

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();
}

Note: When filling in UTF-8 data, you should always add the needAppearances() option. This will make sure, that the PDF reader takes care of using the right fonts for rendering, something that pdftk can't do for you. Also note that flatten() doesn't really work well if you have special characters in your data.

Create a XFDF/FDF file from a PHP array

This is a bonus feature that is not available from 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');

Cat

Assemble a PDF from pages from one or more PDF files.

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');

Shuffle

Like cat() but create "streams" and fill the new PDF with one page from each stream at a time.

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');

Burst

Split a PDF file into one file per page.

use mikehaertl\pdftk\Pdf;

$pdf = new Pdf('/path/my.pdf');
$pdf->burst('/path/page_%d.pdf');     // Supply a printf() pattern

Add background PDF

Add another PDF file as background.

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');

Add overlay PDF

Add another PDF file as overlay.

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');

Generate FDF

Create a FDF file from a given filled PDF form.

use mikehaertl\pdftk\Pdf;

// Create FDF from PDF
$pdf = new Pdf('/path/form.pdf');
$pdf->generateFdfFile('/path/data.fdf');

Get PDF data

Read out metadata or form field information from a PDF file.

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'];

How to perform more than one operation on a PDF

As stated above, you can only perform one of the preceeding operations on a single PDF instance. If you need more than one operation you can feed one Pdf instance into another:

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');

Options

You can combine the above operations with one or more of the following options.

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 Command

The class uses php-shellcommand to execute pdftk. You can pass $options for its Command class as second argument to the constructor:

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
]);

Temporary File

Internally a temporary file is created via php-tmpfile. You can also access that file directly, e.g. if you neither want to send or save the file but only need the binary PDF content:

use mikehaertl\pdftk\Pdf;

$pdf = new Pdf('/path/my.pdf');
$pdf->fillForm(['name' => 'My Name'])
    ->execute();
$content = file_get_contents( (string) $pdf->getTmpFile() );

API

Please consult the source files for a full documentation of each method.

interactively/php-pdftk 适用场景与选型建议

interactively/php-pdftk 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 4.58k 次下载、GitHub Stars 达 0, 最近一次更新时间为 2023 年 02 月 21 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「pdf」 「pdftk」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

我们在过去多个企业项目中使用过 interactively/php-pdftk 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 interactively/php-pdftk 我们能提供哪些服务?
定制开发 / 二次开发

基于 interactively/php-pdftk 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 4.58k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 0
  • 点击次数: 10
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 0
  • Watchers: 0
  • Forks: 134
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2023-02-21