Ever wondered how to get PDF attachments to open directly in the browser? Today we're opening the "XWiki Tips and Tricks" series with a guide on how to achieve just that.
Right now, when you want to open PDF attachments from your wiki you'll get the following default behavior:
- if the PDF file was uploaded by users with no programming rights, then the file is automatically downloaded upon click
- if the PDF file was uploaded by a user with programming rights (e.g. Admin) then the file is opened directly in browser upon click
So it's safe to say most users will download files by default instead of opening them directly in the browser.
To always open PDF attachment directly in the browser you'll need to modify the WEB-INF/xwiki.properties file as follows:
- Open WEB-INF/xwiki.properties and go to the “Attachment” section
- Uncomment the line with the default whitelist and add the "application/pdf" MIME-type to it. You should have: attachment.download.whitelist=audio/basic,audio/L24,audio/mp4,audio/mpeg,audio/ogg,audio/vorbis,audio/vnd.rn-realaudio,audio/vnd.wave,audio/webm,image/gif,image/jpeg,image/pjpeg,image/png,image/svg+xml,image/tiff,text/csv,text/plain,text/xml,text/rtf,video/mpeg,video/ogg,video/quicktime,video/webm,video/x-matroska,video/x-ms-wmv,video/x-flv,application/pdf
- Save the modifications and restart the server.
The below image shows you the modifications in the file
A PDF opened inside the browser
Note the same behavior can also be achieved for other types of attachments that you want to open directly in the browser. You need to search for the right MIME type for your file and add it to the “Attachment” section from WEB-INF/xwiki.properties as shown above.
Iulia Balan - Customer Service Specialist @ XWiki