How to print a set of PDF Files in using node.js - javascript

Hi I want to print(using printer) list of PDF files using nodejs. But not able to find any proper way. I found one JavaScript library called print.js(http://printjs.crabbly.com)
But with that also I m not able to call it in loop.
Is there anything I can do for this.
var pdflist = [a.pdf,b.pdf] //(this is my PDF list)
Thanks for help.

You can use ghostscript command line tools from your node app by forking a child process to execute the commands and loop thru your pdfs.
// OS : windows 64bits (for other OSs : linux, macosx ...etc; it's almost the same thing)
//assuming here that pdf is the path string to your pdf file
//printer name : Apple LaserWriter II NT
pdflist.foreach( function (pdf,index){
require("child_process").exec('gswin64c.exe ... -sOutputFile="%printer%Apple LaserWriter II NT" ' + pdf,
(error, stdout, stderr) => {
if (error) {
console.error(`exec error: ${error}`);
return;
}
console.log(`stdout: ${stdout}`);
console.log(`stderr: ${stderr}`);
}
);
});

Related

How can I access the crontab file with Node.js?

I am currently developing an Electron app and need it to look into the crontab file and parse it.
How can Node.js (or something else in Electron) access the main crontab file and read it?
I looked into the cron-parser library but it is only able to read cron-formatted strings, not access the file.
Thanks in advance.
Since the crontab file we see when we run is just a tmp file that shows whatever cron jobs the current user has created (which are stored in /var/spool/cron/crontabs) when whe don't normally have access to those files, I'd suggest you run a shell script to get these data:
const { exec } = require("child_process");
exec("crontab -l", (error, stdout, stderr) => {
if (error) {
console.log(`error: ${error.message}`);
return;
}
if (stderr) {
console.log(`stderr: ${stderr}`);
return;
}
//Do stuff with the read string on stdout
});
Hopefully this should give you the contents of the crontab under the user that's running the node script

ghostscript conversion not creating output file when run in local environment

I am trying to run a simple script to convert a PDF --> PNG using gs package. The code runs successfully runs when deployed to Firebase Cloud Functions but I test it locally using the firebase functions:shell command it appears to run w/o error but the output file (the .png) is missing and I get the following error:
Error: ENOENT: no such file or directory, open '/var/folders/sr/pjwr5d0s75bgzvx4xgcdcylw0000gp/T/*****/-***********.png'
The path is correct, but the .png file is missing but the pdf is there and gs outputted no errors. I even tried changing the path from os.tmpdir() to a specific path on my desktop (Mac) and it still failed to create the png file.
The same code when deployed to firebase runs fine.
Here is my gs code (I have ommitted the CF code and invocation code):
//============== Ghostscript
//Conver PDF -> PNG
console.log('gs - starting');
gs()
.batch()
.nopause()
.option('-r' + 50 * 2)
.option('-dDownScaleFactor=2')
.executablePath('lambda-ghostscript/bin/./gs')
// .device('png16m')
.device('pngalpha')
.output(outputPath)
.input(pdfPath)
.exec(function (err, stdout, stderr) {
if (!err) {
console.log('gs executed w/o error');
console.log('stdout',stdout);
console.log('stderr',stderr);
console.log('output saved to: ', outputPath);
resolve(outputPath);
} else {
console.log('gs error:', err);
reject();
}
});
})
I have no idea why it doesn't work locally but does when deployed. My path input is correct and the output is path.join(os.tmpdir(), '****.png');
Edit Output from GS in console:
> gs executed w/o error
> stdout
> stderr
> output saved to: /var/folders/sr/pjwr5d0s75bgzvx4xgcdcylw0000gp/T/*******/*********.png

Not able to open file explorer ,on clicking a button

my code in node js side is-
const exec = require('child_process').exec;
var cmdString = 'start C:/myApp/dropbox';
const child = exec(cmdString,
(error, stdout, stderr) => {
console.log(`stdout: ${stdout}`);
console.log(`stderr: ${stderr}`);
if (error !== null) {
console.log(`exec error: ${error}`);
}
})
if i start the application from command prompt, I am able to open windows file explore on clicking a button.
But, if i start the application from windows service. I am not able to open file explorer.
Any idea what mistake i am doing in my coding side.

How to call Shell script or python script in from a Atom electron app

