Skip to content

Artisan Package

Artisan · LGPL-3.0

The Artisan package (yeeefang/tcpdf-nextartisan) provides pixel-perfect HTML-to-PDF conversion powered by Chrome DevTools Protocol (CDP). It renders HTML with full CSS3 support -- including Flexbox, Grid, web fonts, media queries, and animations frozen at render time.

When to Use Artisan

ScenarioRecommended
Generate PDF from HTML/CSS templatesArtisan
Programmatic PDF construction (cells, drawing)Core
HTML emails to PDF archiveArtisan
Invoice from structured dataCore or Artisan
Complex CSS layouts (Grid, Flexbox)Artisan
Signed/encrypted PDFsCore

How It Works

Artisan launches a headless Chrome instance via the Chrome DevTools Protocol, loads your HTML content into a browser page, and uses Chrome's built-in print-to-PDF functionality. This means every CSS feature that Chrome supports is available for your PDFs -- no more fighting with limited CSS parsers.

HTML/CSS --> ChromeBridge --> Chrome CDP --> PDF binary --> save / stream

Installation

bash
composer require yeeefang/tcpdf-nextartisan

Requirements:

  • PHP 8.2+
  • chrome-php/chrome ^1.15 (installed automatically)
  • Chrome or Chromium browser installed on the host
bash
# Ubuntu/Debian
apt-get install -y chromium-browser

# macOS
brew install --cask chromium

# Windows (Chocolatey)
choco install googlechrome

# Set a custom path via environment variable
export CHROME_PATH=/usr/bin/google-chrome

Quick Start

php
use Yeeefang\TcpdfNext\Artisan\HtmlRenderer;

$renderer = HtmlRenderer::create();

$renderer
    ->loadHtml('<h1>Hello, World!</h1><p>Rendered with Chrome CDP.</p>')
    ->save('/output/hello.pdf');

From a URL

php
HtmlRenderer::create()
    ->loadUrl('https://example.com/report')
    ->save('/output/report.pdf');

From a File

php
HtmlRenderer::create()
    ->loadFile('/templates/invoice.html')
    ->save('/output/invoice.pdf');

Package Contents

ClassPurpose
HtmlRendererMain entry point -- load HTML, configure, render
ChromeBridgeChrome DevTools Protocol communication
RenderOptionsRendering configuration (margins, scale, headers)
PageSetupPage size and orientation for rendering
PdfMergerMerge multiple rendered pages into a single PDF
StyleInjectorInject CSS stylesheets before rendering
ScreenshotCaptureCapture page screenshots (PNG/JPEG)

Exception Hierarchy

ExceptionWhen
RenderExceptionGeneric rendering failure
ChromeNotFoundExceptionChrome binary not found at expected path
TimeoutExceptionPage load or rendering exceeded the configured timeout

All exceptions live under the Yeeefang\TcpdfNext\Artisan\Exceptions namespace and extend a common ArtisanException base class.

Comparison with Core HTML Parser

The Core package includes an HtmlParser module for basic HTML-to-PDF conversion. Use it when you need a dependency-free solution. Use Artisan when you need full browser rendering fidelity.

FeatureCore HtmlParserArtisan
External dependencyNoneChrome/Chromium
CSS Flexbox / GridNoYes
Web fonts (@font-face)NoYes
Media queriesNoYes
JavaScript executionNoYes
@page CSS rulesNoYes
Performance (simple docs)FasterSlower
Performance (complex CSS)N/AReliable

Next Steps

Released under the LGPL-3.0-or-later License.