title: How to set custom header and footer content by using phantom package in nodeJS?
body: <p>Actually, I am working with generating pdf reports using phantom package in nodeJS. I've found that we can use phantom.callback method for setting it. But I have a problem that when this callback returns simple text it works fine, but when I try to use complicated function which use closures and jade engine for generating html I have error in phantom output that jade variable is not defined, I think this problem was occurred because callback which is mentioned about work in context of child phantom process, therefore all variables which are defined in my code in that callback don't work. So, how can I solve this issue? Maybe you know better phantomJS wrapper for doing this stuff?
  I use this package <code>phantom": "0.7.x"</code></p>
  
  <pre><code>//there I define all varaibles (jade, fs, etc., so I am sure that they are correct)
  
  function generatePage(_page, reportConfig, phantom, html, report) {
      _page.set('viewportSize', reportConfig.viewportSize);
      var config = _.extend(reportConfig.paperSize, {
              header: {
                  height: "2cm",
                  //contents: phantom.callback(headerCallback)
                  contents: phantom.callback(function (pageNum, numPages) {
                      var fn = jade.compile(headerTemplate); //Jade in undefined in phantom stdout
                      var templateData = _.extend({ currentPage: pageNum, numberPages: numPages }, report);
                      var generatedHtml = fn(templateData);
                      return "&lt;h1&gt;HEADER&lt;/h1&gt;&lt;br /&gt;" /*+ generatedHtml*/;
                  })
              }
              , footer: {
                  height: "1cm",
                  contents: phantom.callback(function (pageNum, numPages) {
                      return "&lt;p&gt;Page " + pageNum + " of " + numPages + "&lt;/p&gt;"; //WORKS fine
                  })
              }
          }
      );
      _page.set('paperSize', config);
      _page.setContent(html);
      return _page;
  }
  </code></pre>