convertToPDF

Overview

This task provides a robust solution for converting HTML content or URLs into well-structured, high-quality PDF files. 
By leveraging a powerful PDF generation service, convertToPDF task ensures accurate rendering of web-based content, including text, images, styles, and page formatting. The process is designed to maintain content integrity, supporting multi-page documents and page-break handling.

Syntax

zoho.file.convertToPDF(content/URL,<options_collection>);
ParameterData TypeDescription
ContentSTRINGA string containing valid HTML markup to convert.
The HTML content can be passed directly as a string literal or assigned to a variable and referenced as the parameter value.
URLSTRINGA publicly accessible URL pointing to the file or webpage to be converted.
Options_collection (Optional)COLLECTION
(KEY-VALUE)
Use this collection to pass one or more of the following parameters as key-value pairs.

options_collection Parameters

The following parameters can be passed as key-value pairs within the options_collection to customize the PDF output.

ParameterData TypeDescription
scaleNUMBERControls how large or small the webpage appears when rendered into the PDF. The default value is 1.
landscapeBOOLEANSet this to true if you want the PDF to be generated in landscape orientation. The default value is false.
top-marginNUMBERSets the amount of space between the content and the top edge of the PDF page. The default value is 0.
bottom-marginNUMBERSets the amount of space between the content and the bottom edge of the PDF page. The default value is 0.
left-marginNUMBERSets the amount of space between the content and the left edge of the PDF page. The default value is 0.
right-marginNUMBERSets the amount of space between the content and the right edge of the PDF page. The default value is 0.
page-widthNUMBERSets the width of each page in the PDF using the unit of measurement you have specified. The default value is 210.
page-heightNUMBERSets the height of each page in the PDF using the unit of measurement you have specified. The default value is 297.
unitsSTRINGSpecifies the unit of measurement to be used for the width, height, and margin values. The accepted values are px, in, cm, and mm. The default value is mm.
headerSTRINGProvide the HTML content that you want to display in the header section of every page in the PDF.
header-spacingNUMBERSets the amount of space between the header and the main content of the page. This setting applies only when a header has been defined. The default value is 0 mm.
footerSTRINGProvide the HTML content that you want to display in the footer section of every page in the PDF.
footer-spacingNUMBERSets the amount of space between the footer and the main content of the page. This setting applies only when a footer has been defined. The default value is 0 mm.
custom-header-footerBOOLEANSet this to true if you want the header and footer height to be automatically calculated based on the content and aligned with the page body. The default value is false.
print-backgroundBOOLEANSet this to true if you want background colors and images to be included when the PDF is generated. The default value is true.
prefer-CSSPageSizeBOOLEANSet this to true if you want the page size defined in the webpage's own styling to take priority over the width and height values you have provided here. The default value is false.
disable-javascriptBOOLEANSet this to true if you want to prevent the webpage from running any scripts while the PDF is being generated. The default value is false.
auto-shrinkBOOLEANSet this to true if you want the webpage to be automatically scaled down to fit within the available page width. If the required element is not found on the page, the scale value you have provided will be used instead. The default value is false.
page-rangesSTRINGSpecifies which pages you want to include in the PDF. You can provide a range of pages (Example: 4-8), individual page numbers separated by commas (Example: 2,3,4), or a combination of both (Example: 2, 4-5).
single-page-pdfBOOLEANSet this to true if you want all the content to be fitted into a single PDF page. If both auto-shrink and single-page-pdf are enabled at the same time, single-page-pdf will take priority. The default value is false.
page-numberSTRINGSet this to header or footer to display page numbers in the respective section of the PDF. Page numbers are centered by default. If a header or footer template is also defined, the template will take priority over the page number.
page-number-locationSTRINGSpecifies where the page number should appear within the header or footer. The accepted values are left, right, and center. The default value is center. This setting will only take effect when the page-number parameter has been enabled.
pdf-metadataJSONAllows you to define document properties for the generated PDF, including the title, creator, and producer, by providing them as a structured object.
mask-htmlBOOLEANSet this to true if you want the HTML content to be hidden from the generation logs. The default value is false.

Example

