Convert Figma logo to code with AI

dompdf logodompdf

HTML to PDF converter for PHP

10,495
1,789
10,495
564

Top Related Projects

A DOMPDF Wrapper for Laravel

4,186

Official clone of PHP library to generate PDF documents and barcodes

4,385

PHP library generating PDF files from UTF-8 encoded HTML

7,241

A pure PHP library for reading and writing word processing documents

Quick Overview

Dompdf is a popular HTML to PDF conversion library for PHP. It allows developers to generate PDF documents from HTML and CSS, supporting a wide range of CSS features and offering flexible configuration options. Dompdf is widely used for creating reports, invoices, and other document types in web applications.

Pros

  • Supports a wide range of CSS features, including floats, positioning, and tables
  • Offers good Unicode support for multi-language documents
  • Provides flexible configuration options for fine-tuning PDF output
  • Actively maintained with regular updates and bug fixes

Cons

  • Can be slower than some alternative PDF generation libraries
  • May have rendering inconsistencies with complex layouts or advanced CSS features
  • Limited support for JavaScript-generated content
  • Memory-intensive for large documents or complex layouts

Code Examples

  1. Basic PDF generation:
require_once 'vendor/autoload.php';
use Dompdf\Dompdf;

$dompdf = new Dompdf();
$html = '<h1>Hello, World!</h1><p>This is a simple PDF generated with Dompdf.</p>';
$dompdf->loadHtml($html);
$dompdf->render();
$dompdf->stream('document.pdf');
  1. Setting paper size and orientation:
$dompdf = new Dompdf();
$dompdf->setPaper('A4', 'landscape');
$dompdf->loadHtml($html);
$dompdf->render();
  1. Saving PDF to a file:
$dompdf = new Dompdf();
$dompdf->loadHtml($html);
$dompdf->render();
$output = $dompdf->output();
file_put_contents('document.pdf', $output);

Getting Started

  1. Install Dompdf using Composer:
composer require dompdf/dompdf
  1. Create a PHP file (e.g., generate_pdf.php) with the following content:
<?php
require_once 'vendor/autoload.php';
use Dompdf\Dompdf;

$dompdf = new Dompdf();
$html = '<h1>My First PDF</h1><p>Generated with Dompdf</p>';
$dompdf->loadHtml($html);
$dompdf->render();
$dompdf->stream('my_first_pdf.pdf');
  1. Run the PHP file in your web server to generate and download the PDF.

Competitor Comparisons

A DOMPDF Wrapper for Laravel

Pros of laravel-dompdf

  • Seamless integration with Laravel framework
  • Simplified configuration through Laravel's service provider
  • Automatic facade registration for easy use in Laravel applications

Cons of laravel-dompdf

  • Limited to Laravel ecosystem, not suitable for non-Laravel PHP projects
  • May introduce additional overhead due to Laravel integration
  • Potential version conflicts with Laravel dependencies

Code Comparison

dompdf:

require_once 'dompdf/autoload.inc.php';
$dompdf = new Dompdf\Dompdf();
$dompdf->loadHtml($html);
$dompdf->render();
$dompdf->stream("document.pdf");

laravel-dompdf:

use Barryvdh\DomPDF\Facade\Pdf;

$pdf = Pdf::loadView('pdf.invoice', $data);
return $pdf->download('invoice.pdf');

The laravel-dompdf package provides a more Laravel-friendly syntax and integration, while dompdf offers a standalone solution for PHP projects. laravel-dompdf simplifies the process of generating PDFs within Laravel applications, but it's limited to the Laravel ecosystem. dompdf, on the other hand, is more versatile and can be used in any PHP project, albeit with slightly more verbose code.

4,186

Official clone of PHP library to generate PDF documents and barcodes

Pros of TCPDF

  • More comprehensive feature set, including support for various image formats and advanced PDF features
  • Better performance for large documents and complex layouts
  • Stronger support for Unicode and non-Latin character sets

Cons of TCPDF

  • Steeper learning curve due to more complex API
  • Larger codebase and memory footprint
  • Less frequent updates and community contributions

Code Comparison

TCPDF:

$pdf = new TCPDF(PDF_PAGE_ORIENTATION, PDF_UNIT, PDF_PAGE_FORMAT, true, 'UTF-8', false);
$pdf->AddPage();
$pdf->SetFont('helvetica', '', 12);
$pdf->Cell(0, 10, 'Hello World!', 0, 1, 'C');
$pdf->Output('example.pdf', 'F');

