How to Embed PDF in HTML: iframe, object, embed, PDF.js
A PDF is a plugin-free format in every current browser, but that does not make it a first-class HTML element: there is no <pdf> tag. What you get instead is four routes — <iframe>, <object>, <embed>, or a JavaScript renderer such as PDF.js — and they differ in what the browser does with the file, what the page can control, and whether the PDF survives a phone.
For a single PDF in a page, <iframe src="file.pdf"> is the answer: one line, the browser’s own viewer, no library. Everything else exists for a specific failure — a PDF that downloads instead of rendering, a frame blocked by a header, a design that has to work on mobile, or a viewer that must do more than display pages.

What you’ll build: A working embed for each method, the browser behaviour behind every failure mode, and a decision table to pick one.
Key Takeaways
<iframe>,<object>and<embed>all hand the file to the browser’s built-in PDF viewer; none of them renders PDF pages as HTML, and none of them works without a PDF viewer in the browser.- Desktop Chrome, Edge, Firefox and Safari all display embedded PDFs. On Android, Chrome and Firefox download the file instead of rendering it — that behaviour has not changed and no attribute overrides it.
- A PDF that downloads instead of appearing is almost always a server header:
Content-Disposition: attachmentor aContent-Typeother thanapplication/pdf. - A blank frame is a framing policy:
X-Frame-Optionsor CSPframe-ancestors. CORS is not involved in a plain<iframe>— it only matters when JavaScript fetches the bytes, as PDF.js does. - A JavaScript viewer is the only route that gives you a consistent UI across desktop and mobile, plus search, annotation and download control. PDF.js gives you rendering and leaves the UI to you.
Which Method Fits
| Method | Desktop | Mobile | Text and annotations | Use when |
|---|---|---|---|---|
<iframe> |
Built-in viewer | Android downloads; iOS shows a preview | Text in the viewer; annotation only if the viewer offers it | A desktop-first page that just has to show a PDF |
<object> |
Built-in viewer | Same as iframe | Same as iframe | You want fallback markup when there is no viewer |
<embed> |
Built-in viewer | Same as iframe | Same as iframe | Legacy pages only; no fallback path |
| PDF.js | Renders anywhere | Renders anywhere | Text only with the separate text layer; no annotations | You need to draw pages yourself, or bypass the browser viewer |
| Server-side conversion | Plain HTML or images | Plain HTML or images | Images lose text; HTML keeps it; no annotations | Archival documents where layout beats interaction |
| Viewer SDK | Renders anywhere | Renders anywhere | Text, search, annotations, download control | You need a product-grade viewer, not an embed |
Prerequisites
Nothing to install for <iframe>, <object> or <embed> — they are part of HTML, and the PDF viewer is the browser’s. PDF.js loads from a CDN in the snippet below. Dynamsoft Document Viewer, covered at the end, needs a license key: Get a 30-day free trial license.
Embed with iframe
The shortest working embed. Give the frame a title for screen readers and a height, because the default is 150 px:
<iframe
src="/files/report.pdf"
title="Quarterly report"
width="100%"
height="600"
loading="lazy">
</iframe>
Two attributes are worth knowing. loading="lazy" defers the PDF until the frame scrolls into view, which matters because the browser fetches the whole file. And the default frame border is visible in some browsers, so style="border: 0" if you do not want it.
The frame’s content is the browser’s PDF viewer: toolbar, zoom and print come from Chrome, Edge, Firefox or Safari, and you cannot remove them. If the PDF is on another origin, the same HTTP headers apply as for any nested page — see Security and CORS.
Embed with object
<object> does the same job and takes fallback content, so a browser without a PDF viewer shows your message instead of an empty box:
<object data="/files/report.pdf" type="application/pdf" width="100%" height="600">
<p>This browser cannot display PDFs. <a href="/files/report.pdf">Download the report</a>.</p>
</object>
That fallback is why <object> is the more correct choice on paper. In practice the fallback almost never fires: every current desktop browser has a viewer, and the mobile failure is a download rather than a missing plugin, so the fallback does not help there.
Embed with embed
<embed> is a void element — it cannot contain fallback content — and for PDFs it is the legacy path from the NPAPI plugin era:
<embed src="/files/report.pdf" type="application/pdf" width="100%" height="600" />
It still renders in desktop browsers because they map it onto the same built-in viewer, but there is nothing to fall back to and nothing to control. New pages should prefer <iframe>.
Why an Embedded PDF Fails, and What to Fix
Four failures account for almost every “the PDF does not show up” report. They are server-side or policy-side, so no tag or attribute fixes them from the HTML.
| Symptom | Cause | Fix |
|---|---|---|
| The file downloads instead of appearing | Content-Disposition: attachment, or Content-Type: application/octet-stream |
Serve the file with Content-Type: application/pdf and Content-Disposition: inline |
The frame is blank, console mentions X-Frame-Options |
The PDF host sends X-Frame-Options: DENY or SAMEORIGIN, or CSP frame-ancestors excludes you |
Allow the embedding origin, or proxy the file from your own domain |
| Blank frame on an HTTPS site, fine locally | The PDF is served over http:// — mixed content is blocked |
Serve the PDF over HTTPS |
| Works on desktop, downloads on Android | Android Chrome and Firefox have no built-in PDF viewer in the page context | Accept the download, or render with PDF.js or a viewer SDK |
A fifth case looks like a failure but is not: iOS Safari shows the PDF in a preview that scrolls and zooms, but its toolbar is not yours and its behaviour changes between iOS releases, so it will not match a desktop layout.
Render Pages Yourself with PDF.js
PDF.js is Mozilla’s renderer — the same engine behind Firefox’s PDF viewer. It draws pages into a canvas, which means it works on mobile and inside any layout, at the cost of building the controls yourself.
Minimal single-page render:
<canvas id="pdf-canvas"></canvas>
<script type="module">
import * as pdfjsLib from "https://cdn.jsdelivr.net/npm/pdfjs-dist@4/build/pdf.min.mjs";
pdfjsLib.GlobalWorkerOptions.workerSrc =
"https://cdn.jsdelivr.net/npm/pdfjs-dist@4/build/pdf.worker.min.mjs";
const pdf = await pdfjsLib.getDocument({ url: "/files/report.pdf" }).promise;
const page = await pdf.getPage(1);
const viewport = page.getViewport({ scale: 1.5 });
const canvas = document.getElementById("pdf-canvas");
canvas.width = viewport.width;
canvas.height = viewport.height;
await page.render({ canvasContext: canvas.getContext("2d"), viewport }).promise;
</script>
Three things bite here:
- CORS, for real this time.
getDocument({url})fetches the bytes with JavaScript, so a cross-origin PDF needsAccess-Control-Allow-Originon the PDF response. An<iframe>pointing at the same file would have worked without it. - The worker must load too.
workerSrchas to resolve to the matchingpdf.workerbuild; a version mismatch between the main script and the worker is the most common PDF.js error. - Page navigation, zoom, search and print are yours to build. PDF.js returns page objects and, separately, a text layer you must render on top of the canvas if you want selectable text.
PDFium.js is the other renderer worth naming: same idea, Chromium’s PDF engine compiled to WebAssembly. Both stop at rendering; neither ships a viewer UI.
Security and CORS: What Actually Applies
The confusion here is worth clearing up, because it sends people to the wrong fix.
| What you do | Is CORS involved? | What can block it |
|---|---|---|
<iframe src="https://other.site/doc.pdf"> |
No — the frame is a navigation, not a fetch | X-Frame-Options, CSP frame-ancestors, mixed content |
<object data> or <embed src> |
No | Same framing policies, plus the file must be served as application/pdf |
pdfjsLib.getDocument({url}) |
Yes — Access-Control-Allow-Origin is required |
CORS preflight/response headers, and the worker file’s own CORS |
<input type="file"> then render locally |
No | Nothing: the file never leaves the page |
Two more security points that come up in reviews. A PDF is an active document format — it can carry JavaScript, forms and embedded files — so embedding a PDF from an untrusted host is equivalent to embedding an untrusted page. The sandbox attribute on an iframe is the lever, but it can also stop the built-in viewer from rendering at all, so test the combination before shipping it. And if the requirement is “view but do not download”, no native tag can do it: the browser viewer always offers save and print, which is one of the reasons teams move to a JavaScript viewer.
Convert the PDF Server-Side Instead
Two conversions predate every JavaScript option and are still the right answer for archival content:
- PDF to images (Apache PDFBox, ImageMagick, Ghostscript). Every browser displays the result with no viewer at all, but text is gone: no search, no copy, no selection, and file size grows quickly at readable resolutions.
- PDF to HTML (
pdf2htmlEX). Keeps text selectable and reflows like a web page, which makes it the best of the conversions — but the output is a fixed-layout HTML plus fonts and images, it is heavy, and the result cannot be annotated as a PDF.
Converted sample · iframe wrapping the converted HTML
Advanced: A Full Viewer with Dynamsoft Document Viewer
Everything above hands the PDF to something else — the browser’s viewer, or PDF.js with your own controls. When the requirement is a viewer rather than an embed — page thumbnails, search, annotations, form fields, redaction, and a toolbar you control — that is a viewer SDK, and it is the last option rather than the first.
Dynamsoft Document Viewer wraps PDFium in a viewer with a built-in UI. The whole integration is a container, a license, and one load call:
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/dynamsoft-document-viewer@latest/dist/ddv.css">
<script src="https://cdn.jsdelivr.net/npm/dynamsoft-document-viewer@latest/dist/ddv.js"></script>
<div id="container" style="width:100%;height:600px"></div>
<script type="module">
Dynamsoft.DDV.Core.license = "YOUR_LICENSE_KEY";
Dynamsoft.DDV.Core.engineResourcePath =
"https://cdn.jsdelivr.net/npm/dynamsoft-document-viewer@latest/dist/engine";
Dynamsoft.DDV.Core.loadWasm();
await Dynamsoft.DDV.Core.init();
const viewer = new Dynamsoft.DDV.EditViewer({
container: "container",
uiConfig: Dynamsoft.DDV.getDefaultUiConfig("editViewer"),
});
const blob = await (await fetch("/files/report.pdf")).blob();
const doc = Dynamsoft.DDV.documentManager.createDocument();
await doc.loadSource(blob);
viewer.openDocument(doc.uid);
</script>
Because the document is loaded from a Blob you fetched, the PDF never has to be reachable by the browser’s viewer, and the rendering stays inside the page — which is what makes mobile behaviour identical to desktop and what makes annotation and download control possible.
Method Comparison
| Method | Technology | Pros | Cons | Best for |
|---|---|---|---|---|
| Native HTML elements | iframe, object, embed | Simple to implement; uses the browser’s viewer; no dependency | Inconsistent on mobile; no control over the UI; cannot prevent download | Quick integration where basic viewing is enough and devices are predictable |
| Server-side conversion | PDFBox, pdf2htmlEX | Renders everywhere; HTML output keeps text selectable | Images lose text; HTML output is heavy; no annotation | Archival documents where layout matters more than interaction |
| JavaScript renderers | PDF.js, PDFium.js | Consistent on every platform; full control of the canvas | You build the UI; CORS and worker setup; no annotation | Custom viewers, or PDFs that must not go through the browser viewer |
| Viewer SDK | Dynamsoft Document Viewer | Consistent UI, search, annotation, download control | A dependency and a license; larger payload | Product-grade document workflows |
Common Issues and Edge Cases
- The iframe downloads the PDF on Android. Chrome and Firefox for Android do not embed a PDF viewer; the navigation becomes a download. Use PDF.js or a viewer SDK if mobile is a requirement, and test on a real device rather than an emulator, where the behaviour often differs.
- The frame is blank but the URL works in a tab. Open DevTools on the page, not the tab:
X-Frame-Optionsand CSPframe-ancestorsfailures appear there as refusals to display, and they are sent by the PDF host, not by your page. - PDF.js renders nothing and logs a worker error. The worker build must match the main library version. Pin both URLs to the same release rather than mixing
@latestwith a pinned copy. - A 300-page PDF freezes the tab. Rendering every page at load is the usual cause. Render the visible page and a page or two around it, or let a viewer SDK handle virtualization; Chrome’s built-in viewer streams with range requests, which is why it feels faster than a naive PDF.js implementation.
- Text cannot be selected in the rendered PDF. PDF.js draws glyphs to a canvas; selection needs the separate text layer rendered above it. If selection matters and you do not want to build that layer, that is the point where a viewer SDK is cheaper than the work.