Convert Figma logo to code with AI

barryvdh logolaravel-dompdf

A DOMPDF Wrapper for Laravel

6,718
967
6,718
97

Top Related Projects

10,495

HTML to PDF converter for PHP

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

A slim PHP wrapper around wkhtmltopdf with an easy to use and clean OOP interface

Quick Overview

barryvdh/laravel-dompdf is a Laravel wrapper for the DOMPDF library, allowing easy PDF generation from HTML in Laravel applications. It provides a simple interface to create PDFs from views, making it convenient for generating reports, invoices, and other documents.

Pros

  • Easy integration with Laravel projects
  • Supports HTML and CSS for PDF styling
  • Ability to generate PDFs from Laravel views
  • Customizable options for PDF output

Cons

  • Limited support for advanced CSS features
  • Performance can be slow for complex or large documents
  • May require additional configuration for certain fonts or special characters
  • Occasional rendering inconsistencies across different browsers

Code Examples

  1. Basic PDF generation from a view:
use Barryvdh\DomPDF\Facade\Pdf;

$pdf = Pdf::loadView('pdf.invoice', $data);
return $pdf->download('invoice.pdf');
  1. Customizing PDF options:
$pdf = Pdf::loadView('pdf.report', $data);
$pdf->setPaper('a4', 'landscape');
$pdf->setOption('dpi', 150);
return $pdf->stream('report.pdf');
  1. Generating PDF in-memory:
$pdf = Pdf::loadView('pdf.document', $data);
$content = $pdf->output();
// Use $content for further processing or storage

Getting Started

  1. Install the package via Composer:

    composer require barryvdh/laravel-dompdf
    
  2. Add the service provider to config/app.php (Laravel 5.4 and below):

    Barryvdh\DomPDF\ServiceProvider::class,
    
  3. Add the facade to config/app.php (optional):

    'PDF' => Barryvdh\DomPDF\Facade\Pdf::class,
    
  4. Publish the configuration file (optional):

    php artisan vendor:publish --provider="Barryvdh\DomPDF\ServiceProvider"
    
  5. Use the facade in your controller or route:

    use Barryvdh\DomPDF\Facade\Pdf;
    
    $pdf = Pdf::loadView('pdf.example', $data);
    return $pdf->download('example.pdf');
    

Competitor Comparisons

10,495

HTML to PDF converter for PHP

Pros of dompdf

  • Standalone PHP library, can be used in any PHP project
  • More flexible and customizable for advanced PDF generation needs
  • Regularly updated with new features and improvements

Cons of dompdf

  • Requires more setup and configuration in Laravel projects
  • Less integrated with Laravel's ecosystem and features
  • May require more code to achieve the same results as the Laravel wrapper

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 the standalone dompdf library offers more flexibility but requires more setup code. laravel-dompdf is ideal for Laravel projects, whereas dompdf is better suited for non-Laravel PHP applications or scenarios requiring advanced customization.

4,186

Official clone of PHP library to generate PDF documents and barcodes

Pros of TCPDF

  • More feature-rich, offering advanced PDF manipulation capabilities
  • Standalone PHP library, not dependent on Laravel framework
  • Supports a wider range of image formats and barcode types

Cons of TCPDF

  • Steeper learning curve due to its extensive feature set
  • Larger file size and potentially slower performance for simple PDF generation tasks
  • Less seamless integration with Laravel compared to laravel-dompdf

Code Comparison

TCPDF:

$pdf = new TCPDF();
$pdf->AddPage();
$pdf->SetFont('helvetica', '', 12);
$pdf->Cell(0, 10, 'Hello, World!', 0, 1);
$pdf->Output('document.pdf', 'I');

laravel-dompdf:

$pdf = PDF::loadView('pdf.document', $data);
return $pdf->stream('document.pdf');

TCPDF offers more granular control over PDF creation, while laravel-dompdf provides a simpler, more Laravel-friendly approach. TCPDF is better suited for complex PDF manipulation tasks, whereas laravel-dompdf excels in quickly generating PDFs from HTML views within Laravel applications.

4,385

PHP library generating PDF files from UTF-8 encoded HTML

Pros of mpdf

  • More feature-rich, supporting a wider range of CSS properties and HTML elements
  • Better support for non-Latin scripts and complex text layouts
  • Offers more customization options for headers, footers, and page numbering

Cons of mpdf

  • Generally slower performance, especially for large documents
  • Steeper learning curve due to its extensive feature set
  • Larger file size, which may impact project size and load times

Code Comparison

mpdf:

$mpdf = new \Mpdf\Mpdf();
$mpdf->WriteHTML($html);
$mpdf->Output('document.pdf', 'D');

laravel-dompdf:

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

Both libraries offer straightforward ways to generate PDFs, but laravel-dompdf provides a more Laravel-specific integration. mpdf offers more low-level control, which can be beneficial for complex documents but may require more setup code.

mpdf excels in handling complex layouts and non-Latin scripts, making it suitable for multilingual or design-heavy documents. However, its performance can be a drawback for large-scale applications.

laravel-dompdf, while more limited in features, offers better performance and easier integration with Laravel projects. It's generally sufficient for simpler PDF generation needs and is more lightweight.

The choice between these libraries depends on the specific requirements of your project, balancing features against performance and ease of use.

