pdfjs-serverless icon indicating copy to clipboard operation
pdfjs-serverless copied to clipboard

Canvas support for serverless environments

Open gregnr opened this issue 2 years ago • 6 comments
trafficstars

Great lib!

Based on https://github.com/johannschopplich/pdfjs-serverless/blob/main/src/mock/canvas.mjs I assume there's currently no intent to support canvas with this lib (ie. to render a PDF page to the canvas in order to export as PNG). I tried to use https://deno.land/x/[email protected] to accomplish this, but hit multiple issues with that implementation where it's missing various Canvas APIs that PDF.js needs.

node-canvas seems to support the spec better, but as of now that library is implemented only as a native Node module (vs. WASM), so won't work in Deno Deploy (or CF Workers).

Just wanted to reach out in the odd chance you had any thoughts on this or any previous attempts to make this happen. Cheers!

gregnr avatar Nov 08 '23 05:11 gregnr

Hi @gregnr!

Thanks for the flowers. And also for your interest in this library! I haven't found a pure WASM solution yet. I remember some libraries with that goal, but without full canvas support. I tried briefly, but not in depth.

If you find a library in the wild, even now or later, feel free to share it. It would be great to somehow transform PDF.js with full image rendering support for workers.

I also maintain unpdf, which inherits the work of this library. Once we have canvas support, you will be able to use renderPageAsImage for Cf workers and Deno Deploy. This method supports passing a canvas-compatible library as an argument, which might work in your Deno situation.

Hopefully we'll figure it out one day!

johannschopplich avatar Nov 08 '23 12:11 johannschopplich

Can it get an image data file inside of a pdf - export it as base 64 https://stackoverflow.com/questions/18680261/extract-images-from-pdf-file-with-javascript This is needed to do pdf-to-html in cloudflare workers, and to keep images common in academic pdfs to show them to user later

possible for rendering canvas https://github.com/renderlet/wander

vtempest avatar Sep 12 '24 05:09 vtempest

How about @napi-rs/canvas

cfrerebeau avatar Oct 22 '24 13:10 cfrerebeau

How about @napi-rs/canvas

https://github.com/Brooooooklyn/canvas/blob/main/example/image-data.js This might work as polyfil.

I'm using this pdf npm to build airesearch.js.org and qwksearch alt to google that shows pdf text alongside result list. Very much need to include the images in a pdf as well, which can be exported as base64 example: https://qwksearch.com/?q=attention+is+all+you+need+pdf

vtempest avatar Oct 30 '24 21:10 vtempest