brenden / node-webshot

Easy website screenshots in Node.js
2.12k stars 285 forks source link

Adding Header/Footer to each page in Webshot #138

Open bsbechtel opened 8 years ago

bsbechtel commented 8 years ago

I'm trying to add a header and footer to each page of a pdf I am creating using webshot. Is it possible to modify the phantomPage options handler function to handle callbacks, per this SO question?

bsbechtel commented 8 years ago

So I spent the day trying to figure this out, as I really needed to be able to modify the header/footers in my project. the question above using eval() didn't work. However, I wrote the following modification to be inserted on line 70 of webshot.phantom.js:

if (key === "paperSize" && page[key] && page[key].header && page[key].header.contents) {
    var header = {
      "height": page[key].header.height,
      "contents": phantom.callback(function() {return options.paperSize.header.contents;})
    }
    page.paperSize.header = header;
  }
  if (key === "paperSize" && page[key] && page[key].footer && page[key].footer.contents) {
    var footer = {
      "height": page[key].footer.height,
      "contents": phantom.callback(function() {return options.paperSize.footer.contents;})
    }
    page[key].footer = footer
  }
  if (key === "paperSize") {
    page.paperSize = {
      "format": options.paperSize.format,
      "orientation": options.paperSize.orientation,
      "margin": options.paperSize.margin,
      "header": header,
      "footer": footer
    }
  } 

The above works for my code, but it is missing a few additional paperSize options to work as a general solution. It also can't handle the pageNum and numPages arguments, but it is an improvement that I think can be helpful. I've never submitted a pull request in git before, but think we can merge this into the main branch?