Clue Mediator

How to create a PDF with a duplicate page in PHP using FPDF and FPDI libraries

๐Ÿ“…May 8, 2023
๐Ÿ—PHP

PDF documents are a widely used format for sharing information online. Often, we need to create a new PDF document that includes a duplicate page from an existing PDF document. In this tutorial, we will explore how to create a new PDF document with a duplicate page in PHP using the FPDF and FPDI libraries.

Prerequisites

Before we start, we need to ensure that we have the following prerequisites:

  • A web server running PHP 5.6 or higher.
  • The FPDF and FPDI libraries were downloaded and extracted in a folder in our PHP project.

You can download these libraries from the following links:
FPDF: http://www.fpdf.org/
FPDI: https://www.setasign.com/products/fpdi/downloads/
After downloading the libraries, extract them into a folder in your PHP project.

Step-by-Step Guide

Step 1: Including the Libraries

First, we need to include the FPDF and FPDI libraries in our PHP script. We can do this by adding the following lines of code to the beginning of our script:

require_once('fpdf/fpdf.php');
require_once('FPDI/src/autoload.php');
require_once('FPDI/src/FpdfTpl.php');

Step 2: Creating a New PDF Document

Next, we need to create a new instance of the FPDI class. We can do this by adding the following line of code to our script:

$pdf = new FPDI();

Step 3: Setting the Source File and Page Number

Now, we need to specify which page from the existing PDF document we want to duplicate. We can do this by setting the source file path and page number using the $src_file and $page_num variables.

$src_file = 'path/to/existing/file.pdf';
$page_num = 1;

Step 4: Importing the Source Page

Next, we use the setSourceFile() method of the FPDI class to set the source file and the importPage() method to import the specified page from the source file. The resulting template index is stored in the $tpl_idx variable.

$pdf->setSourceFile($src_file);
$tpl_idx = $pdf->importPage($page_num);

Step 5: Adding a New Page

We can now add a new page to the document using the addPage() method.

$pdf->addPage();

Step 6: Duplicating the Page

Finally, we can duplicate the same page by calling the useTemplate() method again with the same parameters.

$pdf->useTemplate($tpl_idx, 0, 0, null, null, true);
$pdf->addPage();
$pdf->useTemplate($tpl_idx, 0, 0, null, null, true);

Step 7: Outputting the PDF

We can then output the PDF document as a file or to the browser using the Output() method.

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

The first parameter of the Output() method specifies the output file name, while the second parameter specifies the output destination. In this case, 'D' is used to send the PDF to the browser as a download.

Final Code

Here is the complete code that creates a new PDF document with a duplicate page:

// include the FPDF and FPDI libraries
require_once('fpdf/fpdf.php');
require_once('FPDI/src/autoload.php');
require_once('FPDI/src/FpdfTpl.php');

use \setasign\Fpdi\Fpdi;

// create a new FPDI object
$pdf = new FPDI();

// set the source file path and page number
$src_file = 'path/to/existing/file.pdf';
$page_num = 1;

// set the source page
$pdf->setSourceFile($src_file);
$tpl_idx = $pdf->importPage($page_num);

// add a new page
$pdf->addPage();

// add the first page to the document
$pdf->useTemplate($tpl_idx, 0, 0, null, null, true);


// add the same page to the document again
$pdf->addPage();
$pdf->useTemplate($tpl_idx, 0, 0, null, null, true);

// output the PDF as a file or to the browser
$pdf->Output('my_document.pdf', 'D');

This code imports the first page of an existing PDF file, adds a new page to the document, and then duplicates the imported page onto the new page. The resulting document is then output to the browser as a download.

Conclusion:

In this tutorial, we learned how to create a new PDF document with a duplicate page in PHP using the FPDF and FPDI libraries. We started by downloading the libraries and including them in our PHP project. Then, we created a new instance of the FPDI class, set the source file path and page number, and imported the page into the new document. Finally, we added a new page to the document and duplicated the imported page onto the new page.

Using this technique, we can easily create new PDF documents with duplicate pages from existing PDF documents in PHP. With the power of FPDF and FPDI libraries, we can achieve complex PDF document manipulation tasks with ease.

That concludes our tutorial for today. I hope you found it useful and feel free to reach out if you have any questions or comments. Happy coding!