Deploy a PDF Generation Service In Minutes With Appwrite Functions
Tags: webdev, programming
Published: 2023-09-07 11:38:41

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](https://github.com/appwrite/templates/tree/main/node/generate-pdf). 

Now, let’s navigate to our functions page on **[Appwrite](https://appwrite.io)**. From there, we will select the **Templates** tab, search for and select the **Generate PDF** function template.



![Image of templates on the Appwrite console](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/l2v5yn7ysrav0vm45r0s.png)

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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/h0xa5oxaebxl0mpvhttk.png)



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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/6lz0r80aesiln8zk3rkf.png)

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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/go8676zhyuxghudf1hjl.png)
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:

```html
<a href="[YOUR_FUNCTION_URL]" download>
  Download My Invoice
</a>
```

<aside>
📱 On Flutter you can use the [download](https://pub.dev/packages/download), or [open_filex](https://pub.dev/packages/open_filex/) package to achieve a similar result.

</aside>

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](https://appwrite.io/docs/functions).**
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](https://github.com/appwrite/templates).

For more information about Appwrite and Appwrite Functions:

1. **[Appwrite Function Docs](https://appwrite.io/docs/functions)**: These documents provide more information on how to use Appwrite Functions.
2. **[Functions Announcement](https://dev.to/appwrite/serverless-your-way-unleashing-appwrite-functions-true-potential-2l4f)** Read the full announcement on Functions 1.4.
3. **[Appwrite Discord](https://discord.com/invite/appwrite)**: Connect with other developers and the Appwrite team for discussion, questions, and collaboration.