[Bug]: TypeError: Image or Canvas expected at drawImageAtIntegerCoords #19795
Replies: 2 comments
-
Hello, I also face this issue, here is my original opened issue if you want more information on the problem: I'm trying to use this lib to render a PDF page at certain dimensions, but I can't manage to make it work, could you tell me what I do wrong ? Modules I use: "canvas": "Automattic/node-canvas#master" "pdfjs-dist": "^5.1.91" My code is the following:
And I get this output on the last instruction:
I do the canvas.getContext("2d") as any; workaround because if I don't, I get this error (translated from french by my own) on the await page.render... instruction:
Thank you. |
Beta Was this translation helpful? Give feedback.
-
I found a workaround for this issue, @Shrinivas-Baldawa-lti you can use pdfjs-dist V4.8.69 while waiting for this problem to be solved. |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
Attach (recommended) or Link to PDF file
Hi,
System information:
OS: Windows
canvas: ^3.1.0
pdfjs-dsit: ^5.1.91
Web browser and its version
Chrome Version 135.0.7049.42
Operating system and its version
Windows 11
PDF.js version
^5.1.91
Is the bug present in the latest PDF.js version?
Yes
Is a browser extension
No
Steps to reproduce the problem
When I am trying to convert the pdf into image buffers, I am getting the error.
const pdfData = new Uint8Array(fs.readFileSync(filePath));
const loadingTask = pdfjsLib.getDocument({ data: pdfData });
const pdf = await loadingTask.promise;
const page = await pdf.getPage(1);
const viewport = page.getViewport({scale: 2.0 });
const canvas = createCanvas(viewport.width, viewport.height);
const context = canvas.getContext('2d');
const renderContext = {
canvasContext: context,
viewport: viewport
};
await page.render(renderContext).promise;
const imageBuffer = canvas.toBuffer("image/png");
What is the expected behavior?
The expected behavior is to convert the pdf pages into image buffer.
What went wrong?
TypeError: Image or Canvas expected at drawImageAtIntegerCoords
Link to a viewer
No response
Additional context
No response
Beta Was this translation helpful? Give feedback.
All reactions