I wish to display in an html page and control its scrolling, the image of a pdf hosted on a server.

I am currently using Mozilla’s pdf.js scripts, which work well for pdf files stored on my computer.

On the other hand, I have difficulties displaying a remote pdf whose URL I know.

I have not managed to find any (operational) examples of this use which, a priori, must not be original.

Thank you for providing me with such examples or telling me a way to achieve it.

(please do not offer me iframe, embed or object solutions whose behaviors/displays are different depending on the browsers, IE, Safari, Edge Chrome, Firefox...)

Best regards.

2 Replies 2

You can load and display a remote PDF in a fully browser-independent way using pdf.js,
No need using iframe/embed/object. pdf.js can fetch a PDF directly from a URL as long as the server allows CORS.
Below is a minimal working example that loads a remote PDF, renders every page into a <canvas>, and lets you control scrolling yourself.

html & js

<div id="pdf-container" style="height:600px; overflow-y:auto;"></div>
<script src="https://cdnjs.cloudflare.com/ajax/libs/pdf.js/4.0.379/pdf.min.js"></script>

pdfjsLib.GlobalWorkerOptions.workerSrc =
  "https://cdnjs.cloudflare.com/ajax/libs/pdf.js/4.0.379/pdf.worker.min.js";

const url = "https://example.com/your.pdf"; // remote PDF URL

pdfjsLib.getDocument(url).promise.then(pdf => {

  for (let i = 1; i <= pdf.numPages; i++) {
    pdf.getPage(i).then(page => {
      const viewport = page.getViewport({ scale: 1.2 });

      const canvas = document.createElement("canvas");
      const ctx = canvas.getContext("2d");

      canvas.height = viewport.height;
      canvas.width = viewport.width;

      document.getElementById("pdf-container").appendChild(canvas);

      page.render({
        canvasContext: ctx,
        viewport: viewport
      });
    });
  }

});

The only requirement is that the server hosting the PDF must send the proper CORS header,
here is like example:

Access-Control-Allow-Origin: *

Then that header is in place, pdf.js can fetch and render the PDF .

Slightly unclear but a PDF is a vector file of pages thus cannot be shown normally in a browser.

Hence the need for

  • An embedded frame with scrollbars controlled by a client only (Chromium's FireFox Gecko's based PDF viewers etc.) This Application/PDF, Manages its own inaccessible memory with the after downloading "inline" (inframe).

  • Alternative is something like PDF.JS which is an images renderer with overlays and scrollbars. This Application/PDF, Manages its own inaccessible memory with the after downloading "inline" (inframe).

  • A remaining possibility is (if browsers ALLOW images, not all do) then place canvas images side by side or one over the other and then the page scrolls not the images. This image/png view can be as suggested, generated by PDF.js outside its frame.

The common examples given (such as you see in another answer) are often flawed however this very similar working one will also be flawed as the large number of pages will crash my browser since it is now unmanaged consumer of resources.

<!DOCTYPE html><!-- Download PDF2PNG.html -->
<html><head><meta http-equiv="Content-Type" content="text/html; charset=windows-1252"></head>

<body>
<script> // Use a large typical file that allows for CORS
const url = "https://www.oecd.org/content/dam/oecd/en/publications/reports/2025/11/health-at-a-glance-2025_a894f72e/8f9e3f98-en.pdf";
</script>

<!-- the following div will be filled with
<canvas height="###" width="###"></canvas>
<canvas height="###" width="###"></canvas>
... OTHER PNGS ...
<canvas height="###" width="###"></canvas>
<canvas height="###" width="###"></canvas> -->

<div id="PDF_PNGs"></div>

<!-- // Point to the last of OLDER VERSION 3 worker files (valid ###.js MUST match so set one constant) -->
<script>
  const base = "https://cdnjs.cloudflare.com/ajax/libs/pdf.js/3.11.174/"; const script = document.createElement("script");
  script.src = base + "pdf.min.js"; document.head.appendChild(script);
  script.onload = () => {
    pdfjsLib.GlobalWorkerOptions.workerSrc = base + "pdf.worker.min.js";
    pdfjsLib.getDocument(url).promise.then(pdf => {
      for (let n=1;n <= pdf.numPages;n++) {
        pdf.getPage(n).then(img => {
          const view=img.getViewport({scale:4/3});const html=document.createElement("canvas");
          const ctx = html.getContext("2d"); html.height = view.height; html.width = view.width;
          document.getElementById("PDF_PNGs").appendChild(html); img.render({ canvasContext: ctx, viewport: view });
        });
      }
    });
  };
</script>
</body></html>

EDIT I managed to reduce the crash happening in smaller files of say 200-300 pages with a very small adjustment but lost my whole VM when testing 2000-3000 pages! so have rebuilt and returned to show some limits. Although I have an under 2MB file with over 4000 pages that should work, but small steps first as I have other tasks so I don't want some far flung PDF to take down my system. enter image description here

Your Reply

By clicking “Post Your Reply”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.