oasys/chrome-pdf

Minimal Chrome/Chromium PHP wrapper for generating PDF files from HTML using headless mode

Maintainers

Package info

github.com/kweensey/oasys-chrome-pdf

pkg:composer/oasys/chrome-pdf

Statistics

Installs: 0

Dependents: 0

Suggesters: 0

Stars: 0

Open Issues: 0

v1.0.0 2026-06-13 15:31 UTC

This package is not auto-updated.

Last update: 2026-06-13 18:56:33 UTC


README

Tests Latest Stable Version PHP Version Require License

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:

  • headless
  • print-to-pdf
  • no-pdf-header-footer
  • print-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 .html file 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