PHP Making PDFs

PHP Code Snippets Make a PDF
Share:

About

In this code snippet, we’ll learn how to make a PDF file in PHP.

We will use the FPDF library to generate a PDF. More specifically we’ll see how to create page headers/footers, add page numbers, write text, use cells, use multicell, inserting links and images.

Let’s see how to do it in the example below.

Installing FPDF:

Install FPDF using Composer by opening your project directory in cmd and running this:

composer require setasign/fpdf

If you don’t know what Composer is or how to use it check out this post I made.

Code:

PDF.php
<?php
//Include the dependencies using Composer.
require_once(dirname(__FILE__) ."\\vendor\\autoload.php");
//Official documentation: http://www.fpdf.org/en/doc/
//Making a child class of FPDF with a custom header/footer.
class PDF extends FPDF {
//Page header.
function Header()
{
//Logo (path, x, y, width)
$this->Image(dirname(__FILE__) . "\\logo.png", 180, 5, 15);
//Insert link over the area of the image.
$this->Link(180, 5, 15, 15, "https://eecs.blog");
//Arial bold 15.
$this->SetFont("Helvetica", "B", 15);
//Make empty cell 80 wide(to move to the right).
$this->Cell(80);
//Title Cell(width, height, text, draw border, 1 - same as calling Ln(), center text).
$this->Cell(30, 10, "Title", 1, 0, "C");
$this->setCreator("The EECS Blog");
}
//Page footer.
function Footer()
{
//Position at 1.5 cm from bottom.
$this->SetY(-15);
//Set font.
$this->SetFont("Helvetica", "I", 8);
//Set page number. $this->PageNo() gets current page number.
$this->Cell(0, 10, "Page " . $this->PageNo() . "/{nb}", 0, 0, "C");
}                      
}
//Make PDF////////////////////////////////////////////////////
//Instantiation PDF(child class of FPDF).
$pdf = new PDF(); 
//Defines an alias for the total number of pages.
$pdf->AliasNbPages();
//Add a page.
$pdf->AddPage();
//left, top, right
$pdf->SetMargins(10, 10, 10);
//Enable auto page breaks with a margin of 0.5cm.  
$pdf->SetAutoPageBreak(true, 0.5);
//Font type, style(B - bold, I - italic, U - underline, "" - none), font size
$pdf->SetFont("Helvetica", 'B', 10);
//Set text color in RGB.
$pdf->SetTextColor(35, 31, 32);
//Sets background color.
$pdf->SetFillColor(145, 205, 255);
//Set "cursor" x,y position.
$pdf->SetXY(10, 30);
//Cell(width, height, text, )
$pdf->MultiCell(180, 4, "Multicell text: Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nulla eget magna tristique, dignissim enim nec, suscipit purus. Donec tellus nibh, iaculis ut aliquet ut, faucibus a elit. Sed quam dui, dictum et nisi at, dapibus auctor neque. Donec eu metus vitae augue vestibulum finibus. Maecenas condimentum dui orci, sed vestibulum dolor suscipit quis. Proin tempor imperdiet ex a finibus. Maecenas blandit viverra est, ac maximus erat efficitur a. Cras cursus eros et sapien ultrices varius.", 0);
$pdf->SetXY(10, 60);
$pdf->Cell(30,5, "Some text.", 0);
$pdf->SetXY(10, 70);
$pdf->Cell(30,5, "Some text.", 0);
$pdf->SetXY(10, 80);
$pdf->Cell(30,5, "Some text.", 0);
//Ln() can be used to move the "cursor" down instead of setting the x,y every time.
$pdf->Ln(10);
$pdf->Cell(30,5, "Some text.", 0);
$pdf->Ln(10);
//Degree symbol conversion.
$pdf->Cell(30,5, "Temperature: 25" . iconv('UTF-8', 'windows-1252', '°C'), 0);
$pdf->Ln(10);
//Text() or Write() can also be used to write text insated of Cell().
$pdf->Text(10, 120, "Some other text.");
$pdf->Ln(10);
$pdf->Write(10, "Some other text.");
//Write file as:
//I: send the file inline to the browser. The PDF viewer is used if available.
//D: send to the browser and force a file download with the name given by name.
//F: save to a local file with the name given by name (may include a path).
//S: return the document as a string.
//Write the PDF file. Output(destination, "path")
$pdf->Output('F', dirname(__FILE__) . "\\NewPDF.pdf");  
//////////////////////////////////////////////////////////////

Resulting Output:

Share:

Leave a Reply

Your email address will not be published.

The following GDPR rules must be read and accepted:
This form collects your name, email and content so that we can keep track of the comments placed on the website. For more info check our privacy policy where you will get more info on where, how and why we store your data.

Advertisment ad adsense adlogger