unpdf icon indicating copy to clipboard operation
unpdf copied to clipboard

Canvas doesn't seem to be working

Open himanchau opened this issue 1 year ago • 5 comments

Environment

NodeJS v22

Reproduction

const image = await renderPageAsImage(pdf, i, {
    width: 1700,
    canvas: () => import("canvas"),
  });

Describe the bug

Error processing PDF: TypeError: _canvas.createCanvas is not a function
    at Object._createCanvas (webpack-internal:///(rsc)/./node_modules/unpdf/dist/index.mjs:125:24)
    at Object.create (webpack-internal:///(rsc)/./node_modules/unpdf/dist/index.mjs:130:29)
    at renderPageAsImage$1 (webpack-internal:///(rsc)/./node_modules/unpdf/dist/index.mjs:102:29)

Additional context

No response

Logs

No response

himanchau avatar Nov 04 '24 23:11 himanchau

I have the same issue

ph1losof avatar Nov 08 '24 11:11 ph1losof

Huh, that's weird. I will have to investigate.

johannschopplich avatar Nov 08 '24 12:11 johannschopplich

Any update on this issue ? I have the same issue using Node.js 20 environment

BartaValentin avatar Nov 18 '24 11:11 BartaValentin

I don't have the resource to work on this at the moment. A quick solution didn't seem in reach when I debugged it. PR welcome!

johannschopplich avatar Nov 18 '24 11:11 johannschopplich

I am able to fix this issue, so my problem was I used in Azure Devops different kind of agent pool to build my module, because I updated the pdfjs-dist version 2.15.349 NPM package to use unpdf version 0.12.1, and in the meantime I updated the node version as well from 18.18.0 to 20.18.0, and during this update there were many breaking change between the different versions, but I could handle it well.

So I used ubuntu:22.04 agent and node.js 18.18.0 accidentally to build my node.js module, and I run my integration test on a different ci docker agent pool, which is used node.js 20.18.0, and I got the same error what is mentioned above. So I fixed this little mistake and now everything works fine.

Currently I used this NPM install command:

npm ci --arch=x64 --platform=win32 --target_arch=x64 --target_platform=win32 --target_libc=unknown

My NPM dependencies currently:

 "canvas": "2.11.2
 "unpdf": "0.12.1",

BartaValentin avatar Nov 21 '24 17:11 BartaValentin

Should be fixed in https://github.com/unjs/unpdf/commit/199e2ee9d7ab15ff371b6e4e8fce73fad4b5c318.

johannschopplich avatar Apr 30 '25 10:04 johannschopplich