node-html-pdf icon indicating copy to clipboard operation
node-html-pdf copied to clipboard

Generated PDF file page size/zoom is different from the actual html

Open aldopj opened this issue 4 years ago • 5 comments

I created a pdf from a simple html. i used 'mm' for units for page size as well as a div inside it. i put a 120mm div inside the 240mm page. The div should have been half the width of the page but its more like 80%. I tried zoomFactor, but no change in output. Below is the code:

html = "

" + "" + "
" + Date.now() + "
" + "
"; fs.writeFile("test.html", html);

const options = { height: "140mm", // allowed units: mm, cm, in, px width: "240mm", type: "pdf", };

pdf.create(html, options).toStream(function (err, stream) { if (err) { res.status(500); } else { stream.pipe(fs.createWriteStream("./foo.pdf")); res.status(200).json({ text: "Done" }); } });

aldopj avatar Dec 27 '21 00:12 aldopj

same issue here

michalzan avatar Jan 03 '22 09:01 michalzan

Just installed the package and ran it, same issue. Trying to find a workaround now.

EDIT: Looks like the page size is not respected at all. I've tried setting size manually (210mm x 297mm) or A4 but the actual PDF comes back to 8.26i x 11.69in

Looks like this is a know issue with phantomjs: https://github.com/ariya/phantomjs/issues/12685 You need to experiment to make the page size work. In my case, passing format as "A3" resulted in almost the correct aspect ratio.

d9media avatar Jan 13 '22 22:01 d9media

this "solved" it in case too, but be aware that when you try to print it, the size is incorrect again - it can be solved by tinkering with ratio and page size in printer setting, so it's not a huge deal, but it my affect some business scenarios, so it's best to keep that in mind

michalzan avatar Jan 17 '22 09:01 michalzan

I had the same issue but not on my local machine. This led me to discover that I had phantomjs installed on my device. So the issue seems to be with the phantomjs-prebuilt package somehow since when you have phantomjs installed already it doesn't install it's own. To test this:

  1. Remove your node_modules folder: rm -rf node_modules
  2. Install phantomjs: sudo apt install phantomjs
  3. Re-install npm package: npm i

jdextraze avatar Jan 19 '22 17:01 jdextraze

this worked for me

dhaval5190 avatar Sep 22 '23 06:09 dhaval5190