How to Add a Blank Page to a PDF in PHP using FPDI
PDF (Portable Document Format) files are widely used for sharing and preserving documents. There are instances where you may need to add a blank page to an existing PDF document for various purposes, such as inserting notes, creating placeholders, or organizing content. In this blog, we’ll explore how to add a blank page to a PDF file using PHP with the help of the FPDI (Facultative Path & Add-ons) library, an extension of FPDF that allows us to import and merge existing PDFs.
Steps to Add a Blank Page to a PDF in PHP using FPDI
1. Download FPDF and FPDI Libraries
To get started, we need to obtain the FPDF and FPDI libraries.
Download FPDF:
The FPDF library is the foundation on which FPDI is built. You can download the FPDF library from the official website: http://www.fpdf.org/.
Download FPDI:
The FPDI library extends the capabilities of FPDF, allowing us to import and merge existing PDFs. You can download the FPDI library from the official website: https://www.setasign.com/products/fpdi/downloads/.
2. Set up the Environment
- After downloading the FPDF and FPDI libraries, locate the downloaded zip files on your computer.
- Extract the contents of both zip files.
- A new folder named “fpdf” will be created after extracting the FPDF library. This folder contains the FPDF library files.
- Similarly, after extracting the FPDI library, you’ll find the necessary FPDI files in a new folder.
3. Add a Blank Page
Now that we have the FPDF and FPDI libraries in place, let’s proceed with the PHP code to add a blank page to the PDF file using FPDI.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 | <?php // Include the required libraries require "fpdf.php"; require "fpdi/src/autoload.php"; use setasign\Fpdi\Fpdi; // Input PDF file path (the file to which you want to add a blank page) $inputPdfFilePath = "path/to/input.pdf"; // Output PDF file path (where the modified PDF will be saved) $outputPdfFilePath = "path/to/output.pdf"; // Create a new instance of FPDI $pdf = new Fpdi(); // Add a page from the input PDF to the output PDF $pageCount = $pdf->setSourceFile($inputPdfFilePath); for ($page = 1; $page <= $pageCount; $page++) { // Import the current page $templateId = $pdf->importPage($page); // Get the size of the imported page $size = $pdf->getTemplateSize($templateId); // Add the page with the same size $pdf->AddPage($size); // Use the imported page $pdf->useTemplate($templateId); } // Add a blank page $pdf->AddPage(); // Save the output PDF to the specified file $pdf->Output($outputPdfFilePath, "F"); // Success message echo "A blank page added successfully to the PDF. The modified PDF is saved at: " . $outputPdfFilePath; ?> |
4. Execute the Script
Save the PHP code in a PHP file (e.g., add_blank_page.php
) within your project folder. Ensure that both FPDF and FPDI libraries are in the same folder as the script. Then, execute the PHP script either through the browser or the command line:
1 | php add_blank_page.php |
After executing the script, the modified PDF with an additional blank page will be saved to the specified output file path.
Conclusion:
In this blog, we’ve learned how to add a blank page to a PDF file using PHP and the FPDI library. FPDI extends the capabilities of FPDF, allowing us to import and merge existing PDFs with ease. By using this method, you can conveniently manipulate PDF files and achieve various tasks related to PDF documents in PHP. Whether you need to create placeholders or add extra space for notes, FPDI empowers you to enhance your PDF files programmatically. Happy coding!