tesla-software/chrome2pdf
Composer 安装命令:
composer require tesla-software/chrome2pdf
包简介
Convert HTML to Pdf using headless chrome.
README 文档
README
Convert HTML to pdf using headless chrome.
Since this is based on current Chrome version and not on unmaintained technology like WebKit (wkhtmltopdf), it fully supports all modern CSS/HTML features.
Also this package does not depend on any external js library.
<?php use Tesla\Chrome2Pdf\Chrome2Pdf; $c2p = new Chrome2Pdf(); $c2p->setChromeExecutablePath('/opt/google/chrome/chrome') ->appendChromeArgs(['--disable-gpu']); $pdf = $c2p ->portrait() ->setPaperFormat('A4') ->setMargins(10, 10, 10, 10, 'mm') ->setContent('<h1>Hello world</h1><p>This is a paragraph</p>') ->setHeader('<div style="font-size: 11px">This is a header</div>') ->setFooter('<div style="font-size: 11px">This is a footer <span class="pageNumber"></span>/<span class="totalPages"></span></div>') ->pdf(); file_put_contents('test.pdf', $pdf);
Show pdf in browser (Laravel example):
<?php use Tesla\Chrome2Pdf\Chrome2Pdf; class ExampleController extends Controller { public function showPdf() { $pdf = (new Chrome2Pdf()) ->portrait() ->setPaperFormat('A4') ->setContent('<h1>Hello world</h1><p>This is a paragraph</p>') ->pdf(); return response()->make($pdf, 200, ['Content-Type' => 'application/pdf']); } }
Known issues
Please check this blogpost for known gotchas when creating pdf using headless Chrome.
Usage
This package depends on headless chrome. Install it via your package manager of choice or manually:
wget https://dl.google.com/linux/direct/google-chrome-stable_current_amd64.deb sudo dpkg -i google-chrome-stable_current_amd64.deb
Install this package via composer:
composer require tesla-software/chrome2pdf
Create Chrome2Pdf instance and give it some content:
$pdfContent = (new \Tesla\Chrome2Pdf\Chrome2Pdf()) ->setContent('<h1>Hello world</h1><p>This is a paragraph</p>') ->pdf();
Available pdf options
// Available options: A0, A1, A2, A3, A4, A5, A6, Letter, Legal, Tabloid, Ledger $chrome2pdf->setPaperFormat('A4'); // Custom margins ($top, $right, $bottom, $left, $measurementUnit) // Available units include: mm, cm, px, in $chrome2pdf->setMargins(2, 3, 2, 3, 'mm'); // Custom paper width and height, second parameter accepts measurement unit $chrome2pdf->setPaperWidth(8)->setPaperHeight(12, 'cm'); // Change paper orientation $chrome2pdf->portrait(); $chrome2pdf->landscape(); // Change webpage rendering scale $chrome2pdf->setScale(1); // Set header and footer HTML $chrome2pdf->setHeader('<p>Header text</p>'); $chrome2pdf->setFooter('<p>Footer text</p>'); // Disable/enable header and footer $chrome2pdf->setDisplayHeaderFooter(true); // Set pdf body content $chrome2pdf->setContent('<p>Demo content</p>'); // Set custom page print range, e.g., '1-5, 8, 11-13' $chrome2pdf->setPageRanges('2-3'); // Give any CSS @page size declared in the page priority over what is declared // in width and height or format options $chrome2pdf->setPreferCSSPageSize(true); // Print background graphics $chrome2pdf->setPrintBackground(true);
Change Chrome executable path
$chrome2pdf ->setChromeExecutablePath('/custom/path/to/chrome') ->setContent('<h1>Hello world</h1><p>This is a paragraph</p>') ->pdf();
Change temp directory path
Every time you generate pdf, this package creates a temporary .html file with given content. Make sure that given directory path is writable and readable.
$chrome2pdf ->setTempFolder('/storage/temp/pdf') ->setContent('<h1>Hello world</h1><p>This is a paragraph</p>') ->pdf();
Wait for a specific page lifecycle event
Delays pdf generation until a specific page lifecycle event is triggered. Some helpful values include: load, DOMContentLoaded, networkIdle, networkAlmostIdle, etc.
$chrome2pdf ->setWaitForLifecycleEvent('networkIdle') ->setContent('<h1>Hello world</h1><p>This is a paragraph</p>') ->pdf();
Disable javascript
Disables script execution.
$chrome2pdf ->setDisableScriptExecution(true) ->setContent('<h1>Hello world</h1><p>This is a paragraph</p>') ->pdf();
Additional Chrome arguments
You can add custom arguments to chrome instance.
$chrome2pdf ->appendChromeArgs(['--disable-gpu', '--user-data-dir=/tmp/session-123']) ->setContent('<h1>Hello world</h1><p>This is a paragraph</p>') ->pdf();
Set timeout
Set web socket connection timeout in microseconds.
$chrome2pdf ->setTimeout(10) ->setContent('<h1>Hello world</h1><p>This is a paragraph</p>') ->pdf();
Emulate media
Emulates the given media for CSS media queries.
$chrome2pdf ->setEmulateMedia('screen') ->setContent('<h1>Hello world</h1><p>This is a paragraph</p>') ->pdf();
Testing
$ composer test
tesla-software/chrome2pdf 适用场景与选型建议
tesla-software/chrome2pdf 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 8.1k 次下载、GitHub Stars 达 30, 最近一次更新时间为 2019 年 07 月 03 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「pdf」 「html」 「convert」 「headless」 「chrome」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 tesla-software/chrome2pdf 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 tesla-software/chrome2pdf 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 tesla-software/chrome2pdf 相关的其它包
同方向 / 同关键字的高下载量 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
Provides a simple decimal/roman number converter
A library that provides a simple infrastructure to create your own converters and to perform any conversion you want
Convert xml to array
HTML and form generation
Convert doctrine dbal query result to dto
统计信息
- 总下载量: 8.1k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 30
- 点击次数: 10
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2019-07-03