PDF generation from XHTML in a LAMP environment


The LAMP environment is widely used for web development, and it is an acronym for Linux, Apache, MySQL, and PHP. This environment is an open-source platform that is easy to use and deploy. PDF format is commonly used for sharing and exchanging documents over internet. However, generating PDF documents can be a challenging task in a LAMP environment, especially when converting XHTML documents. In this article, we will explore different methods used for PDF generation from XHTML in a LAMP environment.

What is XHTML?

XHTML stands for Extensible Hypertext Markup Language, which is a markup language that is used to create web pages. It is an extension of HTML language and is based on XML. XHTML is a stricter and more structured version of HTML, and it is designed to be compatible with XML. This means that it follows a set of rules and guidelines, making it easier for web developers to create web pages that are compatible with various web browsers.

PDF generation from XHTML

PDF generation from XHTML involves converting an XHTML document into a PDF document. There are different methods used for PDF generation from XHTML in a LAMP environment, and we will explore some of these methods.

DOMPDF

DOMPDF is a PHP library that is used to convert HTML and XHTML documents to PDF format. It is an open-source library that is easy to use and deploy in a LAMP environment. DOMPDF uses CSS (Cascading Style Sheets) standard to format HTML and XHTML documents, making it easy for web developers to create PDF documents that are consistent with original HTML and XHTML documents.

Example 

To use DOMPDF, we need to first download and install it on server. Once installed, we can use following code to generate a PDF document from an XHTML document.

<?php 
   require_once 'dompdf/autoload.inc.php'; 
   use Dompdf\Dompdf; 
   $dompdf = new Dompdf(); 
   $html = file_get_contents('test.html'); 
   $dompdf->loadHtml($html); 
   $dompdf->setPaper('A4', 'landscape'); 
   $dompdf->render(); 
   $dompdf->stream("test.pdf", array("Attachment" => false)); 
?>

In above code, we first require DOMPDF library and create a new DOMPDF object. We then load HTML or XHTML document using file_get_contents function and set paper size to A4 with a landscape orientation. Finally, we render PDF document and stream it to browser with filename "test.pdf".

TCPDF

TCPDF is another PHP library that is used to generate PDF documents from HTML and XHTML documents. It is an open-source library that is easy to use and deploy in a LAMP environment. TCPDF is a powerful library that offers a wide range of features, including support for multiple languages and character sets, encryption and compression of PDF documents, and support for HTML and XHTML documents.

Example  

To use TCPDF, we need to first download and install it on server. Once installed, we can use following code to generate a PDF document from an XHTML document.

<?php 
   require_once('tcpdf/tcpdf.php'); 
   $html = file_get_contents('test.html'); 
   $pdf = new TCPDF(PDF_PAGE_ORIENTATION, PDF_UNIT, PDF_PAGE_FORMAT, true, 'UTF-8', false); 
   $pdf->SetCreator(PDF_CREATOR); 
   $pdf->SetAuthor('Author Name'); 
   $pdf->SetTitle('Title'); 
   $pdf->SetSubject('Subject'); 
   $pdf->setFontSubsetting(true); 
   $pdf->AddPage(); 
   $pdf->writeHTML($html, true, false, true, false, ''); 
   $pdf->Output('test.pdf', 'I'); 
?>

In above code, we first require TCPDF library and create a new TCPDF object. We then load HTML or XHTML document using file_get_contents function and set PDF metadata (creator, author, title, and subject). Finally, we add a new page to PDF document, write HTML or XHTML document to page, and output PDF document to browser with filename "test.pdf".

FPDF

FPDF is a PHP library that is used to generate PDF documents from scratch. It is an open-source library that is easy to use and deploy in a LAMP environment. FPDF does not support HTML or XHTML documents, but it provides a powerful set of functions for creating PDF documents, including support for different fonts, colors, images, and page formats

Example  

To use FPDF, we need to first download and install it on server. Once installed, we can use following code to generate a PDF document from scratch.

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

In above code, we first require FPDF library and create a new FPDF object. We then add a new page to PDF document, set font to Arial with a bold style and a size of 16, and add a new cell with text "Hello World!". Finally, we output PDF document to browser.

Other tools and libraries used for PDF generation from XHTML in a LAMP environment include −

wkhtmltopdf

wkhtmltopdf is an open-source command-line tool that is used to convert HTML and XHTML documents to PDF format. It is based on WebKit rendering engine, which is used by popular web browsers such as Google Chrome and Safari. wkhtmltopdf offers a wide range of features, including support for CSS, JavaScript, and images.

Example  

To use wkhtmltopdf, we need to first install it on server. Once installed, we can use following command to generate a PDF document from an XHTML document.

wkhtmltopdf test.html test.pdf

In above command, we specify input file (test.html) and output file (test.pdf). wkhtmltopdf automatically converts XHTML document to PDF format.

mPDF

mPDF is a PHP library that is used to generate PDF documents from HTML and XHTML documents. It is an open-source library that is easy to use and deploy in a LAMP environment. mPDF is a powerful library that offers a wide range of features, including support for Unicode characters, HTML and XHTML documents, and multiple languages.

Example 

To use mPDF, we need to first download and install it on server. Once installed, we can use following code to generate a PDF document from an XHTML document.

<?php 
   require_once __DIR__ . '/vendor/autoload.php'; 
   $mpdf = new \Mpdf\Mpdf(); 
   $html = file_get_contents('test.html'); 
   $mpdf->WriteHTML($html); 
   $mpdf->Output(); 
?>

In above code, we first require mPDF library and create a new mPDF object. We then load HTML or XHTML document using file_get_contents function and write HTML or XHTML document to PDF document. Finally, we output PDF document to browser.

Conclusion

PDF generation from XHTML in a LAMP environment can be a challenging task, but there are different methods used for this purpose. DOMPDF, TCPDF, and FPDF are three PHP libraries that are commonly used for PDF generation from XHTML in a LAMP environment. Each library has its own set of features and benefits, and choice of library depends on specific requirements of project. By using these libraries, web developers can easily create PDF documents from XHTML documents and share them over internet.

Updated on: 14-Mar-2023

214 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements