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

The PDF content exported on the Linux server is magnified by about 30%.

Open Alex-Credifranco opened this issue 6 years ago • 18 comments

The PDF content exported on the Linux server is magnified by about 30%. It's good to export in Windows development environment. Does anyone have a problem? Ask God for help.

Alex-Credifranco avatar Aug 09 '19 07:08 Alex-Credifranco

@Alex-Credifranco I have the same problem ... is this project still alive?

giona69 avatar Aug 12 '19 19:08 giona69

Also experiencing this.

I'm on version 2.1.0.

VenomousMaterial avatar Aug 14 '19 21:08 VenomousMaterial

Until there is an official fix for this, I was able to get around it, for the most part, by adding a style of zoom: 0.75 to the tag of the document I was trying to convert. It isn't very precise, so more detailed documents might need a better fix, but it did the trick for my use case.

VenomousMaterial avatar Aug 14 '19 21:08 VenomousMaterial

Add to that HTML CSS with exact font size in px.

p, table tr td, table tr th, li, ol {
    font-size:10px;
    font-family: Impact;
    line-height: 2;
}

Also, make sure no other CSS is applied like bootstrap. Clean of all CSS and create your own fully control everything in PDF.

Serhioromano avatar Aug 21 '19 04:08 Serhioromano

@Serhioromano Hello, how many PX should A4 paper be set in the project. var options = { "height": "1160px", // allowed units: mm, cm, in, px "width": "820px" }; I set it up like this. But the generated PDF size width: 1095px

Alex-Credifranco avatar Sep 26 '19 07:09 Alex-Credifranco

Here is my config

var options = {
	format: "A4",
	border: {
		top: "5mm",
		right: "5mm",
		bottom: "5mm",
		left: "5mm"
	},
	header: {
		height: "5mm",
		contents: '<div class="header">Автор: Сергей Романов</div>'
	},
	footer: {
		height: "5mm",
		contents: {
			first: " ",
			last: '<span class="footer">Стр. {{page}}</span>',
		}
	}
};

Serhioromano avatar Oct 09 '19 06:10 Serhioromano

@Serhioromano I know the reason, but I don't know how to solve it. The reason is that the dpi of Linux and Windows export is different, the dpi of windows is 90, and the dpi of Linux is 72.

Alex-Credifranco avatar Oct 09 '19 09:10 Alex-Credifranco

same here, @Alex-Credifranco are using zoom 0.75 solution?

damianijr avatar Oct 09 '19 09:10 damianijr

zoom: 0.753 was the pixel-perfect workaround that I used, following thread to know when an official fix will release

damianijr avatar Oct 09 '19 10:10 damianijr

@damianijr is "zoomFactor": "0.753"?Did not find zoom in the document.

Alex-Credifranco avatar Oct 11 '19 10:10 Alex-Credifranco

html { zoom: 0.753 }

@Alex-Credifranco

damianijr avatar Oct 11 '19 17:10 damianijr

Same problem

nohaibogdan1 avatar Jan 06 '20 15:01 nohaibogdan1

After a research I found that under the hood, phantomjs uses the default system screen resolution. This is not viewportSize and viewportSize must be smaller than this system limitation.

Say in local you have a 1920x1080 screen with Xorg and on your server you don't have Xorg. PhantomJS then uses the framebuffer resolution.

You can check the available fb resolutions on your server by installing fbset.

On Debian Buster:

sudo apt install fbset
sudo fbset -i

For me:

mode "1024x768"
    geometry 1024 768 1024 768 24
    timings 0 0 0 0 0 0 0
    accel true
    rgba 8/16,8/8,8/0,0/0
endmode

Frame buffer device information:
    Name        : cirrusdrmfb
    Address     : 0xfc000000
    Size        : 33554432
    Type        : PACKED PIXELS
    Visual      : TRUECOLOR
    XPanStep    : 1
    YPanStep    : 1
    YWrapStep   : 0
    LineLength  : 3072
    Accelerator : No

So there is only 1 fb resolution available and it's set:

sudo fbset -s

mode "1024x768"
    geometry 1024 768 1024 768 24
    timings 0 0 0 0 0 0 0
    accel true
    rgba 8/16,8/8,8/0,0/0
endmode

Phantomjs can use 1024x768 at best so the output is a lot bigger than on my 1920x1080. It's even less, my tests show that because of the browser borders, phantomjs can only use a max viewportSize of 1011x768.

I don't see any other solution than designing the HTML of the PDF in 1011x768 or less.

guix77 avatar May 26 '20 11:05 guix77

html { zoom: 0.753 }

@Alex-Credifranco

I've been tinkering with this for a couple of hours already and this solved the problem. Thanks a lot.

jhon-andrew avatar May 27 '20 09:05 jhon-andrew

By default, dpi is set to primary screen's dpi https://github.com/ariya/phantomjs/blob/0a0b0facb16acfbabb7804822ecaf4f4b9dce3d2/src/webpage.cpp line 434

But it can be overridden by setting dpi value on page object line 658

html-pdf just has to expose this option

szogun1987 avatar Aug 23 '21 07:08 szogun1987

For those still curious, here is how I implemented damianijr's solution

const fixedHtml = myHtmlString.replace("</head>", "<style>html {zoom: 0.75}</style></head>")

Techno11 avatar Oct 22 '21 14:10 Techno11

html { zoom: 0.753 }

@Alex-Credifranco Also use media print if you export image too, so export result not affected by zoom

@media print {
    html {
        zoom: 0.753;
    }
}

dimar-hanung avatar Jan 23 '22 13:01 dimar-hanung