kallefrombosnia/tinypdf-php
Composer 安装命令:
composer require kallefrombosnia/tinypdf-php
包简介
Minimal PDF creation library for PHP 8.2+. Zero dependencies, strongly typed, clean code.
README 文档
README
Minimal PDF creation library for PHP 8.2+. Zero dependencies, strongly typed, clean code architecture.
Port of tinypdf from TypeScript to PHP.
Features
- Create PDFs from scratch with zero dependencies
- Strongly typed with PHP 8.2+ type declarations
- Clean architecture with interfaces and value objects
- Support for text, rectangles, lines, JPEG images, and clickable links
- Built-in markdown to PDF conversion
- Text measurement and alignment
- Color support (hex format)
Installation
composer require kallefrombosnia/tinypdf-php
Usage
Basic PDF Creation
use TinyPdf\TinyPdf; use TinyPdf\TextOptions; use TinyPdf\TextAlign; $pdf = TinyPdf::create(); // Add a page (default US Letter: 612x792 points) $pdf->page(function ($ctx) { // Add text $ctx->text('Hello World!', 50, 700, 24); // Text with options $ctx->text( 'Centered text', 50, 650, 16, new TextOptions( align: TextAlign::CENTER, width: 500, color: '#0066cc' ) ); // Draw a rectangle $ctx->rect(50, 600, 200, 100, '#ffcc00'); // Draw a line $ctx->line(50, 580, 250, 580, '#000000', 2); }); // Custom page size $pdf->page(595, 842, function ($ctx) { // A4 size $ctx->text('A4 Page', 50, 700, 18); }); // Build and save $pdfContent = $pdf->build(); file_put_contents('output.pdf', $pdfContent);
Adding Images
$pdf = TinyPdf::create(); $pdf->page(function ($ctx) { $jpegData = file_get_contents('image.jpg'); $ctx->image($jpegData, 50, 500, 200, 150); }); file_put_contents('output.pdf', $pdf->build());
Markdown to PDF
use TinyPdf\MarkdownConverter; $markdown = <<<MD # Document Title This is a paragraph with **bold** text. ## Section Header - List item 1 - List item 2 - List item 3 1. Numbered item 2. Another item --- Another paragraph after a horizontal rule. MD; $converter = new MarkdownConverter( width: 612, // US Letter width height: 792, // US Letter height margin: 72 // 1 inch margins ); $pdfContent = $converter->convert($markdown); file_put_contents('document.pdf', $pdfContent);
Adding Clickable Links
use TinyPdf\LinkOptions; $pdf = TinyPdf::create(); $pdf->page(function ($ctx) { $ctx->text('GitHub Repository', 50, 700, 12, new TextOptions(color: '#0066cc')); $ctx->link('https://github.com/kallefrombosnia/tinypdf-php', 50, 695, 115, 20, new LinkOptions(underline: '#0066cc')); $ctx->text('Check out Webflow agency', 50, 650, 12, new TextOptions(color: '#95a5a6')); $ctx->link('https://www.flowout.com/', 50, 645, 150, 20); }); file_put_contents('output.pdf', $pdf->build());
Text Measurement
use TinyPdf\TinyPdf; $width = TinyPdf::measureText('Hello World', 24); echo "Text width: {$width} points\n";
Examples
The library includes several complete examples in the examples/ directory:
Running Examples
Run all examples at once:
php example.php
Or run individual examples:
# Basic text and shapes php examples/basic/basic.php # Professional invoice php examples/invoice/invoice.php # Markdown to PDF php examples/markdown/markdown.php # Multi-page document php examples/multipage/multipage.php
Available Examples
Each example includes its own README with detailed information and generated PDF output.
- Basic - Text rendering, shapes, colors, alignment, and links (preview)
- Invoice - Professional invoice template (preview)
- Receipt - Sales receipt template (preview)
- Report - Business report with charts (preview)
- Shipping Label - Package shipping label (preview)
- Markdown - Convert markdown to PDF (preview)
- Multi-Page - Multi-page documents (preview)
- Ticket - Event ticket design (preview)
- Data Export - Export data tables (preview)
- Service Agreement - Contract template (preview)
API Reference
For detailed API documentation, see the inline documentation in the source code:
- TinyPdf - Main factory class
- PageContextInterface - Drawing operations (text, rect, line, image, link)
- TextOptions - Text rendering options
- LinkOptions - Link configuration
- MarkdownConverter - Markdown to PDF conversion
Coordinate System
PDF uses a coordinate system where:
- Origin (0, 0) is at the bottom-left corner
- X increases to the right
- Y increases upward
- Units are in points (1/72 inch)
Common page sizes:
- US Letter: 612 x 792 points
- A4: 595 x 842 points
Testing & Quality
composer test # Run all tests composer test:coverage # Run tests with coverage composer phpstan # Run static analysis
77 tests with comprehensive coverage including unit tests, architecture tests, and integration tests.
For detailed information about the test suite, coverage, and quality standards, see tests/README.md.
License
MIT
Credits
This is a PHP port of tinypdf by Lulzx.
kallefrombosnia/tinypdf-php 适用场景与选型建议
kallefrombosnia/tinypdf-php 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 2 次下载、GitHub Stars 达 25, 最近一次更新时间为 2025 年 12 月 22 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「markdown」 「pdf」 「document」 「pdf-generation」 「tinypdf」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 kallefrombosnia/tinypdf-php 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 kallefrombosnia/tinypdf-php 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 kallefrombosnia/tinypdf-php 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Texy converts plain text in easy to read Texy syntax into structurally valid (X)HTML. It supports adding of images, links, nested lists, tables and has full support for CSS. Texy supports hyphenation of long words (which reflects language rules), clickable emails and URL (emails are obfuscated again
Invalidity check of documents in the database of Ministry of the Interior of the Czech Republic
Adds more BBCode
Document indexation and migration tool for Elasticsearch
Creates a markdown changelog for your GitHub repository.
Provides TCPDF integration for Symfony
统计信息
- 总下载量: 2
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 26
- 点击次数: 12
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2025-12-22
