Back to Blog

How to Convert Webpages to PDF with an API: Invoices, Reports & More

Generating PDFs from HTML is one of the most common automation needs in business applications. Instead of wrestling with PDF libraries and their rendering quirks, you can use a screenshot API with PDF output to convert any HTML page into a clean, printable document.

Why HTML-to-PDF via API?

Traditional PDF generation approaches have serious drawbacks:

A screenshot API with PDF format support gives you:

Example: Generating an Invoice PDF

Step 1: Build Your Invoice as HTML

Design your invoice using standard HTML and CSS. Use print-friendly styles:

<style>
  @page { size: A4; margin: 20mm; }
  body { font-family: 'Inter', sans-serif; color: #1e293b; }
  .invoice-header { display: flex; justify-content: space-between; margin-bottom: 40px; }
  .line-items { width: 100%; border-collapse: collapse; }
  .line-items th, .line-items td { padding: 10px; border-bottom: 1px solid #e2e8f0; }
</style>

Step 2: Host the Invoice Template

app.get('/invoice/:id', async (req, res) => {
  const invoice = await db.getInvoice(req.params.id);
  res.send(renderInvoiceHtml(invoice));
});

Step 3: Generate the PDF

const pdfResponse = await fetch(
  `https://pxshot.dev/api/capture?url=${encodeURIComponent(invoiceUrl)}&format=pdf&key=YOUR_KEY`
);
const pdfBuffer = await pdfResponse.arrayBuffer();

Other Use Cases

Tips for Better PDF Output

PxShot supports PDF output with a single parameter change. Read the docs or try it free.

Ready to try PxShot?

Capture any webpage as an image with a single API call. Free tier included.

Get Started Free