1 var page = require('webpage').create(),
2 system = require('system');
4 function someCallback(pageNum, numPages) {
5 return "<h1> someCallback: " + pageNum + " / " + numPages + "</h1>";
8 if (system.args.length < 3) {
9 console.log('Usage: printheaderfooter.js URL filename');
12 var address = system.args[1];
13 var output = system.args[2];
14 page.viewportSize = { width: 600, height: 600 };
18 /* default header/footer for pages that don't have custom overwrites (see below) */
21 contents: phantom.callback(function(pageNum, numPages) {
25 return "<h1>Header <span style='float:right'>" + pageNum + " / " + numPages + "</span></h1>";
30 contents: phantom.callback(function(pageNum, numPages) {
31 if (pageNum == numPages) {
34 return "<h1>Footer <span style='float:right'>" + pageNum + " / " + numPages + "</span></h1>";
38 page.open(address, function (status) {
39 if (status !== 'success') {
40 console.log('Unable to load the address!');
42 /* check whether the loaded page overwrites the header/footer setting,
43 i.e. whether a PhantomJSPriting object exists. Use that then instead
44 of our defaults above.
49 <script type="text/javascript">
50 var PhantomJSPrinting = {
53 contents: function(pageNum, numPages) { return pageNum + "/" + numPages; }
57 contents: function(pageNum, numPages) { return pageNum + "/" + numPages; }
62 <body><h1>asdfadsf</h1><p>asdfadsfycvx</p></body>
65 if (page.evaluate(function(){return typeof PhantomJSPrinting == "object";})) {
66 paperSize = page.paperSize;
67 paperSize.header.height = page.evaluate(function() {
68 return PhantomJSPrinting.header.height;
70 paperSize.header.contents = phantom.callback(function(pageNum, numPages) {
71 return page.evaluate(function(pageNum, numPages){return PhantomJSPrinting.header.contents(pageNum, numPages);}, pageNum, numPages);
73 paperSize.footer.height = page.evaluate(function() {
74 return PhantomJSPrinting.footer.height;
76 paperSize.footer.contents = phantom.callback(function(pageNum, numPages) {
77 return page.evaluate(function(pageNum, numPages){return PhantomJSPrinting.footer.contents(pageNum, numPages);}, pageNum, numPages);
79 page.paperSize = paperSize;
80 console.log(page.paperSize.header.height);
81 console.log(page.paperSize.footer.height);
83 window.setTimeout(function () {