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"
|
---|
7 | phantom.exit 1
|
---|
8 | else
|
---|
9 | address = system.args[1]
|
---|
10 | output = system.args[2]
|
---|
11 | page.viewportSize =
|
---|
12 | width: 600
|
---|
13 | height: 600
|
---|
14 |
|
---|
15 | page.paperSize =
|
---|
16 | format: "A4"
|
---|
17 | margin: "1cm"
|
---|
18 |
|
---|
19 | # default header/footer for pages that don't have custom overwrites (see below)
|
---|
20 | header:
|
---|
21 | height: "1cm"
|
---|
22 | contents: phantom.callback((pageNum, numPages) ->
|
---|
23 | return "" if pageNum is 1
|
---|
24 | "<h1>Header <span style='float:right'>" + pageNum + " / " + numPages + "</span></h1>"
|
---|
25 | )
|
---|
26 |
|
---|
27 | footer:
|
---|
28 | height: "1cm"
|
---|
29 | contents: phantom.callback((pageNum, numPages) ->
|
---|
30 | return "" if pageNum is numPages
|
---|
31 | "<h1>Footer <span style='float:right'>" + pageNum + " / " + numPages + "</span></h1>"
|
---|
32 | )
|
---|
33 |
|
---|
34 | page.open address, (status) ->
|
---|
35 | if status isnt "success"
|
---|
36 | console.log "Unable to load the address!"
|
---|
37 | else
|
---|
38 |
|
---|
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.
|
---|
42 | #
|
---|
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(->
|
---|
63 | typeof PhantomJSPrinting is "object"
|
---|
64 | )
|
---|
65 | paperSize = page.paperSize
|
---|
66 | paperSize.header.height = page.evaluate(->
|
---|
67 | PhantomJSPrinting.header.height
|
---|
68 | )
|
---|
69 | paperSize.header.contents = phantom.callback((pageNum, numPages) ->
|
---|
70 | page.evaluate ((pageNum, numPages) ->
|
---|
71 | PhantomJSPrinting.header.contents pageNum, numPages
|
---|
72 | ), pageNum, numPages
|
---|
73 | )
|
---|
74 | paperSize.footer.height = page.evaluate(->
|
---|
75 | PhantomJSPrinting.footer.height
|
---|
76 | )
|
---|
77 | paperSize.footer.contents = phantom.callback((pageNum, numPages) ->
|
---|
78 | page.evaluate ((pageNum, numPages) ->
|
---|
79 | PhantomJSPrinting.footer.contents pageNum, numPages
|
---|
80 | ), pageNum, numPages
|
---|
81 | )
|
---|
82 | page.paperSize = paperSize
|
---|
83 | console.log page.paperSize.header.height
|
---|
84 | console.log page.paperSize.footer.height
|
---|
85 | window.setTimeout (->
|
---|
86 | page.render output
|
---|
87 | phantom.exit()
|
---|
88 | ), 200
|
---|