spiritix / php-chrome-html2pdf

A PHP library for converting HTML to PDF using Google Chrome
MIT License
112 stars 29 forks source link
html2pdf htmltopdf pdf pdf-converter php puppeteer puppeteer-pdf

PHP Chrome HTML to PDF

A PHP library for converting HTML to PDF using Chrome/Chromium.

PHP Checks Node.js Checks Code Climate Total Downloads Latest Stable Version Latest Unstable Version License

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

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:

Output handlers

The following output handlers are available:

Options

NOTE headerTemplate and footerTemplate markup have the following limitations:

  1. Script tags inside templates are not evaluated.
  2. 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-adjust property to force rendering of exact colors.

Troubleshooting

Contributing

Contributions in any form are welcome. Please consider the following guidelines before submitting pull requests:

License

PHP Chrome HTML to PDF is free software distributed under the terms of the MIT license.