What?
This is a quick article to demonstrate how to use the built-in HTML to PDF generator in Zoho rather than using a third-party site over API. Give zoho.file.convertToPDF() an HTML string or a publicly accessible URL and, on success, it returns a PDF file you can attach to an email, upload to a record or save to WorkDrive.
Why?
It’s useful when a workflow already has the data and needs to turn it into a document people can share or keep:
- Order confirmations: Create a branded PDF when an order is approved and attach it to the customer’s email.
- Service reports: Turn a completed job’s checklist and technician notes into a PDF for the customer.
- Monthly reports: Save a dated snapshot of CRM figures before the live numbers change.
- Certificates: Generate a named PDF when someone completes a course.
- Public web pages: Save a printable copy of a published price list or product sheet.
How?
Convert an HTML string to PDF
Build the HTML in Deluge, then pass it to zoho.file.convertToPDF(). The optional second argument is a collection of PDF settings. This example creates an order confirmation with 15 mm margins and gives the returned file a name.
v_OrderNumber = "1042";
v_Html = "<html><body style='font-family: Arial, sans-serif;'>";
v_Html = v_Html + "<h1>Order confirmation</h1>";
v_Html = v_Html + "<p>Order reference: " + v_OrderNumber + "</p>";
v_Html = v_Html + "<p>2 × Desk lamp</p>";
v_Html = v_Html + "<p><strong>Total: £58.00</strong></p>";
v_Html = v_Html + "</body></html>";
c_Options = collection();
// A4 page size. Other units are "px", "in" and "cm".
c_Options.insert("units" : "mm");
c_Options.insert("page-width" : 210);
c_Options.insert("page-height" : 297);
c_Options.insert("landscape" : false); // true for landscape
c_Options.insert("top-margin" : 15);
c_Options.insert("bottom-margin" : 15);
c_Options.insert("left-margin" : 15);
c_Options.insert("right-margin" : 15);
// Rendering
c_Options.insert("scale" : 1); // 0.85 for smaller content
c_Options.insert("print-background" : true); // false to omit backgrounds
c_Options.insert("prefer-CSSPageSize" : false); // true to prefer the CSS @page size
c_Options.insert("disable-javascript" : false); // true to block scripts
c_Options.insert("auto-shrink" : false); // true to fit the page width
c_Options.insert("single-page-pdf" : false); // true to fit everything on one page
// If both fit options are true, single-page-pdf takes priority.
// Select pages. Use "1", "1-3", "1,3,5" or a mix such as "1,3-5".
// c_Options.insert("page-ranges" : "1,3-5");
// Repeat HTML on each page. Enable each spacing line with its template.
// c_Options.insert("header" : "Order confirmation");
// c_Options.insert("header-spacing" : 4);
// c_Options.insert("footer" : "Customer copy");
// c_Options.insert("footer-spacing" : 4);
// c_Options.insert("custom-header-footer" : true); // auto-size the templates
// Built-in page numbering. A custom template takes priority in the same area.
// c_Options.insert("page-number" : "footer"); // or "header"
// c_Options.insert("page-number-location" : "right"); // or "left" or "center"
// PDF properties. Zoho lists these fields without a sample payload.
// m_PdfMetadata = {"title" : "Order confirmation", "creator" : "Order workflow", "producer" : "Zoho Deluge"};
// c_Options.insert("pdf-metadata" : m_PdfMetadata);
// Set to true to hide the input HTML from Zoho's generation logs.
c_Options.insert("mask-html" : false);
f_Pdf = zoho.file.convertToPDF(v_Html, c_Options);
if(f_Pdf.isFile())
{
f_Pdf.setFileName("Order-" + v_OrderNumber + ".pdf");
info f_Pdf;
}
else
{
info "PDF conversion did not return a file.";
}
info f_Pdf; logs the result. Use f_Pdf in your next workflow step to send or store the file.
Convert a public URL to PDF
Pass the URL in place of the HTML string. If the page requires a login, fetch the data in your workflow and build the HTML string instead.
v_Url = "https://example.com/";
f_Pdf = zoho.file.convertToPDF(v_Url);
info f_Pdf;
Each conversion counts towards the Zoho service’s external call limit. See the Zoho Deluge documentation for options such as landscape orientation, headers, footers and page numbers.
Discussion
Comments
Questions, corrections and useful additions are reviewed before appearing here.
Discussion Newest first
No comments yet. Start the discussion.