Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Puppeteer does not read an existing PDF and turn it into HTML. Its Page.pdf() method does the reverse: it prints a web page to a PDF. To display an existing PDF in a web page, use Mozilla’s PDF.js to render its pages into a canvas. If you need searchable, semantic HTML rather than a visual display, PDF.js’s canvas-rendering example alone does not provide that conversion.

Choose the tool for the direction of conversion

What you want to do Suitable approach What it produces
Show an existing PDF in a web interface PDF.js display APIs A visual rendering of each page, commonly drawn into an HTML canvas.
Generate a PDF from a web page Puppeteer Page.pdf() PDF bytes or a PDF file created by printing the current page.
Navigate a Puppeteer headless-shell page directly to a PDF URL Do not rely on this as the reader The Puppeteer Page API documents that headless shell does not support navigation to a PDF document.

These are different workflows, not interchangeable ways to perform the same conversion. Puppeteer’s documentation says, “For printing PDFs use Page.pdf().” The Puppeteer Page.setContent() API assigns HTML markup, while Page.content() returns the page’s HTML. Neither API is documented as an existing-PDF parser.

PDF.js has distinct core, display, and viewer layers. The display API is the relevant starting point for custom rendering; the viewer is an interface that can inform a custom implementation. Mozilla asks sites embedding its viewer not to use it unmodified.

Render an existing PDF page with PDF.js

The core browser workflow is: load PDF.js and its worker, load the document, request a page, calculate a viewport, size a canvas, and render the page. This example is an ES module intended for an application build setup that can resolve the pdfjs-dist package and bundle a worker asset. Pin the PDF.js release in your project and confirm that its worker and display code are compatible; the official example does not establish one specific released package version.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
#1 Best Overall
PDF Extra 2024| Complete PDF Reader and Editor | Create, Edit, Convert, Combine, Comment, Fill & Sign PDFs | Lifetime License | 1 Windows PC | 1 User [PC Online code]
  • EDIT text, images & designs in PDF documents. ORGANIZE PDFs. Convert PDFs to Word, Excel & ePub.
  • READ and Comment PDFs – Intuitive reading modes & document commenting and mark up.
  • CREATE, COMBINE, SCAN and COMPRESS PDFs
  • FILL forms & Digitally Sign PDFs. PROTECT and Encrypt PDFs
  • LIFETIME License for 1 Windows PC or Laptop. 5GB MobiDrive Cloud Storage Included.
import * as pdfjsLib from 'pdfjs-dist';
import pdfWorkerUrl from 'pdfjs-dist/build/pdf.worker.mjs?url';

pdfjsLib.GlobalWorkerOptions.workerSrc = pdfWorkerUrl;

const canvas = document.querySelector('#pdf-page');
const context = canvas.getContext('2d');
const status = document.querySelector('#status');

async function renderPdfPage(url, pageNumber = 1) {
  status.textContent = 'Loading PDF…';

  const loadingTask = pdfjsLib.getDocument({ url });
  const pdf = await loadingTask.promise;

  if (!Number.isInteger(pageNumber) || pageNumber < 1 || pageNumber > pdf.numPages) {
    throw new RangeError(`Page must be between 1 and ${pdf.numPages}`);
  }

  const page = await pdf.getPage(pageNumber);
  const scale = 1.5;
  const viewport = page.getViewport({ scale });
  const outputScale = window.devicePixelRatio || 1;

  canvas.width = Math.floor(viewport.width * outputScale);
  canvas.height = Math.floor(viewport.height * outputScale);
  canvas.style.width = `${Math.floor(viewport.width)}px`;
  canvas.style.height = `${Math.floor(viewport.height)}px`;

  const renderTask = page.render({
    canvasContext: context,
    viewport,
    transform: outputScale !== 1
      ? [outputScale, 0, 0, outputScale, 0, 0]
      : null
  });

  await renderTask.promise;
  status.textContent = `Page ${pageNumber} of ${pdf.numPages}`;
  return pdf;
}

renderPdfPage('/documents/guide.pdf', 1).catch(error => {
  console.error(error);
  status.textContent = `Could not display the PDF: ${error.message}`;
});
<p id="status" role="status">Preparing PDF…</p>
<canvas id="pdf-page">This browser cannot display the PDF canvas.</canvas>

The worker import shown uses a bundler convention for emitting a worker asset. If your build system does not support the ?url import, configure its supported worker handling and set GlobalWorkerOptions.workerSrc to the resulting worker URL. Do not omit worker setup when adapting the official example. The exact packaging path varies with the PDF.js version and build system.

What the rendering values do

  • getDocument({ url }) starts loading the PDF; awaiting its loading task gives you the document proxy.
  • getPage(pageNumber) retrieves a one-based page. The example checks the requested page against pdf.numPages before rendering.
  • getViewport({ scale }) calculates the rendered page dimensions. Raising the scale makes the page larger and increases the amount of canvas data to draw.
  • The canvas’s CSS dimensions control its displayed size; its pixel dimensions are multiplied by devicePixelRatio to keep the rendering sharp on high-density screens.
  • page.render() draws the page into the canvas. Await its render task before treating the page as ready or starting another render into that canvas.

