Aim
Just embed a PDF (preview) into a website. This is very likely a well-known use case.
Given I have this use case for showing a PDF preview, I am searching for a solution. And yes I know e.g. MDN always recommends just linking to PDF files, but user's UX in some applications (like document management etc.) is just much improved, when you have a preview of your file content directly in your website/application.
Research
I have read this MDN article on embedding technologies. There are basically embed/object and iframe (except of the tags specifically for video and audio etc., which are not relevant for this use case).
It goes into the history etc., but what matters is what it recommends. And first, it reads as if it recommends embed/object for PDF:
the
,andelements.s are for embedding other web pages, and the other two allow you to embed external resources such as PDF files.
And later:
The
andelements serve a different function to— these elements are general purpose embedding tools for embedding external content, such as PDFs.
There is even a full example code for PDF embedding.
The problem/contradiction: sandboxing
However in the security section one big part is to always use the sandbox attribute.
I tested it and at least in Firefox embedding PDFs in an iframe e.g. with sandbox="allow-scripts allow-downloads allow-top-navigation-by-user-activation" works. I would assume, due to the sandboxing being applied, this is a security advantage in favor of object/embed?
So these parts are a little contradictory, because I cannot use sandbox with object at all.
Further research
I found What is the best option for embedding content (it's not about PDF) and a generic explanation of the differences, but all are too generic and do not explain this use case. Also this question just explores the technical 'how-to' aspect, which is not a problem here - how to do it technically is fine.
Question
Thus the 'simple' question is: What is the best, modern and (especially) most secure way to embed a PDF (preview) into a website?