laravel-dompdf

barryvdhMIT更新放缓

在 Laravel 里用 DOMPDF 把 HTML 直接转成 PDF,省去手动配置底层库的麻烦,适合生成发票、报表这类文档。

A DOMPDF Wrapper for Laravel

v3.1.2发布于 2026-03-17 15:52 UTC7,297star +3 / 7 天975fork同步于 2026-11-10 23:34 UTC

怎么上手

由 README 提炼,执行前请确认
composer require barryvdh/laravel-dompdf
硬件要求按 README 说明

README 未说明

下载:源码 zip·GitHub Releases(安装包)本站不镜像安装包

项目说明

由 README 自动翻译提炼

Laravel 的 DOMPDF 封装包,用 HTML 视图或字符串快速生成可下载或流式输出的 PDF

在 Laravel 里用 DOMPDF 把 HTML 直接转成 PDF,省去手动配置底层库的麻烦,适合生成发票、报表这类文档。

核心能力

  • 通过 loadView、loadHTML、loadFile 加载内容生成 PDF
  • 支持 download、stream、save、output 多种输出方式
  • 可链式设置纸张尺寸与横向方向并关闭警告
  • 用 vendor:publish 发布 config/dompdf.php 修改默认配置
  • 支持 PDF/A-3b 及 Factur-X 嵌入 XML 发票附件

容易踩的坑

3.x 起默认关闭远程资源访问,加载远程图片或样式需手动开启 isRemoteEnabled;PDF/A 要求字体全部嵌入,Helvetica、Courier、Times 等核心字体不嵌入会导致校验失败;Lumen 需在 bootstrap/app.php 手动注册 ServiceProvider 并 configure('dompdf')

内容由模型根据 README 生成,可能不完整或不准确,以官方文档为准。看原文 README

最近发版

5 个版本v3.1.2
  1. v3.1.2最新正式版2026-03-17 · 2026-03-17 15:52 UTC

    What's Changed

    New Contributors

    Full Changelog: https://github.com/barryvdh/laravel-dompdf/compare/v3.1.1...v3.1.2

  2. v3.1.12025-02-21 · 2025-02-21 15:53 UTC
    查看更新说明
  3. v3.1.02025-01-21 · 2025-01-21 20:25 UTC
    查看更新说明

    This release updates the config for dompdf/dompdf v3.1.0 which contains the. following breaking URL:

    Breaking Change This release adds the "data://" scheme to the protocol validation rules. Installations that explicitly define the allowed protocols but do not include the "data://" protocol will no longer render data-URIs. This is a change from previous versions, where data-URIs were not processed through the validated rules. Installations that use the default validation rules included with Dompdf should see no impact.

    The update for laravel-dompdf adds this to the default config, but if you have published the config, you need to add the data:// scheme.

    What's Changed

    New Contributors

    Full Changelog: https://github.com/barryvdh/laravel-dompdf/compare/v3.0.1...v3.1.0

  4. v3.0.12025-01-08 · 2025-01-08 17:37 UTC
    查看更新说明

    What's Changed

    Full Changelog: https://github.com/barryvdh/laravel-dompdf/compare/v3.0.0...v3.0.1

  5. v3.0.02024-08-09 · 2024-08-09 12:44 UTC
    查看更新说明

    Version 3.x supports DomPDF version 3.x. See the changelog in https://github.com/dompdf/dompdf/releases/tag/v3.0.0

    The most notable change in laravel-dompdf are the changed defaults, to be more secure;

    • enable_remote is now false by default. Change with caution.
    • allowedRemoteHosts and artifactPathValidation are added the the config. Also, support for Laravel < 9 and PHP < 8.1 is dropped.

    What's Changed

    New Contributors

    Full Changelog: https://github.com/barryvdh/laravel-dompdf/compare/v2.2.0...v3.0.0

用户点评

