antradar/gspdf 问题修复 & 功能扩展

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

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

antradar/gspdf

最新稳定版本:v1.0.1

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.

统计信息

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

GitHub 信息

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

其他信息

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