Skip to content

Images

TCPDF-Next supports embedding raster and vector images into PDF documents. The image subsystem lives in the Graphics module and is accessed through the Document fluent API.

Supported Formats

FormatExtensionAlphaNotes
JPEG.jpg, .jpegNoBaseline and progressive
PNG.pngYes8-bit, 24-bit, 32-bit with transparency
WebP.webpYesLossy and lossless
AVIF.avifYesRequires GD or Imagick with AVIF support
SVG.svg--Vector — rendered via SvgParser
EPS.eps, .ai--PostScript — rendered via EpsParser

image()

php
use Yeeefang\TcpdfNext\Core\Document;

$pdf = Document::create()
    ->addPage()
    ->image('/path/to/logo.png', 10, 10, 50, 0);   // Width=50mm, auto height

Signature

php
image(
    string $file,             // File path, URL, or @base64 string
    float  $x     = '',       // X position ('' = current X)
    float  $y     = '',       // Y position ('' = current Y)
    float  $w     = 0,        // Width  (0 = auto from aspect ratio)
    float  $h     = 0,        // Height (0 = auto from aspect ratio)
    string $type  = '',       // Force format: 'JPEG', 'PNG', 'WebP', etc.
    mixed  $link  = '',       // URL or internal link identifier
    string $align = '',       // Alignment after image: T, M, B, N
    bool   $resize    = false,
    int    $dpi       = 300,
    string $palign    = '',   // Image alignment within cell: L, C, R
    bool   $fitbox    = false,
    bool   $fitonpage = false
): static

Positioning and Scaling

php
$pdf->image('/path/to/photo.jpg', 10, 60, 100, 80);      // Absolute: 100x80mm at (10,60)
$pdf->image('/path/to/banner.png', 10, 10, 190, 0);       // Auto height from width
$pdf->image('/path/to/portrait.jpg', 10, 10, 0, 100);     // Auto width from height
$pdf->image('/path/to/photo.jpg', 10, 10, 80, 60, fitbox: true);    // Fit in box
$pdf->image('/path/to/chart.png', 10, 10, 0, 0, fitonpage: true);   // Fit on page
  • Absolute position — provide explicit $x, $y coordinates (default unit: mm).
  • Auto height/width — set one dimension to 0; the other is calculated from the aspect ratio.
  • Fit within box (fitbox: true) — scale to fit $w x $h preserving aspect ratio.
  • Fit on page (fitonpage: true) — ensure the image never exceeds the printable area.

DPI and Resolution

The $dpi parameter controls pixel-to-physical-size mapping when both $w and $h are 0:

php
$pdf->image('/path/to/scan.jpg', 10, 10, 0, 0, dpi: 150);  // Larger on page
$pdf->image('/path/to/scan.jpg', 10, 10, 0, 0, dpi: 300);  // Smaller on page

Image from String or URL

php
$pdf->image('https://example.com/logo.png', 10, 10, 50, 0);           // From URL
$pdf->image('@' . base64_encode($imageData), 10, 10, 50, 0, 'PNG');   // From base64

When loading from a base64 string, provide the $type parameter so the parser knows the format.

php
$pdf->image('/path/to/logo.png', 10, 10, 40, 0, link: 'https://example.com');

SVG Images

imageSvg() renders SVG as native PDF vector paths — no rasterization:

php
imageSvg(string $file, float $x, float $y, float $w, float $h, mixed $link = '', string $align = '', string $palign = '', mixed $border = 0): static
php
$pdf->imageSvg('/path/to/diagram.svg', 10, 150, 180, 100);

EPS / PostScript Images

php
imageEps(string $file, float $x, float $y, float $w, float $h, mixed $link = '', bool $useBBox = true, string $align = '', string $palign = '', mixed $border = 0): static
php
$pdf->imageEps('/path/to/illustration.eps', 10, 10, 80, 60);

Complete Example

php
use Yeeefang\TcpdfNext\Core\Document;

$pdf = Document::create()
    ->addPage()
    ->image('/path/to/logo.png', 10, 10, 50, 0)
    ->image('/path/to/photo.jpg', 10, 60, 100, 80)
    ->imageSvg('/path/to/diagram.svg', 10, 150, 180, 100)
    ->save('output.pdf');

Tips

  • PNG alpha channels are fully preserved in the PDF output.
  • For best print quality, use images at 300 DPI or higher.
  • SVG rendering supports most static features; animations and JavaScript are ignored.
  • When embedding many images, consider pre-resizing large files to control memory usage.

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