Generate a hosted PDF

This documentation provides an example of how to use the FileforgeClient to generate a hosted PDF file from HTML.

Prerequisites

Ensure you have the following:

  • An API key for Fileforge as an environment variable: process.env.FILEFORGE_API_KEY
  • Node.js and npm installed.
  • The Fileforge Client installed.

Guide

1

Write your HTML content

1// HTML content with an embedded image
2const HTML = `<!DOCTYPE html>
3<html>
4 <head>
5 <title>My First Document</title>
6 </head>
7 <body>
8 <h1>Hello World!</h1>
9 </body>
10</html>
11`
2

Set the host property to true and specify the hosting duration

By setting the options.host property to true, the Fileforge API will now return a url to your file. You can host your document with Fileforge for up to 7 days. You can set duration in the options.expiresAt property as a date object.

3

Generate the PDF

The client returns a PDF object with the URL to the hosted PDF file as pdf.url.

1import { FileforgeClient } from "@fileforge/client";
2
3const ff = new FileforgeClient({
4 apiKey: process.env.FILEFORGE_API_KEY,
5});
6
7(async () => {
8 try {
9 const pdf = await ff.pdf.generate(
10 [
11 new File([HTML],"index.html",{
12 type: "text/html",
13 },
14 ),
15 ],
16 {
17 options: {
18 host: true,
19 expiresAt: new Date(Date.now() + 24 * 60 * 60 * 1000),
20 },
21 },
22 {
23 timeoutInSeconds: 30,
24 },
25 );
26 console.log(pdf.url);
27 } catch (error) {
28 console.error("Error during PDF conversion:", error);
29 }
30})();