I'm trying to use the Atom electron to write a Desktop App for both Mac and Windows.
What I need here is :
A button.
And when the user click the button it runs the following shell (or python script):
ping x.x.x.x
And the result will be displayed in a TextArea.
I tried to use [shelljs] and [yargs] but it seems like it is not workable with Atom electron.
All I want is to use JAVASCRIPT to write Desktop App (with GUI of course) that calls some script (shell && python) to do some automation work.
Any suggestion will be appreciated, thanks :)
It can be done directly with Node, you can use the child_process module. Please notice this is asynchronous.
const exec = require('child_process').exec;
function execute(command, callback) {
exec(command, (error, stdout, stderr) => {
callback(stdout);
});
};
// call the function
execute('ping -c 4 0.0.0.0', (output) => {
console.log(output);
});
I encourage you to also have a look at npm, there are tons of modules that could help you to do what you want, without calling a python script.
Try node-powershell npm. You can directly execute shell script commands and display result.
var shell = require('node-powershell')
var ps = new shell()
ps.addCommand('ping -c 4 0.0.0.0')
ps.invoke()
.then(function (output) {
console.log(output)
})
.catch(function (err) {
console.log(err)
ps.dispose()
})
See: https://www.npmjs.com/package/node-powershell
you could use child_process to archive what you are trying to do by using the following code
var exec = require('child_process').exec
function Callback(err, stdout, stderr) {
if (err) {
console.log(`exec error: ${err}`);
return;
}else{
console.log(`${stdout}`);
}
}
res = exec('ping xxx.xxx.xxx', Callback);

ssh persistent sessions with node.js

I am working with node.js, and I am trying to embed a console in the web browser to work in a remote server. The web application do the connection so the user does not need to do the ssh username#host but only type commands.
I have tried the node.js' ssh2 module and other modules which use ssh2. But I'm experiencing always the same problem. Every time I execute a command programmatically using exec(), the ssh session is restarted. I'll explain it better with an example.
> ls
returns the content of home directory, one of the directories in the home directory is mydir
> cd mydir
> ls
returns the content of my home directory again, because after a command is executed the ssh session is closed/restarted.
Any node.js library which can do the job? or even a library of other technology different to javascript?
Edit: Other example for clarifying, using the node.js' module ssh-exec
The server has to execute some commands in other machine using ssh. A function in the server contains the following code
var c = exec.connection('username#host.com'); // It takes the ssh key from the default location
exec('cd mydir', c).pipe(process.stdout);
exec('ls -lh', c).pipe(process.stdout);
As you can see I am not ending the connection after the first exec but the output I obtain is the content of the home directory not the content of mydir directory, because the ssh session is reset after each exec.
The maintainer of node.js' ssh2 module provided the solution.
To use the method shell() instead of the method exec().
The method shell() creates an interactive session with the server we are connecting.
The method shell() provides a stream as a parameter of its callback (like the method exec()).
Like when using exec(), stream.on('data', function(data, extended) {...}); can be used to get the output of the commands. However, in this case, to provide commands (input) to the machine you connected with, you need to use stream.write(yourcommand+'\n');
PS. Feel free to edit to improve the accuracy of the answer.
I have to guess a bit, but you do something like child = exec('ssh username#host ls')?
You can do something like
child = exec('ssh username#host');
upfront and in the "loop" of your browser
child.stdin.write('ls\n');
When finished, just close stdin:
child.stdin.end()
which also finishes the child process.
I know this link is old but I figured this may help someone if they're looking for a solution. The
To use the method shell() instead of the method exec().
Works. Here's another solution. Use absolute file paths i.e.
conn.exec("mkdir -p /home/user/Direc/{one,two,three}/", function(err, stream) {
if (err) throw err;
stream.on('data', function(data) {
console.log('STDOUT: ' + data);
}).stderr.on('data', function(data) {
console.log('STDERR: ' + data);
});
});
conn.exec("ls -la /home/user/", function(err, stream) {
if (err) throw err;
stream.on('close', function(code, signal) {
console.log('Stream :: close :: code: ' + code + ', signal: ' + signal);
conn.end();
}).on('data', function(data) {
console.log('STDOUT: ' + data);
}).stderr.on('data', function(data) {
console.log('STDERR: ' + data);
});
});

Categories

Resources