How to generate PDF in PHP

https://pixabay.com

Sharing is caring!

424 Views -

There are multiple PDF libraries that you can use in your projects to generate PDF dynamically either from HTML or from static content.

These are independent libraries that you can use directly in your Php projects or Frameworks. You can download them directly from GITHUB or install them in your projects using the composer.

Here is the detailed list from which you can use anyone.

1-Dompdf

https://github.com/dompdf/dompdf
Dompdf is an HTML to PDF converter

Features

It can Handles most CSS 2.1 and a few CSS3 properties, including @import, @media & @page rules
It can support most presentational HTML 4.0 attributes
It can supports external stylesheets, either local or through http/ftp (via fopen-wrappers)
It can supports complex tables, including row & column spans, separate & collapsed border models, individual cell styling
Image support (gif, png (8, 24 and 32 bit with alpha channel), bmp & jpeg)
Has no dependencies on external PDF libraries, thanks to the R&OS PDF class
Inline PHP support
Basic SVG support

Requirements

PHP version 7.1 or higher
DOM extension
MBString extension
php-font-lib
php-svg-lib

Install with composer
To install with Composer, simply require the latest version of this package

composer require dompdf/dompdf

Quick Start

// reference the Dompdf namespace
use Dompdf\Dompdf;

// instantiate and use the dompdf class
$dompdf = new Dompdf();
$dompdf->loadHtml('hello world');

// (Optional) Setup the paper size and orientation
$dompdf->setPaper('A4', 'landscape');

// Render the HTML as PDF
$dompdf->render();

// Output the generated PDF to Browser
$dompdf->stream();

Examples – https://dompdf.net/examples.php

2-FPDF

http://www.fpdf.org/
FPDF is a PHP class which allows to generate PDF files with core PHP, that is to say without using the PDFlib library. F from FPDF stands for Free: you may use it for any kind of usage and modify it to suit your needs.

It requires no extension (except Zlib to enable compression and GD for GIF support). The latest version requires at least PHP 5.1.
Download Latest version from here – http://www.fpdf.org/en/dl.php?v=182&f=zip

Example

<?php
require('fpdf.php');

$pdf = new FPDF();
$pdf->AddPage();
$pdf->SetFont('Arial','B',16);
$pdf->Cell(40,10,'Hello World!');
$pdf->Output();
?>

3-mPDF 
mPDF
mPDF is a PHP library that generates PDF files from UTF-8 encoded HTML.

Requirements

mPDF >=7.0 is supported on PHP ^5.6 || ~7.0.0 || ~7.1.0 || ~7.2.0
PHP 7.3 is supported since mPDF v7.1.7
PHP 7.4 is supported since mPDF v8.0.4

PHP mbstring and gd extensions have to be loaded.

Install via Composer

composer require mpdf/mpdf

Example

<?php

require_once __DIR__ . '/vendor/autoload.php';

$mpdf = new \Mpdf\Mpdf();
$mpdf->WriteHTML('<h1>Hello world!</h1>');
$mpdf->Output();

4-Snappy

Snappy
Snappy is a PHP library allowing thumbnail, snapshot or PDF generation from a url or a html page. It uses the excellent WebKit-based wkhtmltopdf and wkhtmltoimage.

Install via composer

composer require knplabs/knp-snappy

Example

<?php

require __DIR__ . '/vendor/autoload.php';

use Knp\Snappy\Pdf;

$snappy = new Pdf('/usr/local/bin/wkhtmltopdf');

// or you can do it in two steps
$snappy = new Pdf();
$snappy->setBinary('/usr/local/bin/wkhtmltopdf');

Display the pdf in the browser

$snappy = new Pdf('/usr/local/bin/wkhtmltopdf');
header('Content-Type: application/pdf');
echo $snappy->getOutput('http://www.github.com');

Download the pdf from the browser

$snappy = new Pdf('/usr/local/bin/wkhtmltopdf');
header('Content-Type: application/pdf');
header('Content-Disposition: attachment; filename="file.pdf"');
echo $snappy->getOutput('http://www.github.com');

 

If you know some more awesome PHP library to generate PDF, please share it with us.

Enjoy!

0 0 votes
Article Rating
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments