DompdfAdapter¶
The DompdfAdapter class converts EPUB content into PDF using the Dompdf library (composer require dompdf/dompdf).
Dompdf needs the gd extension to embed PNG, GIF and WebP images; without it, converting a book that has such images throws a ConversionException ("The PHP GD extension is required"). TCPDF needs gd as well for PNG images with an alpha channel.
Key Methods¶
-
__construct(array $styles = [], EpubDocumentLoader $loader = new EpubDocumentLoader(), ?PdfConversionOptions $options = null): Optional styling parameters (the options, such as limits, a contents page and a custom page size, are described in Converter; without options the adapter behaves as before, and with options fixed-layout books are refused unlessallowFixedLayoutis set):Style Type Default fontstring DejaVu SansA font family Dompdf knows. font_sizeint or float 12In points; greater than zero. paper_sizestring A4A size Dompdf knows (any case), as in TCPDFAdapter.orientationstring portraitportraitorlandscape.margin_top,margin_right,margin_bottom,margin_leftint or float none In mm ( 12.5is fine; not negative), as inTCPDFAdapter. Without any margin option Dompdf keeps its own default margins; once one is given, sides that are not given are0.The constructor throws an
Exceptionfor a style it does not know (the message lists the valid ones), a value of the wrong type (a numeric string is not a number), an unusable value (a negative margin, an unknown orientation) and a paper size Dompdf does not know (Dompdf itself would fall back toletterwithout a word).Fonts and languages: the default font is DejaVu Sans, which Dompdf ships and which covers Latin, Greek, Cyrillic, Hebrew and Arabic, not only Latin-1 like the PDF core fonts (
Arialis mapped to Helvetica). Chinese, Japanese and Korean need a font installed in Dompdf: pass its family asfont, which always wins. Books withpage-progression-direction="rtl"in the spine, or (when the spine says nothing) with a primary language ofar,he,fa,ur,yi,ps,sd,ugordv, getdir="rtl"on the generated<html>and<body>; the book's language is set aslang. Dompdf does not shape Arabic script, so Arabic letters are not joined.Embedded fonts: the fonts a book loads with
@font-faceare used. Obfuscated fonts (IDPF or Adobe, listed inMETA-INF/encryption.xml) are de-obfuscated in memory and handed to Dompdf asdata:URIs, so nothing is written into the book; a font that cannot be de-obfuscated (an Adobe font in a book without aurn:uuid:identifier) is dropped. Dompdf loads only TrueType fonts (asrcwithoutformat(), orformat("truetype")). Dompdf keeps the fonts it loads in a font directory; each conversion gets a private temporary one, deleted afterwards, so books leave nothing in Dompdf's ownlib/fontsand cannot change the fonts of later conversions.A DRM-protected book is refused with a
ConversionException.Dompdf has no API to write a PDF outline (bookmarks), so use
TCPDFAdapterwhen you need them; it follows the book's nested table of contents. -
convert(string $epubDirectory, string $outputPath): void: Renders every spine document in reading order, each on a new page, sets the PDF title and author from the EPUB metadata, and writes the PDF. Throws aConversionExceptionif the book cannot be read, Dompdf fails (its exception is kept as the previous exception) or the PDF cannot be written. -
buildHtml(string $epubDirectory): string: Returns the HTML document thatconvert()renders, e.g. for previews or debugging.
Dompdf runs with remote resources, PHP and JavaScript disabled and its file access limited to the book directory; see Converter.
Usage Example¶
use PhpEpub\ConversionException;
use PhpEpub\Converters\DompdfAdapter;
$dompdfAdapter = new DompdfAdapter([
'font' => 'Times New Roman',
'font_size' => 14,
'paper_size' => 'A4',
'orientation' => 'landscape',
]);
try {
$dompdfAdapter->convert('/path/to/extracted/epub', '/path/to/output.pdf');
echo "EPUB successfully converted to PDF.";
} catch (ConversionException $e) {
echo "Conversion failed: " . $e->getMessage();
}