Guides / Generate PDFs in a Lovable app

AI app builders · Updated 2026-12-03

Generate PDFs in a Lovable app

A quick way to add a "download invoice" button is a browser-side library such as jsPDF that draws the invoice line by line. That works for a page of plain English text. It gets hard when you want a real layout, multiple pages, or customers who write in Arabic or Japanese.

This guide gives you a prompt that has Lovable build it the other way: your app sends data to DevilPDF from a backend function and gets a finished PDF back. Lovable apps use Supabase for their backend, so the function is a Supabase Edge Function.

#Before you start

  1. Sign up for DevilPDF. There is no credit card, and the free plan includes 500 documents a month.
  2. Open API keys in the dashboard and create a key. It starts with dp_live_. Keep the tab open, you will need it once.
  3. Make sure your Lovable project has its Supabase backend connected.

#The one rule: the key is a secret

The API key must never appear in your app's frontend code. Anything the browser loads, anyone can read, and they could use your key to render documents on your account.

So the key goes into the backend's secrets store, and only the edge function reads it. When Lovable asks for the DEVILPDF_API_KEY value, enter it in the secure secret field it offers, or add it yourself in your backend's secrets settings. Do not paste the key into a chat message.

#The prompt

Paste this into Lovable. Change the table and field names to match your app.

Prompt
Add a "Download PDF" button to the invoice detail page.

Build it like this:
- Create a Supabase Edge Function named invoice-pdf.
- It reads a secret named DEVILPDF_API_KEY. Ask me to add that secret. Never put the key in frontend code.
- The function receives { invoice_id }, checks that the signed-in user owns that invoice, and loads the invoice and its line items from the database.
- It sends a POST request to https://devilpdf.com/v1/render with the header "Authorization: Bearer <DEVILPDF_API_KEY>" and a JSON body:
  {
    "template": "invoice",
    "output": "file",
    "data": {
      "number": invoice number,
      "issue_date": invoice date as YYYY-MM-DD,
      "currency": invoice currency code, e.g. "USD",
      "company": { "name": my business name, "address": my business address },
      "customer": { "name": customer name, "address": customer address },
      "items": [ { "name": line description, "quantity": number, "unit_price": number } ],
      "tax_rate": tax percentage as a number,
      "tax_label": "Tax"
    }
  }
- Do not send totals. The template calculates subtotal, tax and total from items.
- The response body is the PDF file. If the response is not OK, it is JSON with error.message: show that message to the user.
- Upload the PDF to a private Supabase Storage bucket named invoices at the path <user id>/<invoice number>.pdf, create a signed URL valid for one hour, and return { url }.
- The button calls the function with supabase.functions.invoke and opens the returned url in a new tab.

Lovable writes the code, asks for the secret, and deploys the function. Test the button with an invoice that has a few line items.

#What the function should look like

If something does not work, compare what Lovable built with this reference. It is the same function as in our Supabase Edge Function guide, trimmed to the render call:

supabase/functions/invoice-pdf/index.ts
const res = await fetch("https://devilpdf.com/v1/render", {
  method: "POST",
  headers: {
    Authorization: `Bearer ${Deno.env.get("DEVILPDF_API_KEY")}`,
    "Content-Type": "application/json",
  },
  body: JSON.stringify({
    template: "invoice",
    output: "file",
    data: {
      number: invoice.number,
      issue_date: invoice.issued_on,
      currency: invoice.currency,
      customer: { name: invoice.customer_name },
      items: lines.map((l) => ({ name: l.name, quantity: l.quantity, unit_price: l.unit_price })),
    },
  }),
});
if (!res.ok) throw new Error((await res.json()).error.message);
const pdf = await res.arrayBuffer();

Things to check:

  • Deno.env.get("DEVILPDF_API_KEY") is the only place the key is read. Search the frontend code for dp_live_ and make sure there are no hits.
  • The Authorization value is Bearer, a space, then the key.
  • Numbers are numbers (3, not "3"). Text that looks like a number still works, but numbers are cleaner.
  • The function checks that the invoice belongs to the signed-in user before rendering it.

#Other documents

Swap "template": "invoice" for another starter and adjust data:

TemplateGood for
receiptOrder confirmations, payment receipts
quoteSales quotes with terms and a signature block
statementAccount statements with transactions and balances
reportMulti-page reports with summary figures and tables
packing-slipShipping documents with ordered and shipped quantities
certificateCourse completion and award certificates

Each starter's accepted fields are in its sample_data: open https://devilpdf.com/v1/templates/receipt with your key, or ask Lovable to fetch it from the edge function. To use your own design, describe it in plain English in the DevilPDF dashboard or write it in HTML, then use its tpl_... id. See templates.

#Every language

Add one line to the prompt:

Prompt
Also send "locale" in the render request, set to the customer's language code (for example "ar", "fr" or "ja").

The starter's labels, dates and money then follow the language, and Arabic, Hebrew, Persian and Urdu documents mirror right to left. Fonts for every script are built in, so nothing else changes in your app. See languages.

#Next steps