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
Related
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
there! I am developing app with Next js. and i need to run php script to get some value.
For executing php code I use '"exec-php' package from npm. this package requires enter php file path to js function. But when I put path as parameter to function and call this function, next js throws error - unhandledRejection: Error: File './script.php' not found.
execPhp('./script.php', (err, php, out) => {
php.test(obj, process.env.KEY, (err, result, output, printed) => {
signature = result;
});
});
If you have idea how to solve please answer. Thanks~
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}`);
}
);
});
I am using this block of code to create and write a new directory and a file.
I just started to learn nodejs
var lib = {};
lib.baseDir = path.join(__dirname,'/../.data/');
lib.create = function(dir,file,data,callback){
fs.open(lib.baseDir+dir+'/'+file+'.json', 'wx', function(err, fileDescriptor){
if(!err && fileDescriptor){
var stringData = JSON.stringify(data);
// Write to file and close it
fs.writeFile(fileDescriptor, stringData,function(err){
if(!err){
fs.close(fileDescriptor,function(err){
if(!err){
callback(false);
} else {
callback('Error closing new file');
}
});
} else {
callback('Error writing to new file'+'lib.baseDir');
}
});
} else {
callback(err);
}
});
};
but I am repeatedly getting the error
{ Error: ENOENT: no such file or directory, open 'C:\Users\Jawahr\Documents\GitHub\node-api\.data\test\newFile.json'
errno: -4058,
code: 'ENOENT',
syscall: 'open',
path: 'C:\\Users\\Jawahr\\Documents\\GitHub\\node-
api\\.data\\test\\newFile.json' }
calling this library in a index.js as
var _data = require('./lib/data');
_data.create('test','newFile', {"asdksadlk" : "asldj"} ,function(err) {
console.log('this was the error ',err);
});
I've been stuck here for a while, is it because the pathname and filename contained the part "C:" which has colon a reserved char in windows 10, if it is the problem how to solve this.
using windows 10 and NodeJs 8.6.
Can you try this -
fs.open(lib.baseDir+dir+'/'+file+'.json', 'w', function(err, fileDescriptor){
It looks like 'wx' throws an error if file exists -
'w' - Open file for writing. The file is created (if it does not exist) or truncated (if it exists).
'wx' - Like 'w' but fails if the path exists.
'w+' - Open file for reading and writing. The file is created (if it does not exist) or truncated (if it exists).
'wx+' - Like 'w+' but fails if the path exists.
Referred from here
Looks like your put non-existing or non-accessible path to your file. Look:
fs.open('/path/is/not/exists/xx.js','wx',(err,fd)=>{
if (err) {
console.log(err.message);
}
});
and got
Error: ENOENT: no such file or directory, open '/path/is/not/exists/xx.js'
In case when file is already exists you will got something like Error: EEXIST: file already exists, open '...'
And last, but not least. Instead of lib.baseDir+dir+'/'+file+'.json' better solution is use path.join(lib.baseDir,dir,file+'.json') from path module
Add a check for directory or create before fs.open
if (!fs.existsSync(dir)){
fs.mkdirSync(dir);
}
Then the rest of your code will work. as fs.open only creates file if it doesn't exist, it does not create directory
I'm using the html-pdf module to generate pdfs, using the following code:
var ejs = require('ejs');
var fs = require('fs');
var pdf = require('html-pdf');
var build = function (template, data) {
var html = ejs.compile(fs.readFileSync(template, 'utf8'));
html = html(data);
return html;
}
pdf.create(build('views/print_templates/pm_export_pdf.ejs',
{rows:resolve(rows, user_rows, table_rows,
subproject_rows, milestone_rows, release_rows)}), pdfconfig)
.toFile(function (err, pdf) {
if (err)
return console.log(err);
res.sendFile(pdf.filename);
});
PDFconfig is the config variable for html-pdf. And resolve is my own db resolve function, which is not very relevant in this story.
When I run this locally on OSX 10.10 on my MacBook Pro this works like a charm. But when I run this on my server(centOS), I get the following error:
events.js:72
throw er; // Unhandled 'error' event
^
Error: write EPIPE
at errnoException (net.js:904:11)
at Object.afterWrite (net.js:720:19)
Has this something to do with permissions maybe? I'm not really sure what I'm doing wrong..
I run the following versions:
Node: 0.10.31
Express: 4.10.7
html-pdf: 1.2.0
For future purposes:
I figured that I had to rebuild my node-modules. I think I accidentally copied the node-modules folder over to the test server on centOS, and phantomjs had build it's dependencies for OSX. I'm still not sure how the error that it threw correspondents with that, but it fixed it.
Also be sure that you have FreeType and fontconfiglib installed.