PDF.js Direct Downloader

Adds a download button to PDF.js viewers that saves the original PDF file

You will need to install an extension such as Tampermonkey, Greasemonkey or Violentmonkey to install this script.

You will need to install an extension such as Tampermonkey or Violentmonkey to install this script.

You will need to install an extension such as Tampermonkey or Violentmonkey to install this script.

You will need to install an extension such as Tampermonkey or Userscripts to install this script.

You will need to install an extension such as Tampermonkey to install this script.

You will need to install a user script manager extension to install this script.

(I already have a user script manager, let me install it!)

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

(I already have a user style manager, let me install it!)

// ==UserScript==
// @name         PDF.js Direct Downloader
// @namespace    http://tampermonkey.net/
// @version      1.0
// @description  Adds a download button to PDF.js viewers that saves the original PDF file
// @author       Anonymous
// @match        *://*/*
// @grant        none
// @license      MIT
// ==/UserScript==

(function() {
    'use strict';

    function addDownloadButton() {
        // Check if PDF.js is available
        if (typeof PDFViewerApplication === "undefined" ||
            !PDFViewerApplication.pdfDocument) {
            return;
        }

        // Avoid duplicate buttons
        if (document.getElementById("tm-pdf-download-btn")) return;

        // Create button
        let btn = document.createElement("button");
        btn.id = "tm-pdf-download-btn";
        btn.textContent = "Download PDF";
        btn.style.position = "fixed";
        btn.style.top = "10px";
        btn.style.right = "10px";
        btn.style.zIndex = "9999";
        btn.style.padding = "8px 12px";
        btn.style.background = "#007bff";
        btn.style.color = "white";
        btn.style.border = "none";
        btn.style.borderRadius = "5px";
        btn.style.cursor = "pointer";

        btn.onclick = function() {
            PDFViewerApplication.pdfDocument.getData().then(function(data) {
                let blob = new Blob([data], { type: "application/pdf" });
                let url = URL.createObjectURL(blob);

                let a = document.createElement("a");
                a.href = url;
                a.download = "document.pdf";
                document.body.appendChild(a);
                a.click();
                document.body.removeChild(a);
                URL.revokeObjectURL(url);
            });
        };

        document.body.appendChild(btn);
    }

    // Try adding button periodically (since PDF.js may load async)
    setInterval(addDownloadButton, 2000);
})();