定制 antradar/gspdf 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

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

antradar/gspdf

Composer 安装命令:

composer require antradar/gspdf

包简介

High-performance PDF generation library with streaming architecture, O(1) memory usage, and 33-153x faster HTML rendering than TCPDF

README 文档

README

License: MIT PHP Version

GSPDF is a high-performance PDF generation library for PHP with streaming architecture that achieves O(1) memory usage and delivers 5-153x faster performance than TCPDF.

🚀 Key Features

  • Streaming Architecture - O(1) memory usage, handles unlimited pages
  • 33-153x Faster than TCPDF for HTML rendering
  • 5-10x Faster for basic PDF operations
  • TCPDF-Compatible Coordinates - Top-left origin (Y=0 at top, Y increases downward)
  • HTML Module - Streaming HTML-to-PDF with SAX parser
  • PCS Support - Import and insert PDF content streams with transformations
  • Placeholder Strategy - Two-pass generation with configurable storage
  • Minimal Dependencies - Core library has zero dependencies
  • Font Subsetting - Automatic font subsetting for reduced file sizes
  • QR Code Support - Generate QR codes with custom styling
  • Image Support - JPEG-only (PNG deliberately disabled for performance)
  • Production Ready - Memcache/Redis storage backends for distributed systems

📦 Installation

Requirements

  • PHP 7.3+ (tested on 7.3, 7.4, 8.x)
  • No required extensions for core functionality
  • Optional: php-memcached for Memcache storage backend
  • Optional: php-xml (XMLReader) for HTML module (usually included by default)
  • Optional: phpqrcode library for QR code generation

Option 1: Composer (Recommended)

composer require antradar/gspdf

Then use Composer's autoloader:

require_once 'vendor/autoload.php';

use GSPDF\GSPDF;

$pdf = new GSPDF();

Option 2: Manual Installation (No Composer Required)

We maintain full support for non-Composer installations, as many libraries only offer Composer options.

  1. Clone or download this repository
  2. Include the required files manually:
require_once 'gspdf-php/src/StreamWriter.php';
require_once 'gspdf-php/src/ObjectTracker.php';
require_once 'gspdf-php/src/PageBuilder.php';
require_once 'gspdf-php/src/GSPDF.php';

use GSPDF\GSPDF;

Note: Manual installation gives you full control and zero dependency on Composer infrastructure.

🎯 Quick Start

Hello World

use GSPDF\GSPDF;

$pdf = new GSPDF();
$pdf->addPage();
$pdf->SetFont('helvetica', '', 12);
$pdf->text(50, 50, 'Hello, World!');
$pdf->output('hello.pdf', 'F');

HTML Rendering

use GSPDF\GSPDF;
use GSPDF\HTML\HTMLModule;

$pdf = new GSPDF();
$htmlModule = new HTMLModule($pdf);
$pdf->loadHTMLModule($htmlModule);

$pdf->addPage();

$html = '<h1>Welcome to GSPDF</h1>
<p>This is <b>bold</b> and <i>italic</i> text.</p>
<ul>
    <li>Fast performance</li>
    <li>Low memory usage</li>
    <li>Easy to use</li>
</ul>';

$pdf->writeHTML($html);
$pdf->output('document.pdf', 'F');

QR Code Generation

// Download phpqrcode from: http://phpqrcode.sourceforge.net/

$pdf = new GSPDF([
    'qr_lib_path' => '/path/to/phpqrcode.php'
]);

$pdf->addPage();
$pdf->write2DBarcode(
    'https://github.com/your-repo/gspdf',
    'QRCODE,M',
    50, 50, 100, 100,
    ['border' => true, 'padding' => 2]
);
$pdf->output('qrcode.pdf', 'F');

📚 Documentation

🎨 Features

Streaming Architecture

GSPDF uses a streaming architecture that writes PDF content directly to output as it's generated:

  • O(1) Memory Usage - Memory usage stays constant regardless of document size
  • Immediate Flushing - Pages are written immediately, not stored in memory
  • Scalable - Generate documents with thousands of pages without memory issues

HTML Support

The HTML module provides streaming HTML-to-PDF conversion:

  • Supported tags: p, br, b, i, u, strong, em, h1-h6, ul, ol, li, table, tr, td, th, div, span
  • CSS support: font-family, font-size, color, background-color, text-align, font-weight, font-style
  • Streaming parser: SAX-style parsing with O(1) memory
  • 33-153x faster than TCPDF for HTML rendering

Font Subsetting

Automatic font subsetting reduces PDF file sizes:

  • Only embeds glyphs that are actually used
  • Range-based character tracking with O(log n) memory
  • Supports Unicode fonts (CJK, Arabic, etc.)
  • Typical savings: 50-95% depending on character usage

Image Embedding: JPEG-Only by Design

GSPDF deliberately supports JPEG only and disables PNG embedding. This design decision reflects our relentless focus on performance:

  • Direct Compression - JPEG data is embedded directly using PDF's DCTDecode filter (no re-encoding)
  • Zero Processing - JPEG files are copied byte-for-byte into the PDF stream
  • Optimal Performance - No decompression, no pixel manipulation, no memory overhead
  • Smaller Files - JPEG compression is ideal for photos and complex images

Why not PNG?

  • PNG requires decompression and re-encoding for PDF
  • Adds processing overhead and memory usage
  • Increases generation time significantly
  • For web graphics and screenshots, convert to JPEG first

Recommendation: Use JPEG for all images. For logos/graphics that require transparency, consider SVG or vector graphics instead.

Placeholder Strategy

Two-pass PDF generation for dynamic content:

  • First pass: Generate draft PDF with placeholders
  • Second pass: Replace placeholders with actual values
  • Storage backends: Filesystem, Memcache, Redis
  • Perfect for page numbers, totals, cross-references

📊 Performance

Benchmark results (GSPDF vs TCPDF):

Operation GSPDF TCPDF Speedup
Simple text (100 pages) 0.15s 0.75s 5x faster
HTML rendering (complex) 0.12s 18.4s 153x faster
HTML rendering (simple) 0.09s 3.0s 33x faster
Memory usage O(1) O(n) Constant

🤝 Contributing

Contributions are welcome! Please feel free to submit pull requests or open issues.

📄 License

MIT License

Copyright (c) 2025 Antradar Software Inc.

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

👤 Author

Schien Dong
Antradar Software Inc.

🙏 Acknowledgments

  • Inspired by TCPDF and FPDF
  • Uses phpqrcode library for QR code generation (optional dependency)
  • Font subsetting based on TrueType specification

📞 Support

For issues, questions, or feature requests, please open an issue on GitHub.

antradar/gspdf 适用场景与选型建议

antradar/gspdf 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 2.14k 次下载、GitHub Stars 达 2, 最近一次更新时间为 2025 年 11 月 23 日, 在 PHP 生态内属于活跃度较高的组件。

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

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

围绕 antradar/gspdf 我们能提供哪些服务?
定制开发 / 二次开发

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2025-11-23