tried to use several libraries for pdf generation but I catch always one error:
Fatal Error: spawn UNKNOWN
code, something like is:
mammoth.convertToHtml({
path: './backend/common/template.docx'
})
.then(function (result) {
var html = result.value; // The generated HTML
pdf.create(html).toFile("./backend/common/vvvv.pdf", function (err, res) {
if (err) {
callback(err);
console.log(err);
return;
}
callback(null, res);
});
var messages = result.messages; // Any messages, such as warnings during conversion
console.log(messages);
})
.done();
Help please, what is wrong
I think you miss the import
var pdf = require('html-pdf');
Make sure you have installed the html to pdf converter through node and imported/required the file!
Related
How to read the data of a file(say, read.txt) without getting the buffered Data?
I mean I want to get the content of the file directly without getting the buffer data?
and hoe to delete the folder in Node.js?
// Try this //
fs = require('fs')
fs.readFile('/read.txt', 'utf8', function (err,data) {
if (err) {
return console.log(err);
}
console.log(data);
});
try {
fs.rmdirSync(dir, { recursive: true });
console.log(`${dir} is deleted!`);
}catch (err) {
console.error(`Error while deleting ${dir}.`);
}
I would like to create an app that allows me to easily modify the metadata of any given file.
Is there a way to modify the metadata of any given file type?
Are there any NPM packages that facilitate this?
You can use node-ffmetadata npm package for reading & writings file properties.
var ffmetadata = require("ffmetadata");
// Read song.mp3 metadata
ffmetadata.read("song.mp3", function(err, data) {
if (err) console.error("Error reading metadata", err);
else console.log(data);
});
// Set the artist for song.mp3
var data = {
artist: "Me",
};
ffmetadata.write("song.mp3", data, function(err) {
if (err) console.error("Error writing metadata", err);
else console.log("Data written");
});
connection.execute(
`select SSG_RESP_XML from SSG_SR_RESP_DTLS where SERV_PROV_REQ_ID='141220181657' and SSG_RESP_TYPE='NOTIFICATION'`,
(err, result) => {
if (err) {
console.error(err.message);
doRelease(connection);
return;
}
var lob = result.rows[0][0];
console.log(lob);
fs.writeFile('xd.xml',lob, {encoding: 'utf8'}, function (err) {
return doRelease(connection);
});
});
I need to save the xml to a file as well as parse and use it in the further lines in the program.
In xd.xml file [object Object] is seen.
`console.log(lob)` I receive the following
How to fetch the xml from the object?
Thanks in advance.
I would explore two ways.
Use fetchAsString method from oracledb package 1.12.1
Cast your LOB to varchar2
select TO_CHAR(SSG_RESP_XML) from SSG_SR_RESP_DTLS where SERV_PROV_REQ_ID='141220181657' and SSG_RESP_TYPE='NOTIFICATION'
I have a Node application, within which I am using Graphics Magick to do some image/pdf manipulation.
I have the following code which calls mosaic() to combine a pdf and png. If I export the result as a png then the process is successful. However if I try to export the result as a pdf then the resulting pdf file does have a size, but opening it shows that there is nothing to see, it appears to be blank. No errors are thrown.
var newFileName = "result.pdf";
gm()
.in('-page', '+0+0')
.in('C:\\Code\\ProjectName\\src\\api\\test\\TestTemplatePDF.pdf')
.in('-page', '+103+70')
.in('C:\\Code\\ProjectName\\src\\api\\test\\pic1.png')
.mosaic()
.stream('pdf', (err, stdout, stderr) => {
if (err) console.log('stream error', err);
console.log('stream');
var writeStream = fs.createWriteStream('./etc/' + newFileName);
stdout.pipe(writeStream);
stderr.on('end', () => {
fs.readFile('./etc/streamError.txt', (err, data) => {
console.log('reading errorStream');
// if (err) console.error(err);
if (err) {
console.log('We found an error reading streamError.txt', err);
res.send(err);
} else if (data.length !== 0) {
console.log('streamError.txt should contain a detailed error message', data);
res.send(data);
} else {
console.log('streamError.txt contains no errors');
}
});
});
stdout.on('end', () => {
fs.readFile('./etc/' + newFileName, (err, data) => {
if (err) {
console.log("stdout error: " + err);
res.end();
} else {
console.log('Successfully read our new image file');
}
})
})
})
Output/console shows:
stream
reading errorStream
streamError.txt contains no errors
successfully read our new file
In the end this problem went away when I converted the pdf to a png before editing.
Presumably the conclusion is that when using mosaic() that they need to be the same type.
How can I append data to a file using node.js
I already have a file named myfile.json with data. I want to check if the file name exists and then append some data to that file.
I'm using following code
var writeTempFile = function (reportPath, data, callback) {
fs.writeFile(reportPath, data, function (err) {
//if (err) //say(err);
callback(err);
});
}
writeTempFile(reportDir + '_' + query.jobid + ".json", data, function (err) {
context.sendResponse(data, 200, {
'Content-Type': 'text/html'
});
You can use jsonfile
var jf = require('jsonfile');
var yourdata;
var file = '/tmp/data.json';
jf.readFile(file, function(err, obj) {
if(!err) {
var finalData = merge(obj, yourdata);
jf.writeFile(file, finalData, function(err) {
console.log(err);
});
}
});
You need to implement your merging logic in merge(object1, object2)
https://npmjs.org/package/jsonfile
Check out the following code.
function addToFile(reportPath, data, callback){
fs.appendFile(reportPath, data, function (err) {
callback(err);
});
}
Node offers fs module to work with file system.
To use this module do
var fs = require('fs')
To append some data to file you can do:
fs.appendFile('message.txt', 'data to append', function (err) {
if (err) throw err;
console.log('The "data to append" was appended to file!');
});
Node offers you both synchronous and asynchronous method to append data to file, For more information please refer to this documentation