A slim PHP wrapper around wkhtmltopdf with an easy to use and clean OOP interface

Pros of phpwkhtmltopdf

  • Better rendering of complex layouts and CSS3 features
  • Supports JavaScript execution in PDF generation
  • Faster processing for large documents

Cons of phpwkhtmltopdf

  • Requires wkhtmltopdf to be installed on the server
  • Less seamless integration with Laravel compared to laravel-dompdf
  • May have compatibility issues with some hosting environments

Code Comparison

laravel-dompdf:

use Barryvdh\DomPDF\Facade\Pdf;

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

phpwkhtmltopdf:

use mikehaertl\wkhtmlto\Pdf;

$pdf = new Pdf('http://example.com');
if (!$pdf->saveAs('document.pdf')) {
    echo $pdf->getError();
}

Both libraries offer straightforward ways to generate PDFs, but laravel-dompdf provides a more Laravel-specific implementation. phpwkhtmltopdf offers more flexibility and better rendering capabilities, especially for complex layouts, but requires additional server setup. The choice between the two depends on specific project requirements, server environment, and the complexity of the PDFs being generated.

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 Wrapper for Laravel

Laravel wrapper for Dompdf HTML to PDF Converter

Tests Packagist License Latest Stable Version Total Downloads Fruitcake

Installation

Laravel

Require this package in your composer.json and update composer. This will download the package and the dompdf + fontlib libraries also.

composer require barryvdh/laravel-dompdf

Lumen

After updating composer add the following lines to register provider in bootstrap/app.php

$app->register(\Barryvdh\DomPDF\ServiceProvider::class);

To change the configuration, copy the config file to your config folder and enable it in bootstrap/app.php:

$app->configure('dompdf');

Using

You can create a new DOMPDF instance and load a HTML string, file or view name. You can save it to a file, or stream (show in browser) or download.

    use Barryvdh\DomPDF\Facade\Pdf;

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

or use the App container:

    $pdf = App::make('dompdf.wrapper');
    $pdf->loadHTML('<h1>Test</h1>');
    return $pdf->stream();

Or use the facade:

You can chain the methods:

    return Pdf::loadFile(public_path().'/myfile.html')->save('/path-to/my_stored_file.pdf')->stream('download.pdf');

You can change the orientation and paper size, and hide or show errors (by default, errors are shown when debug is on)

    Pdf::loadHTML($html)->setPaper('a4', 'landscape')->setWarnings(false)->save('myfile.pdf')

If you need the output as a string, you can get the rendered PDF with the output() function, so you can save/output it yourself.

Use php artisan vendor:publish to create a config file located at config/dompdf.php which will allow you to define local configurations to change some settings (default paper etc). You can also use your ConfigProvider to set certain keys.

Configuration

The defaults configuration settings are set in config/dompdf.php. Copy this file to your own config directory to modify the values. You can publish the config using this command:

    php artisan vendor:publish --provider="Barryvdh\DomPDF\ServiceProvider"

You can still alter the dompdf options in your code before generating the pdf using this command:

    Pdf::setOption(['dpi' => 150, 'defaultFont' => 'sans-serif']);

Available options and their defaults:

  • rootDir: "{app_directory}/vendor/dompdf/dompdf"
  • tempDir: "/tmp" (available in config/dompdf.php)
  • fontDir: "{app_directory}/storage/fonts" (available in config/dompdf.php)
  • fontCache: "{app_directory}/storage/fonts" (available in config/dompdf.php)
  • chroot: "{app_directory}" (available in config/dompdf.php)
  • logOutputFile: "/tmp/log.htm"
  • defaultMediaType: "screen" (available in config/dompdf.php)
  • defaultPaperSize: "a4" (available in config/dompdf.php)
  • defaultFont: "serif" (available in config/dompdf.php)
  • dpi: 96 (available in config/dompdf.php)
  • fontHeightRatio: 1.1 (available in config/dompdf.php)
  • isPhpEnabled: false (available in config/dompdf.php)
  • isRemoteEnabled: false (available in config/dompdf.php)
  • isJavascriptEnabled: true (available in config/dompdf.php)
  • isHtml5ParserEnabled: true (available in config/dompdf.php)
  • allowedRemoteHosts: null (available in config/dompdf.php)
  • isFontSubsettingEnabled: false (available in config/dompdf.php)
  • debugPng: false
  • debugKeepTemp: false
  • debugCss: false
  • debugLayout: false
  • debugLayoutLines: true
  • debugLayoutBlocks: true
  • debugLayoutInline: true
  • debugLayoutPaddingBox: true
  • pdfBackend: "CPDF" (available in config/dompdf.php)
  • pdflibLicense: ""
  • adminUsername: "user"
  • adminPassword: "password"
  • artifactPathValidation: null (available in config/dompdf.php)

Note: Since 3.x the remote access is disabled by default, to provide more security. Use with caution!

Tip: UTF-8 support

In your templates, set the UTF-8 Metatag:

<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>

Tip: Page breaks

You can use the CSS page-break-before/page-break-after properties to create a new page.

<style>
.page-break {
    page-break-after: always;
}
</style>
<h1>Page 1</h1>
<div class="page-break"></div>
<h1>Page 2</h1>

License

This DOMPDF Wrapper for Laravel is open-sourced software licensed under the MIT license