定制 tesla-software/chrome2pdf 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

tesla-software/chrome2pdf

Composer 安装命令:

composer require tesla-software/chrome2pdf

包简介

Convert HTML to Pdf using headless chrome.

README 文档

README

Latest Version on Packagist Software License Build Status

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

统计信息

  • 总下载量: 8.1k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 30
  • 点击次数: 0
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 30
  • Watchers: 1
  • Forks: 6
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2019-07-03

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固