Pdf to image conversion in angular (Node)
PDF TO IMAGE
IN ANGULAR
Prerequisite:
We need to install the below two exe’s for pdf to
image conversion
http://www.graphicsmagick.org/
-
GraphicsMagick-1.3.34-Q16-win32-dll.exe
https://www.ghostscript.com/ - gs909w64.exe
Available node packages for pdf to image conversion
in Angular:
https://www.npmjs.com/package/pdf2image
https://www.npmjs.com/package/pdf2pic
https://www.npmjs.com/package/pdf-poppler
https://www.npmjs.com/package/pdf-image
https://www.npmjs.com/package/file-convert
Issue faced when integrated these libraries:
Module not found: Error: Can't resolve 'child_process'
in 'D:\Angular\imagepdfpoc\node_modules\pdf-poppler'
What is
child_process:
Child_process
used to execute the terminal commends through node server. This package
executes the (GraphicsMagick, GhostScript) exe’s for convert the pdf to image.
Ref: https://www.npmjs.com/package/child_process
Child_process
package holding by node security holder to avoid malicious use |
Solution:
You may adopt this
package by contacting support@npmjs.com and requesting the name
Command line commands for pdf to file conversion:
Using GraphicsMagick
(gm) + Ghostscript = “gm convert -size 120x120 a.pdf -resize
120x120 temp.jpg”
Using GhostScript
(gm) = “"C:\\Program Files\\gs\\gs9.52\\bin\\gswin64c.exe"
-sDEVICE=pngalpha -o file-%03d.png -r144 a.pdf”
File conversion successfully worked
when we execute the below code through the command line (Terminal) tool
manually.
Sample source code for file conversion using
child_process through command line tool:
const {
exec } = require('child_process');
exec('"C:\\Program
Files\\gs\\gs9.52\\bin\\gswin64c.exe" -sDEVICE=pngalpha -o file-%03d.png
-r144 a.pdf', (error, stdout, stderr) => {
if (error) {
console.error(`error: ${error.message}`);
return;
}
if (stderr) {
console.error(`stderr: ${stderr}`);
return;
}
console.log(`stdout:\n${stdout}`);
});
Conclusion:
This issue
only occurred in windows environment. We can use any one of the above node
library for converting pdf to image. We need to contact to node js support team
and get the access of “child_process” node package.
Comments
Post a Comment