HTML-to-PDF Conversion Service Documentation
Overview
This document describes the technical details and usage of the HTML-to-PDF conversion service designed to be integrated within Salesforce. The service offers an efficient way to convert HTML content or a URL pointing to HTML content into a PDF.
Endpoint
https://www.3bapi.com/v1/html-to-pdf
Request Parameters
1. file
(Object)
This object should have one of the following properties:
- url (string): A public URL pointing to the desired content.
- content (string): Direct HTML content that needs to be converted to PDF.
2. options
(Object)
This object contains the configurations for the PDF generation. Here are the available properties:
- args (Array<string>): Additional arguments for the browser instance. Overwrites default arguments. Full list of Chromium flags. Defaults are
['--no-sandbox', '--disable-setuid-sandbox']
. - path (string): Where to save the PDF. If given a relative path, it resolves to the current working directory. If omitted, the PDF is not saved.
- scale (number): Rendering scale. Defaults to 1. Must be between 0.1 and 2.
- displayHeaderFooter (boolean): Choose to display headers and footers. Default is false.
- headerTemplate & footerTemplate (string): Valid HTML templates for headers and footers. Classes used to inject values include:
date
: Formatted datetitle
: Document titleurl
: Document locationpageNumber
: Current page numbertotalPages
: Total document pages
- printBackground (boolean): Whether to print background graphics. Defaults to false.
- landscape (boolean): Paper orientation. Defaults to portrait (false).
- pageRanges (string): Specific pages to print, such as '1-5, 8, 11-13'. Defaults to all pages.
- format (string): Paper format, e.g., 'A4', 'Letter'. Overrides width or height options. Default is 'Letter'.
- width & height (string|number): Paper dimensions. Accept values with units, e.g., "10cm" or 300.
- margin (Object): Define paper margins. They default to none and accept values with units. Includes:
- top
- right
- bottom
- left
- preferCSSPageSize (boolean): Give priority to any CSS
@page
size declared in the page. Defaults to false.
Return Value
Upon processing the request, the service will return either a SuccessResponse
or an ErrorResponse
:
- SuccessResponse: Indicates the request was processed successfully. Contains:
responseObject
: Data payload, if any.message
: Optional message detailing the response.
- ErrorResponse: Indicates there was an error processing the request. Contains:
responseObject
: Data related to the error, if any.message
: Optional message detailing the error.
const requestPayload = {
file: {
url: "https://example.com/sample.html"
},
options: {
format: "A4",
landscape: true,
displayHeaderFooter: true,
headerTemplate: '<span class="title"></span>',
footerTemplate: '<span class="pageNumber"></span>/<span class="totalPages"></span>'
}
};
fetch("https://www.3bapi.com/v1/html-to-pdf", {
method: "POST",
headers: {
"Content-Type": "application/json"
},
body: JSON.stringify(requestPayload)
})
.then(response => response.json())
.then(data => {
if (data.success) {
console.log("PDF generated successfully!", data.responseObject);
} else {
console.error("Error generating PDF:", data.message);
}
})
.catch(error => console.error("Network error:", error));
This script sends a request to the HTML-to-PDF service, asking it to convert the content at https://example.com/sample.html
into an A4, landscape-oriented PDF with headers and footers. The resulting PDF, or any error messages, will be logged to the console.
Salesforce Integration
To use this service within Salesforce, you can integrate the JavaScript code into your Lightning Web Components or Visualforce Pages. Ensure that your Salesforce instance has appropriate CORS settings to make external API calls to https://www.3bapi.com
.