Skip to content

DOMPDF and Composer Quick start guide

Brian Sweeney edited this page Sep 12, 2017 · 8 revisions

Installing DOMPDF with Composer

  1. Ensure Composer is installed on your system.

  2. Create a file named composer.json, and add the Dompdf package:

    {
        "require": {
            "dompdf/dompdf": "dev-master"
        }
    }
  3. From a shell, cd into your project folder and run composer update. This should install Dompdf and the dependencies php-font-lib and (starting with version 0.7.0) php-svg-lib into the vendor directory.

Sample PHP

Time to write some PHP! We'll use Composer Autoloading. Here's some boilerplate to get you started:

dompdf 0.7.0 or newer

<?php

// Composer's auto-loading functionality
require "vendor/autoload.php";

use Dompdf\Dompdf;

//generate some PDFs!
$dompdf = new DOMPDF();  //if you use namespaces you may use new \DOMPDF()
$dompdf->loadHtml($html);
$dompdf->render();
$dompdf->stream("sample.pdf", array("Attachment"=>0));

dompdf 0.6 family

<?php

// Composer's auto-loading functionality
require "vendor/autoload.php";

// inhibit DOMPDF's auto-loader
define('DOMPDF_ENABLE_AUTOLOAD', false);

//include the DOMPDF config file (required)
require 'vendor/dompdf/dompdf/dompdf_config.inc.php';

//if you get errors about missing classes please also add:
require_once('vendor/dompdf/dompdf/include/autoload.inc.php');

//generate some PDFs!
$dompdf = new DOMPDF();  //if you use namespaces you may use new \DOMPDF()
$dompdf->load_html($html);
$dompdf->render();
$dompdf->stream("sample.pdf", array("Attachment"=>0));