在 PHP 中生成、读取和合并 PDF — 无 GPL,无 FPDI
PDF 是现代 Web 应用程序的核心。从发票、票证到报告和已签署的合同,在 PHP 项目中生成、操作和合并 PDF 的需求几乎无处不在。然而,在不遇到许可障碍和复杂依赖关系的情况下实现这一目标可能很棘手。传统的解决方案(如 mpdf)很棒,但它们的 GPL-2.0 许可证在将其集成到商业或 OEM 产品时可能会带来麻烦。类似 FPDI 的库虽然功能强大,但增加了额外的复杂性。在本文中,我们将探讨一种强大的替代方案,它允许您在 PHP 中生成、读取和合并 PDF,而无需 GPL,也无需 FPDI。
传统解决方案的痛点
让我们回顾一下源文章中提到的挑战:
- 使用
mpdf 生成 PDF
mpdf 是一种广泛使用的用于在 PHP 中生成 PDF 的库。它功能丰富、用途广泛且相对容易使用。然而,它的 GPL-2.0 许可证要求您在以商业方式分发应用程序时遵守其条款。这对于需要在其产品中避免开源许可证的企业来说是一个重大障碍。
```php
// 示例:使用 mPDF 生成简单 PDF
require_once DIR . '/vendor/autoload.php';
$mpdf = new \Mpdf\Mpdf();
$mpdf->WriteHTML('
Hello, World!
');
$mpdf->Output('hello_world.pdf', \Mpdf\Output\Destination::FILE);
```
- 使用 FPDI 合并或导入 PDF
FPDI(FDF 导入)非常适合将现有 PDF 内容导入到您的文档中。它特别适用于向现有 PDF 添加页眉、页脚或标志。然而,它并非没有缺点。FPDI 对外部库的依赖以及其有些混乱的 API 使其难以使用,特别是对于更喜欢更干净、更现代解决方案的开发人员。
```php
// 示例:使用 FPDI 合并 PDF(概念性)
require_once DIR . '/fpdi.php';
$pdf = new FPDI();
$pdf->setSourceFile('page1.pdf');
$templateId = $pdf->importPage(1);
$pdf->AddPage();
$pdf->useTemplate($templateId);
$pdf->Output('merged.pdf', 'F');
```
鉴于这些挑战,很明显需要一个更精简、更符合许可要求的解决方案。让我们探讨一个能够直接解决这些痛点的替代方案。
引入一个符合许可要求的替代方案
mpdf 和 FPDI 最有希望的替代方案之一是 setasign 公司提供的一系列库,特别是 PDFlib 和 FPDI2。这些库提供了一种强大且灵活的方式来在 PHP 中生成、读取和合并 PDF,而无需 GPL 限制。
setasign 库为何脱颖而出?
-
商业许可
与处于 GPL-2.0 许可证下的 mpdf 不同,setasign 库是商业许可的。这意味着您可以在商业、OEM 和本地部署的产品中使用它们,而无需担心开源许可义务。这对于需要严格控制其软件许可证的企业来说是一个游戏规则的改变者。
-
性能和可靠性
PDFlib 是一个经过长期验证的库,以其性能和可靠性而闻名。多年来,它一直被企业环境使用,并有着良好的记录。FPDI2 在原始 FPDI 的基础上构建,但提供了一个更现代、更直观的 API,使其更易于使用。
-
丰富的功能集
PDFlib 和 FPDI2 都提供了全面的 PDF 操作功能,包括文本提取、图像渲染、表单创建等。这使得它们适用于广泛的 PDF 相关任务。
开始使用 setasign 库
让我们逐步了解如何使用这些库生成、读取和合并 PDF。
1. 使用 PDFlib 生成 PDF
首先,您需要安装 PDFlib 库。通常可以通过 Composer 安装:
composer require setasign/pdflib
安装后,生成一个简单的 PDF 非常直接:
<?php
require_once __DIR__ . '/vendor/autoload.php';
use setasign\Pdflib\Pdflib;
$pdf = new Pdflib();
$pdf->setOption('licensekey', 'YOUR_LICENSE_KEY');
$pdf->open();
$pdf->addPage();
$pdf->setFont('Helvetica', 'normal', 12);
$pdf->drawText('Hello, World!', 50, 700);
$pdf->close();
$pdf->save('hello_world.pdf', Pdflib::SDF);
将 'YOUR_LICENSE_KEY' 替换为您从 setasign 网站获取的实际许可证密钥。
2. 使用 FPDI2 读取 PDF
接下来,让我们使用 FPDI2 读取现有 PDF。首先,通过 Composer 安装库:
composer require setasign/fpdi2
以下是您如何读取 PDF 并提取其内容的方法:
<?php
require_once __DIR__ . '/vendor/autoload.php';
use setasign\Fpdi\Fpdi;
$pdf = new Fpdi();
$pdf->setOption('licensekey', 'YOUR_LICENSE_KEY');
$pdf->addPage();
$pdf->setSourceFile('example.pdf');
$tplId = $pdf->importPage(1);
$pdf->useTemplate($tplId);
$pdf->Output('read_example.pdf', 'F');
同样,将 'YOUR_LICENSE_KEY' 替换为您许可证密钥。
3. 使用 FPDI2 合并 PDF
使用 FPDI2 合并 PDF 也非常简单。以下是一个合并两个 PDF 的示例:
<?php
require_once __DIR__ . '/vendor/autoload.php';
use setasign\Fpdi\Fpdi;
$pdf = new Fpdi();
$pdf->setOption('licensekey', 'YOUR_LICENSE_KEY');
// 添加第一个 PDF
$pdf->addPage();
$pdf->setSourceFile('page1.pdf');
$tplId = $pdf->importPage(1);
$pdf->useTemplate($tplId);
// 添加第二个 PDF
$pdf->addPage();
$pdf->setSourceFile('page2.pdf');
$tplId = $pdf->importPage(1);
$pdf->useTemplate($tplId);
$pdf->Output('merged.pdf', 'F');
高级功能和用例
setasign 库不仅适用于基本的 PDF 操作。它们提供了高级功能,使其适用于复杂的用例:
- 表单创建和填写
您可以创建交互式表单并编程填写它们。这对于生成动态填写的 PDF(如发票或合同)非常有用。
php
// 示例:创建表单字段
$pdf->addPage();
$pdf->setFont('Helvetica', 'bold', 12);
$pdf->drawText('Name:', 50, 750);
$pdf->createTextField('name', 50, 730, 100, 20, array('border' => true));
- 条形码生成
生成条形码是另一个强大的功能。您可以创建各种类型的条形码,包括 QR 码、EAN 和 UPC。
php
// 示例:生成 QR 码
$pdf->addPage();
$pdf->setSourceFile('qrcode.pdf');
$tplId = $pdf->importPage(1);
$pdf->useTemplate($tplId);
- PDF 安全性
您可以向 PDF 添加安全功能,例如密码保护、加密和访问权限。
php
// 示例:添加密码保护
$pdf->setProtection(array('print', 'copy'), 'user_password', 'owner_password');
结论:为何选择 setasign 库?
setasign 库为在 PHP 中生成、读取和合并 PDF 提供了一个强大、灵活且符合许可要求的解决方案。与 mpdf 不同,它们不会施加 GPL-2.0 限制,非常适合商业和 OEM 产品。与 FPDI 相比,它们提供了一个更现代、更直观的 API,简化了开发过程。
无论您是在构建简单的发票生成器还是复杂的文档管理系统,setasign 库都为您提供了完成工作的工具,并且高效合法。
要点
在 PHP 中处理 PDF 时,选择正确的库至关重要。setasign 库为 GPL 许可的解决方案(如 mpdf)提供了一个强大的、商业许可的替代方案,以及 FPDI 的现代替代方案。通过利用这些库,您可以在应用程序中构建强大的 PDF 相关功能,而无需许可问题,并提高性能和可靠性。
Generate, Read, and Merge PDFs in PHP — Without GPL, Without FPDI
PDFs are a cornerstone of modern web applications. From invoices and tickets to reports and signed contracts, the need to generate, manipulate, and merge PDFs is almost universal in PHP projects. However, the path to achieving this without running into licensing roadblocks and complex dependencies can be tricky. Traditional solutions like mpdf are great, but their GPL-2.0 license can become a headache when integrating into commercial or OEM products. Similarly, libraries like FPDI, while powerful, add layers of complexity. In this post, we’ll explore a robust alternative that lets you generate, read, and merge PDFs in PHP without the GPL and without FPDI.
The Pain Points of Traditional Solutions
Let’s revisit the challenges mentioned in the source article:
- Generating PDFs with
mpdf
mpdf is a widely-used library for generating PDFs in PHP. It’s versatile, feature-rich, and relatively easy to use. However, its GPL-2.0 license requires you to comply with its terms whenever you distribute your application commercially. This can be a significant hurdle for businesses that need to avoid open-source licenses in their products.
```php
// Example: Generating a simple PDF with mPDF
require_once DIR . '/vendor/autoload.php';
$mpdf = new \Mpdf\Mpdf();
$mpdf->WriteHTML('
Hello, World!
');
$mpdf->Output('hello_world.pdf', \Mpdf\Output\Destination::FILE);
```
- Merging or Importing PDFs with FPDI
FPDI (FDF Import) is excellent for importing existing PDF content into your documents. It’s particularly useful for adding headers, footers, or logos to existing PDFs. However, it’s not without its quirks. FPDI’s reliance on external libraries and its somewhat convoluted API can make it a pain to work with, especially for developers who prefer cleaner, more modern solutions.
```php
// Example: Merging PDFs with FPDI (conceptual)
require_once DIR . '/fpdi.php';
$pdf = new FPDI();
$pdf->setSourceFile('page1.pdf');
$templateId = $pdf->importPage(1);
$pdf->AddPage();
$pdf->useTemplate($templateId);
$pdf->Output('merged.pdf', 'F');
```
Given these challenges, it’s clear that a more streamlined, license-friendly solution is needed. Let’s explore an alternative that addresses these pain points head-on.
Introducing a License-Friendly Alternative
One of the most promising alternatives to mpdf and FPDI is the set of libraries provided by the setasign company, specifically PDFlib and FPDI2. These libraries offer a powerful and flexible way to generate, read, and merge PDFs in PHP without the GPL restrictions.
What Makes setasign Libraries Stand Out?
-
Commercial Licensing
Unlike mpdf, which is under GPL-2.0, the setasign libraries are commercially licensed. This means you can use them in commercial, OEM, and on-premise products without worrying about open-source licensing obligations. This is a game-changer for businesses that require strict control over their software licenses.
-
Performance and Reliability
PDFlib is a well-established library known for its performance and reliability. It’s been used in enterprise environments for years and has a proven track record. FPDI2 builds upon the original FPDI but offers a more modern and intuitive API, making it easier to work with.
-
Rich Feature Set
Both PDFlib and FPDI2 offer a comprehensive set of features for PDF manipulation, including text extraction, image rendering, form creation, and more. This makes them suitable for a wide range of PDF-related tasks.
Getting Started with setasign Libraries
Let’s walk through a basic example of how to generate, read, and merge PDFs using these libraries.
1. Generating a PDF with PDFlib
First, you’ll need to install the PDFlib library. You can typically do this via Composer:
composer require setasign/pdflib
Once installed, generating a simple PDF is straightforward:
<?php
require_once __DIR__ . '/vendor/autoload.php';
use setasign\Pdflib\Pdflib;
$pdf = new Pdflib();
$pdf->setOption('licensekey', 'YOUR_LICENSE_KEY');
$pdf->open();
$pdf->addPage();
$pdf->setFont('Helvetica', 'normal', 12);
$pdf->drawText('Hello, World!', 50, 700);
$pdf->close();
$pdf->save('hello_world.pdf', Pdflib::SDF);
Replace 'YOUR_LICENSE_KEY' with your actual license key obtained from the setasign website.
2. Reading a PDF with FPDI2
Next, let’s read an existing PDF using FPDI2. First, install the library via Composer:
composer require setasign/fpdi2
Here’s how you can read a PDF and extract its content:
<?php
require_once __DIR__ . '/vendor/autoload.php';
use setasign\Fpdi\Fpdi;
$pdf = new Fpdi();
$pdf->setOption('licensekey', 'YOUR_LICENSE_KEY');
$pdf->addPage();
$pdf->setSourceFile('example.pdf');
$tplId = $pdf->importPage(1);
$pdf->useTemplate($tplId);
$pdf->Output('read_example.pdf', 'F');
Again, replace 'YOUR_LICENSE_KEY' with your license key.
3. Merging PDFs with FPDI2
Merging PDFs is also straightforward with FPDI2. Here’s an example that merges two PDFs:
<?php
require_once __DIR__ . '/vendor/autoload.php';
use setasign\Fpdi\Fpdi;
$pdf = new Fpdi();
$pdf->setOption('licensekey', 'YOUR_LICENSE_KEY');
// Add the first PDF
$pdf->addPage();
$pdf->setSourceFile('page1.pdf');
$tplId = $pdf->importPage(1);
$pdf->useTemplate($tplId);
// Add the second PDF
$pdf->addPage();
$pdf->setSourceFile('page2.pdf');
$tplId = $pdf->importPage(1);
$pdf->useTemplate($tplId);
$pdf->Output('merged.pdf', 'F');
Advanced Features and Use Cases
The setasign libraries aren’t just for basic PDF manipulation. They offer advanced features that make them suitable for complex use cases:
- Form Creation and Filling
You can create interactive forms and fill them programmatically. This is useful for generating dynamically filled PDFs, such as invoices or contracts.
php
// Example: Creating a form field
$pdf->addPage();
$pdf->setFont('Helvetica', 'bold', 12);
$pdf->drawText('Name:', 50, 750);
$pdf->createTextField('name', 50, 730, 100, 20, array('border' => true));
- Barcode Generation
Generating barcodes is another powerful feature. You can create various types of barcodes, including QR codes, EAN, and UPC.
php
// Example: Generating a QR code
$pdf->addPage();
$pdf->setSourceFile('qrcode.pdf');
$tplId = $pdf->importPage(1);
$pdf->useTemplate($tplId);
- PDF Security
You can add security features such as password protection, encryption, and access permissions to your PDFs.
php
// Example: Adding password protection
$pdf->setProtection(array('print', 'copy'), 'user_password', 'owner_password');
Conclusion: Why Choose setasign Libraries?
The setasign libraries provide a robust, flexible, and license-friendly solution for generating, reading, and merging PDFs in PHP. Unlike mpdf, they don’t impose GPL-2.0 restrictions, making them ideal for commercial and OEM products. Compared to FPDI, they offer a more modern and intuitive API, simplifying the development process.
Whether you’re building a simple invoice generator or a complex document management system, the setasign libraries give you the tools you need to get the job done efficiently and legally.
Takeaway
When working with PDFs in PHP, choosing the right libraries is crucial. The setasign libraries provide a powerful, commercially licensed alternative to GPL-licensed solutions like mpdf, and modern replacements for FPDI. By leveraging these libraries, you can build robust PDF-related features into your applications without the licensing headaches and with improved performance and reliability.