dompdf:

$dompdf = new Dompdf();
$html = '<h1>Hello World!</h1>';
$dompdf->loadHtml($html);
$dompdf->render();
$dompdf->stream("example.pdf");

Both TCPDF and dompdf are popular PHP libraries for generating PDF documents. TCPDF offers more advanced features and better performance for complex documents, while dompdf provides a simpler API and is easier to use for basic HTML-to-PDF conversion. The choice between them depends on the specific requirements of your project, such as the complexity of the documents you need to generate and the level of control you require over the PDF output.

4,385

PHP library generating PDF files from UTF-8 encoded HTML

Pros of mPDF

  • Better support for complex layouts and CSS3 features
  • Improved handling of non-Latin scripts and right-to-left languages
  • More extensive documentation and examples

Cons of mPDF

  • Slower performance, especially for large documents
  • Larger file size and memory footprint
  • Steeper learning curve for beginners

Code Comparison

mPDF:

$mpdf = new \Mpdf\Mpdf();
$mpdf->WriteHTML($html);
$mpdf->Output();

dompdf:

$dompdf = new Dompdf();
$dompdf->loadHtml($html);
$dompdf->render();
$dompdf->stream();

Both libraries offer similar basic usage, but mPDF provides more advanced configuration options and methods for complex layouts. dompdf has a simpler API, making it easier to get started for basic PDF generation tasks.

mPDF excels in handling complex layouts, CSS3 features, and non-Latin scripts, making it suitable for multilingual and design-heavy projects. However, it comes at the cost of performance and resource usage.

dompdf, on the other hand, is lighter and faster, making it a good choice for simpler PDF generation tasks or when performance is a priority. It has limitations in advanced CSS support and complex layouts compared to mPDF.

7,241

A pure PHP library for reading and writing word processing documents

Pros of PHPWord

  • Supports creation and editing of Word documents (.docx, .doc, .odt)
  • Offers more advanced formatting options and document structure manipulation
  • Can generate documents from templates, making it versatile for various use cases

Cons of PHPWord

  • Steeper learning curve due to more complex API and features
  • Larger library size and potentially higher resource usage
  • May require additional setup for PDF output (e.g., using a converter)

Code Comparison

PHPWord:

$phpWord = new \PhpOffice\PhpWord\PhpWord();
$section = $phpWord->addSection();
$section->addText('Hello World!');
$writer = \PhpOffice\PhpWord\IOFactory::createWriter($phpWord, 'Word2007');
$writer->save('helloWorld.docx');

dompdf:

$dompdf = new Dompdf();
$html = '<html><body><h1>Hello World!</h1></body></html>';
$dompdf->loadHtml($html);
$dompdf->render();
$dompdf->stream('document.pdf');

While PHPWord is focused on creating and manipulating Word documents with advanced features, dompdf specializes in converting HTML to PDF. PHPWord offers more flexibility for Word document creation, but dompdf is simpler for generating PDFs from HTML content. The choice between them depends on the specific requirements of your project and the desired output format.

Convert Figma logo designs to code with AI

Visual Copilot

Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.

Try Visual Copilot

README

Dompdf

Build Status Latest Release Total Downloads License

Dompdf is an HTML to PDF converter

At its heart, dompdf is (mostly) a CSS 2.1 compliant HTML layout and rendering engine written in PHP. It is a style-driven renderer: it will download and read external stylesheets, inline style tags, and the style attributes of individual HTML elements. It also supports most presentational HTML attributes.

This document applies to the latest stable code which may not reflect the current release. For released code please navigate to the appropriate tag.


Check out the demo and ask any question on StackOverflow or in Discussions.

Follow us on Twitter.


Features

  • Handles most CSS 2.1 and a few CSS3 properties, including @import, @media & @page rules
  • Supports most presentational HTML 4.0 attributes
  • Supports external stylesheets, either local or through http/ftp (via fopen-wrappers)
  • 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)
  • No dependencies on external PDF libraries, thanks to the R&OS PDF class
  • Inline PHP support
  • Basic SVG support (see "Limitations" below)

Requirements

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

Note that some required dependencies may have further dependencies (notably php-svg-lib requires sabberworm/php-css-parser).

