Welcome to CodeYourCraft's PHP tutorial on FPDF Header/Footer! Today, we'll learn how to create professional-looking PDF documents using the FPDF library. Let's get started!
FPDF is a PHP class for creating PDF documents. It's a popular choice for developers due to its simplicity and wide range of features. Today, we will focus on creating headers and footers for our PDF documents.
Before we dive into the header/footer creation, let's make sure you have FPDF installed on your system. If not, you can download it from here and install it manually.
For those using Composer, you can easily install FPDF by running the following command:
composer require setasign/fpdfBefore we add headers and footers, let's first create a basic PDF document using FPDF.
require_once 'fpdf.php';
$pdf = new FPDF();
$pdf->AddPage();
$pdf->SetTitle('My First PDF');
$pdf->SetFont('Arial', 'B', 16);
$pdf->Cell(0, 10, 'Hello, World!', 0, 1, 'C');
$pdf->Output();In this example, we first require the FPDF class, create a new instance, add a page, set the title, set the font, and finally output the PDF.
Now, let's add a header to our PDF document. We'll add a title and page number.
require_once 'fpdf.php';
$pdf = new FPDF();
$pdf->AddPage();
$pdf->SetTitle('My First PDF');
$pdf->SetFont('Arial', 'B', 16);
// Set the header
$pdf->SetTopMargin(10);
$pdf->SetHeaderData('', '', 'My First PDF', 'Page {PAGENO}');
$pdf->setFooterData('', '', '');
// Output the PDF
$pdf->Output();In the SetHeaderData function, we set the title, subtitle, and page number position. The fourth argument, 'Page {PAGENO}', represents the page number.
Next, let's add a footer to our PDF document. We'll add a copyright notice.
require_once 'fpdf.php';
$pdf = new FPDF();
$pdf->AddPage();
$pdf->SetTitle('My First PDF');
$pdf->SetFont('Arial', 'B', 16);
// Set the header
$pdf->SetTopMargin(10);
$pdf->SetHeaderData('', '', 'My First PDF', 'Page {PAGENO}');
$pdf->setFooterData('', '', 'Copyright 2023 CodeYourCraft');
// Output the PDF
$pdf->Output();In the setFooterData function, we set the footer text.
In some cases, you might need multiple headers and footers for different pages or sections of your document. FPDF supports this with the AddPage method.
require_once 'fpdf.php';
$pdf = new FPDF();
$pdf->AddPage();
$pdf->SetTitle('Advanced PDF');
$pdf->SetFont('Arial', 'B', 16);
// Set the first header
$pdf->SetTopMargin(10);
$pdf->SetHeaderData('', '', 'First Section', 'Page {PAGENO}');
// Output the first section
$pdf->Cell(0, 10, 'First Section', 0, 1, 'C');
$pdf->AddPage();
// Set the second header
$pdf->SetTopMargin(10);
$pdf->SetHeaderData('', '', 'Second Section', 'Page {PAGENO}');
// Output the second section
$pdf->Cell(0, 10, 'Second Section', 0, 1, 'C');
// Output the PDF
$pdf->Output();In this example, we added two sections with different headers and footers.
What function is used to set the header data in FPDF?
We hope you enjoyed learning about FPDF header and footer creation in PHP! Stay tuned for more tutorials on CodeYourCraft. Happy coding! π