Question
What is the recommended way to display a PDF inside an HTML page?
Common options include:
iframeobjectembed
Which approach is generally preferred, and what guidance exists from Adobe or browser behavior in practice?
In this case, the PDF is generated dynamically on the server, so it is not possible to upload it to a third-party viewer before sending it to the browser.
Short Answer
By the end of this page, you will understand the main HTML options for displaying a PDF in a web page, how iframe, object, and embed differ, when each is useful, and why a fallback link is important. You will also see practical examples for dynamically generated PDFs and learn what developers usually do in real projects.
Concept
Displaying a PDF in a web page is really about embedding external content in HTML. The browser may render the PDF itself, use a built-in viewer, rely on a plugin-like PDF handler, or fail to show it at all depending on browser support and user settings.
The three HTML elements most commonly used are:
iframe— embeds another document inside the pageobject— embeds an external resource and can include fallback HTML contentembed— a simple embedded external resource element
Why this matters
In real applications, PDFs are often used for:
- invoices
- reports
- receipts
- contracts
- exported dashboards
- downloadable documents generated on demand
A good solution should:
- work in modern browsers as often as possible
- degrade gracefully when inline viewing is unavailable
- support dynamically generated PDFs
- avoid depending on third-party upload workflows when files are created on the fly
Practical recommendation
There is no single perfect HTML tag that guarantees PDF display everywhere. In practice:
- Use
objectwhen you want a built-in fallback message or download link inside the markup. - Use
iframewhen you simply want to show the PDF viewer as a separate embedded document. - Use a direct download link as a fallback in all cases.
The key idea is: .
Mental Model
Think of a PDF like a document you want to place inside a picture frame on a wall.
iframeis like hanging a second mini-window inside your page.objectis like placing an item in a frame, but keeping a note underneath in case the item cannot be displayed.embedis like a very direct “put this file here” instruction.
The important part is that the wall is the browser. If the wall supports hanging that kind of frame, the PDF appears. If not, you need a backup plan, usually a download link.
Syntax and Examples
Basic syntax
Using iframe
<iframe src="/files/report.pdf" width="100%" height="600"></iframe>
This loads the PDF as a nested document inside the page.
Using object
<object data="/files/report.pdf" type="application/pdf" width="100%" height="600">
<p>Your browser could not display the PDF. <a href="/files/report.pdf">Download it here</a>.</p>
</object>
This is often a strong choice because it includes fallback content.
Using embed
Step by Step Execution
Example
<object data="/reports/daily.pdf" type="application/pdf" width="100%" height="500">
<p>Preview unavailable. <a href="/reports/daily.pdf">Download the PDF</a>.</p>
</object>
What happens step by step
- The browser reads the
<object>element. - It sees
data="/reports/daily.pdf"and requests that URL from the server. - The server responds with PDF content, ideally with a header like:
Content-Type: application/pdf
- The browser checks whether it can render PDFs inline.
- If it can, the PDF viewer appears inside the area defined by
widthandheight. - If it cannot, the browser shows the fallback HTML inside
<object>:
Real World Use Cases
Common use cases
Invoices and receipts
An app generates a PDF invoice after checkout and shows it immediately in a customer portal.
Reports and dashboards
An admin exports a monthly report as PDF and previews it before downloading.
Contracts and forms
A legal or HR system shows a generated agreement for review inside the browser.
Educational material
A learning platform displays PDF handouts or worksheets directly on a lesson page.
Internal tools
A business app embeds shipping labels, purchase orders, or audit documents in a management screen.
In all of these cases, the file may be generated on demand, so embedding a server URL is more practical than relying on a third-party viewer upload workflow.
Real Codebase Usage
In real projects, developers usually treat PDF embedding as a progressive enhancement rather than a guarantee.
Common patterns
1. Provide inline preview plus download
A page tries to show the PDF, but always includes a download link nearby.
<p><a href="/export/report.pdf">Download PDF</a></p>
<object data="/export/report.pdf" type="application/pdf" width="100%" height="700">
<p>Inline preview is not available. <a href="/export/report.pdf">Download the file</a>.</p>
</object>
2. Use guard-style fallbacks
If preview is not essential, developers often avoid overengineering and simply ensure the document is accessible even when embedding fails.
3. Generate secure URLs
For private documents, the PDF route may require authentication or use a temporary signed URL.
Common Mistakes
1. Assuming one tag works everywhere
Beginners often expect iframe, object, or embed to behave identically in all browsers.
<embed src="/file.pdf">
This may work in one browser and fail or behave differently in another.
Better approach
Use a fallback link, especially with object.
2. Forgetting the correct MIME type
If the server does not send the PDF with the correct content type, the browser may not render it properly.
Problem
Content-Type: text/html
Correct
Content-Type: application/pdf
3. Forcing download when you want preview
If the server sends:
Content-Disposition: attachment
many browsers will download the file instead of displaying it inline.
Better for inline viewing
Comparisons
iframe vs object vs embed
| Option | Best for | Strengths | Weaknesses |
|---|---|---|---|
iframe | Simple inline display | Familiar, easy to use, behaves like a nested page | No natural inner fallback content |
object | Inline display with fallback | Can include fallback HTML, semantically suited to external resources | Browser behavior still varies |
embed | Minimal direct embedding | Short syntax | Less flexible fallback handling |
object vs direct link
Cheat Sheet
Quick reference
Embed with object
<object data="/file.pdf" type="application/pdf" width="100%" height="600">
<p><a href="/file.pdf">Download the PDF</a></p>
</object>
Embed with iframe
<iframe src="/file.pdf" width="100%" height="600"></iframe>
Embed with embed
<embed src= = = =>
FAQ
Is object better than iframe for PDFs?
Often yes, because object lets you include fallback HTML content such as a download link. But both are commonly used.
Should I use embed for PDF files?
You can, but it is usually less flexible than object, especially when you want graceful fallback content.
Why does my PDF download instead of showing in the page?
Your server may be sending Content-Disposition: attachment, or the browser may not support inline PDF viewing.
Can I embed a dynamically generated PDF?
Yes. Point iframe, object, or embed to a server route that generates and returns the PDF with Content-Type: application/pdf.
Do I need Adobe Reader to display PDFs in HTML?
Usually not in modern browsers, because many have built-in PDF viewers. Behavior still varies by browser and environment.
What is the safest fallback if embedding fails?
A direct link to open or download the PDF.
Can I style the inside of the PDF viewer with CSS?
Not in the same way as normal HTML content. You can style the container element, but not the internal PDF document rendering like regular page markup.
Mini Project
Description
Build a simple document preview page that shows a generated PDF in the browser when possible and provides a download link when inline viewing is not available. This reflects a common pattern in dashboards, billing systems, and internal tools.
Goal
Create an HTML page that previews a PDF using a robust embedding approach and still lets the user access the file if preview fails.
Requirements
- Embed a PDF from a URL using HTML.
- Include a clear fallback download link.
- Set a visible width and height for the preview area.
- Use a URL that could represent a dynamically generated PDF route.
Keep learning
Related questions
Can You Style Half a Character in CSS? Text Effects with CSS and JavaScript
Learn how to style half of a character using CSS and JavaScript, including overlay techniques for dynamic text effects.
Check If a Checkbox Is Checked with jQuery
Learn how to check whether a checkbox is checked in jQuery using the correct selector, with examples, mistakes, and practical patterns.
Convert HTML and CSS to PDF in PHP: Options, Limits, and Practical Approaches
Learn how HTML-to-PDF conversion works in PHP, why CSS support varies, and how to choose a practical approach for Linux web servers.