Recommendations

  • GD (for image processing)
    • Additionally, the IMagick or GMagick extension improves image processing performance for certain image types
  • OPcache (OPcache, XCache, APC, etc.): improves performance

Visit the wiki for more information: https://github.com/dompdf/dompdf/wiki/Requirements

About Fonts & Character Encoding

PDF documents internally support the following fonts: Helvetica, Times-Roman, Courier, Zapf-Dingbats, & Symbol. These fonts only support Windows ANSI encoding. In order for a PDF to display characters that are not available in Windows ANSI, you must supply an external font. Dompdf will embed any referenced font in the PDF so long as it has been pre-loaded or is accessible to dompdf and reference in CSS @font-face rules. See the font overview for more information on how to use fonts.

The DejaVu TrueType fonts have been pre-installed to give dompdf decent Unicode character coverage by default. To use the DejaVu fonts reference the font in your stylesheet, e.g. body { font-family: DejaVu Sans; } (for DejaVu Sans). The following DejaVu 2.34 fonts are available: DejaVu Sans, DejaVu Serif, and DejaVu Sans Mono.

Easy Installation

Install with composer

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

composer require dompdf/dompdf

Make sure that the autoload file from Composer is loaded.

// somewhere early in your project's loading, require the Composer autoloader
// see: http://getcomposer.org/doc/00-intro.md
require 'vendor/autoload.php';

Download and install

Download a packaged archive of dompdf and extract it into the directory where dompdf will reside

Use the packaged release autoloader to load dompdf, libraries, and helper functions in your PHP:

// include autoloader
require_once 'dompdf/autoload.inc.php';

Note: packaged releases are named according using semantic versioning (dompdf_MAJOR-MINOR-PATCH.zip). So the 1.0.0 release would be dompdf_1-0-0.zip. This is the only download that includes the autoloader for Dompdf and all its dependencies.

Install with git

From the command line, switch to the directory where dompdf will reside and run the following commands:

git clone https://github.com/dompdf/dompdf.git
cd dompdf/lib

git clone https://github.com/PhenX/php-font-lib.git php-font-lib
cd php-font-lib
git checkout 0.5.1
cd ..

git clone https://github.com/PhenX/php-svg-lib.git php-svg-lib
cd php-svg-lib
git checkout v0.3.2
cd ..

git clone https://github.com/sabberworm/PHP-CSS-Parser.git php-css-parser
cd php-css-parser
git checkout 8.1.0

Require dompdf and it's dependencies in your PHP. For details see the autoloader in the utils project.

Framework Integration

Quick Start

Just pass your HTML in to dompdf and stream the output:

// 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();

Setting Options

Set options during dompdf instantiation:

use Dompdf\Dompdf;
use Dompdf\Options;

$options = new Options();
$options->set('defaultFont', 'Courier');
$dompdf = new Dompdf($options);

or at run time

use Dompdf\Dompdf;

$dompdf = new Dompdf();
$options = $dompdf->getOptions();
$options->setDefaultFont('Courier');
$dompdf->setOptions($options);

See Dompdf\Options for a list of available options.

Resource Reference Requirements

In order to protect potentially sensitive information Dompdf imposes restrictions on files referenced from the local file system or the web.

Files accessed through web-based protocols have the following requirements:

  • The Dompdf option "isRemoteEnabled" must be set to "true"
  • PHP must either have the curl extension enabled or the allow_url_fopen setting set to true

Files accessed through the local file system have the following requirement:

  • The file must fall within the path(s) specified for the Dompdf "chroot" option

Limitations (Known Issues)

  • Table cells are not pageable, meaning a table row must fit on a single page.
  • Elements are rendered on the active page when they are parsed.
  • Embedding "raw" SVG's (<svg><path...></svg>) isn't working yet, you need to either link to an external SVG file, or use a DataURI like this:
    $html = '<img src="data:image/svg+xml;base64,' . base64_encode($svg) . '" ...>';
    
    Watch https://github.com/dompdf/dompdf/issues/320 for progress
  • Does not support CSS flexbox.
  • Does not support CSS Grid.
  • A single Dompdf instance should not be used to render more than one HTML document because persisted parsing and rendering artifacts can impact future renders.

Donate button

If you find this project useful, please consider making a donation. Any funds donated will be used to help further development on this project.)