定制 himelali/pdf-generator 二次开发

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

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

himelali/pdf-generator

Composer 安装命令:

composer require himelali/pdf-generator

包简介

Laravel wrapper for Snappy, DomPDF, mPDF, fPDF

README 文档

README

Total Downloads License Stars Forks

📄 PdfGenerator for Laravel

PdfGenerator is a flexible and extensible PDF generation wrapper for Laravel that supports multiple drivers including DomPDF, Snappy (wkhtmltopdf), mPDF, and FPDF — all with a unified interface.

🚀 Features

  • Multiple driver support: dompdf, snappy, mpdf, fpdf
  • Unified API for all drivers
  • Configurable driver options
  • Easily extendable with custom drivers
  • Laravel service provider and config publishing
  • Supports UTF-8, HTML5, CSS3, custom fonts, header/footer, margins, and more

📦 Installation

composer require himelali/pdf-generator

⚙️ Configuration

🧪 Laravel Version Compatibility

Depending on your Laravel version, register the service provider as follows:

Laravel 11+

Edit bootstrap/providers.php:

return [
    // Other providers...
    \Himelali\PdfGenerator\PdfServiceProvider::class,
];

Laravel 10 and below

'providers' => [
    // Other providers...
    \Himelali\PdfGenerator\PdfServiceProvider::class,
],

For older versions, register the alias manually in config/app.php:

'aliases' => Facade::defaultAliases()->merge([
    'Pdf' => Himelali\PdfGenerator\Facades\Pdf::class,
])->toArray(),

⚠️ If Facade::defaultAliases() doesn't exist in your Laravel version, just use:

'aliases' => [
    // Other aliases...
    'Pdf' => Himelali\PdfGenerator\Facades\Pdf::class,
],

The service provider will register the PDF manager and allow you to publish the config file using:

php artisan vendor:publish --tag=config

This will publish config/pdf-generator.php. Example configuration:

return [
    'default' => env('PDF_DRIVER', 'dompdf'),

    'drivers' => [
        'dompdf' => [
            'options' => [
                'is_html5_parser_enabled' => true,
                'is_php_enabled' => true,
            ],
        ],
        'snappy' => [
            'binary' => env('SNAPPY_PDF_BINARY', '/usr/local/bin/wkhtmltopdf'),
            'options' => [
                'page-size' => 'A4',
                'margin-top' => '10mm',
                'margin-bottom' => '10mm',
                'footer-left' => 'Your Company Name',
                'footer-center' => '[page]',
                'footer-right' => 'Confidential',
            ],
        ],
        'mpdf' => [
            'format' => 'A4',
            'orientation' => 'P',
            'margin_top' => 10,
            'margin_bottom' => 10,
            'margin_left' => 10,
            'margin_right' => 10,
            'temp_dir' => storage_path('app/mpdf/temp'),
            'font_dir' => storage_path('app/pdf/fonts'),
        ],
        'fpdf' => [
            'orientation' => 'P',
            'unit' => 'mm',
            'format' => 'A4',
            'font_dir' => storage_path('app/pdf/fonts'),
        ],
    ],
];

🧩 Supported Drivers

Driver Package Dependency Binary Required
dompdf dompdf/dompdf
snappy knplabs/knp-snappy + wkhtmltopdf binary link
mpdf mpdf/mpdf
fpdf setasign/fpdf

Install the required dependencies based on your driver.

use Pdf;

$html = '<h1>Hello PDF</h1>';
Pdf::loadHtml($html)->download('hello.pdf');
Pdf::setPageSize('A4')
    ->setMargins(10, 15, 10, 15)
    ->setHeader('<p>Header HTML</p>')
    ->setFooter('<p>Footer HTML</p>')
    ->loadHtml('<h1>With Headers and Margins</h1>')
    ->download('report.pdf');

📂 Available Methods

Method Description
setPageSize() Set paper size (e.g., A4, Letter)
setMargins() Set page margins (top, right, bottom, left)
setHeader() Set HTML for header
setFooter() Set HTML for footer
setFooterText() Set simple text footer (fallback)
setPageNumbers() Enable or disable page numbers
setFonts() Load custom fonts
loadHtml() Load HTML content
download() Download the PDF
stream() Stream the PDF in browser
Pdf::extend('custom', \App\Pdf\CustomPdfDriver::class);
Pdf::driver('custom')->loadHtml('<h1>Custom Driver</h1>')->download();

❗Exceptions

Exception Class When it’s Thrown
BinaryNotFoundException wkhtmltopdf binary missing (Snappy only)
InvalidDriverException Driver not found in config or map
PdfGenerationException General rendering failure
RenderException Library-specific render issues

✅ Requirements

License

This package is open-sourced software licensed under the MIT license.

Please note: This is a wrapper around the following libraries, each of which is maintained under its own license:

Make sure to review and comply with each library’s license if you use this package in your projects.

himelali/pdf-generator 适用场景与选型建议

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

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

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

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

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

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

GitHub 信息

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

其他信息

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