登录用户的真实使用记录,可按版本和硬件筛选
共 0 条

    还没有人点评。第一个说出真实体验的人最有价值。

    README 原文

    未翻译

    DOMPDF Wrapper for Laravel

    Laravel wrapper for Dompdf HTML to PDF Converter

    Tests Packagist License Latest Stable Version Total Downloads Fruitcake

    Installation

    Laravel

    Require this package in your composer.json and update composer. This will download the package and the dompdf + fontlib libraries also.

    composer require barryvdh/laravel-dompdf
    

    Lumen

    After updating composer add the following lines to register provider in bootstrap/app.php

    $app->register(\Barryvdh\DomPDF\ServiceProvider::class);
    

    To change the configuration, copy the config file to your config folder and enable it in bootstrap/app.php:

    $app->configure('dompdf');
    

    Using

    You can create a new DOMPDF instance and load a HTML string, file or view name. You can save it to a file, or stream (show in browser) or download.

        use Barryvdh\DomPDF\Facade\Pdf;
    
        $pdf = Pdf::loadView('pdf.invoice', $data);
        return $pdf->download('invoice.pdf');
    

    or use the App container:

        $pdf = App::make('dompdf.wrapper');
        $pdf->loadHTML('<h1>Test</h1>');
        return $pdf->stream();
    

    Or use the facade:

    You can chain the methods:

        return Pdf::loadFile(public_path().'/myfile.html')->save('/path-to/my_stored_file.pdf')->stream('download.pdf');
    

    You can change the orientation and paper size, and hide or show errors (by default, errors are shown when debug is on)

        Pdf::loadHTML($html)->setPaper('a4', 'landscape')->setWarnings(false)->save('myfile.pdf')
    

    If you need the output as a string, you can get the rendered PDF with the output() function, so you can save/output it yourself.

    Use php artisan vendor:publish to create a config file located at config/dompdf.php which will allow you to define local configurations to change some settings (default paper etc). You can also use your ConfigProvider to set certain keys.

    Configuration

    The defaults configuration settings are set in config/dompdf.php. Copy this file to your own config directory to modify the values. You can publish the config using this command:

        php artisan vendor:publish --provider="Barryvdh\DomPDF\ServiceProvider"
    

    You can still alter the dompdf options in your code before generating the pdf using this command:

        Pdf::setOption(['dpi' => 150, 'defaultFont' => 'sans-serif']);
    

    Available options and their defaults:

    • rootDir: "{app_directory}/vendor/dompdf/dompdf"
    • tempDir: "/tmp" (available in config/dompdf.php)
    • fontDir: "{app_directory}/storage/fonts" (available in config/dompdf.php)
    • fontCache: "{app_directory}/storage/fonts" (available in config/dompdf.php)
    • chroot: "{app_directory}" (available in config/dompdf.php)
    • logOutputFile: "/tmp/log.htm"
    • defaultMediaType: "screen" (available in config/dompdf.php)
    • defaultPaperSize: "a4" (available in config/dompdf.php)
    • defaultFont: "serif" (available in config/dompdf.php)
    • dpi: 96 (available in config/dompdf.php)
    • fontHeightRatio: 1.1 (available in config/dompdf.php)
    • isPhpEnabled: false (available in config/dompdf.php)
    • isRemoteEnabled: false (available in config/dompdf.php)
    • isJavascriptEnabled: true (available in config/dompdf.php)
    • isHtml5ParserEnabled: true (available in config/dompdf.php)
    • allowedRemoteHosts: null (available in config/dompdf.php)
    • isFontSubsettingEnabled: false (available in config/dompdf.php)
    • isPdfAEnabled: false (available in config/dompdf.php)
    • debugPng: false
    • debugKeepTemp: false
    • debugCss: false
    • debugLayout: false
    • debugLayoutLines: true
    • debugLayoutBlocks: true
    • debugLayoutInline: true
    • debugLayoutPaddingBox: true
    • pdfBackend: "CPDF" (available in config/dompdf.php)
    • pdflibLicense: ""
    • adminUsername: "user"
    • adminPassword: "password"
    • artifactPathValidation: null (available in config/dompdf.php)
    Note: Since 3.x the remote access is disabled by default, to provide more security. Use with caution!

    PDF/A-3b Support

    Requires dompdf >= 3.1.0 and the CPDF backend (default).

    You can generate PDF/A-3b compliant PDFs for archival purposes. Enable it globally via config or per-PDF at runtime.

    Global (config/dompdf.php):

        'pdfa' => [
            'enabled' => true,
        ],
    

    Per-PDF at runtime:

        $pdf = Pdf::loadView('invoice', $data)
            ->setPdfA()
            ->download('invoice.pdf');
    

    Important: PDF/A requires all fonts to be embedded. Core PDF fonts (Helvetica, Courier, Times) are not embedded and will cause validation failures. Use fonts like DejaVu Sans or DejaVu Serif instead.

    Zugferd / Factur-X (embedded XML invoices)

    For PDF/A-3b with embedded XML attachments (e.g. Zugferd/Factur-X electronic invoicing), you can use addEmbeddedFile() and setAdditionalXmpRdf():

        $pdf = Pdf::loadView('invoice', $data)->setPdfA();
    
        // Add XMP metadata for Factur-X
        $pdf->setAdditionalXmpRdf(
            '<rdf:Description rdf:about=""
                xmlns:fx="urn:factur-x:pdfa:CrossIndustryDocument:invoice:1p0#">
                <fx:DocumentType>INVOICE</fx:DocumentType>
                <fx:DocumentFileName>factur-x.xml</fx:DocumentFileName>
                <fx:Version>1.0</fx:Version>
                <fx:ConformanceLevel>BASIC</fx:ConformanceLevel>
            </rdf:Description>'
        );
    
        // Embed the XML invoice
        $pdf->addEmbeddedFile(
            storage_path('invoices/factur-x.xml'),
            'factur-x.xml',
            'Factur-X Invoice',
            'text/xml'
        );
    
        return $pdf->download('invoice.pdf');
    

    Tip: UTF-8 support

    In your templates, set the UTF-8 Metatag:

    <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
    

    Tip: Page breaks

    You can use the CSS page-break-before/page-break-after properties to create a new page.

    <style>
    .page-break {
        page-break-after: always;
    }
    </style>
    <h1>Page 1</h1>
    <div class="page-break"></div>
    <h1>Page 2</h1>
    

    License

    This DOMPDF Wrapper for Laravel is open-sourced software licensed under the MIT license