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

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:

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:

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.

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.

Step 5: Adding a New Page

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

Step 6: Duplicating the Page

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

Step 7: Outputting the PDF

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

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:

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!

If you found value in this article,
you can support us by buying me a coffee! ☕

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *