htmlToPdf([filename], [html], [usePrintCss]) Last updated: 17. Oct 2026
API only function
Converts html to a PDF file using headless Chrome.
Parameters
| Name | Type | Description |
|---|---|---|
| filename (optional) | string | Output filename. Default is "export.pdf". The PDF is sent as a download (attachment). |
| html (optional) | string | A string containing HTML to convert to PDF. Passing null does NOT fall back to the output buffer. To use print CSS with the output buffer, put <!-- ChromePDF print --> in the HTML instead of passing usePrintCss. |
| usePrintCss (optional) | bool | Use the print media when creating PDF, default is false. |
Returns
PDF file in bytes.
Options set with HTML comments in the document:
<!-- PDF Landscape -->— landscape orientation<!-- PDF No pageno -->— no page numbers in the footer (page numbers are on by default)<!-- ChromePDF print -->— use the print media CSS<!-- ChromePDF csspagesize -->— use the page size from CSS @page
The page is rendered in a 1920×1280 viewport.
Example
Code example (JS)
JS is normal JavaScript either running in the browser or on the Docly™ server.// Use the write function to write to the output buffer
write('<table>');
write('<tr><th>Column1</th><th>Column2</th></tr>'); // headers
write('<tr><td>Row1Value1</td><td>Row1Value2</td></tr>'); // row 1
write('<tr><td>Row2Value1</td><td>Row2Value2</td></tr>'); // row 2
write('</table>');
// Produce a download of the file:
return docly.htmlToPdf("test.pdf");
// Or, to display the PDF in the browser instead:
// let pdf = docly.htmlToPdf();
// docly.setContentDisposition("inline", "test.pdf"); // overwrites the filename if specified before
// return pdf;