For a multi-page document, call the render function with the selected page number and build navigation controls around pdf.numPages. If users can request another page before the current render finishes, manage render state: wait for the current task or cancel it before drawing a replacement to the same canvas. A single canvas displays one rendered page at a time; a document-style interface needs page navigation or a separate canvas per visible page.

Rank #2
PDF Extra Ultimate | Complete PDF Reader and Editor | Create, Edit, Convert, Combine, Comment, Fill & Sign PDFs | Yearly License | 1 Windows PC & 2 Mobile Devices | 1 User
  • EDIT text, images & designs in PDF documents. ORGANIZE PDFs. Convert PDFs to Word, Excel & ePub.
  • READ and Comment PDFs – Intuitive reading modes & document commenting and mark up.
  • CREATE, COMBINE, SCAN and COMPRESS PDFs
  • FILL forms & Digitally Sign PDFs. PROTECT and Encrypt PDFs
  • 1 Year License for 1 Windows & 2 Mobile (Android and/or iOS) devices.

Handle same-origin and remote PDFs

A PDF at the same origin as your HTML page is the simplest case. For a PDF hosted on another origin, the PDF host must allow the browser request through its CORS configuration. The PDF.js browser example specifically calls out configuring CORS headers on the server. A browser-side code change cannot grant access to a remote server that does not permit the request.

Check the browser’s developer console and Network panel when loading fails. A CORS error points to the serving host’s policy; a missing file or incorrect path points to the URL or server response. Make sure the URL resolves to the intended PDF and that your application handles a rejected loading task rather than leaving an empty canvas without explanation.

What’s actually slowing this PC down?

Pick the symptom - the matching free tool is one click away.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
Rank #3
PDF Extra Lifetime - Professional PDF Editor - Best Adobe Acrobat Pro Alternative - Lifetime License for Windows PC
  • Perfect Adobe Acrobat Pro alternative – lifetime license for Windows 10 and 11.
  • EDIT text, images, pages, hyperlinks, designs in PDF documents. ORGANIZE PDFs.
  • READ and Comment on PDFs – Intuitive reading modes & document commenting and mark up tools!
  • CREATE, COMBINE, SCAN and COMPRESS PDFs.
  • FILL forms & Digitally Sign PDFs. Work with Digital certificates

Use Puppeteer when you want to create a PDF

If your actual goal is to turn HTML into a PDF, Puppeteer is the appropriate tool. Its documented workflow navigates to a web page and calls page.pdf(). The method returns a Promise<Uint8Array>; provide a path to write the result as a file.

import puppeteer from 'puppeteer';

const browser = await puppeteer.launch();
try {
  const page = await browser.newPage();
  await page.goto('https://example.com', { waitUntil: 'networkidle0' });
  await page.pdf({ path: 'page.pdf', format: 'A4' });
} finally {
  await browser.close();
}

Replace the example URL with a page you are authorized to access. The documented PDF output is based on print media and print-oriented color behavior. If the page must use screen media instead, emulate screen media before generating the PDF:

Rank #4
My PDF Reader
  • Open PDF files easily on your smartphone.
  • Cool User Interface and look.
  • Zoom and pan easily by using gesture with your fingers.
  • Scroll through pages easily vertically.
  • Fullscreen viewing capability.
await page.emulateMediaType('screen');
await page.pdf({ path: 'page.pdf', format: 'A4' });

The PDF API also supports page dimensions, margins, background graphics, and other print options. Review the API for your installed Puppeteer version before depending on a particular option or default. For exact colors, Puppeteer documents using the CSS property -webkit-print-color-adjust. Print styles can hide, rearrange, or restyle page content, so inspect the resulting PDF when the output matters.

Common failures and practical fixes

Symptom Likely cause What to check
Canvas stays blank and loading reports an error PDF request failed, the URL is wrong, or the remote host blocks the cross-origin request. Inspect the loading-task error and Network panel; verify the PDF URL and configure CORS on the PDF host when origins differ.
PDF.js reports a worker-related error The worker was not packaged, its URL is wrong, or the worker and library builds do not match. Set GlobalWorkerOptions.workerSrc to the worker asset emitted by your build, and keep it aligned with the installed PDF.js release.
Requested page cannot be displayed The page number is outside the document’s one-based range. Use a page number from 1 through pdf.numPages and update the controls when a new document loads.
Rendered page looks blurry on a high-density display The canvas backing dimensions are only sized for CSS pixels. Scale the backing dimensions and render transform by window.devicePixelRatio, as in the example.
page.goto(pdfUrl) does not behave like a PDF viewer in headless shell Puppeteer documents that headless shell does not support navigation to PDF documents. Use PDF.js in the web page for display, or use Puppeteer to print a normal web page into a PDF instead.
The Puppeteer PDF differs from the visible page page.pdf() uses print-oriented output, and print CSS may change layout or colors. Check print styles; emulate screen media if that is the intended output, and use -webkit-print-color-adjust when exact colors are required.
Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

