printheaderfooter.js 3.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. var page = require('webpage').create(),
  2. system = require('system');
  3. function someCallback(pageNum, numPages) {
  4. return "<h1> someCallback: " + pageNum + " / " + numPages + "</h1>";
  5. }
  6. if (system.args.length < 3) {
  7. console.log('Usage: printheaderfooter.js URL filename');
  8. phantom.exit(1);
  9. } else {
  10. var address = system.args[1];
  11. var output = system.args[2];
  12. page.viewportSize = { width: 600, height: 600 };
  13. page.paperSize = {
  14. format: 'A4',
  15. margin: "1cm",
  16. /* default header/footer for pages that don't have custom overwrites (see below) */
  17. header: {
  18. height: "1cm",
  19. contents: phantom.callback(function(pageNum, numPages) {
  20. if (pageNum == 1) {
  21. return "";
  22. }
  23. return "<h1>Header <span style='float:right'>" + pageNum + " / " + numPages + "</span></h1>";
  24. })
  25. },
  26. footer: {
  27. height: "1cm",
  28. contents: phantom.callback(function(pageNum, numPages) {
  29. if (pageNum == numPages) {
  30. return "";
  31. }
  32. return "<h1>Footer <span style='float:right'>" + pageNum + " / " + numPages + "</span></h1>";
  33. })
  34. }
  35. };
  36. page.open(address, function (status) {
  37. if (status !== 'success') {
  38. console.log('Unable to load the address!');
  39. } else {
  40. /* check whether the loaded page overwrites the header/footer setting,
  41. i.e. whether a PhantomJSPriting object exists. Use that then instead
  42. of our defaults above.
  43. example:
  44. <html>
  45. <head>
  46. <script type="text/javascript">
  47. var PhantomJSPrinting = {
  48. header: {
  49. height: "1cm",
  50. contents: function(pageNum, numPages) { return pageNum + "/" + numPages; }
  51. },
  52. footer: {
  53. height: "1cm",
  54. contents: function(pageNum, numPages) { return pageNum + "/" + numPages; }
  55. }
  56. };
  57. </script>
  58. </head>
  59. <body><h1>asdfadsf</h1><p>asdfadsfycvx</p></body>
  60. </html>
  61. */
  62. if (page.evaluate(function(){return typeof PhantomJSPrinting == "object";})) {
  63. paperSize = page.paperSize;
  64. paperSize.header.height = page.evaluate(function() {
  65. return PhantomJSPrinting.header.height;
  66. });
  67. paperSize.header.contents = phantom.callback(function(pageNum, numPages) {
  68. return page.evaluate(function(pageNum, numPages){return PhantomJSPrinting.header.contents(pageNum, numPages);}, pageNum, numPages);
  69. });
  70. paperSize.footer.height = page.evaluate(function() {
  71. return PhantomJSPrinting.footer.height;
  72. });
  73. paperSize.footer.contents = phantom.callback(function(pageNum, numPages) {
  74. return page.evaluate(function(pageNum, numPages){return PhantomJSPrinting.footer.contents(pageNum, numPages);}, pageNum, numPages);
  75. });
  76. page.paperSize = paperSize;
  77. console.log(page.paperSize.header.height);
  78. console.log(page.paperSize.footer.height);
  79. }
  80. window.setTimeout(function () {
  81. page.render(output);
  82. phantom.exit();
  83. }, 200);
  84. }
  85. });
  86. }