How to create PDF with php
PDF (Portable Document Format) is a widely used file format for documents. In many cases, you may need to generate PDFs dynamically based on user input, such as generating invoices or reports. PHP provides several libraries and extensions for generating PDFs, including FPDF, TCPDF, and Dompdf. In this guide, we will walk through the process of generating PDFs with PHP using the FPDF library.
Step 1: Install FPDF
The first step in generating PDFs with PHP is to install the FPDF library. FPDF is a popular PHP library for generating PDFs, and it is available on Packagist for easy installation using Composer. To install FPDF, run the following command in your terminal:
composer require setasign/fpdf
This command installs the FPDF library and its dependencies in your project’s vendor directory.
Step 2: Create a Basic PDF
Once you have installed FPDF, you can start creating PDFs. The first step is to create a new instance of the FPDF class:
require('vendor/autoload.php');
$pdf = new FPDF();
This creates a new instance of the FPDF class, which represents the PDF document. The next step is to add a new page to the PDF document:
$pdf->AddPage();
This adds a new page to the PDF document. You can then add content to the PDF document using the various methods provided by FPDF. For example, you can add text to the PDF document using the Cell
method:
$pdf->Cell(40, 10, 'Hello, World!');
This adds the text “Hello, World!” to the PDF document, starting at the current position with a width of 40 and a height of 10. You can also add images, lines, and other content to the PDF document using the methods provided by FPDF.
Step 3: Save or Output the PDF
Once you have added content to the PDF document, you can save it to a file or output it to the browser. To save the PDF to a file, you can use the Output
method:
$pdf->Output('document.pdf', 'F');
This saves the PDF to a file named document.pdf
in the current directory. The second parameter (F
) specifies that the output should be saved to a file.
To output the PDF to the browser, you can use the Output
method with the I
parameter:
$pdf->Output('document.pdf', 'I');
This outputs the PDF to the browser, prompting the user to download the file.
Step 4: Customize the PDF
FPDF provides a wide range of customization options for PDFs. You can customize the page size and orientation, the font and font size, and many other aspects of the PDF. For example, you can set the page size and orientation using the AddPage
method:
$pdf->AddPage('P', 'A4');
This adds a new page to the PDF document with a portrait orientation and an A4 page size. You can also set the font and font size using the SetFont
method:
$pdf->SetFont('Arial', 'B', 16);
This sets the font to Arial, the font style to bold, and the font size to 16. You can also add custom headers and footers, set the background color or image, and add watermarks to the PDF.
Step 5: Generate Dynamic PDFs
Generating dynamic PDFs requires integrating user input into the PDF generation process. For example, you may need to generate invoices or reports based on data from a database. To do this, you can use PHP to retrieve the data from the database and then use FPDF to generate the PDF. Here’s an example of how to generate a PDF invoice with FPDF and PHP:
// Retrieve the data from the database
$invoiceData = // Retrieve the invoice data from the database
// Create a new instance of the FPDF class
$pdf = new FPDF();
// Add a new page to the PDF document
$pdf->AddPage();
// Set the font and font size
$pdf->SetFont('Arial', 'B', 16);
// Add the invoice header
$pdf->Cell(0, 10, 'Invoice', 0, 1, 'C');
$pdf->Cell(0, 10, 'Customer: ' . $invoiceData['customer'], 0, 1, 'C');
$pdf->Cell(0, 10, 'Invoice #: ' . $invoiceData['invoice_number'], 0, 1, 'C');
$pdf->Cell(0, 10, 'Date: ' . $invoiceData['date'], 0, 1, 'C');
// Add the invoice table
$pdf->SetFont('Arial', '', 12);
$pdf->Cell(30, 10, 'Item', 1, 0, 'C');
$pdf->Cell(30, 10, 'Quantity', 1, 0, 'C');
$pdf->Cell(30, 10, 'Price', 1, 0, 'C');
$pdf->Cell(30, 10, 'Total', 1, 1, 'C');
foreach ($invoiceData['items'] as $item) {
$pdf->Cell(30, 10, $item['name'], 1, 0, 'L');
$pdf->Cell(30, 10, $item['quantity'], 1, 0, 'C');
$pdf->Cell(30, 10, '$' . $item['price'], 1, 0, 'R');
$pdf->Cell(30, 10, '$' . $item['total'], 1, 1, 'R');
}
// Add the invoice footer
$pdf->Cell(0, 10, 'Total: $' . $invoiceData['total'], 0, 1, 'R');
// Output the PDF to the browser
$pdf->Output('invoice.pdf', 'I');
This example retrieves the invoice data from the database, adds a new page to the PDF document, adds the invoice header, invoice table, and invoice footer using the various methods provided by FPDF, and then outputs the PDF to the browser.
Conclusion
Generating PDFs with PHP is a useful skill for web developers. FPDF is a powerful PHP library that provides a simple and flexible way to generate PDFs. In this guide, we walked through the process of generating PDFs with FPDF, from installing the library to customizing the PDF and generating dynamic PDFs. With this knowledge, you can create PDFs for a wide range of use cases, including invoices, reports, and other documents.