2 var page = require('webpage').create(),
3 system = require('system');
5 function someCallback(pageNum, numPages) {
6 return "<h1> someCallback: " + pageNum + " / " + numPages + "</h1>";
9 if (system.args.length < 3) {
10 console.log('Usage: printheaderfooter.js URL filename');
13 var address = system.args[1];
14 var output = system.args[2];
15 page.viewportSize = { width: 600, height: 600 };
19 /* default header/footer for pages that don't have custom overwrites (see below) */
22 contents: phantom.callback(function(pageNum, numPages) {
26 return "<h1>Header <span style='float:right'>" + pageNum + " / " + numPages + "</span></h1>";
31 contents: phantom.callback(function(pageNum, numPages) {
32 if (pageNum == numPages) {
35 return "<h1>Footer <span style='float:right'>" + pageNum + " / " + numPages + "</span></h1>";
39 page.open(address, function (status) {
40 if (status !== 'success') {
41 console.log('Unable to load the address!');
43 /* check whether the loaded page overwrites the header/footer setting,
44 i.e. whether a PhantomJSPriting object exists. Use that then instead
45 of our defaults above.
50 <script type="text/javascript">
51 var PhantomJSPrinting = {
54 contents: function(pageNum, numPages) { return pageNum + "/" + numPages; }
58 contents: function(pageNum, numPages) { return pageNum + "/" + numPages; }
63 <body><h1>asdfadsf</h1><p>asdfadsfycvx</p></body>
66 if (page.evaluate(function(){return typeof PhantomJSPrinting == "object";})) {
67 paperSize = page.paperSize;
68 paperSize.header.height = page.evaluate(function() {
69 return PhantomJSPrinting.header.height;
71 paperSize.header.contents = phantom.callback(function(pageNum, numPages) {
72 return page.evaluate(function(pageNum, numPages){return PhantomJSPrinting.header.contents(pageNum, numPages);}, pageNum, numPages);
74 paperSize.footer.height = page.evaluate(function() {
75 return PhantomJSPrinting.footer.height;
77 paperSize.footer.contents = phantom.callback(function(pageNum, numPages) {
78 return page.evaluate(function(pageNum, numPages){return PhantomJSPrinting.footer.contents(pageNum, numPages);}, pageNum, numPages);
80 page.paperSize = paperSize;
81 console.log(page.paperSize.header.height);
82 console.log(page.paperSize.footer.height);
84 window.setTimeout(function () {