Convert HTML String to PDF in Zoho Deluge

This script demonstrates how to convert an HTML string into a PDF file using Zoho Deluge. It is particularly useful for generating dynamic documents such as reports, invoices, or certificates directly from HTML content.

//  Define the HTML content with a page break between two sections
content = "<html><body>HTML Content on page One <div style='page-break-after:always'></div> HTML Content on page Two </body></html>";
//  Convert the HTML content to a PDF file
file = zoho.file.convertToPDF(content);
//  Log the PDF file object details for verification
info file;
//  Check and confirm if the returned object is a valid file
info file.isFile();

Convert a URL to PDF and Rename the File in Zoho Deluge

This script demonstrates how to convert a publicly accessible URL to a PDF file and apply file-level operations, such as renaming the output using Deluge scripting.

// Step 1: Specify the URL to convert
url = "https://www.zoho.com/deluge/";
// Step 2: Convert the URL content to a PDF file
file = zoho.file.convertToPDF(url);
// Step 3: Log file details and confirm file object
info file;
info file.isFile();
// Step 4: Rename the generated PDF file
file.setFileName("CRMAccounts");
// Step 5: Log the updated file object with the new name
info file;

Convert Multi-Section HTML Content to a Customized Single-Page PDF

This script demonstrates how to convert multi-section HTML content to a PDF file and apply custom layout settings, such as top margin, page width, and single-page rendering.

// Initialize a collection to store custom PDF layout parameters
customisedParam = collection();
customisedParam.insert("top-margin":"5");
customisedParam.insert("page-width":"3.3");
customisedParam.insert("single-page-pdf":true);
// Define HTML content with a page break between two sections
content = "<html><body>HTML Content on page One <div style='page-break-after:always'></div> HTML Content on page Two </body></html>";
// Convert the HTML content to a PDF file using the custom parameters
file = zoho.file.convertToPDF(content, customisedParam);
info file;

Usecase

A sales manager wants to archive monthly CRM dashboard reports as PDF documents for compliance and internal review automatically. These dashboards are hosted on a secure internal web URL and display key sales metrics, including revenue trends, lead conversions, and sales rep performance. 
Each month, the manager wants:

  • The dashboard page to be converted into a PDF
  • The file to be renamed with a standard naming convention (e.g., CRM_Monthly_Report_April_2025.pdf)
  • The PDF to be saved or emailed for record-keeping and performance analysis
// Define the URL of the public CRM dashboard (make sure it’s accessible if hosted internally)
dashboard_url = "https://crm.zoho.com/reports/monthly-dashboard?month=April&year=2025";
// Convert the live dashboard page to a PDF file
pdf_file = zoho.file.convertToPDF(dashboard_url);
// Verify the file object
if (pdf_file.isFile())
{
// Construct a meaningful filename based on the report context
    report_name = "CRM_Monthly_Report_April_2025";
    
// Rename the file for easy identification
    pdf_file.setFileName(report_name);
// (Optional) Save the file to WorkDrive, attach to an email, or upload to a record
// Example: Send email to manager with attached file
    sendmail
    [
        from: zoho.adminuserid
        to: "sales.manager@company.com"
        subject: "Monthly CRM Report - April 2025"
        message: "Attached is your monthly CRM performance dashboard."
        attachments: file: pdf_file
    ];
    info "PDF report generated and emailed successfully.";
}
else
{
    info "PDF generation failed. Please check the URL or network accessibility.";
}

Response Format

Note: On success, the response will return a PDF file.

Failure Response for Exceeding Maximum URL Length

{"Message":"Unable to process your request","details":{"param name":"url","Error":"More than maximum length","StatusCode":400}}

Points to Note

  1. The maximum number of characters allowed for HTML content is 12,000,000.
  2. The maximum length of a URL that can be converted is 25,000 characters.
  3. The maximum processing time for a conversion request is 301 seconds.
  4. The converted PDF files support built-in file operations, such as renaming using setFileName().
  5. Each time the zoho.file.convertToPDF function is executed, it triggers an API request in the back-end. This call is deducted from the external calls limit available for the service from which the task is executed, based on your pricing plan.

Related Links