DEV Community

Cover image for Deploy a PDF Generation Service In Minutes With Appwrite Functions
Luke B. Silver for Appwrite

Posted on • Updated on

Deploy a PDF Generation Service In Minutes With Appwrite Functions

As a part of Appwrite 1.4, one of the new features we have introduced to Appwrite Functions is Function Templates. Function templates are pre-built Appwrite Functions that can be integrated into your Appwrite project with just a few clicks. Using them, you can easily incorporate new features and integrations into your app without writing additional code or managing infrastructure.

Generating PDFs is a requirement of many applications. For example: creating invoices, generating reports, or any other form of documentation. Yet, it can be difficult to build a robust service in a short amount of time. In this blog, we’ll learn to build a robust PDF generator build with an Appwrite Functions template. The template benefits from the built-in scalability, reliability, and security of Appwrite Functions.

Setting up the Template

The template generates an invoice using fake order data - it includes details such as an order ID, date, customer's name, list of purchased items, and the total cost. It should get you up and running, but you will need to add real data to build a useful invoice.

If you want to see the source code, you can find it on our templates GitHub repository.

Now, let’s navigate to our functions page on Appwrite. From there, we will select the Templates tab, search for and select the Generate PDF function template.

Image of templates on the Appwrite console

The function doesn’t have any required environment variables, so you can proceed straight to the Connect step.

Appwrite Console Template Setup Wizard Connect Step

Select Create a new repository (this will generate a GitHub repository for you with the function), and leave the production branch and root settings as default to create this function.

Using the Function

Visit the Domains tab on the function page and copy the domain URL to test the function.

Appwrite Console Function Domains page

Go to the function URL in your web browser, and you'll see a PDF invoice like the one shown below.

Random generated PDF invoice
You can refresh the page in your browser to create a new invoice with new and random information.

If you’d like to let people download the invoice from your web app, you can do it using plain HTML. Use the <a> tag along with the download attribute:

<a href="[YOUR_FUNCTION_URL]" download>
  Download My Invoice
</a>
Enter fullscreen mode Exit fullscreen mode

📱 On Flutter you can use the download, or open_filex package to achieve a similar result.

The next step is extend the template to show real invoice data. Here’s a high-level overview of what’s required:

  1. Change the function to take the order ID as a query parameter in the function.
  2. Get the order document from your Appwrite database, or external data source. Unsure about this? View code recipes on the Appwrite Function Docs.
  3. If necessary, use the order document to ensure the user has permission to see the order. For example, compare the x-appwrite-user-id header with the document user ID.
  4. Change the function to populate the PDF template with the real order document.

Next Steps

We’ve covered the basics, and now it’s your time to shine! With a few changes, you can extend this template to fit your application. Be sure to check out the other available Function Templates. We’ve created many that could be of use in your projects. You can find the templates GitHub repository here.

For more information about Appwrite and Appwrite Functions:

  1. Appwrite Function Docs: These documents provide more information on how to use Appwrite Functions.
  2. Functions Announcement Read the full announcement on Functions 1.4.
  3. Appwrite Discord: Connect with other developers and the Appwrite team for discussion, questions, and collaboration.

Top comments (0)