| Drupal

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

How to Display a PDF in Browser with Drupal 9 & 8

 

I have just a simple PDF Demo field which is a "media field". 

How to Display a PDF in Browser with Drupal 9 & 8

NOTE: You will need to have installed that over here under the extend menu (see below) - nothing fancy.

DHow to Display a PDF in Browser with Drupal 9 & 8

  • Return to Manage Fields tab
  • Click Edit button
  • Select Document under Media type

How to Display a PDF in Browser with Drupal 9 & 8

 

  • Click Manage display tab

NOTE: The format is Rendered entity and it is Rendered as Default. That's important!

How to Display a PDF in Browser with Drupal 9 & 8

 

  • Click Content tab

I have a node in this content type.

  • Click Edit tab
  • Click Add media button

How to Display a PDF in Browser with Drupal 9 & 8

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.

How to Display a PDF in Browser with Drupal 9 & 8

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."


About the author

Rod holds two masters degrees and has been training people how to do "things" for over 25 years. Originally from Australia, he grew up in Canada and now resides just outside Cincinnati, Ohio.