Reputation: 162
I have expressJs application i am going to save image in PDF file Here is my code to save image in pdf file
exports.getPDF = function (req, res) {
doc = new PDF();
doc.pipe(fs.createWriteStream('./public/Competition.pdf'));
db.competitions.find({where: {id: req.params.competitionId}}).success(function(competition){
//MY_URL_IS = http://d16dgegkincj5i.cloudfront.net/1_2_brandLogo_new_update_code-thumbnail.png
var options = {
host: 'http://d16dgegkincj5i.cloudfront.net',
port: 80,
path: '/1_2_brandLogo_new_update_code-thumbnail.png'
}
var request = http.get(options, function(res){
var imagedata = ''
res.setEncoding('binary')
res.on('data', function(chunk){
imagedata += chunk
})
res.on('end', function(){
fs.writeFile('./public/Competition.pdf', imagedata, 'binary', function(err){
if (err) throw err
console.log('File saved.')
})
})
})
}).error(function(error){
});
};
I am getting error
events.js:85
throw er; // Unhandled 'error' event
^
Error: getaddrinfo ENOTFOUND
when i get image by http.get and after write to pdf file it will give error and not writing image
Upvotes: 1
Views: 175
Reputation: 7862
The error:
Error: getaddrinfo ENOTFOUND
means that the url was not found.
Just removing the http://
from your host should do the job, like:
var options = {
host: 'd16dgegkincj5i.cloudfront.net',
port: 80,
path: '/1_2_brandLogo_new_update_code-thumbnail.png'
}
Upvotes: 2