Download/read PDF files from tables

Hi,
Looking for some advice here:

I upload a pdf file on the client side to a table row, then I call a server function that reads the PDF file and I send the row object to that function but I cannot make it work. I am not able to read the PDF file on the server code because I cannot access it. When I try the server code locally, pulling a pfd file locally, it works. Any advice/examples?

Try this, you get the view and the download button all in one:

https://anvil.works/forum/t/embeddable-pdfs-the-easy-way/13590?u=james.patrick

Otherwise just pass the media to your download (lots in the forum on that),

1 Like

thanks for your reply!
the issue comes when I try to get camelot or PyPDF2 reading from the link I get. How I get the link?

Client side:
getting the PDF file from the user, uploading to table and sending the row component to the server code that does the rest of the job

server:

frow[“file”].url → this is basically the link to the file on the table

I get:
https://xvp7mwnakjh6zuau.anvil.app/debug/ME4MQQZADMIMUC3ILNZD5CQS45D2XNFT%3DRYKJ44KUKPFELWETGQKXRAXB/_/lm/table-media/6e26dac0f92a9b0db8428a0127be3fa2/78596519/cot5162%20-%20Paola%20Rodriguez.pdf?s=

which works when clicking the link, I can download the file. When I try to use camelot or PyPDF2, I get all kinds of errors related to the link.

it definitely works in another file I have as a sandbox:

‘’'python
anvil.server.connect(“sjhgjlkhg”)

frow = app_tables.orders.get(recent = “recent”)
print(frow)
#print(frow[“client”])
print(frow[“file”].url)
filePDF = frow[“file”].url
#frow[“recent”] = “”

response = requests.get(filePDF)
raw_data = response.content
with BytesIO(raw_data) as data:
readPDF = PdfFileReader(data)
pag = readPDF.getNumPages()
print(pag)
‘’’
I try the same in my server code and it does not work
‘’‘python
@anvil.server.callable
def addQuote(file):
frow = app_tables.orders.get(recent = “recent”)
print(frow)
#print(frow[“client”])
print(frow[“file”].url)
filePDF = frow[“file”].url
‘’’

You may be running into the fact that the media url is temporary for the current session, but the requests request isn’t passing the right session information.

In any case, there’s no reason to use requests to fetch data through the media url. Each media object has a get_bytes() function that will give you a byte string of the media contents. You should be able to pass that to your other libraries.

1 Like