I'm using html-pdf to generate dynamic pdf file with html.
But pdf file is ugly, I think the reason is dpi not good.
I tried setting:
var options = {
dpi: 96,
};
pdf.create(htmlTemplate, options).toStream(function(err, stream){
if (err) {
return res.end(err.stack)
}
res.setHeader('Content-type', 'application/pdf');
stream.pipe(res);
});
But no lucky. It still not work. I changed the value of dpi many times but pdf file looks the same.
Anyone can help me? Thank you!
Related
I have a little problem with node js.
If I save a file with fs.writeFile it saves but the file has no content, but if I read the file and print the content there is content in the file
Thats the node js code to save the file:
let sjson = JSON.parse(fs.readFileSync("./saves/User_VipList.json", "utf8"));
sjson['users'].push({"name": args[0]});
fs.writeFile("./saves/User_VipList.json", JSON.stringify(sjson), (err) => {
if(err) throw err;
});
Thats how i read the content:
let sjson = JSON.parse(fs.readFileSync("./saves/User_VipList.json", "utf8"));
console.log(sjson);
And that's the JSON file:
{
"users": []
}
But in users should be content because
Hope you guys can help me
Due to async nature looks like your program doesnt wait when file write ends occurs, so please try to use fs.writeFileSync instead, for example:
try {
fs.writeFileSync("./saves/User_VipList.json", JSON.stringify(sjson))
} catch(err) {
console.log('Error writing file:', err)
}
Found the Problem
Oh i think i found the problem if i start node index.js with a bat file the problem happends but if i start node manually over cmd it works.
Didn't mention the Bat file because i didn't think thats could be a problem :)
But why is that a problem?
I'm using fs.writeFileSync(file, data[, options]) to save a file returned from http.get(options[, callback])
This works fine for text files but images, pdfs etc end up being corrupted. From the searching around that I've done, it's apparently because fs.writeFileSync(file, data[, options]) defaults to UTF-8
I've tried setting the encoding to 'binary', the mime-type and the extension to no avail. It feels like something really obvious that I'm overlooking, can anyone point me in the right direction?
Thank you in advance
Update
I'm running this through electron. I didn't think it was worth mentioning as electron is just running node, but I'm not a node or electron expert so I'm not sure
Create a Buffer from the image data and set its encoding to binary. Then pass that data into a stream.PassThrough and pipe that into a stream.Writable.
var fs = require('fs');
var stream = require('stream');
var imgStream = new stream.PassThrough();
imgStream.end(Buffer.from(data, 'binary'));
var wStream = fs.createWriteStream('./<dest>.<ext>');
imgStream.once('end', () => {
console.log('Image Written');
});
imgStream.once('error', (err) => {
console.log(err);
});
imgStream.pipe(wStream);
I'm trying to create a simple dynamic-badge (png) to embed in static pages to let know the status of my application.
I'd like so to use an existing PNG image and write on it some text with Node.js.
I've found lot of libraries but all of them use Imagemagick or Cairo as native dependencies, I'd like to avoid to install anything else on the server.
I've then found lwip, but I can't really understand how to write text on an image with it. How can I do?
You could use Jimp. It has a print method:
var Jimp = require("jimp");
var fileName = 'test.png';
var imageCaption = 'Image caption';
var loadedImage;
Jimp.read(fileName)
.then(function (image) {
loadedImage = image;
return Jimp.loadFont(Jimp.FONT_SANS_16_BLACK);
})
.then(function (font) {
loadedImage.print(font, 10, 10, imageCaption)
.write(fileName);
})
.catch(function (err) {
console.error(err);
});
If you want to use a ttf file you can use gm
This will also align the text automatically so you don't have to keep track of your letter sizing / location.
const gm = require('gm').subClass({imageMagick: true});
gm('path/to/image.png')
.fill("#FFFFFF")
.font("./font.ttf", 20)
.drawText(15, 10, "your text", 'southeast') //can be any location
.write("./output.png", function (err) {
if (!err) console.log('done');
});
I'm using http.request to download JPEG file. I am then using fs.writeFile to try to write the JPEG file out to the hard drive.
None of my JPEG files can be opened, they all show an error (but they do have a file size). I have tried all of the different encodings with fs.writeFile.
What am I messing up in this process?
Here's what the working one is showing when viewing it raw:
And here is what the bad one using fs.writeFile is showing:
Figured it out, needed to use res.setEncoding('binary'); on my http.request.
Thank you, looking to the previous response, I was able to save de media correctly:
fs.writeFile(
filepath + fileName + extension,
mediaReceived, // to use with writeFile
{ encoding: "binary" }, // to use with writeFile ***************WORKING
(err) => {
if (err) {
console.log("An error ocurred while writing the media file.");
return console.log(err);
}
}
);
So my issue is basically what the title says. When I run resize on a set of images i get no error but I also get no images in the final directory. I made the permissions on the directory 777 just in case that was the issue. I can use imagemagick to read the metadata from the images but it won't resize them. Can you find a simple, or huge, mistake I've made?
var im = require('imagemagick');
im.resize({
srcPath: srcPath,
destPath: destPath,
width: 256,
height: 256
}, function(err, stdout, stderr) {
if (err) {
console.log(err);
} else {
console.log("Image Resized");
}
});
All I ever get out is "Image Resized" out on the console.
Thanks in advance for any help and sorry if this is vague or I missed something.
I feel like an idiot. The problem is the parameter is 'dstPath' not 'destPath'