Create PDF file from external API in Xano
Hi all,
I am working with an external API that responds with pdf data. I am encoding this pdf data using the base64_encode filter. The encoded data saves fine to a database column. However, what I am having trouble with is generating the PDF file in Xano. I have tried to use the create file resource function using a base64_decode filter but the file doesn't get created. I have also tried to create a HTTP custom header to download the file but it downloads an empty file. It seems to be saving the file data as the file name. I'm assuming this decode filter just decodes it as text?
I believe the base64 encoded data is working as intended because when I use a base64 to PDF converter, the file can be generated successfully. I have tested it through this website here: https://base64.guru/converter/decode/pdf
It seems as if I am doing something wrong in the create file resource function or Xano does not have the ability to decode to PDF. Is someone able to provide me with some help? My intention is to create the PDF file in Xano, store it in the files and have a database column link to it.
Thank you. :)
Comments
-
I'm assuming what I am trying to do above is just decode the PDF file currently to text. I have read a couple of stack overflow posts and it seems there are different types of decoders. I'm assuming Xano does not support PDF, PNG, or JPG decoders? Please let me know if my assume is wrong.
I am encoding the API response result (file data), which I'm not entirely sure I need to do. Is there a way to not encode and create the file some other way?
Thank you.
-
What's the API you are calling? maybe understanding the inputs will help us make progress.
-
Hi @Ray Deck
Thank you for responding to me again. I'm consuming this API by neutrinoapi - HTML render - https://www.neutrinoapi.com/api/html-render/. It essentially renders HTML to a PDF, PNG or JPG. I just want to be able to save the file to Xano.
I just tested using this in postman and postman is able to save the response. I'm trying to get Xano to do the samething but save to the file resource and DB.
-
You can save the downloaded body as a file resource directly - assuming it downloaded correctly. The body is (usually) in api_1.response.result. Then make a file resource. Then you can make it into a kind of attachment for the purpose of saving metadata in the table. But actually once it is a file resource it's saved and available both for download and in your file explorer. (Note that all files are downloadable by any client - Xano does not gate that access.)
This is a multipart process, and dealing with files is one of the topics we cover in our State Change Pro office hours. If you don't crack it over the weekend, here's a ticket to join us as a guest to help with this issue and see if you want to join. https://tickets.statechange.ai/ticket/85ca07e4-da57-48aa-b10c-7d89df9880d0
-
Hi @Ray Deck
I definitely feel like I have done a similar process but obviously haven't done it correctly.
Created a function to interact with the neutrino API. I encode the PDF response data into a database table. See screenshot below.
Next I use another function to decode the data from the database table and attempt to create the file resource. See screen shots below.
The result is no file is created.
I'll have a look at your state file change. I have a session booked with Xano on Wednesday but happy to also jump into your session to if I don't get anything out of the Xano meeting. Thank you for your help Ray, appreciate it.
-
Based on this screenshot, I'd recommend a stop and debug to inspect what you are getting from each of the order_receipt and pdf.
I also note that the api_response field looks for JSON instead of a string. Putting a string in here might work, but its a bit hacky and could generate unexpected results.
One alternative path is to save a file on Xano when you first download from the API endpoint and save the path to the DB (either using file metadata or saving the filename itself for later reconstruction as a URL.
Let us know if you get a better solution on your own or from the Xano team!
-
I ran into this an issue like this a week or two ago and then I used this base64 decode method instead of the one you have in your function and things started working.
It's worth giving it a try.
-
Thanks for the feedback @Petar Trajanovski
I have tried using "urlsafe" as well as the one without with no luck. When I grab the base 64 encoded response from the API and run it through the decoder on this website: https://base64.guru/converter/decode/pdf
It creates the PDF fine. I'm assuming I am doing something wrong when I create the file resource.
-
You may find this helpful
https://docs.xano.com/working-with-data/functions/utility-functions#stop-and-debug
After step 2 you can write the data in the PDF variable to see what exactly you have.
If you do get something that is base64 encoded then you can copy and paste that into the base64 converter site to see what you get and if this work then you are probably correct about the file resource logic that you are using.
If you don't get a base64 encoded response then the issue is somewhere else.
-
@Tim McIntosh came to office hours for this question. We found that he needed to use Create Attachment from File to create the file once we had the output from the Create File Resource variable.
-
Thank you @Michael Udinski & @Ray Deck for the help!!
-
Categories
- All Categories
- 53 ? Announcements
- 47 ? Releases
- 37 ? Welcome
- 983 ? Help! I'm a Noob
- 125 ? No-Code Front-Ends
- 633 ? Working with APIs
- 439 ? Transforming data
- 126 ? Connect Xano to ...
- 50 ?? Find an Expert
- 348 ❓Other questions
- 35 ? Security
- 22 ✂️ Snippets
- 19 ? Showcase
- 7 ?️ Xano Chatter
- 62 ? Video Tutorials
- 171 ? Request a feature
- 229 ? Report a Bug
- 19 ? Templates & Extensions
- 7 ? Feedback