tesla-software/chrome2pdf

使用无头Chrome将HTML转换为PDF。

v0.2.3 2022-03-05 09:42 UTC

This package is auto-updated.

Last update: 2024-09-05 16:30:50 UTC


README

Latest Version on Packagist Software License Build Status

使用无头Chrome将HTML转换为PDF。

由于它基于当前Chrome版本而不是像WebKit(wkhtmltopdf)这样的不维护技术,因此它完全支持所有现代CSS/HTML功能。

此外,此包不依赖于任何外部js库。

<?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);

在浏览器中显示PDF(Laravel示例)

<?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']);
    }
}

已知问题

请参阅这篇博客文章,了解使用无头Chrome创建PDF时可能遇到的问题。

用法

此包依赖于无头Chrome。通过您选择的包管理器安装或手动安装。

wget https://dl.google.com/linux/direct/google-chrome-stable_current_amd64.deb
sudo dpkg -i google-chrome-stable_current_amd64.deb

通过Composer安装此包

composer require tesla-software/chrome2pdf

创建Chrome2Pdf实例并给它一些内容

$pdfContent = (new \Tesla\Chrome2Pdf\Chrome2Pdf())
    ->setContent('<h1>Hello world</h1><p>This is a paragraph</p>')
    ->pdf();

可用的PDF选项

// 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);

更改Chrome可执行文件路径

$chrome2pdf
    ->setChromeExecutablePath('/custom/path/to/chrome')
    ->setContent('<h1>Hello world</h1><p>This is a paragraph</p>')
    ->pdf();

更改临时目录路径

每次生成PDF时,此包都会创建一个包含给定内容的临时.html文件。请确保指定的目录路径可写和可读。

$chrome2pdf
    ->setTempFolder('/storage/temp/pdf')
    ->setContent('<h1>Hello world</h1><p>This is a paragraph</p>')
    ->pdf();

等待特定的页面生命周期事件

延迟PDF生成,直到触发特定的页面生命周期事件。一些有用的值包括:loadDOMContentLoadednetworkIdlenetworkAlmostIdle等。

$chrome2pdf
    ->setWaitForLifecycleEvent('networkIdle')
    ->setContent('<h1>Hello world</h1><p>This is a paragraph</p>')
    ->pdf();

禁用JavaScript

禁用脚本执行。

$chrome2pdf
    ->setDisableScriptExecution(true)
    ->setContent('<h1>Hello world</h1><p>This is a paragraph</p>')
    ->pdf();

附加Chrome参数

您可以向Chrome实例添加自定义参数。

$chrome2pdf
    ->appendChromeArgs(['--disable-gpu', '--user-data-dir=/tmp/session-123'])
    ->setContent('<h1>Hello world</h1><p>This is a paragraph</p>')
    ->pdf();

设置超时

设置微秒级的WebSocket连接超时。

$chrome2pdf
    ->setTimeout(10)
    ->setContent('<h1>Hello world</h1><p>This is a paragraph</p>')
    ->pdf();

模拟媒体

模拟CSS媒体查询中给定的媒体。

$chrome2pdf
    ->setEmulateMedia('screen')
    ->setContent('<h1>Hello world</h1><p>This is a paragraph</p>')
    ->pdf();

测试

$ composer test