Make PDFs open directly (not via download) by default

Currently, the core handling of PDFs is via a download:

allaboutcats.pdf (42.2 KB)

While this is very functional, it adds a couple of unnecessary steps for the most common use case: “I want to read this now” - especially on mobile devices.

The need to download / save something is very much secondary. Required sometimes, but usually the user just wants to read it and then move on.

The Inline PDF Previews TC addresses this by serving an inline PDF on desktop - and does this with aplomb. However, it misses these common situations:

  1. PDFs on mobile devices
  2. Multiple PDFs (due to visual clutter)
  3. Viewing PDFs in a full browser tab

Of note, there are other paths to downloading a PDF. They can be downloaded from a link directly (via right-click / context menu), or from within the browser-based PDF viewer that this TC serves.

Suggestion: open PDFs in browser by default

This little tweak solves both of these situations nicely, and simply key into the default behaviour for other links in Discourse. So with the proposed change:

  1. PDF links in mobile typically open in the same browser window
    • enables single tap viewing
  2. PDF links on desktop (i.e. above the inline PDF) open in a new tab
    • this allows one-click full tab viewing
  3. When inline behaviour is deliberately suppressed (by adding a space in the link text)
    • allows one-click viewing

I’ve wrapped these up into this PR:

https://github.com/discourse/discourse-pdf-previews/pull/47

3 Likes

Can anyone of your team maybe have a look at this PR @david ?

I left some comments about the code itself on the PR

When I click this link, it opens the PDF in the browser. No download. Are you seeing something different?

I’m using Chrome on macOS. We’re not running this d-pdf-previews theme component on Meta.

2 Likes

Here in meta.discourse.org, my link above opens in the same tab as this page (just like you describe). This is unchanged in safe-mode.

However, on three of my recently updated sites (with the TC disabled) it attempts a download. This is also the case with safe-mode. I’m not sure why that is. I can’t see any site settings that govern this at all.

Whatever the reason, my fork fixes it for my sites nicely!

This makes no sense to me!!

1 Like

I’m going to see if we can get this inconsistency fixed up in core. Will update here with progress.

2 Likes

Any joy with this thusfar?

1 Like

We do have a work-in-progress. Hopefully will be able to land it in the next couple of weeks. It’s a little tricky because there are lots of different situations to verify (local uploads, s3, s3-compatible, with cdn, without cdn, etc).

4 Likes

I just checked a PDF that I uploaded to a topic today on a recently updated site and clicking the pdf link opens the PDF url on the CDN in my browser, fwiw

2 Likes

This should be resolved since SECURITY: Download allowlist for uploaded files · discourse/discourse@9c0642a · GitHub

We now have centralised logic to determine which files should be shown ‘inline’. That means that PDFs are consistently shown inline, and some less safe file types are consistently served as downloads. These changes should work on all types of upload storage (local & S3, with or without CDNs).

5 Likes

This topic was automatically closed after 9 days. New replies are no longer allowed.

David, I’ve just tested this - and while this definitely seems fixed on desktop, it doesn’t seem to be the case for mobile.

When I click on a PDF link in mobile, I get download (which I don’t want). I want it opening in the browser directly (i.e. the same as desktop), regardless of whether the TC is installed or not.

Can you expand on your setup (S3? CDN?), and your mobile device OS?

Clicking the pdf in the OP here on my iPhone seems to open inline correctly.

1 Like

What about mp4 files? On most of websites, links to mp4 naturally plays them in the browser, but not in discourse, which is rather annoying :slight_smile:

Example: https://unicyclist.com/uploads/default/original/3X/4/f/4fb882b8ca5a0b0e3d75ff932506d57325f5582c.mp4

Expected behavior: https://d.canapin.dev/uploads/default/original/2X/4/4fb882b8ca5a0b0e3d75ff932506d57325f5582c.mp4

In theory, mp4 should be served inline:

which uses:

How did you achieve this? i.e. what’s the difference from unicyclist.com?

Sorry I should have mentioned the distinction. The latter uses a plugin: Discourse Video Inline

On meta, discourse forces mp4 downloads as well: https://d11a6trkgmumsb.cloudfront.net/original/4X/3/f/0/3f09f895d21cf0ae897d90c947abb816830b00a2.mp4

2 Likes

It has changed!!! I’ve just updated again today, and they are opening inline on mobile just fine.

Did you tweak something for us ‘simple self-hosters (no CDN/S3)’? Or was this some dumb caching thing?

No tweaks since the security fix linked above. It could’ve been caching, especially if you did the upgrade via the UI the first time. If you’ve now done a full rebuild, then that will’ve reset the NGINX cache. Glad to hear it’s working now!

I opened a new topic for the mp4 issue

3 Likes

This topic was automatically closed after 3 days. New replies are no longer allowed.