Create PDF file from external API in Xano

Options
Tim McIntosh
Tim McIntosh Member
edited December 2022 in ? Working with APIs

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. :)

Tagged:

Comments

  • Tim McIntosh
    Options

    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.

  • Ray Deck
    Ray Deck Trusted Xano Expert ✭✭✭
    Options

    What's the API you are calling? maybe understanding the inputs will help us make progress.

  • Tim McIntosh
    Options

    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.

  • Ray Deck
    Ray Deck Trusted Xano Expert ✭✭✭
    Options

    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

  • Tim McIntosh
    Options

    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.

  • Ray Deck
    Ray Deck Trusted Xano Expert ✭✭✭
    Options

    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!

  • pete letkeman
    Options

    @Tim McIntosh

    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.

  • Tim McIntosh
    Options

    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.

  • pete letkeman
    Options

    You may find this helpfulhttps://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.

  • Michael Udinski
    Michael Udinski Administrator

    ADMIN

    Options

    @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.

  • Tim McIntosh
    Options

    Thank you @Michael Udinski & @Ray Deck for the help!!

  • FJP H
    FJP H Member
    edited October 2023
    Options