渲染pdf时,PhantomJs将页眉边距设置为0

我正在使用Node.js和模块phridge(版本1.0.3)来渲染与PhantomJs的pdf。

我想在页眉和页脚中没有任何页边空白的情况下渲染pdf。 目前,我只发现一个黑客通过负左右边距去除左右边距。

用pagenumbers打印页眉和页脚的例子可以在这里find: https : //github.com/ariya/phantomjs/blob/master/examples/printheaderfooter.js

// creates a new webpage internally var page = phantom.createPage(), outputFile = path.join(options.destDir, options.destFile); /*global window:false */ // page.run(fn) runs fn inside PhantomJS page.run(html, outputFile, options.header, options.footer, options.delay, function (html, outputFile, header, footer, delay, resolve /*, reject*/) { // Here we're inside PhantomJS, so we can't reference variables in the scope. // 'this' is an instance of PhantomJS' WebPage as returned by require("webpage").create() var page = this; page.content = html; page.viewportSize = { width: 1190, height: 1684 }; // 144dpi page.paperSize = { format: 'A4', header: { // How do I set the margin to 0px? margin: 0px or border: 0px doesn't work. height: '80px' contents: phantom.callback(function (pageNum, numPages) { return header.contents.replace(/<%= pageNum %>/g, pageNum).replace(/<%= numPages %>/g, numPages); }) }, footer: { height: '80px' contents: phantom.callback(function (pageNum, numPages) { return footer.contents.replace(/<%= pageNum %>/g, pageNum).replace(/<%= numPages %>/g, numPages); }) }, }; // Wait until JavaScript has run window.setTimeout(function () { page.render(outputFile, { format: 'pdf', quality: '100' }); page.close(); page = null; resolve(outputFile); }, delay); 

我通过使用负边距删除了左侧和右侧的页眉页边距。 为此,我使用内联样式。 例如,当header.contents是:

 <div style="background-color: rgb(230, 231, 232);margin:-20px;height:80px;"> <img style="text-align:center" src="file:///C:/image.png" alt="image"> </div> 

在之前的尝试删除边距我包括以下内部CSS样式表。 不幸的是,这种风格似乎不适用。 在其他地方,我读到这种行为的原因是只有内联样式应用在使用phantom.callback()时。

 <style type="text/css"> @page { margin: 0; } * { margin: 0px; padding: 0px; } body { margin: 0px; } </style> 

我应该如何删除页眉和页脚的上下边距?

谢谢阅读!

事实certificate,页眉和页脚在不同的平台(Linux,Windows)上渲染的方式不同,因为dpi是不同的。

以下是关于它的讨论: https : //groups.google.com/forum/#!topic/phantomjs/YQIyxLWhmr0

更改基于平台的zoomFactor删除了白色的差距:

 page.zoomFactor = (PLATFORM === 'linux'? 0.654545: 0.9) 

尝试以下CSS来呈现您要呈现给PDF的html文件:

 body { padding: 0; margin: 0; } 

看起来像其他的body有一些填充顶部和底部“默认”。