All Projects → tesla-software → chrome2pdf

tesla-software / chrome2pdf

Licence: MIT license
Convert HTML to Pdf using headless Chrome and ChromeDevTools protocol.

Programming Languages

PHP
23972 projects - #3 most used programming language

Projects that are alternatives of or similar to chrome2pdf

ubuntu-vnc-xfce-chromium
Retired. Headless Ubuntu/Xfce container with VNC/noVNC and Chromium (Generation 1)
Stars: ✭ 20 (-23.08%)
Mutual labels:  headless
Excel2Object
excel convert to .NET Object | Excel与.NET 对象进行转换,支持公式、多Sheet等功能
Stars: ✭ 35 (+34.62%)
Mutual labels:  convert
NeoConsole
NeoConsole offers a command line (REPL) interface to a Pharo image, as well as other tools.
Stars: ✭ 22 (-15.38%)
Mutual labels:  headless
ansi-to-svg
😹 convert ANSI Escaped CLI strings to SVGs
Stars: ✭ 18 (-30.77%)
Mutual labels:  convert
laravel-storyblok
Make Laravel and Storyblok work together beautifully.
Stars: ✭ 45 (+73.08%)
Mutual labels:  headless
silverstripe-graphql-jwt
JWT Authentication for GraphQL
Stars: ✭ 17 (-34.62%)
Mutual labels:  headless
dry
Dry is a new template engine and language, and is a superset of Shopify's Liquid, with first-class support for advanced inheritance features, and more. From the creators of Enquirer, Assemble, Remarkable, and Micromatch.
Stars: ✭ 66 (+153.85%)
Mutual labels:  headless
linear16
Converts an audio file to LINEAR16 Google-speech compatible file.
Stars: ✭ 14 (-46.15%)
Mutual labels:  convert
ttf2hershey
Convert True Type Fonts (.ttf) to Hershey vector fonts
Stars: ✭ 29 (+11.54%)
Mutual labels:  convert
json-table-converter
Convert any json to table.
Stars: ✭ 37 (+42.31%)
Mutual labels:  convert
hubble-frontend-pwa
E-Commerce PWA Frontend
Stars: ✭ 43 (+65.38%)
Mutual labels:  headless
json-to-html-converter
Converts JSON data to HTML table with collapsible details view for nested objects.
Stars: ✭ 13 (-50%)
Mutual labels:  convert
e2-scripts
Scripts for working with electribe 2.
Stars: ✭ 20 (-23.08%)
Mutual labels:  convert
yllet
Yllet is a set of packages for the WordPress API for both React and non-React projects
Stars: ✭ 46 (+76.92%)
Mutual labels:  headless
nextjs-shopify
The ultimate starter for headless Shopify stores
Stars: ✭ 231 (+788.46%)
Mutual labels:  headless
persian
Some utilities for Persian language in Go (Golang)
Stars: ✭ 65 (+150%)
Mutual labels:  convert
nuxt-ghost
Easy Ghost content API integration with Nuxt.js.
Stars: ✭ 27 (+3.85%)
Mutual labels:  headless
project graphql blog
With featured and recent posts, categories. full markdown articles, author information, comments, and much more, this fully responsive CMS Blog App is the best Blog Application that you can currently find on YouTube. And what's best of all is that you and your clients can manage the blog from a dedicated Content Management System.
Stars: ✭ 843 (+3142.31%)
Mutual labels:  headless
rubium
Rubium is a lightweight alternative to Selenium/Capybara/Watir if you need to perform some operations (like web scraping) using Headless Chromium and Ruby
Stars: ✭ 65 (+150%)
Mutual labels:  headless
node-headless-chrome
⚠️ 🚧 Install precompiled versions of the Chromium/Chrome headless shell using npm or yarn
Stars: ✭ 20 (-23.08%)
Mutual labels:  headless

Chrome2Pdf

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
Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].