File Management

Converting HTML to PDF using Lambda Functions

If you've ever needed to turn structured HTML data into a downloadable PDF directly from your Xano backend, this guide walks you through exactly how to do it. By leveraging Xano's Lambda functions and the Deno environment, you can import the necessary packages, process your HTML, and save the resulting PDF as an attachment — all without leaving your workspace.

Setting Up Your Data

To get started, you'll need HTML content stored somewhere accessible in your function stack — either pulled from a database record or defined as a value directly. In this example, the HTML is stored in a database column formatted as HTML display type, containing a simple H1 heading and a paragraph. You'll use a Get Record step to retrieve that data and store it in a variable (for example, data1) to pass into your Lambda function.

Running the Lambda Function

Once your data is ready, you pass it into a Lambda function that handles the heavy lifting. Inside the Lambda, your HTML content variable is defined and processed using packages available in the Deno environment to render and export it as a PDF. The Lambda returns the result as a Base64-encoded string.

You can use Xano's Lambda Assistant to build or refine this code using natural language prompts — which is especially helpful if you want to customize page margins, layout sizes, or other formatting options.

Adding the Base64 Prefix

Xano needs a specific prefix to understand how to handle the Base64 data. After your Lambda runs, you'll use the concat filter to prepend data:application/pdf;base64, to the returned value. This tells Xano that the data is a PDF encoded in Base64, and it ensures the file is processed correctly in the next steps.

Creating the File Resource and Attachment

With your properly prefixed Base64 string in hand, the next step is to use the Create File Resource step. Give your file a name (like pdf.pdf) and pass in the Base64 variable as the file data. After that, use the Create Attachment Metadata step to generate the metadata Xano needs to store and reference the file — optionally adding a file name here as well.

Saving the PDF to Your Record

Finally, use an Edit Record step to save the attachment output to your PDF column (which should be set to the Attachment type in your database). Return the updated record at the end of your function stack so you can confirm everything was saved correctly.

When you run the function, Xano will process the HTML, generate the PDF, and return a URL you can open directly in the browser to see your rendered document.

Customizing Your Output

If you need different page sizes, orientations, or margin settings, head back into your Lambda function and use the Lambda Assistant with a natural language prompt to adjust the configuration. The snippet linked in the video description is a great starting point you can import directly into your workspace and build from there.

Sign up for Xano

Join 100,000+ people already building with Xano.
Start today and scale to millions.