Performance, reliability, and the right output

Rendering a PDF page into a canvas is a visual presentation technique, not a conversion into semantic HTML. The canvas does not by itself expose the page as ordinary HTML text and elements. If your requirement is to extract text, make document content accessible to assistive technology, or build semantic HTML from PDF content, do not treat this canvas example as a solution to that separate problem.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
Best Value
PDF Reader
  • ❖ Open, PDF Reader application utility, simple.
  • ❖ Full PDF reading mode
  • ❖ Quickly reader PDF files from storage of your phone, ...
  • ❖ Move, zoom in, zoom out the PDF File with PDF Reader
  • ❖ Move to the desired page in the PDF Reader

Canvas size grows with the viewport scale and device pixel ratio, so high-resolution pages can use substantial browser memory. Render only the pages users need at the size they need, especially for long documents, and avoid drawing many full-size pages at once. For navigation, ensure that a previous render has completed or been cancelled before reusing the canvas. If reliability matters, show a loading state, catch document and render failures, and provide a useful error message rather than assuming every URL is readable.

For Puppeteer PDF creation, wait for the page state your document requires before calling page.pdf(); page navigation completing is not necessarily proof that application-specific content or fonts are ready. The PDF call produces bytes, so you can write them to a path or handle the bytes in your application. Test with the actual print styles and target output settings instead of assuming the screen presentation will be reproduced exactly.

Or skip the browser setup

If the task is to capture a web page as an image or PDF rather than read an existing PDF, ScreenshotNeo provides a website screenshot API and MCP server. A single GET request returns a PNG, JPEG, WebP, or PDF. For example, this cURL request saves a screenshot of a page:

curl -G "https://api.screenshotneo.com/v1/shot" -d access_key=YOUR_API_KEY --data-urlencode url=https://stripe.com -o shot.webp

See the ScreenshotNeo documentation for request options. Cookie banners, popups, and chat widgets are removed before the shot; bot checks, blank pages, and failed loads are never billed. Its MCP server lets AI agents take screenshots. The free plan includes 1,000 screenshots a month with no card; paid plans start at $5 for 3,000. Sign up for the free plan.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Frequently Asked Questions

Does Puppeteer’s Page.content() read PDF text?

No. It returns the HTML of the current page; it is not documented as an API for extracting text from an existing PDF.

Can I make the rendered PDF canvas searchable by selecting its text?

Not with the canvas-rendering approach shown here alone; it displays the page visually rather than producing semantic HTML text.

Quick Recap

Bestseller No. 1
PDF Extra 2024| Complete PDF Reader and Editor | Create, Edit, Convert, Combine, Comment, Fill & Sign PDFs | Lifetime License | 1 Windows PC | 1 User [PC Online code]
PDF Extra 2024| Complete PDF Reader and Editor | Create, Edit, Convert, Combine, Comment, Fill & Sign PDFs | Lifetime License | 1 Windows PC | 1 User [PC Online code]
READ and Comment PDFs – Intuitive reading modes & document commenting and mark up.; CREATE, COMBINE, SCAN and COMPRESS PDFs
$99.99
Bestseller No. 2
PDF Extra Ultimate | Complete PDF Reader and Editor | Create, Edit, Convert, Combine, Comment, Fill & Sign PDFs | Yearly License | 1 Windows PC & 2 Mobile Devices | 1 User
PDF Extra Ultimate | Complete PDF Reader and Editor | Create, Edit, Convert, Combine, Comment, Fill & Sign PDFs | Yearly License | 1 Windows PC & 2 Mobile Devices | 1 User
READ and Comment PDFs – Intuitive reading modes & document commenting and mark up.; CREATE, COMBINE, SCAN and COMPRESS PDFs
$83.88
Bestseller No. 3
PDF Extra Lifetime - Professional PDF Editor - Best Adobe Acrobat Pro Alternative - Lifetime License for Windows PC
PDF Extra Lifetime - Professional PDF Editor - Best Adobe Acrobat Pro Alternative - Lifetime License for Windows PC
Perfect Adobe Acrobat Pro alternative – lifetime license for Windows 10 and 11.; EDIT text, images, pages, hyperlinks, designs in PDF documents. ORGANIZE PDFs.
$99.99
Bestseller No. 4
My PDF Reader
My PDF Reader
Open PDF files easily on your smartphone.; Cool User Interface and look.; Zoom and pan easily by using gesture with your fingers.
Bestseller No. 5
PDF Reader
PDF Reader
❖ Open, PDF Reader application utility, simple.; ❖ Full PDF reading mode; ❖ Quickly reader PDF files from storage of your phone, ...
$1.99

Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.