PhantomJs set header margin to 0 when rendering pdf

I'm using Node.js and the module phridge (version 1.0.3) to render a pdf with PhantomJs.

I want to render a pdf without any margins in the header and footer. For the moment I only found a hack to remove the left and right margin, by means of a negative left and right margin.

An example for printing a header and footer with pagenumbers can be found here: 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);

I removed the left and right header margins by using a negative margin. For this I used inline style. For example when header.contents is:

<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>

In an earlier attempt to remove the margins I included following internal CSS stylesheet. Unfortunately, it seems like this style is not applied. Elsewhere I read that the cause for this behavior is the fact that only inline style is applied when using the phantom.callback().

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

How should I remove the top and bottom margin of the header and footer?

Thanks for reading!

Try the following CSS for the html files you're rendering to pdf:

body {
    padding: 0;
    margin: 0;
}

Seems like otherwisebody has some padding at top and bottom "by default".

It turns out that the header and footer is rendered differently on different platforms (Linux, Windows) because the dpi is different.

Here is a discussion about it: https://groups.google.com/forum/#!topic/phantomjs/YQIyxLWhmr0

Changing the zoomFactor based on the platform removed the white gaps:

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