Skip to content

More explicitly recommend iframe for PDFs - #45614

Merged
wbamberg merged 4 commits into
mdn:mainfrom
Josh-Cena:iframe-pdf
Sep 16, 2026
Merged

wbamberg merged 4 commits into
mdn:mainfrom
Josh-Cena:iframe-pdf

Conversation

@Josh-Cena

Copy link
Copy Markdown
Member

Fix #44512. I did some research and came to the conclusion that there's no real reason to use <object> or <embed> at all for any purpose.

@Josh-Cena
Josh-Cena requested review from a team as code owners September 10, 2026 22:54
@Josh-Cena
Josh-Cena requested review from dipikabh and wbamberg and removed request for a team September 10, 2026 22:54
@github-actions github-actions Bot added Content:HTML Hypertext Markup Language docs Content:Learn Learning area docs (outside of one of the Learn:* subtags) size/m [PR only] 51-500 LoC changed labels Sep 10, 2026
@github-actions

github-actions Bot commented Sep 10, 2026

Copy link
Copy Markdown
Contributor

If absolutely required, you can add permissions back one by one (inside the `sandbox=""` attribute value) — see the [`sandbox`](/en-US/docs/Web/HTML/Reference/Elements/iframe#sandbox) reference entry for all the available options. One important note is that you should _never_ add both `allow-scripts` and `allow-same-origin` to your `sandbox` attribute — in that case, the embedded content could bypass the [Same-origin policy](/en-US/docs/Glossary/Same-origin_policy) that stops sites from executing scripts, and use JavaScript to turn off sandboxing altogether.
If absolutely required, you can add permissions back one by one (inside the `sandbox=""` attribute value) — see the [`sandbox`](/en-US/docs/Web/HTML/Reference/Elements/iframe#sandbox) reference entry for all the available options. One important note is that you should _never_ add both `allow-scripts` and `allow-same-origin` to your `sandbox` attribute when the embedded content has the same origin as the embedding page — in that case, the embedded content could use JavaScript to remove the attribute and reload itself to turn off sandboxing altogether.

This advice applies to embedded HTML and SVG documents, where the embedded script might access the outside. For PDF documents, the browser's built-in PDF viewer already sandboxes any executable content. In fact, adding `sandbox` may prevent the PDF viewer from working.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this is a bit elliptical: is "this advice" the whole section "use the sandbox attribute" or just the bit we were just talking about? Is the guidance we want to give: that "if you are using an iframe to display a PDF, don't use sandbox? If so we should say that clearly.

Is it even worth having a separate section on embedding PDFs using iframes, since ISTM that a lot of this guidance doesn't apply there.

@Josh-Cena Josh-Cena Sep 12, 2026

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I meant you shouldn't use sandbox.

Are you suggesting a single "Embedding PDFs" section on this page, or such a subsection for each of the two H2 sections (<iframe> and <object>)? TBH there's really not that much to talk about, other than that the browser sandboxes the PDF by itself.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK so I think I'm being a bit of a perfectionist maybe but. A lot of this page is implicitly and sometimes explicitly only about embedding documents.

Especially, the core of this page, "iframes in detail" as far as I can see is all about embedding documents. That section starts off with "<iframe> elements are designed to allow you to embed other web documents into the current document" and doesn't mention PDFs at all until this bit at the end about sandboxing, and includes lots of other guidance that's not relevant to PDFs, like clickjacking. But none of this is really very clearly signposted.

So I thought it might be clearer if this doc had a separate standalone H2 on "Embedding PDFs", that covers just this niche topic, and let the rest of the page be about embedding documents.

But maybe it's not worth it. TBH this document has lots of other problems, but they're not caused by this PR.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

explicitly only about embedding documents

What does "D" in "PDF" stand for again? 😆

Added an "Embedding PDFs" section.

Comment thread files/en-us/web/html/reference/elements/embed/index.md Outdated
Comment thread files/en-us/web/html/reference/elements/iframe/index.md Outdated
Josh-Cena and others added 3 commits September 11, 2026 19:08

@wbamberg wbamberg left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍 thank you!

@wbamberg
wbamberg merged commit 456c370 into mdn:main Sep 16, 2026
9 checks passed
@Josh-Cena
Josh-Cena deleted the iframe-pdf branch September 16, 2026 03:53
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Content:HTML Hypertext Markup Language docs Content:Learn Learning area docs (outside of one of the Learn:* subtags) size/m [PR only] 51-500 LoC changed

Projects

None yet

Development

Successfully merging this pull request may close these issues.

It's unclear whether embedding PDF files is nowadays better/more secure with iframe or "traditional" embed/object HTML tags

3 participants