oasys / chrome-pdf
Minimal Chrome/Chromium PHP wrapper for generating PDF files from HTML using headless mode
Requires
- php: ^8.2
Requires (Dev)
- phpdocumentor/phpdocumentor: ^3.0
- phpunit/phpunit: ^11.0
This package is not auto-updated.
Last update: 2026-06-13 18:56:33 UTC
README
Minimal Chrome/Chromium PHP wrapper for generating PDF files from HTML using headless mode
- Save PDF to the filesystem
- Get PDF contents as base64
- Get PDF temporary file path
- Uses local Chrome/Chromium
- Configurable Chrome flags
Requirements
- PHP 8.2+
- Chrome or Chromium installed on the system
Installation
composer require oasys/chrome-pdf
Quick start
<?php declare(strict_types=1); use Oasys\Pdf\ChromePdf; $pdf = new ChromePdf(); $pdf->save( html: '<h1>Hello world</h1>', output: __DIR__ . '/document.pdf' );
Binary
By default, the package uses google-chrome
$pdf = new ChromePdf();
On systems using Chromium:
$pdf = new ChromePdf( binary: 'chromium' );
On Windows, pass the full executable path
$pdf = new ChromePdf( binary: 'C:\Program Files\Google\Chrome\Application\chrome.exe' );
Chrome flags
Additional Chrome flags can be passed through the constructor
$pdf = new ChromePdf( flags: [ 'disable-gpu' => true, 'user-data-dir' => '/tmp/chrome' ] );
Flags with true values are rendered as standalone flags
[
'no-sandbox' => true
]
--no-sandbox
Standalone flags can also be passed as list values
[
'no-sandbox',
'disable-gpu'
]
--no-sandbox --disable-gpu
Flags with scalar values are rendered as --key=value
[
'user-data-dir' => '/tmp/chrome'
]
--user-data-dir=/tmp/chrome
Flags with array values are joined with commas
[
'window-size' => [1280, 720]
]
--window-size=1280,720
Flags with false or null values are skipped
[
'no-sandbox' => false,
'disable-gpu' => null
]
The following Chrome flags are controlled internally and cannot be passed manually:
headlessprint-to-pdfno-pdf-header-footerprint-to-pdf-no-header
Output methods
Save to file
$pdf->save( '<h1>Invoice</h1>', __DIR__ . '/invoice.pdf' );
Base64
$base64 = $pdf->base64( '<h1>Invoice</h1>' );
"JVBERi0xLj...CiUlRU9GCg=="
Temporary path
$path = $pdf->tempPath( '<h1>Invoice</h1>' );
"/tmp/pdf-abc123.pdf"
The caller is responsible for deleting the returned file
@unlink($path);
Print CSS
Use standard print CSS to control page layout
<style> @page { size: A4; margin: 12mm; } body { font-family: sans-serif; } </style> <h1>Invoice</h1>
Design notes
- Chrome/Chromium must be installed separately
- HTML is written to a temporary
.htmlfile before rendering - Temporary HTML files are deleted automatically
- Temporary PDF files from
base64()are deleted automatically - Raw Chrome flags are also accepted, e.g.
flags: ['--no-sandbox'] - Header and footer generation is disabled