OSTips – How to Display a PDF in Browser with Drupal 9 & 8
We’ve been posting blogs about Drupal for over 10 years at OSTraining, and so every once in a while someone finds an old blog post from Drupal 6 or even Drupal 7 and asks a question. Well that’s exactly what happened this week when somebody asked, “How do we display a pdf in the browser with Drupal 9 when someone clicks on a link?”
Keep reading to learn how!
“Hi! This is OSTips from OSTraining. My name is rod martin, and in this video, I’m going to answer that question.
Actually, it’s a really simple answer with Drupal 9. So a few years ago this was a big issue. Before the Media Manager came along, we were left with modules like PDF Reader (which now really isn’t being maintained) or Embedded Google Docs Viewer which is still only in Drupal 8 and hasn’t been updated since 2018. Now we have the Media Module that allows us to embed pdfs right on our site!
- Click Structure/Content types/Testing/Manage fields
I have just a simple PDF Demo field which is a “media field”.
NOTE: You will need to have installed that over here under the extend menu (see below) – nothing fancy.
- Return to Manage Fields tab
- Click Edit button
- Select Document under Media type
- Click Manage display tab
NOTE: The format is Rendered entity and it is Rendered as Default. That’s important!
- Click Content tab
I have a node in this content type.
- Click Edit tab
- Click Add media button
I have one pdf uploaded.
- Click Insert selected button
- Click Save button
Now when I click on the pdf link, it’s going to open it right up in my browser. And of course, site visitors can save it from here.
So a difficult thing to do in Drupal 7 has now been brought into core in Drupal 8 and 9 and is now something that’s really simple.
Well thanks for joining me again today. This has been OSTips from OSTraining, and I’m Rod Martin.”
Displaying a PDF in a browser has nothing todo with version of drupal or other cms. The browser itself does the work. 😉
Hi Tobias,
you are, of course, correct.
The point of the video is to demonstrate how easy it is to embed and display the PDF in Drupal 8/9.
thanks
This only shows how to add the media field and uploaded file, this didn’t show how to actually display the PDF content into the page.
Hi Niklas,
the pdf opens right up in the browser as demonstrated at around 1:53 in the above video.
I hope that helps.