Skip to content

Images

Embed raster and vector images in your PDFs using the fluent image() and imageSvg() methods, with full control over position and size.

Full Example

php
<?php

declare(strict_types=1);

require __DIR__ . '/vendor/autoload.php';

use TcpdfNext\Document;

Document::create()
    ->setTitle('Image Examples')
    ->addPage()

    // -- Title -----------------------------------------------------------
    ->setFont('helvetica', style: 'B', size: 18)
    ->cell(0, 12, 'Embedding Images', newLine: true)

    // -- 1. JPEG (flow mode) --------------------------------------------
    ->setFont('helvetica', style: 'B', size: 13)
    ->cell(0, 10, '1. JPEG -- auto height', newLine: true)
    ->image(
        file:   __DIR__ . '/assets/photo.jpg',
        x:      null,                         // current X
        y:      null,                         // current Y
        width:  60,                           // 60 mm wide
        height: null,                         // auto (preserve ratio)
    )

    // -- 2. PNG with transparency ---------------------------------------
    ->setFont('helvetica', style: 'B', size: 13)
    ->cell(0, 10, '2. PNG with alpha channel', newLine: true)
    ->image(
        file:   __DIR__ . '/assets/logo.png',
        x:      null,
        y:      null,
        width:  40,
        height: null,
    )

    // -- 3. SVG vector graphic ------------------------------------------
    ->setFont('helvetica', style: 'B', size: 13)
    ->cell(0, 10, '3. SVG vector image', newLine: true)
    ->imageSvg(
        file:   __DIR__ . '/assets/diagram.svg',
        x:      null,
        y:      null,
        width:  80,
        height: 50,
    )

    // -- 4. Absolute positioning ----------------------------------------
    ->image(
        file:   __DIR__ . '/assets/badge.png',
        x:      150,                          // 150 mm from left edge
        y:      10,                           // 10 mm from top
        width:  30,
        height: 30,
    )

    ->save(__DIR__ . '/images.pdf');

echo 'PDF created.' . PHP_EOL;

Positioning Modes

Flow Mode

Pass null for x and y. The image is placed at the current cursor position and the cursor advances downward:

php
->image(file: 'photo.jpg', x: null, y: null, width: 60, height: null)

Absolute Mode

Pass explicit coordinates. The cursor does not move -- ideal for logos, badges, or watermark overlays:

php
->image(file: 'badge.png', x: 150, y: 10, width: 30, height: 30)

Scaling Behavior

widthheightResult
60nullFixed width, auto height (aspect ratio preserved)
null40Auto width, fixed height (aspect ratio preserved)
6040Exact dimensions (may stretch)
nullnullOriginal pixel size converted to mm at 96 DPI

Supported Formats

FormatNotes
JPEGBaseline and progressive; CMYK color space supported
PNG8-bit, 24-bit, and 32-bit with full alpha transparency
SVGVia imageSvg() -- renders paths, text, and basic CSS

Text Wrap Around an Image

Use absolute positioning for the image and constrain multiCell() width to avoid overlap:

php
Document::create()
    ->setTitle('Text Wrap')
    ->addPage()
    ->image(file: 'photo.jpg', x: 140, y: 30, width: 50, height: null)
    ->setFont('helvetica', size: 11)
    ->setXY(10, 30)
    ->multiCell(width: 125, height: 6, text: 'Your paragraph text here...')
    ->save('text-wrap.pdf');

Output

The full example produces one page with a JPEG photo, a transparent PNG logo, an SVG diagram, and an absolutely positioned badge in the top-right corner.

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