Converting image to pdf

I’m trying to convert an image from the fileloader to a pdf.

Pseudo code:
1. when file added using fileloader upload to googledrive folder
2. get url of file
3. from url open this as an image in PIL.Image (use of response library and io.BytesIO)
4. save PIL.Image as pdf
5. upload pdf to google drive

I’m not sure how to get from steps 4 to 5. I don’t know where the saved PIL.Image PDF is in order to then upload it to google drive.
normally this would be a file within the current directory so I’m not sure how to access it.

Thanks for the help

You can write to /tmp just as you would write to your own hard drive. You have your own private space on the Anvil servers. The file might not survive server calls (I’m not 100% sure when the /tmp is purged).

Thanks, that seems to work.

Now I’m struggling to upload my pdf to google drive. A pdf doesn’t seem to be supported by the Google Drive Service?

My aim is to get a url for a pdf and this seemed the easiest way. Is there another way to do this?

I don’t use the google drive stuff, sorry.

If you just want a URL for the PDF, you can put it into a media object on a data table, and use the get_url () method on the media column.

Thanks for the help. This make sense. What I’m struggling with now is how to take the pdf from /tmp and put it into a media object?

You can read the contents of the file into a binary string, then create a BlobMedia with it. Then you can pass that media object into add_row() on a database table which has a Media column.

BTW, you won’t want to call get_url() on a Media object from a table (that will get you a temporary URL that’s only valid from this browser session, which is no good if you want to pass it to an API). Instead, create an HTTP Endpoint in your Anvil app, and have that return the appropriate Media when you call it. (You can use parameters to request different files from the same endpoint.)