k-yle / pdf-to-img Goto Github PK
View Code? Open in Web Editor NEW📃📸 Converts PDFs to images in nodejs
Home Page: https://npm.im/pdf-to-img
License: MIT License
📃📸 Converts PDFs to images in nodejs
Home Page: https://npm.im/pdf-to-img
License: MIT License
Hey there.
As noted in #225, there is an upcoming major release of node-canvas
that appears to actually compile correctly with electron. I have been testing this with pdf-to-img
, and it more or less works... But there is an issue I encountered I wanted to bring up because I think it will need to be addressed in some capacity or another. I'm not sure if this is a pdfjs
or pdf-to-img
problem (actually I'm about certain it is a pdfjs
problem, but the canvas dependency resides in this repo).
First things first, my package.json
looks something like this:
{
...
"dependencies": {
"pdf-to-img": "^2.1.2"
},
"overrides": {
"pdf-to-img": {
"canvas": "https://github.com/Automattic/node-canvas.git#master"
}
}
}
This appears to work as expected and installs the 3.0
version of canvas.
My test file looks like this:
const { fork } = require('node:child_process');
const fs = require('node:fs');
const { pdf } = require('pdf-to-img');
const file = `<some-file>`;
async function getPages () {
const doc = await pdf(file);
for await (const page of doc) {
fs.writeFileSync("test.png", page);
}
}
getPages();
When running the file, I'm presented with this error:
...\pdfjs-test\node_modules\pdfjs-dist\legacy\build\pdf.js:8743
ctx.save = ctx.__originalSave;
^
TypeError: Cannot assign to read only property 'save' of object '#<CanvasRenderingContext2D>'
at CanvasRenderingContext2D.ctx._removeMirroring (...\pdfjs-test\node_modules\←[4mpdfjs-dist←[24m\legacy\build\pdf.js:8743:14)
at CanvasGraphics.endSMaskMode (...\pdfjs-test\node_modules\←[4mpdfjs-dist←[24m\legacy\build\pdf.js:9793:14)
at CanvasGraphics.checkSMaskState (...\pdfjs-test\node_modules\←[4mpdfjs-dist←[24m\legacy\build\pdf.js:9770:12)
at CanvasGraphics.restore (...\pdfjs-test\node_modules\←[4mpdfjs-dist←[24m\legacy\build\pdf.js:9841:12)
at CanvasGraphics._restoreInitialState2 (...\pdfjs-test\node_modules\←[4mpdfjs-dist←[24m\legacy\build\pdf.js:10945:10)
at CanvasGraphics.endDrawing (...\pdfjs-test\node_modules\←[4mpdfjs-dist←[24m\legacy\build\pdf.js:9572:79)
at InternalRenderTask.cancel (...\pdfjs-test\node_modules\←[4mpdfjs-dist←[24m\legacy\build\pdf.js:6253:82)
Looking under the hood of pdfjs
, the problem seems to arise from the mirrorContextOperations
function, which is overwriting many of the built-in canvas context methods such as save
, restore
, scale
, etc.
This doesn't seem to have been an issue for the previous version of canvas, but looking over some of the update it received to support NAPI, it seems like those methods are now defined as read-only properties.
Now I'm not sure if this is really an issue for this repo or for the pdfjs
one. The problem certainly comes from their usage of the canvas context, but considering they don't even have a canvas dependency, I thought maybe this was the place to start. Maybe a sort of proxy object or something that would allow the methods overridden in mirrorContextOperations
to be done so safely.
Just wanted to open a discussion!
It's not really clear how to use the document returned from the pdf function call. It's not mentioned what type of image is created either.
The README mentions the package has no native dependencies, yet on macOS, brew install mango && brew install cairo
is needed
Hi @k-yle,
I've recently come across this package while looking for a PDF-to-PNG utility and it's been pretty convenient for my purposes. However, I see that the PDF input type is required to be a string representing a URL or local file path.
My application deals mostly with streamed & buffered PDF contents and I was curious if you'd be interested in adding support for passing the input PDF data as a Buffer or ReadableStream?
I have implemented & tested this locally in my installed node_modules, so if you're open to this enhancement but don't feel up to making the change then I'd be happy to submit a PR, just let me know.
Error when running my Electron app
App threw an error during load
Error: The module '/home/untitled1/VSCodeProjects/batcher/node_modules/canvas/build/Release/canvas.node'
was compiled against a different Node.js version using
NODE_MODULE_VERSION 108. This version of Node.js requires
NODE_MODULE_VERSION 109. Please try re-compiling or re-installing
the module (for instance, using `npm rebuild` or `npm install`).
at process.func [as dlopen] (node:electron/js2c/asar_bundle:5:1810)
at Module._extensions..node (node:internal/modules/cjs/loader:1205:18)
at Object.func [as .node] (node:electron/js2c/asar_bundle:5:1810)
at Module.load (node:internal/modules/cjs/loader:988:32)
at Module._load (node:internal/modules/cjs/loader:829:12)
at c._load (node:electron/js2c/asar_bundle:5:13343)
at Module.require (node:internal/modules/cjs/loader:1012:19)
at require (node:internal/modules/cjs/helpers:102:18)
at Object.<anonymous> (/home/untitled1/VSCodeProjects/batcher/node_modules/canvas/lib/bindings.js:3:18)
at Module._compile (node:internal/modules/cjs/loader:1120:14)
A JavaScript error occurred in the main process
Uncaught Exception:
Error: The module '/home/untitled1/VSCodeProjects/batcher/node_modules/canvas/build/Release/canvas.node'
was compiled against a different Node.js version using
NODE_MODULE_VERSION 108. This version of Node.js requires
NODE_MODULE_VERSION 109. Please try re-compiling or re-installing
the module (for instance, using `npm rebuild` or `npm install`).
at process.func [as dlopen] (node:electron/js2c/asar_bundle:5:1810)
at Module._extensions..node (node:internal/modules/cjs/loader:1205:18)
at Object.func [as .node] (node:electron/js2c/asar_bundle:5:1810)
at Module.load (node:internal/modules/cjs/loader:988:32)
at Module._load (node:internal/modules/cjs/loader:829:12)
at c._load (node:electron/js2c/asar_bundle:5:13343)
at Module.require (node:internal/modules/cjs/loader:1012:19)
at require (node:internal/modules/cjs/helpers:102:18)
at Object.<anonymous> (/home/untitled1/VSCodeProjects/batcher/node_modules/canvas/lib/bindings.js:3:18)
at Module._compile (node:internal/modules/cjs/loader:1120:14)
I've tried deleting my package-lock.json and node_modules folder and re-running npm i
> [email protected] postinstall
> electron-builder install-app-deps
• electron-builder version=23.6.0
• loaded configuration file=/home/untitled1/VSCodeProjects/batcher/electron-builder.yml
• rebuilding native dependencies [email protected] platform=linux arch=x64
⨯ cannot execute cause=exit status 1
errorOut=npm ERR! code 1
npm ERR! path /home/untitled1/VSCodeProjects/batcher/node_modules/canvas
npm ERR! command failed
npm ERR! command sh -c node-pre-gyp install --fallback-to-build --update-binary
npm ERR! Failed to execute '/home/untitled1/.volta/tools/image/node/18.16.0/bin/node /home/untitled1/.volta/tools/image/node/18.16.0/lib/node_modules/npm/node_modules/node-gyp/bin/node-gyp.js configure --fallback-to-build --update-binary --module=/home/untitled1/VSCodeProjects/batcher/node_modules/canvas/build/Release/canvas.node --module_name=canvas --module_path=/home/untitled1/VSCodeProjects/batcher/node_modules/canvas/build/Release --napi_version=8 --node_abi_napi=napi --napi_build_version=0 --node_napi_label=electron-v21.4' (1)
npm ERR! node-pre-gyp info it worked if it ends with ok
npm ERR! node-pre-gyp info using [email protected]
npm ERR! node-pre-gyp info using [email protected] | linux | x64
npm ERR! node-pre-gyp http GET https://github.com/Automattic/node-canvas/releases/download/v2.10.2/canvas-v2.10.2-electron-v21.4-linux-glibc-x64.tar.gz
npm ERR! node-pre-gyp ERR! install response status 404 Not Found on https://github.com/Automattic/node-canvas/releases/download/v2.10.2/canvas-v2.10.2-electron-v21.4-linux-glibc-x64.tar.gz
npm ERR! node-pre-gyp WARN Pre-built binaries not installable for [email protected] and [email protected] (electron-v21.4 ABI, glibc) (falling back to source compile with node-gyp)
npm ERR! node-pre-gyp WARN Hit error response status 404 Not Found on https://github.com/Automattic/node-canvas/releases/download/v2.10.2/canvas-v2.10.2-electron-v21.4-linux-glibc-x64.tar.gz
npm ERR! gyp info it worked if it ends with ok
npm ERR! gyp info using [email protected]
npm ERR! gyp info using [email protected] | linux | x64
npm ERR! gyp info ok
npm ERR! gyp info it worked if it ends with ok
npm ERR! gyp info using [email protected]
npm ERR! gyp info using [email protected] | linux | x64
npm ERR! gyp info find Python using Python version 3.11.3 found at "/usr/bin/python3"
npm ERR! gyp info spawn /usr/bin/python3
npm ERR! gyp info spawn args [
npm ERR! gyp info spawn args '/home/untitled1/.volta/tools/image/node/18.16.0/lib/node_modules/npm/node_modules/node-gyp/gyp/gyp_main.py',
npm ERR! gyp info spawn args 'binding.gyp',
npm ERR! gyp info spawn args '-f',
npm ERR! gyp info spawn args 'make',
npm ERR! gyp info spawn args '-I',
npm ERR! gyp info spawn args '/home/untitled1/VSCodeProjects/batcher/node_modules/canvas/build/config.gypi',
npm ERR! gyp info spawn args '-I',
npm ERR! gyp info spawn args '/home/untitled1/.volta/tools/image/node/18.16.0/lib/node_modules/npm/node_modules/node-gyp/addon.gypi',
npm ERR! gyp info spawn args '-I',
npm ERR! gyp info spawn args '/home/untitled1/.electron-gyp/21.4.4/include/node/common.gypi',
npm ERR! gyp info spawn args '-Dlibrary=shared_library',
npm ERR! gyp info spawn args '-Dvisibility=default',
npm ERR! gyp info spawn args '-Dnode_root_dir=/home/untitled1/.electron-gyp/21.4.4',
npm ERR! gyp info spawn args '-Dnode_gyp_dir=/home/untitled1/.volta/tools/image/node/18.16.0/lib/node_modules/npm/node_modules/node-gyp',
npm ERR! gyp info spawn args '-Dnode_lib_file=/home/untitled1/.electron-gyp/21.4.4/<(target_arch)/node.lib',
npm ERR! gyp info spawn args '-Dmodule_root_dir=/home/untitled1/VSCodeProjects/batcher/node_modules/canvas',
npm ERR! gyp info spawn args '-Dnode_engine=v8',
npm ERR! gyp info spawn args '--depth=.',
npm ERR! gyp info spawn args '--no-parallel',
npm ERR! gyp info spawn args '--generator-output',
npm ERR! gyp info spawn args 'build',
npm ERR! gyp info spawn args '-Goutput_dir=.'
npm ERR! gyp info spawn args ]
npm ERR! Package pixman-1 was not found in the pkg-config search path.
npm ERR! Perhaps you should add the directory containing `pixman-1.pc'
npm ERR! to the PKG_CONFIG_PATH environment variable
npm ERR! Package 'pixman-1', required by 'virtual:world', not found
npm ERR! gyp: Call to 'pkg-config pixman-1 --libs' returned exit status 1 while in binding.gyp. while trying to load binding.gyp
npm ERR! gyp ERR! configure error
npm ERR! gyp ERR! stack Error: `gyp` failed with exit code: 1
npm ERR! gyp ERR! stack at ChildProcess.onCpExit (/home/untitled1/.volta/tools/image/node/18.16.0/lib/node_modules/npm/node_modules/node-gyp/lib/configure.js:325:16)
npm ERR! gyp ERR! stack at ChildProcess.emit (node:events:513:28)
npm ERR! gyp ERR! stack at ChildProcess._handle.onexit (node:internal/child_process:291:12)
npm ERR! gyp ERR! System Linux 6.2.15-300.fc38.x86_64
npm ERR! gyp ERR! command "/home/untitled1/.volta/tools/image/node/18.16.0/bin/node" "/home/untitled1/.volta/tools/image/node/18.16.0/lib/node_modules/npm/node_modules/node-gyp/bin/node-gyp.js" "configure" "--fallback-to-build" "--update-binary" "--module=/home/untitled1/VSCodeProjects/batcher/node_modules/canvas/build/Release/canvas.node" "--module_name=canvas" "--module_path=/home/untitled1/VSCodeProjects/batcher/node_modules/canvas/build/Release" "--napi_version=8" "--node_abi_napi=napi" "--napi_build_version=0" "--node_napi_label=electron-v21.4"
npm ERR! gyp ERR! cwd /home/untitled1/VSCodeProjects/batcher/node_modules/canvas
npm ERR! gyp ERR! node -v v18.16.0
npm ERR! gyp ERR! node-gyp -v v9.3.1
npm ERR! gyp ERR! not ok
npm ERR! node-pre-gyp ERR! build error
npm ERR! node-pre-gyp ERR! stack Error: Failed to execute '/home/untitled1/.volta/tools/image/node/18.16.0/bin/node /home/untitled1/.volta/tools/image/node/18.16.0/lib/node_modules/npm/node_modules/node-gyp/bin/node-gyp.js configure --fallback-to-build --update-binary --module=/home/untitled1/VSCodeProjects/batcher/node_modules/canvas/build/Release/canvas.node --module_name=canvas --module_path=/home/untitled1/VSCodeProjects/batcher/node_modules/canvas/build/Release --napi_version=8 --node_abi_napi=napi --napi_build_version=0 --node_napi_label=electron-v21.4' (1)
npm ERR! node-pre-gyp ERR! stack at ChildProcess.<anonymous> (/home/untitled1/VSCodeProjects/batcher/node_modules/@mapbox/node-pre-gyp/lib/util/compile.js:89:23)
npm ERR! node-pre-gyp ERR! stack at ChildProcess.emit (node:events:513:28)
npm ERR! node-pre-gyp ERR! stack at maybeClose (node:internal/child_process:1091:16)
npm ERR! node-pre-gyp ERR! stack at ChildProcess._handle.onexit (node:internal/child_process:302:5)
npm ERR! node-pre-gyp ERR! System Linux 6.2.15-300.fc38.x86_64
npm ERR! node-pre-gyp ERR! command "/home/untitled1/.volta/tools/image/node/18.16.0/bin/node" "/home/untitled1/VSCodeProjects/batcher/node_modules/.bin/node-pre-gyp" "install" "--fallback-to-build" "--update-binary"
npm ERR! node-pre-gyp ERR! cwd /home/untitled1/VSCodeProjects/batcher/node_modules/canvas
npm ERR! node-pre-gyp ERR! node -v v18.16.0
npm ERR! node-pre-gyp ERR! node-pre-gyp -v v1.0.10
npm ERR! node-pre-gyp ERR! not ok
npm ERR! A complete log of this run can be found in:
npm ERR! /home/untitled1/.npm/_logs/2023-05-15T02_49_01_414Z-debug-0.log
command=/home/untitled1/.volta/tools/image/node/18.16.0/bin/node /home/untitled1/.volta/tools/image/node/18.16.0/lib/node_modules/npm/bin/npm-cli.js rebuild [email protected]
workingDir=
npm ERR! code 1
npm ERR! path /home/untitled1/VSCodeProjects/batcher
npm ERR! command failed
npm ERR! command sh -c electron-builder install-app-deps
npm ERR! A complete log of this run can be found in:
npm ERR! /home/untitled1/.npm/_logs/2023-05-15T02_47_31_149Z-debug-0.log
canvas
has recently solved its compatibility with Node 18 (please see Automattic/node-canvas#2025). By updating to >2.10.2
of canvas
this package could also be compatible with Node 18 and following that, the downstream projects could update to Node 18.
The update has already been merged to main
in a626d1f but no new release has been done yet.
Please I need help on how to solve this issue. Thank you!
hi!
I want to use this library in electron, but there is a problem with it when I try to run electron-rebuild
.
I saw this PR - Automattic/node-canvas#2235 and I think it might help.
I would love to hear your opinon.
The error:
> rebuild
> electron-rebuild --parallel --types prod,dev,optional --module-dir release/app
\ Building modules: 1/2Warning: Missing input files:
C:\GTK\bin\libpango-1.0-0.dll
C:\GTK\bin\zlib1.dll
C:\GTK\bin\libpangoft2-1.0-0.dll
C:\GTK\bin\libintl-8.dll
C:\GTK\bin\libpangocairo-1.0-0.dll
C:\GTK\bin\libfreetype-6.dll
C:\GTK\bin\libgthread-2.0-0.dll
C:\GTK\bin\libpng14-14.dll
C:\GTK\bin\libfontconfig-1.dll
C:\GTK\bin\libglib-2.0-0.dll
C:\GTK\bin\libpangowin32-1.0-0.dll
C:\GTK\bin\libgmodule-2.0-0.dll
C:\GTK\bin\libcairo-2.dll
C:\GTK\bin\libgobject-2.0-0.dll
C:\GTK\bin\libexpat-1.dll
Building the projects in this solution one at a time. To enable parallel build, please add the "-m" switch.
Backend.cc
C:\Users\app\node_modules\canvas\src\backend\Backend.h(3,10): fatal error C1083: Cannot open include file: 'cairo.h': No such file or directory [C:\software\release\app\node_modules\canvas\build\canvas.vcxproj]
Error: `C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\MSBuild\Current\Bin\MSBuild.exe` failed with exit code: 1
at ChildProcess.onExit (C:\software\node_modules\node-gyp\lib\build.js:203:23)
at ChildProcess.emit (node:events:390:28)
at Process.ChildProcess._handle.onexit (node:internal/child_process:290:12)
× Rebuild Failed
An unhandled error occurred inside electron-rebuild
node-gyp failed to rebuild C:\software\release\app\node_modules\canvas'
Error: node-gyp failed to rebuild C:\software\release\app\node_modules\canvas'
at ChildProcess.<anonymous> (C:\software\node_modules\@electron\rebuild\lib\module-type\node-gyp\node-gyp.js:118:24)
at ChildProcess.emit (node:events:390:28)
at Process.ChildProcess._handle.onexit (node:internal/child_process:290:12)
Thanks!
Hi: Is it possible to set a specific size of the image? I have size information from OCR and in my use case, the size must conform to the OCR information. Thank you.
Thought the package-lock.json file contains the correct version, the version pattern for pdfjs-dist allows for a version of pdfjs-dist that seems to rename the es5
directory to legacy
. Thus, an error is thrown when this package is used as a dependency of another project without that dependent project explicitly setting its pdfjs-dist version to 2.7.570
Please update:
"pdfjs-dist": "^2.7.570"
to
"pdfjs-dist": "~2.7.570"
High quality conversions of PDFs with a high number of pages takes a lot of time.
Adding an option to select which pages of the PDF to convert would optimize scenarios in which you know which pages you want to convert.
Apart from that the package work wonders!
The pdfjs-dist dependency is pinned to version 3.2.146 which has vulnerability CVE-2024-4367. This is fixed in pdfjs-dist version 4.2.67.
This should be solved by #233... but there is a problem with "PromiseWithResolvers". The TypeScript error might be avoided by setting skipLibCheck in tsconfig.
Unfortunately that still leaves the following error, which I don't know how to solve.
src/index.ts:111:15 - error TS2740: Type 'CanvasRenderingContext2D' is missing the following properties from type 'CanvasRenderingContext2D': getContextAttributes, isPointInStroke, createConicGradient, filter, and 5 more.
111 canvasContext: context,
~~~~~~~~~~~~~
node_modules/pdfjs-dist/types/src/display/api.d.ts:372:5
372 canvasContext: CanvasRenderingContext2D;
~~~~~~~~~~~~~
The expected type comes from property 'canvasContext' which is declared here on type 'RenderParameters'
Found 1 error in src/index.ts:111
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.