marcbachmann / node-html-pdf

This repo isn't maintained anymore as phantomjs got dreprecated a long time ago. Please migrate to headless chrome/puppeteer.
MIT License
3.56k stars 545 forks source link

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

Open aldopj opened 2 years ago

aldopj commented 2 years ago

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" }); } });

michalzan commented 2 years ago

same issue here

d9media commented 2 years ago

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.

michalzan commented 2 years ago

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

jdextraze commented 2 years ago

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
dhaval5190 commented 11 months ago

this worked for me