3

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

?>
Wojtek Grabczak
  • 131
  • 1
  • 1
  • 6

1 Answers1

0

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.

Jan Slabon
  • 4,736
  • 2
  • 14
  • 29
  • Thank you for your help, I've managed to solve it in the end but now I have much more serious problems. I've ran into problems with encoding using chinese fonts. Link to the question: http://stackoverflow.com/questions/30123302/using-chinese-fonts-in-tcpdf-and-fpdi-encoding-problems – Wojtek Grabczak May 08 '15 at 12:00