iamgerwin/php-pdf-to-markdown-parser
Composer 安装命令:
composer require iamgerwin/php-pdf-to-markdown-parser
包简介
A lightweight PHP library to convert PDF documents into clean, structured Markdown. Supports text extraction, headings, lists, tables, diagrams and code blocks for easier content reuse and publishing.
README 文档
README
A lightweight PHP library to convert PDF documents into clean, structured Markdown. Supports text extraction, headings, lists, tables, diagrams and code blocks for easier content reuse and publishing.
Because sometimes PDFs just need to chill out and become Markdown.
Features
- 📝 Text Extraction with Styling - Preserves headings, bold, italic, and strikethrough formatting
- 📊 Table Parsing - Extracts tables with proper headers and body formatting
- 🎨 Diagram Support - Converts diagrams to Mermaid and dbdiagram.io formats
- Flowcharts
- Sequence diagrams
- Entity Relationship Diagrams (ERD)
- Gantt charts
- Class diagrams
- State diagrams
- Pie charts
- 📋 List Detection - Automatically converts bullet points and numbered lists
- 💻 Code Block Recognition - Identifies and formats code snippets
- 🚀 PHP 8.3 Compatible - Built with modern PHP features
- ✅ PSR-12 Compliant - Follows PHP coding standards
Installation
You can install the package via composer:
composer require iamgerwin/php-pdf-to-markdown-parser
Usage
Basic Usage
use Iamgerwin\PdfToMarkdownParser\PdfToMarkdownParser; $parser = new PdfToMarkdownParser(); // Parse a PDF file $markdown = $parser->parseFile('path/to/document.pdf'); // Parse PDF content $pdfContent = file_get_contents('path/to/document.pdf'); $markdown = $parser->parseContent($pdfContent); // Output the markdown echo $markdown;
Working with Tables
The parser automatically detects and converts tables in your PDF:
| Header 1 | Header 2 | Header 3 | | --- | --- | --- | | Row 1 Col 1 | Row 1 Col 2 | Row 1 Col 3 | | Row 2 Col 1 | Row 2 Col 2 | Row 2 Col 3 |
Diagram Extraction
Diagrams are automatically detected and converted to appropriate formats:
Mermaid Flowcharts:
```mermaid flowchart TD Start --> Process --> End
**ERD (dbdiagram.io format):**
```markdown
```dbdiagram
Table users {
id int
name varchar
email varchar
}
**Sequence Diagrams:**
```markdown
```mermaid
sequenceDiagram
User->>System: Request
System->>Database: Query
Database->>System: Response
System->>User: Result
### Text Styling
The parser preserves text styling from PDFs:
- Headings (H1-H6) based on font size and formatting
- **Bold text**
- *Italic text*
- ~~Strikethrough text~~
- Lists (bulleted and numbered)
- Code blocks
## Advanced Configuration
### Custom Extractors
You can extend the parser with custom extractors:
```php
use Iamgerwin\PdfToMarkdownParser\PdfToMarkdownParser;
use Iamgerwin\PdfToMarkdownParser\Extractors\TextExtractor;
use Iamgerwin\PdfToMarkdownParser\Extractors\TableExtractor;
use Iamgerwin\PdfToMarkdownParser\Extractors\DiagramExtractor;
$parser = new PdfToMarkdownParser();
// The parser uses these extractors internally:
// - TextExtractor: Handles text and styling
// - TableExtractor: Processes tables
// - DiagramExtractor: Converts diagrams
Testing
Run the test suite:
composer test
Run tests with coverage:
composer test-coverage
Run PHPStan static analysis:
composer analyse
Format code with Laravel Pint:
composer format
Requirements
- PHP 8.3 or higher
- ext-mbstring
How It Works
The parser uses a multi-stage extraction process:
- PDF Parsing - Uses the robust smalot/pdfparser library to extract raw content
- Text Analysis - Identifies text styling, headings, and formatting patterns
- Table Detection - Recognizes table structures (pipe, tab, or space-separated)
- Diagram Recognition - Detects diagram patterns and converts to Mermaid/dbdiagram formats
- Markdown Generation - Combines all elements into properly formatted Markdown
Limitations
- Images: Currently, images are not extracted (coming in future versions)
- Complex Layouts: Multi-column layouts may require manual adjustment
- Font Styling: Basic bold/italic detection is simplified (font metadata parsing is limited)
- Diagrams: Pattern matching may not catch all diagram types
Changelog
Please see CHANGELOG for more information on what has changed recently.
Contributing
Contributions are welcome! Please feel free to submit a Pull Request.
Security
If you discover any security related issues, please email iamgerwin@live.com instead of using the issue tracker.
Credits
License
The MIT License (MIT). Please see License File for more information.
Acknowledgments
Built with inspiration from the PHP community and the need to make PDF content more accessible and reusable. Special thanks to the maintainers of smalot/pdfparser for their excellent PDF parsing library.
iamgerwin/php-pdf-to-markdown-parser 适用场景与选型建议
iamgerwin/php-pdf-to-markdown-parser 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 5.08k 次下载、GitHub Stars 达 7, 最近一次更新时间为 2025 年 09 月 30 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「parser」 「markdown」 「pdf」 「converter」 「tables」 「diagrams」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 iamgerwin/php-pdf-to-markdown-parser 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 iamgerwin/php-pdf-to-markdown-parser 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 iamgerwin/php-pdf-to-markdown-parser 相关的其它包
同方向 / 同关键字的高下载量 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
Easy to use SDK with grabber for multiple platforms at once like YouTube, Dailymotion, Facebook and more.
An MT940 bank statement parser for PHP
Adds more BBCode
Laravel integration for the jsonapi.org parser
A modern HTML DOM parser for PHP using DOMDocument and Symfony CssSelector.
统计信息
- 总下载量: 5.08k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 7
- 点击次数: 18
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2025-09-30