imal-h/pdf-box 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

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

imal-h/pdf-box

Composer 安装命令:

composer require imal-h/pdf-box

包简介

The most advanced, driver-based PDF manipulation library for PHP v3.0. Supports Ghostscript, Chrome Headless (HTML to PDF), OpenSSL/FPDI (Signing), and PDFtk (Forms).

README 文档

README

Banner

Try out before you actually use it

docker run --pull always -p 9090:80 treineticprojects/demo_opensource:latest

PDFLib v3.1

Issues Software License Forks

The most advanced, driver-based PDF manipulation library for PHP.

PDFLib v3.1 is the mature, stable release of the new driver-based architecture. It allows you to switch between powerful backends like Ghostscript, PDFtk, OpenSSL, and Tesseract for different tasks, all under a single, beautiful fluent API.

👉 Try the Interactive Demo | 📚 Read the Documentation

🛡️ Stability Guarantee (v4.0 Ready)

We take backward compatibility seriously.

The v3.1 architecture introduces a strict separation between the API (Facade) and the Execution Logic (Drivers).

  • Zero Breaking Changes Promise: This structure allows us to upgrade the underlying engine (e.g., adding Cloud/Async support in v4.0) without changing a single line of your application code.
  • Pipeline Pattern: Operations like rotate() or ocr() are queued, decoupling your intent from immediate execution. Use PDFLib with confidence knowing the API is frozen and stable.

🚀 What's New in v3.1?

  • OCR Support: Extract text from images and PDFs using Tesseract.
  • Redaction: Securely blackout sensitive text.
  • Metadata: Read/Write PDF metadata.
  • Laravel Wrapper: First-party ServiceProvider and Facade.
  • Stateful Chaining: Queue multiple operations (->rotate()->watermark()->save()).

📦 Requirements

  • PHP >= 8.1
  • Ghostscript >= 9.16 (for GhostscriptDriver)
  • Google Chrome or Chromium (for HTML to PDF)
  • pdftk (PDF Toolkit) (for Form Filling)

Quick Start: Run ./bin/install-dependencies to automatically install these tools.

👉 See Installation Guide for detailed setup instructions on macOS, Ubuntu, and Windows.

🔧 Installation

composer require imal-h/pdf-box

✨ Features

Feature Description Driver
HTML to PDF Generate PDF from HTML/CSS Chrome
Digital Sign Sign PDFs with X.509 Certs OpenSSL
OCR Extract text from PDF/Images Tesseract
Redact Blackout sensitive text Ghostscript
Fill Forms Fill AcroForms (FDF) PDFtk
Inspect Forms Get Field Names PDFtk
Convert PDF to Images (PNG/JPG) Ghostscript
Merge Combine multiple PDFs Ghostscript
Split Extract pages or ranges Ghostscript
Compress Optimize PDF file size Ghostscript
Encrypt Password protection and permissions Ghostscript
Watermark Overlay text on pages Ghostscript
Rotation Rotate pages 90/180/270° Ghostscript
Metadata Edit Title, Author, Keywords Ghostscript
Flatten Burn forms into content Ghostscript

📖 Usage

HTML to PDF (New in v3.0)

Generate PDFs from HTML content or URLs using Chrome Headless.

use ImalH\PDFLib\PDF;

// From HTML String
PDF::init()
    ->driver(PDF::DRIVER_CHROME)
    ->convertFromHtml('<h1>Hello World</h1>', 'output.pdf');

// From URL (Coming Soon)
// PDF::init()->driver(PDF::DRIVER_CHROME)->fromUrl('https://google.com')->save('output.pdf');

Digital Signatures (New in v3.0)

Digitally sign PDFs using OpenSSL (requires tecnickcom/tcpdf).

use ImalH\PDFLib\PDF;

