Generating PDFs from HTML with SecretPDF

NicolasBrondinBernard

Author
@NicolasBrondinBernard

Learn how to generate PDFs from HTML with a single API call!

Article published on 17/02/2026, last updated on 10/08/2026

Generating a PDF in 2026 is supposed to no longer be a technical problem. But doing it simply, cleanly, in a maintainable, scalable and production-ready way… now that's a different story.

In this article, we'll see how to generate PDFs from an HTML template in the simplest way possible with SecretPDF.

Why generating PDFs is often complex

The PDF format is both simple and complex. It's simple to use, since it's portable (it includes all the resources needed for rendering), but it's complex to produce.

A PDF can contain text, fonts, images, vector graphics, etc…

It's possible to generate a PDF file directly from the browser, but as soon as you need a slightly complex rendering (in other words, "nice-looking"), it becomes a headache, especially on mobile.

If your goal is to:

  • Generate an actual document
  • With an interesting design
  • Selectable text content (not just an image)
  • Without obscure CSS hacks

Then you need to generate PDFs server-side, which means finding the solution that fits your constraints, setting up an architecture, sizing it, and making sure it doesn't impact the performance of your other applications.

That's where SecretPDF comes in!

What is SecretPDF?

SecretPDF is an easy-to-use SaaS, with a modern API for generating PDF documents from HTML.

You send it:

  • HTML/CSS (even Tailwind classes for the design if you want)
  • dynamic data
  • a page format

It returns to you:

  • a ready-to-download PDF

And on top of that, the tool is GDPR-compliant and carbon-neutral. What more could you ask for?

Pricing

SecretPDF works with a credit system (1 pdf = 1 credit for PDFs <2MB)

You get:

  • 10 free credits every month
  • Then $0.01 per credit

It's ideal for startups and SaaS companies: No document generated, no payment.

In fact, you're not even required to enter your credit card to get started.

Also worth noting, you can generate as many test documents as you want, thanks to a "sandbox" option that lets you test your templates for free!

Step 1: Create an account

To create an account, simply connect your Github account, in a single click!

You'll then be guided towards creating your first template and your first API key. One of the big advantages of SecretPDF is that it lets you create your templates directly from the interface (with a preview) or from the API:

Step 2: Install the client

You can use the API with any language or framework by following the official documentation, but you can also use the dedicated NodeJS client to make your life easier.

In your Node.js project run the following command:

npm install @secretpdf/sdk

Then in your code, initialize it:

import SecretPDF from "@secretpdf/sdk";

const client = new SecretPDF({
  apiKey: "<secret>",
});

It is obviously recommended to define your API key in your environment variables

Step 3 (optional): Create a template from the API

If you'd rather create your first template directly from the API, that's possible too, like this:

const response = await client.createTemplate({
  name: "Facture",
  size: "A4",
  content: `
    <main class="p-10">
      <h1 class="text-3xl font-bold text-blue-600">
        Facture
      </h1>
      <p>Prestataire : {{name}} (SIRET {{siret}})</p>
      <p>Montant: {{amount}}€</p>
      <p class="mt-4 text-gray-700">
        Merci pour votre confiance.
      </p>
    </main>
  `,
});

const templateId = response.data.id;

That said, I'd still recommend using the interface, it will make your life easier.

Step 4: Generate your first PDF

Here is the simplest possible example to generate a PDF from your template:

const response = await client.generate({
  sandbox: true,
  templateId: "<template_id>",
  data: {
    name: "Nicolas Brondin-Bernard",
    siret: "784 671 695 00103",
    amount: 139.99,
    date: new Date()
  }
});

sandbox lets you generate a test document with a watermark on top, which won't consume any credits.

The response contains a simple data property with the content of the generated file (Base64-encoded), it's as simple as that!

Step 5 (optional): Save the file to disk

Nothing forces you to store the generated file, you could for instance send it back as an HTTP response to your user, but if you want to do it, nothing could be simpler:

import fs from "fs";

const response = await client.generate({
  //...
});

const pdfBuffer = Buffer.from(result.content, 'base64');
fs.writeFileSync('output.pdf', pdfBuffer);

And that's it, job done!


Finished reading this article?
Our complete courses
Take it to the next level with our courses!

Complete courses, exercises and certificates to really learn programming!

4.8 average rating

Comments (0)

to leave a comment

No comments yet

Frequently asked questions covered in this article

HTML to PDF Generation Generate a PDF in NodeJS