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
| Format | Extension | Alpha | Notes |
|---|---|---|---|
| JPEG | .jpg, .jpeg | No | Baseline and progressive |
| PNG | .png | Yes | 8-bit, 24-bit, 32-bit with transparency |
| WebP | .webp | Yes | Lossy and lossless |
| AVIF | .avif | Yes | Requires 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 heightSignature
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
): staticPositioning 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,$ycoordinates (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$wx$hpreserving 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 pageImage 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 base64When loading from a base64 string, provide the $type parameter so the parser knows the format.
Images with Links
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): staticphp
$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): staticphp
$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.