Guides / Generate PDFs in a Bolt app

AI app builders · Updated 2026-12-03

Generate PDFs in a Bolt app

Bolt builds a working app from a prompt, and a "Download PDF" button is a common next request. This guide gives you a prompt that has Bolt generate PDFs on the server with DevilPDF: your app sends data, DevilPDF returns a finished invoice, receipt or certificate. You get a real document layout, page breaks and every language, without a PDF library in the browser.

#Before you start

  1. Sign up for DevilPDF. No credit card. The free plan includes 500 documents a month.
  2. Create a key under API keys in the dashboard. It starts with dp_live_.
  3. Know where your Bolt app's server code runs. Bolt can connect a Supabase backend and deploy to Netlify, and the prompt below covers both. If your app is a Next.js project, see Generate PDFs in Next.js instead.

#Keep the key out of the browser

If your Bolt app has a Vite frontend, every variable starting with VITE_ is compiled into the JavaScript that ships to visitors. If the DevilPDF key ends up there, anyone can copy it.

So the key lives only on the server:

  • Supabase: an Edge Function secret, set in the Supabase dashboard under Edge Functions secrets, or with supabase secrets set DEVILPDF_API_KEY=....
  • Netlify: an environment variable in the site's settings. Without the VITE_ prefix, it is only visible to functions.

Never paste the key into the chat with Bolt or into a source file.

#The prompt

Paste this into Bolt and adjust the names to your app.

Prompt
Add a "Download PDF" button to each order on the orders page.

Generate the PDF on the server, never in the browser:
- If this app uses Supabase, create a Supabase Edge Function named order-pdf. Otherwise create a Netlify Function at netlify/functions/order-pdf.mts served at /api/order-pdf.
- The function reads the secret DEVILPDF_API_KEY from the environment. Do not create any VITE_ variable for it and do not put it in frontend code.
- It receives { order_id }, checks that the signed-in user owns the order, and loads the order and its items.
- It sends POST https://devilpdf.com/v1/render with header "Authorization: Bearer <DEVILPDF_API_KEY>", header "Content-Type: application/json", and body:
  {
    "template": "receipt",
    "output": "file",
    "data": {
      "number": order number,
      "paid_at": order date as YYYY-MM-DD,
      "currency": currency code like "USD",
      "company": { "name": my store name },
      "customer": { "name": customer name, "email": customer email },
      "payment": { "method": card brand, "last4": last four digits },
      "items": [ { "name": product name, "quantity": number, "unit_price": number } ]
    }
  }
- Do not calculate totals; the template does it from items.
- The response body is the PDF. Return it with Content-Type application/pdf. If DevilPDF answers with an error status, the body is JSON with error.message: return that message with status 502.
- The button calls the function and opens or downloads the PDF.

#Netlify Function reference

If Bolt chose Netlify, the function should look close to this:

netlify/functions/order-pdf.mts
import type { Config } from "@netlify/functions";

export default async (req: Request) => {
  const { order_id } = await req.json();
  const order = await loadOrderForUser(req, order_id); // your auth + database lookup
  if (!order) return new Response("Not found", { status: 404 });

  const res = await fetch("https://devilpdf.com/v1/render", {
    method: "POST",
    headers: {
      Authorization: `Bearer ${process.env.DEVILPDF_API_KEY}`,
      "Content-Type": "application/json",
    },
    body: JSON.stringify({
      template: "receipt",
      output: "file",
      data: {
        number: order.number,
        paid_at: order.paid_at,
        currency: order.currency,
        company: { name: "Lumen Cycle Works" },
        customer: { name: order.customer_name, email: order.customer_email },
        items: order.items,
      },
    }),
  });

  if (!res.ok) {
    const { error } = await res.json();
    return Response.json({ error: error.message }, { status: 502 });
  }

  return new Response(await res.arrayBuffer(), {
    headers: {
      "Content-Type": "application/pdf",
      "Content-Disposition": `attachment; filename="receipt-${order.number}.pdf"`,
    },
  });
};

export const config: Config = { path: "/api/order-pdf" };

loadOrderForUser stands for your own code: verify the user's session and return the order only if it is theirs.

For the Supabase version, compare with the full function in Generate PDFs from a Supabase Edge Function. It also saves the PDF to Supabase Storage and returns a signed link.

#The button

Because the function returns the file itself, the frontend downloads it as a blob:

async function downloadReceipt(orderId: string) {
  const res = await fetch("/api/order-pdf", {
    method: "POST",
    headers: { "Content-Type": "application/json" },
    body: JSON.stringify({ order_id: orderId }),
  });
  if (!res.ok) throw new Error((await res.json()).error);
  const url = URL.createObjectURL(await res.blob());
  window.open(url, "_blank");
}

#Check what Bolt built

  • Search the project for dp_live_ and for VITE_DEVILPDF. Neither should exist.
  • The Authorization header is Bearer, a space, then the key.
  • The function refuses orders that belong to someone else.
  • Try an order with several items and a long product name.

#Other templates and languages

Change "template" to invoice, quote, statement, report, packing-slip or certificate. Each one lists the fields it accepts in its sample_data, from GET https://devilpdf.com/v1/templates/<id>. For your own design, create a template in the DevilPDF dashboard by describing it in plain English or writing HTML, and use its tpl_... id.

To render in the customer's language, ask Bolt to add "locale" to the request body, for example "es", "ar" or "ja". Labels, dates and money follow the language, and right-to-left languages mirror the page. See languages.

#Next steps