Skip to content

Gambar

Embed gambar raster dan vektor di PDF Anda menggunakan method fluent image() dan imageSvg(), dengan kontrol penuh atas posisi dan ukuran.

Contoh Lengkap

php
<?php

declare(strict_types=1);

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

use TcpdfNext\Document;

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

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

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

    // -- 2. PNG dengan transparansi -----------------------------------
    ->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. Grafik vektor SVG -----------------------------------------
    ->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. Posisi absolut --------------------------------------------
    ->image(
        file:   __DIR__ . '/assets/badge.png',
        x:      150,                          // 150 mm dari tepi kiri
        y:      10,                           // 10 mm dari atas
        width:  30,
        height: 30,
    )

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

echo 'PDF created.' . PHP_EOL;

Mode Posisi

Mode Flow

Kirimkan null untuk x dan y. Gambar ditempatkan pada posisi kursor saat ini dan kursor bergerak ke bawah:

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

Mode Absolut

Kirimkan koordinat eksplisit. Kursor tidak bergerak -- ideal untuk logo, badge, atau overlay watermark:

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

Perilaku Skala

widthheightHasil
60nullLebar tetap, tinggi otomatis (rasio aspek dipertahankan)
null40Lebar otomatis, tinggi tetap (rasio aspek dipertahankan)
6040Dimensi tepat (mungkin stretch)
nullnullUkuran piksel asli dikonversi ke mm pada 96 DPI

Format yang Didukung

FormatCatatan
JPEGBaseline dan progressive; color space CMYK didukung
PNG8-bit, 24-bit, dan 32-bit dengan transparansi alpha penuh
SVGVia imageSvg() -- me-render path, teks, dan CSS dasar

Teks Wrap di Sekitar Gambar

Gunakan posisi absolut untuk gambar dan batasi lebar multiCell() untuk menghindari tumpang tindih:

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

Contoh lengkap menghasilkan satu halaman dengan foto JPEG, logo PNG transparan, diagram SVG, dan badge yang diposisikan secara absolut di pojok kanan atas.

Didistribusikan di bawah lisensi LGPL-3.0-or-later.