printheaderfooter.js 3.6 KB

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