Wojtek Grabczak
Wojtek Grabczak

Reputation: 131

Adding images over PDF using FPDI and TCPDF

I'm trying to add images on top of an existing PDF.

The pdf contains a blank grid, I am writing a script that will insert images on top of the PDF and output a new modified PDF file.

I am using FPDI and TCPDF libraries to load and edit the PDF file.

Any image or text i try to add using Write(); and Image(); functions does not appear anywhere in the Output file.

<?php

// defining encoding and linking libraries
header('Content-Type: text/html; charset=utf-8');

require_once('tcpdf/tcpdf.php');
require_once('fpdi/fpdi.php');

// Creating new page with PDF as a background
$pdf = new FPDI();

$pdf->setSourceFile("resources/worksheet_template.pdf");

$tplIdx = $pdf->importPage(1);
$pdf->AddPage();
$pdf->useTemplate($tplIdx, 0, 0, 0, 0, true);



// $pdf->MultiCell(0, 5,'$pdf', 0, 'J', 0, 2, '', '', true, 0, false);

$pdf->Image('resources/lantern.png', 50, 50, 100, '', '', 'http://www.tcpdf.org', '', false, 300);


ob_clean();
$pdf->Output('WorksheetTest.pdf', 'I');

?>

Upvotes: 3

Views: 15294

Answers (1)

Jan Slabon
Jan Slabon

Reputation: 5058

This script works without problems! Expecting that all resources are accessible. The MultiCell call looks something special but as you'd used single quotes at least the string $pdf will appear in the resulting document. Furthermore the header() is obsolete too. Make sure that your PHP file is saved without a BOM to get rid of the ob_clean() call, too.

Generally it should be a good practice to define a font and size before writing any text but it seems that TCPDF handles this internally by a standard font for you.

Also make sure that you are using the latest version of both FPDI and TCPDF.

Upvotes: 1

Related Questions