You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Anyway, the slighlty same code doesn't draw the images included in the pdf in the output pngs. I know that this could seem more related to pdf.js, but the fact that the rest of the pdf is correctly processed makes me think that it's more of a rendering issue.
For reference, this is my code:
constdata=newUint8Array(fs.readFileSync(file.inputPath));constloadingTask=pdfjs.getDocument({
data,cMapUrl: CMAP_URL,cMapPacked: CMAP_PACKED,standardFontDataUrl: STANDARD_FONT_DATA_URL,});constpdfDocument=awaitloadingTask.promise;constnumPages=pdfDocument.numPages;console.log(`PDF loaded. Number of pages: ${numPages}`);letprogress=0;// Process each pagefor(letpageNumber=1;pageNumber<=numPages;pageNumber++){if(signal?.aborted){thrownewError('Conversion stopped by user');}constpage=awaitpdfDocument.getPage(pageNumber);constviewport=page.getViewport({scale: 3.0});// Higher scale for better quality// Create canvas and contextconstcanvasFactory=pdfDocument.canvasFactory;constcanvasAndContext=canvasFactory.create(viewport.width,viewport.height);// Render PDF page to canvasconstrenderContext={canvasContext: canvasAndContext.context,viewport: viewport,};constrenderTask=page.render(renderContext);awaitrenderTask.promise;// Convert canvas to bufferletimageBuffer=awaitcanvasAndContext.canvas.toBuffer("image/png");fs.writeFile("output_"+pageNumber+".png",image,function(error){if(error){console.error("Error: "+error);}else{console.log("Finished converting first page of PDF file to a PNG image.");}});}
Using this code with a pdf like this produces a white image, since the content is raster only.
I'm using electron v33 with node v20.18.0, napi-rs/canvas v0.1.64 on windows 11. Has anyone had similar issues?
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
-
I'm having a weird bug trying to save each page of a pdf as png. There is an example in the pdf.js library on this task, and it works fine in plain node (https://github.com/mozilla/pdf.js/blob/master/examples/node/pdf2png/pdf2png.mjs).
Anyway, the slighlty same code doesn't draw the images included in the pdf in the output pngs. I know that this could seem more related to pdf.js, but the fact that the rest of the pdf is correctly processed makes me think that it's more of a rendering issue.
For reference, this is my code:
Using this code with a pdf like this produces a white image, since the content is raster only.
I'm using electron v33 with node v20.18.0, napi-rs/canvas v0.1.64 on windows 11. Has anyone had similar issues?
Beta Was this translation helpful? Give feedback.
All reactions