[SOLVED] PDF Media Files not previewing in browser

If I have a media file which is a PDF document, I can’t get it to open in my browser. Instead, the browser (both firefox and chrome) offer to download the file or open it with a list of suitable local apps.

I’ve set the content_type to ‘application/pdf’ and I’ve tested this both with PDFs that I’ve uploaded manually and ones that I’ve created in code.

Hunting around suggests checking some browser settings, which I’ve done and tested on other PDFs, or that the server might not be setting the content type correctly.

Anyone else got this working?

By default, if you get the url from a Media object in a data table, it’s set up for download (Content-Disposition: attachment). You can create an HTTP endpoint that serves up the file without that header (the opposite of @neriksen85’s question here).

Or, instead of the url property, you can use the (ahem, undocumented) method get_url(False), which gets the non-auto-downloading version.

9 Likes

I shall give the undocumented method a try!!

That worked perfectly. Thanks @meredydd

Hi @owen.campbell,

I have a similar issue to your original post, but in trying to use @meredydd’s tips, I can only get None as the return for my URL query. Perhaps you guys can give me some pointers as to what I’m doing wrong.

I have a data table with an image/jpeg object (For simplicity. Eventually I need PDFs) in the media column and I’m currently trying to use an Uplink’d Python interpreter to return its URL. Below is a print out of what I’ve tried:

>>>row['media'] = anvil.BlobMedia('image/jpeg', open(r"C:\Test\Test.jpg", 'rb').read(), name='TEST.jpeg')
>>>print(row['media'].get_content_type())
image/jpeg
>>>print(row['media'].url)
None
>>>print(row['media'].get_url(False))
None

I have also tried the above with application/pdf files with the same results. My ultimate goal is to be able to get the URL of a PDF so that I can put it in a link along with some arguments to allow the PDF to open to a particular page in a new tab of the browser. Let me know if you have any ideas, thanks!

Hi @bryndonlee,

BlobMedia doesn’t have a URL, because it doesn’t live on any servers! (You can get URLs for Media in data tables or Google Drive, because they can be reached at a central location, whereas a BlobMedia might only exist in the browser.)

You can, however, assign any Media object to the url property of a Link, like so:

my_media = BlobMedia('text/plain', 'Hello, world', name='hello.txt')
self.link_1.url = my_media
1 Like

So in testing that I’m doing the right thing per your advice, I stumbled on an interesting situation: I can use row['media'].url to return the URL string in the client side code but not in a server module.

In a test app, I query a row in my data table that contains a Media object (row). I have one button that calls a server function for row, and the code attempts to print the URL once on the server side and once on the client side.

Client code (in Form1):

def button_1_click (self, **event_args):
  row = anvil.server.call('get_row')
  print(row['media'].url)  # prints URL

Server code:

@anvil.server.callable
def get_row():
  row = tables.app_tables.my_table.get(key='PDF Row')
  print(row['media'].url)  # prints None
  return row

The output then prints:

None
(The huge URL for the file)

I also tested the server side with Python 3.6, 2.7, and restricted 2, and all resulted in printing None. Is this to be expected? It seems like a bit of an odd inconsistency.

2 posts were split to a new topic: Problem with presenting/downloading a PDF that is base64 encoded