ImageMagick 7.0.4 / NodeJS 6.9.2 / GPLGS 9.15 / Win 7: Failing to convert files - node.js

Problem
I am able to do a conversion from PDF to another image format from the command-line using the convert.exe ImageMagick command. However, when running the following JS in Node I get the error that follows the code below.
JavaScript
fs = require('fs');
var PDFImage = require("pdf-image").PDFImage;
console.log("Start");
var pdfImage = new PDFImage('test.pdf');
pdfImage.convertPage(0).then(function (imagePath) {
// 0-th page (first page) of the slide.pdf is available as slide-0.png
console.log('Converted');
fs.existsSync('test-0.png') // => true
}, function (err) {
console.log(err);
});
package.json
{
"name": "pdftester",
"version": "1.0.0",
"description": "PDF Tester",
"main": "PDFTester.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "John Doe",
"license": "ISC",
"dependencies": {
"async": "^2.1.4",
"imagemagick": "^0.1.3",
"pdf-image": "^1.1.0",
"pdftotextjs": "^0.4.0"
}
}
Results
Start
{ message: 'Failed to convert page to image',
error:
{ Error: Command failed: convert 'test.pdf[0]' 'test-0.png'
convert: unable to open image ''test.pdf[0]'': No such file or directory # error/blob.c/OpenBlob/2691.
convert: unable to open module file 'C:\ImageMagick-7.0.4-Q16\modules\coders\IM_MOD_RL_PDF[0]'_.dll': No such file or directory # warning/module.c/GetMagickModulePath/680.
convert: no decode delegate for this image format `PDF[0]'' # error/constitute.c/ReadImage/509.
convert: no images defined `'test-0.png'' # error/convert.c/ConvertImageCommand/3254.
at ChildProcess.exithandler (child_process.js:206:12)
at emitTwo (events.js:106:13)
at ChildProcess.emit (events.js:191:7)
at maybeClose (internal/child_process.js:877:16)
at Socket.<anonymous> (internal/child_process.js:334:11)
at emitOne (events.js:96:13)
at Socket.emit (events.js:188:7)
at Pipe._handle.close [as _onclose] (net.js:498:12)
killed: false,
code: 1,
signal: null,
cmd: 'convert \'test.pdf[0]\' \'test-0.png\'' },
stdout: '',
stderr: 'convert: unable to open image \'\'test.pdf[0]\'\': No such file or directory # error/blob.c/OpenBlob/2691.\r\nconvert: unable to open module file \'C:\\ImageMagick-7.0.4-Q16\\modules\\coders\\IM_MOD_RL_PDF[0]\'_.dll\': No such file or directory # warning/module.c/GetMagickModulePath/680.\r\nconvert: no decode delegate for this image format `PDF[0]\'\' # error/constitute.c/ReadImage/509.\r\nconvert: no images defined `\'test-0.png\'\' # error/convert.c/ConvertImageCommand/3254.\r\n' }
Analysis
The offending line is the command-line built for convert.exe, that is:
'convert \'test.pdf[0]\' \'test-0.png\''
What is adding the extra slashes, ticks ('), and '[0]'?
Thanks in advance!

according to your question , extra slashes denotes to enter in folder.
you have to put your file with respect to server file from where it starts. or put your pdf file into from where your running your apps.

Related

Error when i try to convert file using imagemagick

I try to make a PDF file cover in JPG for this I use Node JS and the following libraries ImageMagick Graphicsmagick and Ghostcript.
Code:
root/controller/convertorController.js
const path = require('path');
const {exec} = require('child_process')
let baseDir = path.join(__dirname,'../tmp');
exports.customExecution = async(req,res,next)=>{
console.log(testDir)
exec(`magick convert ${testDir} -quality 100 awbtest.jpg`,(err,stdout,stderr)=>{
if(err){
console.log(err)
}else{
console.log('success')
next();
}
})
}
Project Structure
I checked the file path is correct if when I execute the function does nothing:
Error: Command failed: magick convert D:\Project\pdf-jpg convertor\tmp/test.pdf -quality 100 awbtest.jpg
convert: unable to open image 'D:\Project\pdf-jpg convertor\tmp/test.pdf': No such file
or directory # error/blob.c/OpenBlob/3570.
convert: no images defined `awbtest.jpg' # error/convert.c/ConvertImageCommand/3342.
at ChildProcess.exithandler (node:child_process:397:12)
at ChildProcess.emit (node:events:390:28)
at maybeClose (node:internal/child_process:1064:16)
at Process.ChildProcess._handle.onexit (node:internal/child_process:301:5) {
killed: false,
code: 1,
signal: null,
cmd: 'magick convert D:\Project\pdf-jpg convertor\tmp/test.pdf -quality 100
awbtest.jpg'
}

I can't run commands from nodejs as child_process, or read the file system in an electron application with snap package configuration

I have working on an electron desktop application, the app is quite simple its about building a file browser for Linux. From Nodejs apis I use child_process, fs, path, os, and so on.
I am using electron-builder to package and build the application.
When I build for linux with target like "zip", "deb", "rpm", the application works as expected.
But when I compile for snap. I can't run commands like:
const util = require('util');
const exec = util.promisify(require('child_process').exec);
await exec('which code');
.....
await openExternalApp('code -n', `"${file.path}"`)
.....
async function openExternalApp(cmd, path) {
const util = require('util');
const exec = util.promisify(require('child_process').exec);
await exec(`${cmd} ${path}`);
}
.....
I got an error:
Error: Command failed: which code
at ChildProcess.exithandler (node:child_process:406:12)
at ChildProcess.emit (node:events:390:28)
at maybeClose (node:internal/child_process:1064:16)
at Socket.<anonymous> (node:internal/child_process:450:11)
at Socket.emit (node:events:390:28)
at Pipe.<anonymous> (node:net:687:12) {
killed: false,
code: 1,
signal: null,
cmd: 'which code',
stdout: '',
stderr: ''
}
Error: Command failed: which code
Also in my program when i have to read directories of the file system related with hard drive or another location simply i can't.
I've started to study a little bit the docs in snap store and I realized that you have to configure the slot, layout and slugs to access the whole file system, and perhaps to run bash or shell command like the one above.
After several configuration i continue with the same issues. Here is my configuration for snap in pakage.json
"snap": {
"plugs": [
"desktop",
"desktop-legacy",
"home",
"x11",
"unity7",
"browser-support",
"network",
"gsettings",
"opengl",
"block-devices",
"classic-support",
"hardware-observe",
"home",
"system-backup",
"system-observe",
"process-control",
"hostname-control",
"removable-media",
{
"system-files": {
"read": [
"/etc",
"/usr",
"/home",
"/media",
"/mnt",
"/var",
"/temp",
"/opt",
"/sys",
"/dev",
"/bin",
"/snap"
],
"write": [
"/home",
"/mnt"
]
}
}
],
"desktop": {
"Encoding": "UTF-8",
"Icon": "${SNAP}/icon.png"
}
}
snap image config in pakage.json

Cucumber testing example is not running

I have setup a working nightwatch environment and now want to use cucumber for testing. I have followed this guide: https://www.youtube.com/watch?v=Jdlsv0CQ2CY&t=22s. No matter what I try I cannot get the test to work, I get this as output:
> cucumberpractice#1.0.0 test /Users/kieran/Documents/cucumberPractice
> cucumber-js --require cucumber.conf.js --require step-definitions --format node_modules/cucumber-pretty
Feature: Hacker News Search
Scenario: Searching Hacker News
Given I open Hacker News's home page
? undefined
Then the title is "Hacker News"
? undefined
And the Hacker News search form exists
? undefined
Warnings:
1) Scenario: Searching Hacker News # features/hackernews.feature:3
? Given I open Hacker News's home page
Undefined. Implement with the following snippet:
Given('I open Hacker News\'s home page', function () {
// Write code here that turns the phrase above into concrete actions
return 'pending';
});
? Then the title is "Hacker News"
Undefined. Implement with the following snippet:
Then('the title is {string}', function (string) {
// Write code here that turns the phrase above into concrete actions
return 'pending';
});
? And the Hacker News search form exists
Undefined. Implement with the following snippet:
Then('the Hacker News search form exists', function () {
// Write code here that turns the phrase above into concrete actions
return 'pending';
});
1 scenario (1 undefined)
3 steps (3 undefined)
0m00.000s
npm ERR! code ELIFECYCLE
npm ERR! errno 1
npm ERR! cucumberpractice#1.0.0 test: `cucumber-js --require cucumber.conf.js --require step-definitions --format node_modules/cucumber-pretty`
npm ERR! Exit status 1
npm ERR!
npm ERR! Failed at the cucumberpractice#1.0.0 test script.
npm ERR! This is probably not a problem with npm. There is likely additional logging output above.
npm ERR! A complete log of this run can be found in:
npm ERR! /Users/kieran/.npm/_logs/2019-09-13T20_22_09_135Z-debug.log
I have package.json, nightwatch.conf.js, cucumber.conf.js, hackernews.feature and hackernews.js files.
const { client } = require('nightwatch-api');
const { Given, Then } = require('cucumber');
Given(/^I open Hacker News's home page$/, () => {
return client
.url('https://news.ycombinator.com/')
.waitForElementVisible('body', 1000);
});
Then(/^the title is "([^"]*)"$/, title => {
return client.assert.title(title);
});
Then(/^the Hacker News search form exists$/, () => {
return client.assert.visible('input[name="q"]');
});
Feature: Hacker News Search
Scenario: Searching Hacker News
Given I open Hacker News's home page
Then the title is "Hacker News"
And the Hacker News search form exists
const chromedriver = require('chromedriver');
module.exports = {
"src_folders": ["tests"],
test_settings: {
default: {
webdriver: {
start_process: true,
server_path: chromedriver.path,
port: 4444,
cli_args: ['--port=4444']
},
desiredCapabilities: {
browserName: 'chrome'
}
}
}
};
{
"name": "cucumberpractice",
"version": "1.0.0",
"description": "Cucumber framework",
"main": "index.js",
"scripts": {
"test": "cucumber-js --require cucumber.conf.js --require step-definitions --format node_modules/cucumber-pretty"
},
"repository": {
"type": "git",
"url": "git+https://github.com/BinaryJava/nightwatch-testing.git"
},
"author": "Kieran Marriott",
"license": "ISC",
"bugs": {
"url": "https://github.com/BinaryJava/nightwatch-testing/issues"
},
"homepage": "https://github.com/BinaryJava/nightwatch-testing#readme",
"devDependencies": {
"chromedriver": "^76.0.1",
"nightwatch": "^1.2.2"
},
"dependencies": {
"cucumber": "^5.1.0",
"cucumber-pretty": "^1.5.2",
"nightwatch-api": "^2.3.0"
}
}
How do I fix this?
you might giving the path wrong for step definition file in your package.json file,
"scripts": {
"test": "cucumber-js --require cucumber.conf.js --require step-definitions --format node_modules/cucumber-pretty"
},
for example my, here is my code in package,json,
"scripts": {
"test": "cucumber-js --require cucumber.conf.js --require tests --format node_modules/cucumber-pretty"
},
"--require tests": here "test" is the location folder where my feature file and my step definition files are exist.
Instead of Scenario: Searching Hacker News
you have to put
Scenario Outline: Searching Hacker News
to add Examples

Unable to convert pdf to image on AWS Lambda

I have a Nodejs 8.10 lambda which converts pdf to png which was working fine untill few days ago.
This is the error which seems to be caused because of AWS update
https://aws.amazon.com/blogs/compute/upcoming-updates-to-the-aws-lambda-execution-environment/
{
Error: Command failed: convert -density 200 -quality 75 "/tmp/temp_file.pdf[0]" "/tmp/temp_file-0.png"
convert: unable to load module `/usr/lib64/ImageMagick-6.7.8/modules-Q16/coders/pdf.la': file not found # error/module.c/OpenModule/1278.
convert: no decode delegate for this image format `/tmp/temp_file.pdf' # error/constitute.c/ReadImage/544.
convert: no images defined `/tmp/temp_file-0.png' # error/convert.c/ConvertImageCommand/3046.
}
The error was resolved by adding this public layer which will stop working after July 22:
arn:aws:lambda:::awslayer:AmazonLinux1703
I tried creating ImageMagick Static Binaries for AWS Lambda.
I followed these instructions for ImageMagic version 6.9.10-5
https://gist.github.com/bensie/56f51bc33d4a55e2fc9a
https://imagemagick.org/download/ImageMagick-6.9.10-55.tar.gz
Folder structure for lambda:
I tried basic resize of png image:
var IM_PATH = "var/task/imagemagick/bin/";
process.env['LD_LIBRARY_PATH'] = 'var/task/imagemagick/lib/';
process.env['PATH'] = process.env['PATH'] + ':' + IM_PATH + ':' + process.env['LD_LIBRARY_PATH'];
var gm = require('gm').subClass({
imageMagick: true,
appPath: 'var/task/imagemagick/bin/',
});
gm('image.png')
.resize(240, 240, '!')
.write('/tmp/resize.png', function (err) {
if (!err) console.log('done');
else {
console.log(err);
}
});
Tried running on Node 8.10 and node 10.x lambdas and here the the errors:
1) Node 8.10
{ Error: Command failed: convert: UnableToOpenConfigureFile `delegates.xml' # warning/configure.c/GetConfigureOptions/677.
convert: NoDecodeDelegateForThisImageFormat `PNG' # error/constitute.c/ReadImage/560.
convert: NoImagesDefined `/tmp/resize.png' # error/convert.c/ConvertImageCommand/3235.
at ChildProcess.onExit (/var/task/node_modules/gm/lib/command.js:301:17)
at emitTwo (events.js:126:13)
at ChildProcess.emit (events.js:214:7)
at maybeClose (internal/child_process.js:925:16)
at Socket.stream.socket.on (internal/child_process.js:346:11)
at emitOne (events.js:116:13)
at Socket.emit (events.js:211:7)
at Pipe._handle.close [as _onclose] (net.js:567:12) code: 1, signal: null }
2) Node 10.x
{ Error: Command failed: var/task/imagemagick/bin/convert: error while loading shared libraries: libgomp.so.1: cannot open shared object file: No such file or directory
at ChildProcess.onExit (/var/task/node_modules/gm/lib/command.js:301:17)
at ChildProcess.emit (events.js:189:13)
at ChildProcess.EventEmitter.emit (domain.js:441:20)
at maybeClose (internal/child_process.js:970:16)
at Socket.stream.socket.on (internal/child_process.js:389:11)
at Socket.emit (events.js:189:13)
at Socket.EventEmitter.emit (domain.js:441:20)
at Pipe._handle.close (net.js:597:12) code: 127, signal: null }

What is the best way to configure the pkg option scripts in electron-builder?

I am using the pkg option in my package.json configuration for electron-builder. I have some preinstall scripts that I want to run upon installation. I have followed the electron-builder instructions in order to set where the scripts are, but npm can't seem to find the files correctly.
I have tried changing ownership of files and folders with chmod +x file command, but the error persits.
This is the bit of code about pkg scripts configuration:
"build": {
"mac": {
"target":[
"pkg"
]
},
"pkg":{
"scripts":"build/pkg-scripts"
}
}
The error message I get is:
Error: Exit code: 1. Command failed: pkgbuild --root /Users/user/myApp/myApp-out/electron-builder/mac --component-plist /Users/user/myApp/myApp/electron-builder/com.dessci.myApp.plist --install-location /Applications --scripts /Users/user/myApp/build/build/pkg-scripts /Users/user/mtdesktop/mtdesktop-out/electron-builder/com.dessci.myApp.pkg
pkgbuild: error: Cannot write package to "/Users/user/mtdesktop/mtdesktop-out/electron-builder/com.dessci.myApp.pkg". (The file “pkg-scripts” couldn’t be opened.)
pkgbuild: Reading components from /Users/user/mtdesktop/mtdesktop-out/electron-builder/com.dessci.myApp.plist
pkgbuild: Adding component at myApp.app
pkgbuild: error: Cannot write package to "/Users/user/mtdesktop/mtdesktop-out/electron-builder/com.dessci.myApp.pkg". (The file “pkg-scripts” couldn’t be opened.)
at /Users/user/mtdesktop/node_modules/builder-util/src/util.ts:126:16
at ChildProcess.exithandler (child_process.js:306:5)
at ChildProcess.emit (events.js:193:13)
at maybeClose (internal/child_process.js:999:16)
at Process.ChildProcess._handle.onexit (internal/child_process.js:266:5)
From previous event:
at /Users/user/mtdesktop/node_modules/graceful-fs/graceful-fs.js:111:16
at /Users/user/mtdesktop/node_modules/graceful-fs/graceful-fs.js:45:10
at FSReqCallback.args [as oncomplete] (fs.js:145:20)
From previous event:
at PkgTarget.buildComponentPackage (/Users/user/mtdesktop/node_modules/app-builder-lib/src/targets/pkg.ts:115:113)
at /Users/user/mtdesktop/node_modules/app-builder-lib/src/targets/pkg.ts:57:12
at Generator.next (<anonymous>)
From previous event:
at PkgTarget.build (/Users/user/mtdesktop/node_modules/app-builder-lib/src/targets/pkg.ts:31:42)
at Function.buildAsyncTargets (/Users/user/mtdesktop/node_modules/app-builder-lib/src/platformPackager.ts:140:36)
at MacPackager.packageInDistributableFormat (/Users/user/mtdesktop/node_modules/app-builder-lib/src/platformPackager.ts:119:24)
at nonMasPromise.then.then (/Users/user/mtdesktop/node_modules/app-builder-lib/src/macPackager.ts:90:26)
at processImmediate (internal/timers.js:443:21)
From previous event:
at /Users/user/mtdesktop/node_modules/app-builder-lib/src/macPackager.ts:90:10
at Generator.next (<anonymous>)
From previous event:
at MacPackager.pack (/Users/user/mtdesktop/node_modules/app-builder-lib/src/macPackager.ts:80:95)
at /Users/user/mtdesktop/node_modules/app-builder-lib/src/packager.ts:430:24
at Generator.next (<anonymous>)
at xfs.stat (/Users/user/mtdesktop/node_modules/fs-extra-p/node_modules/fs-extra/lib/mkdirs/mkdirs.js:56:16)
at /Users/user/mtdesktop/node_modules/graceful-fs/polyfills.js:285:20
at FSReqCallback.oncomplete (fs.js:159:5)
From previous event:
at Packager.doBuild (/Users/user/mtdesktop/node_modules/app-builder-lib/src/packager.ts:396:24)
at /Users/user/mtdesktop/node_modules/app-builder-lib/src/packager.ts:366:57
at Generator.next (<anonymous>)
at /Users/user/mtdesktop/node_modules/graceful-fs/graceful-fs.js:111:16
at /Users/user/mtdesktop/node_modules/graceful-fs/graceful-fs.js:45:10
at FSReqCallback.args [as oncomplete] (fs.js:145:20)
From previous event:
at Packager._build (/Users/user/mtdesktop/node_modules/app-builder-lib/src/packager.ts:335:133)
at /Users/user/mtdesktop/node_modules/app-builder-lib/src/packager.ts:331:23
at Generator.next (<anonymous>)
at processImmediate (internal/timers.js:443:21)
From previous event:
at Packager.build (/Users/user/mtdesktop/node_modules/app-builder-lib/src/packager.ts:288:14)
at build (/Users/user/mtdesktop/node_modules/app-builder-lib/src/index.ts:59:28)
at build (/Users/user/mtdesktop/node_modules/electron-builder/src/builder.ts:227:10)
at then (/Users/user/mtdesktop/node_modules/electron-builder/src/cli/cli.ts:46:19)
You need to provide the relative path of pkg-scripts for scripts value.
package.json
"build": {
"appId": "com.audio.application",
"productName": "Audio-App",
"artifactName": "${productName}-Setup-${version}.${ext}",
"copyright": "Copyright © 2020 Audio Corp",
"mac": {
"category": "com.audio.application",
"target": [
"pkg"
],
"icon": "dist",
"identity": "identity",
"darkModeSupport": true,
"hardenedRuntime": true,
"gatekeeperAssess": false,
"artifactName": "${productName}.${ext}"
},
"pkg": {
"scripts": "../build/pkg-scripts",
"installLocation": "/Applications",
"background": {
"file": "build/icon/sound.png",
"alignment": "bottomleft"
},
"allowAnywhere": true,
"allowCurrentUserHome": true,
"allowRootDirectory": true,
"license": "build/license.html",
"welcome": "build/resources/welcome.txt",
"conclusion": "build/resources/conclusion.txt",
"isVersionChecked": true,
"isRelocatable": false,
"overwriteAction": "upgrade"
},
"directories": {
"buildResources": "release",
"output": "release"
}
},
folder structure of build directory -
(which at root level of project)
preinstall.sh -
(make sure you have run chmod +x on this file to make it executable)
#!/bin/sh
echo "Executing preinstaller script for custom installer"
# Deleting App components from Application folder.
echo "Deleting Audio-Configration Logs"
logsPath=~/Library/Logs/Audio-Configration
if [ -d "$logsPath" ]
then
rm -rf ~/Library/Logs/Audio-Configration
echo "***Deleted Audio-Configration Logs Successfully***"
fi
echo "Deleting Audio-Configration Application Support"
applicationSupportPath=~/Library/Application\ Support/Audio-Configration
if [ -d "$applicationSupportPath" ]
then
rm -rf ~/Library/Application\ Support/Audio-Configration
echo "***Deleted Audio-Configration Application Support Successfully***"
fi
echo "Finished:preflight"
exit 0
Above pre-installation script is to make sure when user re-install the app, need to delete the previous Logs and application related content in Application Support directory
"scripts":"build/pkg-scripts"
You don't need this line as it is a default directory for storing scripts commands, in the case you have define it, you need to create a directory name in ../build/pkg-scripts/ in your app directory and only file name "preinstall" and "postinstall" are allowed in this directory, otherwise electron-builder will get error why running the release function.

Resources