1. Products
  2.   HTML
  3.   PHP
  4.   HTML2PDF
 
  

Open Source PHP Library for Creating PDF Files from HTML

Free PHP API for Creating PDF Documents from HTML Content. It supports HTML and CSS rendering into PDF via PHP code.

In today's digital era, the need for converting web content into portable document formats (PDFs) has become increasingly important. One powerful and versatile library that simplifies this process is HTML2PDF. HTML2PDF is a popular open-source library that provides a simple and effective way to convert HTML documents to PDF format. It allows software developers to seamlessly generate high-quality PDFs from HTML content, including CSS styling, images, and complex layouts. With HTML2PDF, software developers can transform web pages, HTML templates, or even dynamically generated content into professional-looking PDF documents.

The HTML2PDF library can be implemented very straightforward, thanks to its well-documented APIs and intuitive usage. There are several important features part of the library, such as comprehensive support for HTML and CSS elements, customizing headers and footers of PDF documents, defining the page size and orientation for the generated PDFs, embedding images and fonts in the PDFs, archive web-based information as PDFs, generate printable reports, create professional-looking invoices, convert web-based documentation and tutorials into downloadable PDF files and many more.

The HTML2PDF library offers easy integration with various programming languages and frameworks. HTML2PDF is built to be platform-independent, making it versatile enough to run on various operating systems, including Windows, macOS, and Linux. Whether you are archiving web content, generating reports, or creating invoices, HTML2PDF simplifies the conversion process, enhancing the overall user experience. So, if you find yourself in need of web to PDF conversion, consider giving HTML2PDF a try and unlock its potential to streamline your workflow.

Previous Next

Getting Started with HTML2PDF

The recommended and easiest way to install HTML2PDF is using Composer, the dependency management tool for PHP. Please use the following command a smooth installation.

Install HTML2PDF via Composer

composer require spipu/html2pdf 

You can also install it manually; download the latest release files directly from GitHub repository.

Generate PDF from HTML using PHP

The HTML2PDF library has provided simple and effective way to convert HTML documents to PDF format inside PHP applications. First, users need to create or obtain the HTML content they wish to convert into a PDF. Ensure that it adheres to proper standards and includes any necessary CSS stylesheets and images. Now, utilize the HTML2PDF library functions or methods to convert the HTML content into a PDF document. Customize the conversion process as needed by specifying page settings, headers, footers, and other options. Once the PDF is generated, users can choose to save it to a local file or stream it directly to the user's browser, depending on their application's requirements. The following example shows how software developers can convert an existing HTML file to PDF using PHP commands.

How to Convert HTML Documents to PDF via PHP?

require_once dirname(__FILE__).'/../vendor/autoload.php';

use Spipu\Html2Pdf\Html2Pdf;
use Spipu\Html2Pdf\Exception\Html2PdfException;
use Spipu\Html2Pdf\Exception\ExceptionFormatter;

try {
    ob_start();
    include dirname(__FILE__).'/res/test.php';
    $content = ob_get_clean();

    $html2pdf = new Html2Pdf('P', 'A4', 'fr');
    $html2pdf->writeHTML($content);
    $html2pdf->output('test.pdf');
} catch (Html2PdfException $e) {
    $html2pdf->clean();

    $formatter = new ExceptionFormatter($e);
    echo $formatter->getHtmlMessage();
}

Manage Image in HTML to PDF via PHP API Handling

The open source HTML2PDF library allows software developers to add and manage images inside their PDF documents using PHP API. The library enables seamless inclusion of images inside your PDFs. It supports various image formats, including PNG, JPEG, BMP and GIF, allowing software developers to create visually appealing documents with embedded graphics. The following example shows how to add SVG images to PDF file using PHP API.

How to Add SVG Images to PDF File inside PHP Apps?

require_once dirname(__FILE__).'/../vendor/autoload.php';

use Spipu\Html2Pdf\Html2Pdf;
use Spipu\Html2Pdf\Exception\Html2PdfException;
use Spipu\Html2Pdf\Exception\ExceptionFormatter;

try {
    ob_start();
    include dirname(__FILE__).'/res/svg.php';
    $content = ob_get_clean();

    $html2pdf = new Html2Pdf('P', 'A4', 'fr');
    $html2pdf->pdf->SetDisplayMode('fullpage');
    $html2pdf->writeHTML($content);
    $html2pdf->output('svg.pdf');
} catch (Html2PdfException $e) {
    $html2pdf->clean();

    $formatter = new ExceptionFormatter($e);
    echo $formatter->getHtmlMessage();
}