Render pdf to fixed size?
See original GitHub issueI’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 64x64
worth 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:
- Created 5 years ago
- Comments:9 (1 by maintainers)
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
You’ll need to resize the viewport as well; look at how the thumbnail canvas size is set in https://github.com/mozilla/pdf.js/blob/309b6f820e5250e6290f1fb986c7c92f7a1431cd/web/pdf_thumbnail_view.js#L106-L112 and how the scale of the viewport is then changed in https://github.com/mozilla/pdf.js/blob/309b6f820e5250e6290f1fb986c7c92f7a1431cd/web/pdf_thumbnail_view.js#L316 before rendering is triggered in https://github.com/mozilla/pdf.js/blob/309b6f820e5250e6290f1fb986c7c92f7a1431cd/web/pdf_thumbnail_view.js#L329-L333
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.