minutemanservices/pdftk-php-client

使用我们的 PDFtk Microservice 从 HTML 生成 PDF 的 PHP 客户端库

1.0.0 2021-03-05 17:28 UTC

README

贡献指南

  1. 更新 ApiClient 类中的版本常量。
  2. 更新 composer.json 中的版本。
  3. 用相同的版本号标记提交。

需求

  • php: >=7.4.0
  • illuminate/support: >=5.0.0
  • guzzlehttp/guzzle: ^7.2
  • konekt/enum: ^3.1
  • ext-json: *

安装

composer require minutemanservices/pdftk-php-client

Laravel 设置

php artisan vendor:publish --provider=MinuteMan\PdftkClient\PdftkServiceProvider

用法

1. 创建文档

首先,创建一个 ApiClient 类的新实例,并提供 PDFtk 微服务 API 的 URL 和您的访问密钥。

$apiClient = new MinuteMan\PdftkClient\ApiClient($apiUrl, $apiKey);

然后,为要生成的每个 PDF 创建一个 PdftkDocument 实例。

ApiClient 类的实例传递给 PDF 文档实例。

$pdfDocument = new MinuteMan\PdftkClient\PdftkDocument($apiClient);

Laravel

在 Laravel 中,您可以在不创建 ApiClient 实例的情况下创建文档实例。

$pdfDocument = app()->make(MinuteMan\PdftkClient\PdftkDocument::class);

2. 提供源 PDF

您必须提供一个 PDF,API 将在其上执行命令。您可以通过以下 1 种方式完成此操作:

远程 URL

$sourcePdf = new MinuteMan\PdftkClient\PdfSources\RemoteUrl('https://www.google.com/');
$pdfDocument->setSourcePdf($sourcePdf);

本地文件路径

$sourcePdf = new MinuteMan\PdftkClient\PdfSources\File(__DIR__ . '/example.pdf');
$pdfDocument->setSourcePdf($sourcePdf);

流资源

$sourcePdf = new MinuteMan\PdftkClient\PdfSources\Stream(fopen('example.pdf'));
$pdfDocument->setSourcePdf($sourcePdf);

3. 指定命令

您必须指定 PDFtk API 在 PDF 上执行的命令。可用命令

  • 填写表单
  • 旋转
  • 背景
  • 多背景
  • 盖章
  • 多重盖章
$command = new MinuteMan\PdftkClient\Commands\FillForm(['first_name' => 'Jane']);
$pdfDocument->setCommand($command);

4. 配置标志和选项(可选)

$pdfDocument->setFlatten();
$pdfDocument->setUserPw('123456');

// Method chaining is supported
$pdfDocument->setNeedAppearances()->setInputPw();

取消设置标志或选项:$pdfDocument->unsetFlatten();$pdfDocument->unsetUserPw();

5. 生成 PDF

通过提供绝对路径保存 PDF 为文件:$pdfDocument->save(__DIR__ . '/finalized.pdf');

您也可以生成 PDF 并将其作为流资源返回:$pdfDocument->getStream();

选项 & 标志

访问 PDFtk 服务器手册了解每个标志的详细信息:https://www.pdflabs.com/docs/pdftk-man-page/

可用标志

可用选项

命令示例

填写表单

$command = new MinuteMan\PdftkClient\Commands\FillForm(['first_name' => 'Jane']);

if (!$command->hasField('last_name')) {
    $command->setField('last_name', 'Doe');
}

if ($command->hasField('fax')) {
    $command->unsetField('fax');
}

$pdfDocument->setCommand($command);

旋转

$command = new MinuteMan\PdftkClient\Commands\Rotate();

$command->setRotation(180)       // Integer. Default: 0. Must be multiples of 90. Maximum: 270. Minimum: -270.
         ->setStartPage(1)       // Integer. Default: 0.
         ->setEndPage(10)        // Integer or null. Default: null. Null applies rotation to every page after the start page.
         ->setQualifier('even'); // String or null. Default: null. Values can be 'even', 'odd', or null.
         
$pdfDocument->setCommand($command);

盖章、多重盖章、背景和多重背景

$command = new MinuteMan\PdftkClient\Commands\Stamp();
// OR $command = new MinuteMan\PdftkClient\Commands\Multistamp();
// OR $command = new MinuteMan\PdftkClient\Commands\Background();
// OR $command = new MinuteMan\PdftkClient\Commands\Multibackground();

// Set the PDF source that will be used as the stamp or background
$sourcePdf = new MinuteMan\PdftkClient\PdfSources\RemoteUrl('https://www.google.com/');
$command->setSourcePdf($sourcePdf);

// Assign the command to the document
$pdfDocument->setCommand($command);