Automating document creation is a crucial feature for many SaaS products, whether to generate invoices, reports, or certificates. Using an HTML renderer, developers can convert dynamic HTML content into high-quality PDF documents or PNG images with just a few lines of code. In this guide, we will show you how to integrate ExoAPI's document creation capabilities into your SaaS product, saving time and effort.
Automating document creation allows businesses to:
Here are a few practical examples where document automation capabilities can simplify your SaaS product workflows:
Let's walk through how to set up automatic document generation using ExoAPI's HTML Renderer.
Visit ExoAPI and sign up for an account to obtain your API key.
npm install @flower-digital/exoapi-sdk
In your JavaScript or Node.js application, initialize the ExoAPI with your API key:
import { ExoAPI } from "@flower-digital/exoapi-sdk";
const exoapi = new ExoAPI({ apiKey: YOUR_API_KEY });
Now, let's look at a simple example of generating a PDF from HTML content. The following code snippet shows how to create a PDF document with basic styling:
import { ExoAPI } from "@flower-digital/exoapi-sdk";
import fs from "fs";
const exoapi = new ExoAPI({ apiKey: YOUR_API_KEY });
async function htmlRenderer() {
try {
const res = await exoapi.htmlRenderer({
html: '<p style="padding:8px;font-size:36px">Hello <b style="color: green">world!</b></p>',
format: "pdf",
});
fs.writeFileSync("document.pdf", Buffer.from(await res.arrayBuffer()));
console.log("PDF document generated successfully!");
} catch (err) {
console.error("Error generating PDF:", err);
}
}
htmlRenderer();
htmlRenderer()
function sends an HTML string to ExoAPI, which processes it and returns a PDF document.fs
module, writing the file as document.pdf
.ExoAPI offers a variety of options to customize your PDFs. You can control the page size, margins, orientation, headers, footers, and even inject custom CSS for precise styling.
Here's an advanced example showing how to customize the PDF output:
async function generateAdvancedPDF() {
try {
const res = await exoapi.htmlRenderer({
html: '<p style="font-size:36px">Hello <b>world!</b></p>',
format: "pdf",
title: "Sample Document",
pageSize: "a4",
margin: "10mm",
landscape: true,
css: `
body {
font-family: Arial, sans-serif;
font-size: 14px;
}
b {
color: blue;
}
`,
header: '<div style="background:grey; width:100%; height:30px; font-size:20px">Page <span class="pageNumber"></span> of <span class="totalPages"></span></div>',
footer: '<div style="background:grey; width:100%; height:30px; font-size:20px">Page <span class="pageNumber"></span> of <span class="totalPages"></span></div>',
});
fs.writeFileSync("advanced-document.pdf", Buffer.from(await res.arrayBuffer()));
console.log("Custom PDF document generated successfully!");
} catch (err) {
console.error("Error generating custom PDF:", err);
}
}
generateAdvancedPDF();
title
: The title of the PDF document, which will appear in the metadata.pageSize
: Defines the size of the PDF (e.g., A4, Letter, etc.). You can also set explicit dimensions using pageWidth
and pageHeight
.margin
: Sets uniform margins for the document, or use marginTop
, marginLeft
, etc., for more granular control.landscape
: Set to true
for landscape orientation or false
for portrait.css
: Custom CSS can be added to style the document's content.header
& footer
: You can add headers and footers with placeholders for pageNumber
, totalPages
, and date
.ExoAPI also supports generating PNG images from HTML content. This is useful when you need visual output, such as badges, certificates, or visual reports. Here's an example of how to generate a PNG image:
async function generatePNG() {
try {
const res = await exoapi.htmlRenderer({
html: '<p style="padding:8px;font-size:36px">Hello <b style="color: green">world!</b></p>',
format: "png",
imageWidth: 120,
imageHeight: 120,
});
fs.writeFileSync("image.png", Buffer.from(await res.arrayBuffer()));
console.log("PNG image generated successfully!");
} catch (err) {
console.error("Error generating PNG:", err);
}
}
generatePNG();
imageWidth
and imageHeight
parameters allow you to specify the size of the PNG image in pixels.imageHeight
is not provided, it will automatically adapt to fit the content.ExoAPI's HTML Renderer makes document creation in SaaS products incredibly easy and customizable. With just a few lines of code, you can automate PDF and image generation, streamlining processes such as invoicing, reporting, and creating visually appealing documents. By integrating ExoAPI into your workflow, you can enhance your application's functionality while saving time on manual document creation tasks.
Try out ExoAPI today to see how it can simplify document creation and boost productivity in your SaaS applications. Check out the official ExoAPI documentation to explore more features and customization options.