Function API only Updated 17 Oct 2026
htmlToPdf([filename], [html], [usePrintCss])
Converts html to a PDF file using headless Chrome.
Parameters
filenamestringoptionalOutput filename. Default is "export.pdf". The PDF is sent as a download (attachment).
htmlstringoptionalA string containing HTML to convert to PDF.
If not specified, the current output buffer (what you have written with write) is used.
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.
usePrintCssbooloptionalUse 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
Normal JavaScript, 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;