spiritix/php-chrome-html2pdf
Composer 安装命令:
composer require spiritix/php-chrome-html2pdf
包简介
A PHP library for converting HTML to PDF using Google Chrome
README 文档
README
A PHP library for converting HTML to PDF using Chrome/Chromium.
Contributors wanted! Have a look at the open issues and send me an email if you are interested in a quick introduction via Hangouts.
How it works
This library is based on puppeteer, a headless Chrome Node API which is maintained by the Chrome DevTools team.
It provides a simple PHP wrapper around the Node API, focused on generating beautiful PDF files.
In contrast to other HTML to PDF converters like wkhtmltopdf, the corresponding PHP wrapper or similar libraries, it is based on a current Chrome version instead of outdated and unmaintained WebKit builds. This library therefore fully supports CSS3, HTML5, SVGs, SPAs, and all the other fancy stuff people use these days.
Requirements
- PHP 8.1+ with enabled program execution functions (proc_open) and 'fopen wrappers'
- Node.js 18+ (for older Node.js versions use see changelog)
- A few OS specific dependencies
Installation
PHP Chrome HTML to PDF can be installed via Composer by requiring the
spiritix/php-chrome-html2pdf package in your project's composer.json.
Or simply run this command:
composer require spiritix/php-chrome-html2pdf
The required JS packages are installed automatically in the background.
Usage
Using this library is pretty straight forward. Decide for an input and an output handler, pass them to the converter, set some options if you like and depending on the output handler, proceed with the generated PDF file.
use Spiritix\Html2Pdf\Converter; use Spiritix\Html2Pdf\Input\UrlInput; use Spiritix\Html2Pdf\Output\DownloadOutput; $input = new UrlInput(); $input->setUrl('https://www.google.com'); $converter = new Converter($input, new DownloadOutput()); $converter->setOption('landscape', true); $converter->setOptions([ 'printBackground' => true, 'displayHeaderFooter' => true, 'headerTemplate' => '<p>I am a header</p>', ]); $converter->setLaunchOptions([ 'ignoreHTTPSErrors' => true, 'headless' => true, 'executablePath' => '/usr/bin/google-chrome-stable', 'args' => [ '--no-sandbox', '--disable-web-security', '--font-render-hinting=none', '--proxy-server="direct://"', '--proxy-bypass-list=*', '--media-cache-size=0', '--disk-cache-size=0', '--disable-application-cache', '--disk-cache-dir=/dev/null', '--media-cache-dir=/dev/null' ] ] ); $output = $converter->convert(); $output->download('google.pdf');
Input handlers
The following input handlers are available:
- StringInput - Accepts the HTML content as a string
- UrlInput - Fetches the HTML content from an URL
Output handlers
The following output handlers are available:
- StringOutput - Returns the binary PDF content as a string
- FileOutput - Stores the PDF file on the server's file system
- DownloadOutput - Forces the browser to download the PDF file
- EmbedOutput - Forces the browser to embed the PDF file
Options
scale<[number]> Scale of the webpage rendering. Defaults to1.displayHeaderFooter<[boolean]> Display header and footer. Defaults tofalse.headerTemplate<[string]> HTML template for the print header. Should be valid HTML markup with following classes used to inject printing values into them:dateformatted print datetitledocument titleurldocument locationpageNumbercurrent page numbertotalPagestotal pages in the document
footerTemplate<[string]> HTML template for the print footer. Should use the same format as theheaderTemplate.printBackground<[boolean]> Print background graphics. Defaults tofalse.landscape<[boolean]> Paper orientation. Defaults tofalse.pageRanges<[string]> Paper ranges to print, e.g., '1-5, 8, 11-13'. Defaults to the empty string, which means print all pages.format<[string]> Paper format. If set, takes priority overwidthorheightoptions. Defaults to 'Letter'.width<[string]> Paper width, accepts values labeled with units.height<[string]> Paper height, accepts values labeled with units.margin<[array]> Paper margins, defaults to none.top<[string]> Top margin, accepts values labeled with units.right<[string]> Right margin, accepts values labeled with units.bottom<[string]> Bottom margin, accepts values labeled with units.left<[string]> Left margin, accepts values labeled with units.
preferCSSPageSize<[boolean]> Give any CSS @page size declared in the page priority over what is declared in width and height or format options. Defaults tofalse, which will scale the content to fit the paper size.omitBackground<[boolean]> Hides default white background and allows capturing screenshots with transparency. Defaults tofalse.timeout<[number]> Maximum time in milliseconds, defaults to 30 seconds, pass 0 to disable timeout.mediaType<?[string]> Changes the CSS media type of the page. The only allowed values are'screen','print'andnull. Passingnulldisables media emulation.viewport<[array]>width<[number]> page width in pixels.height<[number]> page height in pixels.deviceScaleFactor<[number]> Specify device scale factor (can be thought of as dpr). Defaults to1.isMobile<[boolean]> Whether themeta viewporttag is taken into account. Defaults tofalse.hasTouch<[boolean]> Specifies if viewport supports touch events. Defaults tofalseisLandscape<[boolean]> Specifies if viewport is in landscape mode. Defaults tofalse.
pageWaitFor<[integer]> Timeout in milliseconds to wait for.cookies<[array]> Cookie objects to set.
NOTE
headerTemplateandfooterTemplatemarkup have the following limitations:
- Script tags inside templates are not evaluated.
- Page styles are not visible inside templates.
NOTE By default, this library generates a pdf with modified colors for printing. Use the
-webkit-print-color-adjustproperty to force rendering of exact colors.
Troubleshooting
- Make sure you've installed all OS specific dependencies.
- Manually set the path to your Node.js executable using the
$converter->setNodePath()method. - Footer or header are not visible? See here.
Contributing
Contributions in any form are welcome. Please consider the following guidelines before submitting pull requests:
- Coding standard - It's mostly PSR.
- Add tests! - Your PR won't be accepted if it doesn't have tests.
License
PHP Chrome HTML to PDF is free software distributed under the terms of the MIT license.
spiritix/php-chrome-html2pdf 适用场景与选型建议
spiritix/php-chrome-html2pdf 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 486.5k 次下载、GitHub Stars 达 151, 最近一次更新时间为 2018 年 01 月 04 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「php」 「pdf」 「html」 「headless」 「html2pdf」 「chrome」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 spiritix/php-chrome-html2pdf 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 spiritix/php-chrome-html2pdf 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 spiritix/php-chrome-html2pdf 相关的其它包
同方向 / 同关键字的高下载量 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
HTML and form generation
A modern HTML DOM parser for PHP using DOMDocument and Symfony CssSelector.
Reusable utilities library for Lacus Solutions' packages (type description, HTML escaping, random sequences)
Provides TCPDF integration for Symfony
Yii2 block extension
统计信息
- 总下载量: 486.5k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 153
- 点击次数: 37
- 依赖项目数: 3
- 推荐数: 1
其他信息
- 授权协议: MIT
- 更新时间: 2018-01-04