question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Render pdf to fixed size?

See original GitHub issue

I’m currently writing an Electron app, which needs to get the rgba pixel data from PDF files. Now the below code I have is working fine, but I was wondering if it’s possible to render the pdf to a specific size?

This would probably(?) make processing large PDF files faster, as I only need 64x64worth of pixel data.

const create2dContext = (width, height) => {
  const canvas = document.createElement("canvas");
  canvas.width = width;
  canvas.height = height;
  return canvas.getContext("2d");
};

const decodePdf = async (imagePath) => {
  const pdf = await PDFJS.getDocument(imagePath);
  const page = await pdf.getPage(1);

  const viewport = page.getViewport(1);
  const { width, height } = viewport;

  const canvasContext = create2dContext(width, height);
  await page.render({ canvasContext, viewport });

  return canvasContext.getImageData(0, 0, width, height);
};

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:9 (1 by maintainers)

github_iconTop GitHub Comments

0reactions
Glinkiscommented, Aug 11, 2018

My solution is currently to scale the smallest axis down to 64, and then scale the other one proportionally.

And then I squish the image at a later stage. This is a decent tradeoff, and certainly saves some memory compared to previously.

However, if I could scale the initial render, I would avoid the extra memory allocation of the intermediate step.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Change PDF page size - Resize your PDF pages online
How to resize a PDF online? · Upload your PDF file. · Choose the page size from the dropdown list of common page...
Read more >
PDF.js scale PDF on fixed width - javascript - Stack Overflow
I updated the example from the Pdf.js github http://jsbin.com/pdfjs-prevnext-v2/edit#html,live to scale properly to a fixed canvas width.
Read more >
Resize PDF - Change PDF Page Size/Margins Online Free
How to resize your PDF file · 1Choose file · 2Choose your resize PDF file settings · 3View and download.
Read more >
How to Compress a PDF and Reduce Its File Size Manually
Adobe Acrobat Pro DC will set you back $14.99 per month, ... To reduce the size of your PDF, open the file, then...
Read more >
How to Change PDF Page Size Easily and Quickly
Is there a way to change page size of PDF? Open a PDF file in PDFelement, click the Page>Page Boxes button to resize...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found