How to generate PDF documents with FPDI

Several solutions exist to generating PDF with PHP. Amongst them, FPDI is quite efficient and easy to use. Unfortunately, Setasign’s documentation about FPDI is not exactly abundant. So here’s a quick start guide to get you up and running.

The basics

FPDI is a tool that lets you import an existing PDF document as a template for generating another PDF document with FPDF. Put simply: it generates PDF files based on an existing PDF template.

This is how the code looks like, in its simplest form:

require_once('fpdf.php');
require_once('fpdi.php');

$pdf = new FPDI();

$pdf->setSourceFile('template_doc.pdf');
$tplidx = $pdf->importPage(1);

$pdf->addPage();
$pdf->useTemplate($tplidx, 10, 10, 90);

$pdf->Output('newpdf.pdf', 'D');

Multiple page template

If your PDF template contains several pages, each page has to be imported and added individually before outputting the final document, like this:

$pdf->setSourceFile('several_pages.pdf');

$tplidx1 = $pdf->importPage(1);
$pdf->addPage();
$pdf->useTemplate($tplidx1, 10, 10, 90);
// Generate some content for page 1

$tplidx2 = $pdf->importPage(2);
$pdf->addPage();
$pdf->useTemplate($tplidx2, 10, 10, 90);
// Generate some content for page 2

$pdf->Output('newpdf.pdf', 'D');

Useful functions

You should be able to find everything you need in the FPDF documentation, but here are the most useful/basic functions to help you get started.

setxy

Sets the current position to X and Y coordinates. Example:

$pdf->setxy(181, 10);

SetFont

Sets the font. Arguments: font, style, size. Example:

$pdf->SetFont('Arial','B',12);

MultiCell

Writes some text in a box of given dimensions. Arguments: width, height, text, border, alignment. Example:

$pdf->MultiCell(10, 20, 'Hello world!', 0, 'L');

Comments

comments

Comments are closed.

Services aux professionnels du webServices aux professionnels du web

PHP, MySQL, jQuery, HTML, CSS... Ce sont tous de vieux amis à moi. Que ce soit du développement back-end ou front-end, du Wordpress ou du Joomla, un site tout simple, un magasin en ligne ou une application web, je veillerai à ce que votre projet soit robuste.

Solutions web tout-en-unSolutions web tout-en-un

Un nouveau site pour votre société ? Un e-shop ? Avec des campagnes d'e-mailing ? Aucun problème, je m'en charge. Je livre des solutions web clé-en-main intuitives et performantes. Et en cas de besoin, j'assure également la formation et le coaching.

All-in-one Web SolutionsAll-in-one Web Solutions

A new website for your company ? Maybe an e-shop ? With e-mailing campaigns? No problem, I take care of that. I deliver turnkey web solutions that are easy to use. And just in case, I also do training and coaching if needed.

Services for Web ProfessionalsServices for Web Professionals

PHP, MySQL, jQuery, HTML, CSS... They are good ol' friends of mine. From back-end to front-end development, from Wordpress to Joomla, from simple websites to online shops to web applications, I'll help you build solid web projects.

Last Articles

Welcome to Bytes & Pixels!

Finally! After two years of freelancing, it was about time I had my own website.

Stop focusing, you’ll be more productive

I can't stay focused on one thing for a long time. Without even realising it, I

How to generate PDF documents with FPDI

Several solutions exist to generating PDF with PHP. Amongst them, FPDI is quite

In my ears

  • No recently listened tracks.

Useful Info

Galgendries 5
1630 Linkebeek
Belgium

VAT: BE 0541 284 249
IBAN: BE52 6511 5134 9409
BIC: KEYTBEBB

Bytes & Pixels membre de la