1 someCallback = (pageNum, numPages) ->
2 "<h1> someCallback: " + pageNum + " / " + numPages + "</h1>"
3 page = require("webpage").create()
4 system = require("system")
5 if system.args.length < 3
6 console.log "Usage: printheaderfooter.js URL filename"
9 address = system.args[1]
10 output = system.args[2]
19 # default header/footer for pages that don't have custom overwrites (see below)
22 contents: phantom.callback((pageNum, numPages) ->
23 return "" if pageNum is 1
24 "<h1>Header <span style='float:right'>" + pageNum + " / " + numPages + "</span></h1>"
29 contents: phantom.callback((pageNum, numPages) ->
30 return "" if pageNum is numPages
31 "<h1>Footer <span style='float:right'>" + pageNum + " / " + numPages + "</span></h1>"
34 page.open address, (status) ->
35 if status isnt "success"
36 console.log "Unable to load the address!"
39 # check whether the loaded page overwrites the header/footer setting,
40 # i.e. whether a PhantomJSPriting object exists. Use that then instead
41 # of our defaults above.
46 # <script type="text/javascript">
47 # var PhantomJSPrinting = {
50 # contents: function(pageNum, numPages) { return pageNum + "/" + numPages; }
54 # contents: function(pageNum, numPages) { return pageNum + "/" + numPages; }
59 # <body><h1>asdfadsf</h1><p>asdfadsfycvx</p></body>
63 typeof PhantomJSPrinting is "object"
65 paperSize = page.paperSize
66 paperSize.header.height = page.evaluate(->
67 PhantomJSPrinting.header.height
69 paperSize.header.contents = phantom.callback((pageNum, numPages) ->
70 page.evaluate ((pageNum, numPages) ->
71 PhantomJSPrinting.header.contents pageNum, numPages
74 paperSize.footer.height = page.evaluate(->
75 PhantomJSPrinting.footer.height
77 paperSize.footer.contents = phantom.callback((pageNum, numPages) ->
78 page.evaluate ((pageNum, numPages) ->
79 PhantomJSPrinting.footer.contents pageNum, numPages
82 page.paperSize = paperSize
83 console.log page.paperSize.header.height
84 console.log page.paperSize.footer.height