Anvil Logo
Products
Industries
Resources
Developers
Engineering

Generate an invoice PDF with Anvil

Ben Ogle
By Ben Ogle
Updated February 05, 2021

Quickly generate a custom invoice PDF with our PDF generation API endpoint.

Back to all articles
Post hero image

Invoices are an essential part of running any business, but they can be a chore to create invoice PDFs programmatically; running headless chrome is painful, relying on the user's browser is a unreliable, and cobbling together several libraries is time consuming and brittle.

We've created a PDF generation API endpoint to ease this pain. It allows you to create new PDFs from a simple JSON payload.

This guide will walk through how to create a PDF invoice using our PDF generation API endpoint.

Overview

Our goal is to generate an example invoice that looks like the following:

PDF invoice example

We will create a JSON payload step-by-step, then we'll make a POST request to a single REST endpoint: https://app.useanvil.com/api/v1/generate-pdf.

This tutorial will cover the following concepts to help you quickly get familiar with the Anvil PDF generation endpoint:

  • Building a data payload to generate a dynamic PDF
  • Customizing the PDF with your logo
  • Adding variable length tables to your PDF

Get your API key

First, you'll need to get your API key by signing up for an Anvil account.

Once logged in, you will be directed to copy the API key from your organization’s API settings page. For more information see the getting started guide.

Step 1: Build the data payload

The body of the PDF is specified by an array of JSON objects. In the final generated PDF, each object in the array is rendered as its own section, visually separated from other sections by a small margin.

Each object can have a label, content, a heading, and / or a table key depending on the information you want to display.

Next, we need to set the title of the PDF and include a logo. We'll set the title and logo properties at the root of the JSON payload, then include the data we created in the previous section at the data key.

Step 3: Create the PDF

Finally, it's time to generate the PDF. We'll use the https://app.useanvil.com/api/v1/generate-pdf REST endpoint and POST the complete JSON payload we assembled over the last 2 sections. The POST request's response will be the brand new invoice PDF file!

Below are examples of the generate request using our Anvil node library, Postman, and curl.

Option 1. Generating the PDF with the node-anvil library:

Option 2. Our Postman collection has a Generate PDF example you can use to generate this invoice. Just paste the generatePayload into the body:

create an invoice in postman

Option 3. Generating the invoice PDF with curl:

That's it!

Summary

Now you have an understanding of the concepts needed to generate dynamic PDFs. The most important part is constructing the payload with data from your system, and then making a single request to Anvil’s PDF generation endpoint.

This is a more reliable, scalable, and consistent way to generate PDFs in your application than using headless chrome, relying on the user’s browser, or managing your own PDF code library. Anvil is here to make PDFs easy, so you can focus on building your application.

Additional resources

Having trouble? Contact us at support@useanvil.com.

Subscribe to the Anvil blog

Get new stories like this delivered directly to your inbox.

Loading...

Get a demo
(from a real person)

Schedule some time on our calendar to talk through your specific use case and see which Anvil products can help.
    Want to try Anvil first?
    Want to try Anvil first?