PHP: How to write to a PDF file

Updated: January 12, 2024 By: Guest Contributor Post a comment

Overview

PHP, a powerful server-side scripting language, is widely used for web development. Moreover, handling PDF files through PHP can be an essential feature for web applications – may it be for generating reports, invoices, or receipts. In this tutorial, we are going to discuss how to write to a PDF file using PHP.

Getting Started

In order to write to a PDF file in PHP, we need to rely on external libraries, as the core PHP language does not have built-in functionality for PDF manipulation. Some of the most popular PHP libraries for PDF creation and manipulation include TCPDF, FPDF, and DOMPDF.

For this tutorial, we will use the TCPDF library, which is robust and supports many features.

Step 1: Installing TCPDF

You can install TCPDF by downloading it from the TCPDF website or by using Composer:

composer require tecnickcom/tcpdf

Once installed, you can include TCPDF in your PHP script:

require_once 'tcpdf_include.php';

Step 2: Create a New PDF Document

The first step in writing content to a PDF is to create a new instance of the TCPDF class.

$pdf = new TCPDF(PDF_PAGE_ORIENTATION, PDF_UNIT, PDF_PAGE_FORMAT, true, 'UTF-8', false);

Step 3: Adjust Page and Document Settings

After creating the TCPDF object, you’ll want to set some document properties and page settings:

$pdf->SetCreator(PDF_CREATOR);
$pdf->SetAuthor('Your Name');
$pdf->SetTitle('PDF Generation in PHP');
$pdf->SetSubject('TCPDF Tutorial');
$pdf->SetKeywords('TCPDF, PDF, PHP, tutorial');

// set default header data
$pdf->SetHeaderData(PDF_HEADER_LOGO, PDF_HEADER_LOGO_WIDTH, PDF_HEADER_TITLE.' 006', PDF_HEADER_STRING);

// set header and footer fonts
$pdf->setHeaderFont(Array(PDF_FONT_NAME_MAIN, '', PDF_FONT_SIZE_MAIN));
$pdf->setFooterFont(Array(PDF_FONT_NAME_DATA, '', PDF_FONT_SIZE_DATA));

// set default monospaced font
$pdf->SetDefaultMonospacedFont(PDF_FONT_MONOSPACED);

// set margins
$pdf->SetMargins(PDF_MARGIN_LEFT, PDF_MARGIN_TOP, PDF_MARGIN_RIGHT);
$pdf->SetHeaderMargin(PDF_MARGIN_HEADER);
$pdf->SetFooterMargin(PDF_MARGIN_FOOTER);

// set auto page breaks
$pdf->SetAutoPageBreak(TRUE, PDF_MARGIN_BOTTOM);

// set image scale factor
$pdf->setImageScale(PDF_IMAGE_SCALE_RATIO);

// set some language-dependent strings (optional)
$pdf->setLanguageArray($l);

// ---------------------------------------------------------

// set default font subsetting mode
$pdf->setFontSubsetting(true);

// Set font
// dejavusans is a UTF-8 Unicode font, if you only need to
// print standard ASCII chars, you can use core fonts like
// helvetica or times to reduce file size.
$pdf->SetFont('dejavusans', '', 14, '', true);

Step 4: Adding Content

To add content to your PDF, you must first add a page to the document:

$pdf->AddPage();

Only after you’ve added a page can you begin writing content, whether it’s text, images, or shapes:

// Add some text
text on multiple lines with no constraints in terms of width
$width = 0;
$height = 0;
$pdf->MultiCell($width, $height, 'Your text content goes here, you can write multiple lines with this method.', 0, 'L', 0, 1, '', '', true);

// Draw a rectangle
$pdf->Rect(10, 50, 150, 50);

Step 5: Outputting Your PDF

Once all your contents are added, you can output your PDF to a file, download, or string.

// Close and output PDF document
// This method has several options, check the source code documentation for more information.
$pdf->Output('example_006.pdf', 'I');

Putting it All Together

You can assemble the previous steps into a complete script to create a basic PDF file:


require_once 'tcpdf_include.php';
$pdf = new TCPDF(PDF_PAGE_ORIENTATION, PDF_UNIT, PDF_PAGE_FORMAT, true, 'UTF-8', false);

// [...] All the settings from Step 3

$pdf->AddPage();
$pdf->SetFont('dejavusans', '', 14);
$pdf->MultiCell(0, 0, 'Your text content goes here, you can write multiple lines with this method.', 0, 'L', 0, 1, '', '', true);
$pdf->Rect(10, 50, 150, 50);
$pdf->Output('example.pdf', 'I');

Advanced Usage and Considerations

Working with PDFs in PHP can become much more complex depending on your needs. Here are some advanced considerations:

  • Handling different languages and fonts.
  • Adding bookmarks for easy navigation.
  • Protecting your PDF with passwords.
  • Creating interactive forms within your PDFs.
  • Producing barcodes within your document.

The TCPDF library is well-documented, and by reading through its documentation, you can take advantage of its robust set of tools that cater to these advanced PDF manipulation requirements.

Conclusion

Writing to a PDF file using PHP requires utilizing a library like TCPDF. Once integrated into your project, the library’s versatile features help you generate and manipulate PDF content with PHP. Whether you’re creating simple invoices, or dynamically generated reports, embracing the functionality of TCPDF can greatly enhance your web application.

Writing to a PDF is just the tip of the iceberg. With increased practice and exploring the libraries’ documentation, can continuously expand your skills in generating sophisticated and professional PDF documents seamlessly into your PHP applications.