How To Open A PDF In A New Tab or Window in Drupal
Whenever I embed a PDF, or any document for that matter in a Drupal website, I always prefer to have the document open in a new tab or window, so that it’s easier to download and also easier to get back to the main site since it’s still there when you close out the PDF. Drupal doesn’t do that “out-of-the-box”. Don’t fear, you can definitely do that in Drupal.
The EXTLink Module For Drupal
When you want to add a feature to your Drupal website, you typically look for a contributed module that will provide that functionality. In Drupal 10, one of the projects (modules) that does that is the EXTLink module and it’s the one we’ll use to add that functionality to our site (https://drupal.org/project/extlink).
Steps:
- Install end enable the module using Composer/Drush or manually in the user interface from the link above.
- Head over to Configuration -> User Interface -> External Links
- Find the checkbox that says: “Open external links in a new window or tab” and put a check mark in the box.
- Scroll down to “Patterns” and open the drop down.
- In the “Include links matching the pattern field type: .*.pdf
- Click Save
- Add a PDF to a node and click on the link – it will open in a new tab/window!
Note: Following the above steps will open every pdf in a new tab or window. There is currently no “switch” to opt-out of that behavior.
If you’d like to see it in action, please watch the following OSTip!
If you have any suggestions for OSTip videos, feel free to leave a comment!