PDFObject: PDF Embedding Utility

2.3.1 · active · verified Sun Apr 19

PDFObject is a lightweight JavaScript utility designed for embedding PDF documents directly into HTML pages. Currently at version 2.3.1, it provides a robust solution for displaying PDFs inline across various browsers. The project maintains a steady release cadence, with significant updates (like 2.3) introducing major architectural changes approximately annually, alongside minor patches for fixes and enhancements. A key differentiator is its shift to a pure `<iframe>` approach in v2.3, abandoning the less consistent `<embed>` element, which enhances cross-browser compatibility and reliability. It also intelligently leverages `navigator.pdfViewerEnabled` to determine native PDF viewing capabilities and automatically falls back to download options on unsupported platforms, especially mobile devices. This makes it a pragmatic choice for integrating PDF content without external plugins.

Common errors

Warnings

Install

Imports

Quickstart

Demonstrates how to embed a PDF directly into an HTML element using PDFObject's `embed` method, including feature detection and fallback content.

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>PDFObject Quickstart</title>
    <style>
        #pdf-container {
            width: 800px;
            height: 600px;
            border: 1px solid #ccc;
            margin: 20px auto;
        }
    </style>
</head>
<body>
    <h1>Embedding a PDF with PDFObject</h1>
    <div id="pdf-container"></div>

    <!-- Load PDFObject from a CDN -->
    <script src="https://unpkg.com/pdfobject/pdfobject.min.js"></script>
    <script>
        // Replace with your actual PDF URL
        const pdfUrl = 'https://pdfobject.com/pdf/sample.pdf'; 

        // Check if PDFObject is available and embedding is supported
        if(PDFObject.supportsPDFs) {
            PDFObject.embed(pdfUrl, "#pdf-container", {
                height: "100%", // Make it fill the container
                width: "100%",
                id: "my-embedded-pdf",
                // Example PDF Open Parameters (e.g., open to page 2, fit view)
                pdfOpenParams: {
                    view: "FitV",
                    page: "2"
                },
                // Fallback content for browsers that don't support inline PDFs
                fallbackLink: `<p>This browser does not support inline PDFs. You can <a href="${pdfUrl}">download the PDF</a> instead.</p>`
            });
            console.log("PDF embedded successfully!");
        } else {
            // Display fallback content if embedding is not supported
            document.getElementById("pdf-container").innerHTML = `<p>Your browser does not support inline PDFs. You can <a href="${pdfUrl}">download the PDF</a> instead.</p>`;
            console.warn("PDF embedding not supported in this browser.");
        }
    </script>
</body>
</html>

view raw JSON →