8000
Skip to content
William Desportes edited this page Sep 30, 2025 · 9 revisions

Creating PDF/A documents with Dompdf

With the 3.1.0 release Dompdf is able to generate PDF documents that conform to the PDF/A-3b specification.

Note: Dompdf configures the backend PDF generation libraries (CPDF, PDFLib) to perform additional processing to ensure conformance to the specification, but it does not enforce the requirements of the specification. Dompdf also does not warn when a document contains elements that will prevent PDF/A validation. As such it is possible to produce a non-conforming PDF even if PDF/A support is enabled.

You enable PDF/A support via the isPdfAEnabled option:

$options = new Options([
    "isPdfAEnabled" => true
]);

Requirements

Use only embedded fonts

Dompdf uses core PDF fonts for certain family mappings (see About Fonts and Character Encoding for details). Because these fonts are not embedded in the PDF, you must ensure your document does not reference a font family that maps to one of these fonts.

The most common scenario where Dompdf will use one of these fonts is when your document specifies a generic font family (e.g., sans-serif), or a core PDF font (e.g., Helvetica). You may remap these font families to use an embeddable font.

Remapping these fonts programmatically is the easiest method.

$dompdf = new Dompdf();
$font_metrics = $dompdf->getFontMetrics();
$font_metrics->setFontFamily("courier", $font_metrics->getFamily("DejaVu Sans Mono"));
$font_metrics->setFontFamily("fixed", $font_metrics->getFamily("DejaVu Sans Mono"));
$font_metrics->setFontFamily("helvetica", $font_metrics->getFamily("DejaVu Sans"));
$font_metrics->setFontFamily("monospace", $font_metrics->getFamily("DejaVu Sans Mono"));
$font_metrics->setFontFamily("sans-serif", $font_metrics->getFamily("DejaVu Sans"));
$font_metrics->setFontFamily("serif", $font_metrics->getFamily("DejaVu Serif"));
$font_metrics->setFontFamily("times", $font_metrics->getFamily("DejaVu Serif"));
$font_metrics->setFontFamily("times-roman", $font_metrics->getFamily("DejaVu Serif"));

Remapping can also be done via CSS. A @font-face definition would have to be supplied for all variants (normal, bold, italic, bold-italic) of each generic family (monospace, sans-serif, serif). Only a single variant is shown below as an example.

<style>
@font-face {
    font-family: "sans-serif";
    font-weight: normal;
    font-style: normal;
    src: url(...) format(truetype);
}
</style>

You could alternately craft your document to omit any reference to the generic font families, in which case an embeddable font should also be specified as Dompdf's default font.

$options = new Options([
    "defaultFont" => "DejaVu Sans"
]);

When using the Cpdf backend you might also consider setting the default font to an embeddable font. Though not necessary, it can help avoid unintentional core font usage for content added outside the Dompdf rendering pipeline. This should be done immediately after Dompdf is instantiated.

$dompdf->get_canvas()->get_cpdf()->defaultFont = "./lib/fonts/DejaVuSerif.ttf";

Special note for rendering SVG documents

The SVG library used by Dompdf (SvgLib) does not yet support any kind of font management. As a result, specifying a non-core font for SVG content is particularly challenging (see dompdf/php-svg-lib#110 for more information). In order to avoid issues with PDF/A validation when your HTML includes a reference to an SVG document you should specify a font on the root element of an SVG using an inline style.

<svg height="100" width="100" xmlns="http://www.w3.org/2000/svg" style="font-family: DejaVu Sans;">

Zugferd / factur-x

With 3.1.1 additional support for features required by the zugferd / factur-x standard were added. To produce a document adhering to this standard enable PDF/A support, enhance the XMP metadata with the factur-x additions, and add your conforming XML invoice to the document.

For example:

$cpdf = $dompdf->getCanvas()->get_cpdf();
$cpdf->setAdditionalXmpRdf('...');
$cpdf->addEmbeddedFile("path/to/invoice.xml", "invoice.xml", "Invoice for services (Factur-X)", "text/xml", [$cpdf->catalogId => "Data"]);

Known limitations

  • When using the CPDF backend producing a document that utilizes CMYK colors or images is not currently supported.
  • SVG document rendering does not support full font management.

Clone this wiki locally

0