2

I am working with Python 3.6.6 and need to display several pdf-files in the browser.

I tried several pdf files and got 2 different results.

For example:

import webbrowser
webbrowser.open('https://www.adobe.com/content/dam/acom/en/devnet/pdf/pdfs/pdf_reference_archives/PDFReference.pdf')

opens a new tab in my browser and displays the file.

Other files are downloaded immediatelly, instead of displaying in a new tab in the browser.

I want to know the reason for, why some files are downloaded automatically and others are displayed in the browser, where I can manually download the file.

I need a solution where I can force the file to open in the browser.

I already read through this question but it seems it is more restricted to HTML.

One last thing I noticed. If I try to download any pdf file from the internet it opens the file in a new tab. The problem with downloading them only appears with files which are saved on a webserver within my company. Is this just a missing configuration on the webserver? If yes, is it anyhow possible to get around this configuration using python?

milanbalazs
  • 4,811
  • 4
  • 23
  • 45
adama
  • 537
  • 2
  • 10
  • 29
  • Do you have sample links that you've tried that downloads directly? – CodeRed Sep 12 '19 at 07:18
  • 3
    The headers sent by the content provider can indeed influence the way files are handled by the browser ([see this related question](https://stackoverflow.com/questions/6293893/how-do-i-force-files-to-open-in-the-browser-instead-of-downloading-pdf)) - but keep in mind that most browsers also have **user settings** that tell the browser how to handle certain file types (e.g. "always download" or "always open with this app"). So even if you find a way that always works on your browser does no mean it will work with a different browser. – Mike Scotty Sep 12 '19 at 07:21
  • @CodeRed unfortunately I can not share these links – adama Sep 12 '19 at 07:24

1 Answers1

0

It's the web-browser's decision whether to put the file in the downloads directory (and not display it) or whether to cache the file and open it in-browser. As such, do not use the web-browser installed on the end-user's system.

Instead, have your program include its own web-browser. Open the pdf (and everything else your program displays) in your program's web-browser. That way, you can always modify your program's web-browser to view files in-browser instead of saving to the downloads directory.

Several free open-source web-browsers are available on the market. Just use one of them.

Toothpick Anemone
  • 4,290
  • 2
  • 20
  • 42