3.x 起默认关闭远程资源访问,加载远程图片或样式需手动开启 isRemoteEnabled;PDF/A 要求字体全部嵌入,Helvetica、Courier、Times 等核心字体不嵌入会导致校验失败;Lumen 需在 bootstrap/app.php 手动注册 ServiceProvider 并 configure('dompdf')
composer require barryvdh/laravel-dompdfREADME 未说明
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 发票附件
容易踩的坑
- v3.1.2最新正式版2026-03-17 · 2026-03-17 15:52 UTC
What's Changed
- Fix typo in dompdf.php comment by @chengkangzai in https://github.com/barryvdh/laravel-dompdf/pull/1076
- PHP 8.5 tests by @erikn69 in https://github.com/barryvdh/laravel-dompdf/pull/1079
- Laravel 13.x Compatibility by @laravel-shift in https://github.com/barryvdh/laravel-dompdf/pull/1083
New Contributors
- @chengkangzai made their first contribution in https://github.com/barryvdh/laravel-dompdf/pull/1076
Full Changelog: https://github.com/barryvdh/laravel-dompdf/compare/v3.1.1...v3.1.2
- v3.1.12025-02-21 · 2025-02-21 15:53 UTC
查看更新说明
What's Changed
- Laravel 12.x Support by @erikn69 in https://github.com/barryvdh/laravel-dompdf/pull/1072
Full Changelog: https://github.com/barryvdh/laravel-dompdf/compare/v3.1.0...v3.1.1
- 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
- Update allowed_protocols config for compatibility with Dompdf v3.1 by @wout-o in https://github.com/barryvdh/laravel-dompdf/pull/1071
New Contributors
- @wout-o made their first contribution in https://github.com/barryvdh/laravel-dompdf/pull/1071
Full Changelog: https://github.com/barryvdh/laravel-dompdf/compare/v3.0.1...v3.1.0
- v3.0.12025-01-08 · 2025-01-08 17:37 UTC
查看更新说明
What's Changed
- Redirect rendering issues by adding
contact_linksby @erikn69 in https://github.com/barryvdh/laravel-dompdf/pull/1057 - Test against php 8.4 by @sergiy-petrov in https://github.com/barryvdh/laravel-dompdf/pull/1066
Full Changelog: https://github.com/barryvdh/laravel-dompdf/compare/v3.0.0...v3.0.1
- Redirect rendering issues by adding
- 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_remoteis nowfalseby default. Change with caution.allowedRemoteHostsandartifactPathValidationare added the the config. Also, support for Laravel < 9 and PHP < 8.1 is dropped.
What's Changed
- Test against php 8.3 and laravel 10.x by @sergiy-petrov in https://github.com/barryvdh/laravel-dompdf/pull/1005
- Fix workflow by @barryvdh in https://github.com/barryvdh/laravel-dompdf/pull/1048
- Allow Dompdf3, add new options by @barryvdh in https://github.com/barryvdh/laravel-dompdf/pull/1047
- Only support DomPDF 3 by @barryvdh in https://github.com/barryvdh/laravel-dompdf/pull/1049
- Check filename, add fallback filename by @barryvdh in https://github.com/barryvdh/laravel-dompdf/pull/1050
- Updating the codestyle of the configuration file with laravel/pint. by @maks-oleksyuk in https://github.com/barryvdh/laravel-dompdf/pull/1051
- Fix badges by @erikn69 in https://github.com/barryvdh/laravel-dompdf/pull/1062
New Contributors
- @sergiy-petrov made their first contribution in https://github.com/barryvdh/laravel-dompdf/pull/1005
- @maks-oleksyuk made their first contribution in https://github.com/barryvdh/laravel-dompdf/pull/1051
Full Changelog: https://github.com/barryvdh/laravel-dompdf/compare/v2.2.0...v3.0.0
用户点评
登录用户的真实使用记录,可按版本和硬件筛选还没有人点评。第一个说出真实体验的人最有价值。
DOMPDF Wrapper for Laravel
Laravel wrapper for Dompdf HTML to PDF Converter
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