HTML-to-PDF with PHP


Code Examples

Composer Installation

composer require docraptor/docraptor


Below is a barebones example, more robust examples with file output and error handling, asynchronous generation, hosted documents, or asynchronous hosted documents are also available.

$docraptor = new DocRaptor\DocApi();
// $docraptor->getConfig()->setDebug(true);

$doc = new DocRaptor\Doc();
$doc->setTest(true);                                                   // test documents are free but watermarked
$doc->setDocumentContent("<html><body>Hello World</body></html>");     // supply content directly
// $doc->setDocumentUrl(""); // or use a url
$doc->setName("docraptor-php.pdf");                                    // help you find a document later
$doc->setDocumentType("pdf");                                          // pdf or xls or xlsx
// $doc->setJavascript(true);                                          // enable JavaScript processing
// $prince_options = new DocRaptor\PrinceOptions();                    // pdf-specific options
// $doc->setPrinceOptions($prince_options);
// $prince_options->setMedia("screen");                                // use screen styles instead of print styles
// $prince_options->setBaseurl("");                    // pretend URL when using document_content

$create_response = $docraptor->createDoc($doc);

Documents created synchronously like above are limited to 60 seconds of generation time, the asynchronous method allows up to 10 minutes.

Our styling documentation and knowledge base contain tips and guides on creating headers, footers, page numbers, table of contents, and much more.

With DocRaptor, it's fast and painless to convert HTML, CSS, and JavaScript into PDF and XLS documents with PHP. Below are working PHP examples for creating documents. Our API reference lists all the generation options and our style and formatting guide will help make it look perfect.