[英]html-pdf npm library gives different output on windows & ubuntu
I'm using https://www.npmjs.com/package/html-pdf library which is based on Phantom JS which internally uses webkit . 我正在使用https://www.npmjs.com/package/html-pdf库,该库基于Phantom JS ,内部使用webkit 。 I'm pasting the dummy HTML & JS code(keep these files in 1 folder) and also attaching the output screenshot.
我正在粘贴虚拟HTML和JS代码(将这些文件保存在1个文件夹中)并附加输出屏幕截图。
The issue I'm facing is that on windows the PDF is generated with some extra space at top(empty space above red) which I can't get rid of . 我面临的问题是, 在Windows上生成的PDF顶部有一些额外的空间(红色以上的空白空间),这是我无法摆脱的 。
Here is a forum(outdated) discussing similar issues, https://groups.google.com/forum/#!topic/phantomjs/YQIyxLWhmr0 . 这是一个讨论类似问题的论坛(过时), https://groups.google.com/forum/# ! topic /phantomjs / YQIyxLWhmr0 。
input.html input.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
</head>
<body>
<div id="pageHeader" style="border-style: solid;border-width: 2px;color:red;">
header <br/> header <br/> header <br/> header
</div>
<div id="pageContent" style="border-style: solid;border-width: 2px;color:green;">
<div>
body <br/> body <br/> body
</div>
</div>
JS (You require path, fs, handlebars, html-pdf npm packages) JS (你需要路径,fs,把手,html-pdf npm包)
var path = require('path');
var fs = require('fs');
var handlebars = require('handlebars');
var pdf = require('html-pdf');
saveHtml();
function saveHtml() {
fs.readFile('input.html', 'utf-8', {
flag: 'w'
}, function(error, source) {
handlebars.registerHelper('custom_title', function(title) {
return title;
})
var template = handlebars.compile(source);
var data = {};
var html = template(data);
var options = {
'format': 'A4',
'base': "file://",
/* You can give more options like height, width, border */
};
pdf.create(html, options).toFile('./output.pdf', function(err, res) {
if (err) {
console.log('err pdf');
return;
} else {
console.log('no err pdf');
return;
}
});
});
}
Extra space at top(empty space above red) in Windows is the issue. Windows中顶部的额外空间(红色上方的空白区域)是个问题。
THINGS that didn't work 1. Adding "border": { "top": "0px" // or mm, cm, in } to options in JS file 那些没有用的东西 1.在JS文件中添加“border”:{“top”:“0px”//或mm,cm,in}
https://www.npmjs.com/package/html-pdf#options https://www.npmjs.com/package/html-pdf#options
Giving fixed "height": "10.5in" & "width": "8in" in options in JS file 在JS文件的选项中给出固定的“高度”:“10.5in”和“width”:“8in”
Making margin-top & padding-top to 0px/-50px to pageHeader div. 将margin-top和padding-top设置为0px / -50px到pageHeader div。
Any help will be greatly appreciated. 任何帮助将不胜感激。 Thanks.
谢谢。
You can manually set the CSS property to html tag. 您可以手动将CSS属性设置为html标记。 In my case I was having problems developing the template in Windows and deploying it to Linux (Heroku).
在我的情况下,我在Windows中开发模板并将其部署到Linux(Heroku)时遇到问题。
I put zoom: 0.7
in the label html and now both views look the same. 我在标签html中放了
zoom: 0.7
,现在两个视图看起来都一样。
html{zoom: 0.7;}
I was able to get more consistent results by removing the ID's so that it treated everything as content rather than separate header and content areas. 通过删除ID,我能够获得更一致的结果,以便将所有内容视为内容,而不是单独的标题和内容区域。
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
</head>
<body>
<div style="border-style: solid;border-width: 2px;color:red;">
header
</div>
<div style="border-style: solid;border-width: 2px;color:green;">
<div>
body
</div>
</div>
</body>
</html>
If you need an ID for styling, use something other than pageHeader / pageFooter to avoid the special treatment associated with those IDs. 如果您需要样式的ID,请使用pageHeader / pageFooter以外的其他内容,以避免与这些ID相关的特殊处理。
Have you tried using a normalize style sheet to compensate for cross platform differences? 您是否尝试使用规范化样式表来弥补跨平台差异?
https://necolas.github.io/normalize.css/ https://necolas.github.io/normalize.css/
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.