Introduction:
In web development, creating PDFs from data is a common requirement, especially when dealing with reports or downloadable content. In PHP, several libraries and methods are available to achieve this, and in this blog, we will explore different approaches to converting an array into a PDF document.
Method 1: Using FPDF Library
The FPDF library is a popular choice for creating PDFs in PHP. It provides a simple and lightweight way to generate PDF documents.
<?php
require('fpdf/fpdf.php');
// Sample Array
$data = array(
array('Name', 'Age', 'Country'),
array('John Doe', 25, 'USA'),
array('Jane Smith', 30, 'Canada'),
);
// Create PDF
$pdf = new FPDF();
$pdf->AddPage();
// Add table headers
$pdf->SetFont('Arial', 'B', 12);
foreach ($data[0] as $header) {
$pdf->Cell(40, 10, $header, 1);
}
// Add table rows
$pdf->Ln();
$pdf->SetFont('Arial', '', 12);
foreach ($data as $row) {
$pdf->Ln();
foreach ($row as $cell) {
$pdf->Cell(40, 10, $cell, 1);
}
}
// Output PDF
$pdf->Output('output.pdf', 'D');
?>
- We use the FPDF library to create a PDF instance and add pages.
- The
SetFont
method sets the font, style, and size for the text. - We iterate through the array to add table headers and rows using the
Cell
method. - The
Output
method generates the PDF file and prompts the user to download it.
Method 2: Utilizing TCPDF Library
TCPDF is another powerful library for creating PDFs in PHP, offering more advanced features than FPDF.
<?php
require('tcpdf/tcpdf.php');
// Sample Array
$data = array(
array('Name', 'Age', 'Country'),
array('John Doe', 25, 'USA'),
array('Jane Smith', 30, 'Canada'),
);
// Create PDF
$pdf = new TCPDF();
$pdf->AddPage();
// Add table headers
$pdf->SetFont('times', 'B', 12);
$header = array('Name', 'Age', 'Country');
$pdf->SetFillColor(200, 220, 255);
$pdf->MultiCell(60, 10, implode("\t", $header), 1, 'C', 1);
// Add table rows
$pdf->SetFont('times', '', 12);
foreach ($data as $row) {
$pdf->MultiCell(60, 10, implode("\t", $row), 1, 'C');
}
// Output PDF
$pdf->Output('output_tcpdf.pdf', 'D');
?>
- We use the TCPDF library to create a PDF instance and add pages.
- The
SetFont
method sets the font, style, and size for the text. SetFillColor
is used to set the background color of the header row.- The
MultiCell
method allows multiline text cells, useful for creating a table. - The
Output
method generates the PDF file and prompts the user to download it.
Method 3: HTML to PDF Conversion with Dompdf
Dompdf is a library that converts HTML and CSS to PDF. This approach is beneficial when you want to leverage the power of HTML for styling.
<?php
require 'vendor/autoload.php';
use Dompdf\Dompdf;
use Dompdf\Options;
// Sample Array
$data = array(
array('Name', 'Age', 'Country'),
array('John Doe', 25, 'USA'),
array('Jane Smith', 30, 'Canada'),
);
// Create PDF
$options = new Options();
$options->set('isHtml5ParserEnabled', true);
$options->set('isPhpEnabled', true);
$pdf = new Dompdf($options);
$pdf->loadHtmlFile('table_template.html'); // HTML template with placeholders
// Replace placeholders with array data
$html = str_replace(['{{header}}', '{{body}}'], [implode('</th><th>', $data[0]), array_reduce($data, function ($carry, $row) {
$carry .= '<tr><td>' . implode('</td><td>', $row) . '</td></tr>';
return $carry;
}, '')], file_get_contents('table_template.html'));
$pdf->loadHtml($html);
$pdf->setPaper('A4', 'portrait');
$pdf->render();
// Output PDF
$pdf->stream('output_dompdf.pdf');
?>
- We use Dompdf to convert HTML to PDF.
- An HTML template with placeholders for the header and body is used.
- The placeholders are replaced with array data using
str_replace
. - The
loadHtml
method loads the HTML content, and therender
method generates the PDF. - The
stream
method sends the PDF to the browser.
Conclusion:
In this blog, we have explored different methods to convert an array to a PDF in PHP. FPDF is simple and lightweight, suitable for basic use cases. TCPDF offers more advanced features and customization options. Dompdf allows leveraging HTML and CSS for styling, providing a more flexible approach.
Comments (0)