PDF::init()
    ->driver(PDF::DRIVER_OPENSSL)
    ->from('contract.pdf')
    ->sign('certificate.crt', 'private_key.pem', 'signed_contract.pdf', [
        'info' => [
            'Name' => 'John Doe',
            'Location' => 'Colombo, LK',
            'Reason' => 'Digital Contract Signature'
        ],
        // New in v3.2: Visual Signature with Relative Positioning
        'image' => 'signature.png',
        'page' => 1,
        'x' => ($pdf->getPageDimensions(1)['w'] - 60) / 2, // Center X
        'y' => 250,
        'w' => 60,
        'h' => 30
    ]);

Note: If you use a self-signed certificate (like in testing), PDF viewers will show "Signature Validity Unknown". For a green "Trusted" checkmark, use a certificate issued by a recognized Certificate Authority (CA) or explicitly trust your self-signed certificate in the viewer's settings.

Laravel Integration

Publish the config file:

php artisan vendor:publish --tag=pdflib-config

Use the Facade in your controllers:

use ImalH\PDFLib\Laravel\Facades\PDF;

// The driver is automatically configured from config/pdflib.php
PDF::from('upload.pdf')->ocr('output.txt');

OCR (New in v3.1)

Extract text from scanned PDFs or images.

PDF::init()
    ->driver(PDF::DRIVER_TESSERACT)
    ->from('scanned_doc.pdf') // Automatically converts PDF to Image internally
    ->ocr('extracted_text');

Redaction (New in v3.1)

Permanently remove sensitive text.

PDF::init()
    ->driver(PDF::DRIVER_GHOSTSCRIPT)
    ->from('invoice.pdf')
    ->redact('Confidential', 'clean_invoice.pdf');

Interactive Forms

Fill PDF forms programmatically using pdftk.

use ImalH\PDFLib\PDF;

// 1. Inspect Fields (Optional)
$fields = PDF::init()->driver(PDF::DRIVER_PDFTK)->getFormFields('form_template.pdf');
// returns ['full_name', 'date', ...]

// 2. Fill Form
PDF::init()
    ->driver(PDF::DRIVER_PDFTK)
    ->from('form_template.pdf')
    ->fillForm([
        'full_name' => 'Imal Perera',
        'date' => '2025-01-01'
    ], 'filled_form.pdf');

The Modern Way (Fluent API)

use ImalH\PDFLib\PDF;

// Convert PDF Page 1 to JPEG
PDF::init()
    ->driver(PDF::DRIVER_GHOSTSCRIPT)
    ->from('document.pdf')
    ->to('output_folder')
    ->convert();

The Legacy Way (v2.x Facade)

Existing code continues to work without changes, but is marked as deprecated.

use ImalH\PDFLib\PDFLib; // Legacy Class

$pdfLib = new PDFLib();
$pdfLib->setPdfPath("document.pdf")
       ->setOutputPath("output_folder")
       ->convert();

Example: Advanced Chain

PDF::init()
    ->from('source.pdf')
    ->from('source.pdf')
    ->encrypt('userPass', 'ownerPass', 'processed.pdf');

(Note: Current driver operations like encrypt, rotate, and watermark are immediate and require a destination path. Fully stateful chaining for these methods is planned for v3.1)

🔮 Roadmap

Want to see what's coming next (v3.1+)? Check out our Roadmap.

🤝 Contributing

We welcome contributions! Please see CONTRIBUTING for details on our new coding standards (Pint, PHPStan) and architecture.

📄 License

The MIT License (MIT). Please see License File.

Initiative of Treinetic (Pvt) Ltd.

imal-h/pdf-box 适用场景与选型建议

imal-h/pdf-box 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 415.35k 次下载、GitHub Stars 达 60, 最近一次更新时间为 2016 年 09 月 05 日, 在 PHP 生态内属于活跃度较高的组件。

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

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

围绕 imal-h/pdf-box 我们能提供哪些服务?
定制开发 / 二次开发

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

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

GitHub 信息

  • Stars: 60
  • Watchers: 7
  • Forks: 38
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2016-09-05