Skip to content

Imagens

Incorpore imagens raster e vetoriais em seus PDFs usando os métodos fluentes image() e imageSvg(), com controle total sobre posição e tamanho.

Exemplo Completo

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;

Modos de Posicionamento

Modo Flow

Passe null para x e y. A imagem é colocada na posição atual do cursor e o cursor avança para baixo:

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

Modo Absoluto

Passe coordenadas explícitas. O cursor não se move -- ideal para logos, badges ou sobreposições de marca d'água:

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

Comportamento de Escala

widthheightResultado
60nullLargura fixa, altura automática (proporção preservada)
null40Largura automática, altura fixa (proporção preservada)
6040Dimensões exatas (pode distorcer)
nullnullTamanho original em pixels convertido para mm a 96 DPI

Formatos Suportados

FormatoNotas
JPEGBaseline e progressivo; espaço de cor CMYK suportado
PNG8-bit, 24-bit e 32-bit com transparência alpha completa
SVGVia imageSvg() -- renderiza paths, texto e CSS básico

Texto Envolvendo uma Imagem

Use posicionamento absoluto para a imagem e restrinja a largura do multiCell() para evitar sobreposição:

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');

Saída

O exemplo completo produz uma página com uma foto JPEG, um logo PNG transparente, um diagrama SVG e um badge posicionado absolutamente no canto superior direito.

Distribuído sob a licença LGPL-3.0-or-later.