aboutsummaryrefslogtreecommitdiffstats
path: root/vnfmarket/src/main/webapp/vnfmarket/node_modules/phantomjs/lib/phantom/examples/printheaderfooter.coffee
blob: fd82b340e7dff02a02f2ad83433b41a74349dcb4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
someCallback = (pageNum, numPages) ->
  "<h1> someCallback: " + pageNum + " / " + numPages + "</h1>"
page = require("webpage").create()
system = require("system")
if system.args.length < 3
  console.log "Usage: printheaderfooter.js URL filename"
  phantom.exit 1
else
  address = system.args[1]
  output = system.args[2]
  page.viewportSize =
    width: 600
    height: 600

  page.paperSize =
    format: "A4"
    margin: "1cm"
    
    # default header/footer for pages that don't have custom overwrites (see below) 
    header:
      height: "1cm"
      contents: phantom.callback((pageNum, numPages) ->
        return ""  if pageNum is 1
        "<h1>Header <span style='float:right'>" + pageNum + " / " + numPages + "</span></h1>"
      )

    footer:
      height: "1cm"
      contents: phantom.callback((pageNum, numPages) ->
        return ""  if pageNum is numPages
        "<h1>Footer <span style='float:right'>" + pageNum + " / " + numPages + "</span></h1>"
      )

  page.open address, (status) ->
    if status isnt "success"
      console.log "Unable to load the address!"
    else
      
      # check whether the loaded page overwrites the header/footer setting,
      #               i.e. whether a PhantomJSPriting object exists. Use that then instead
      #               of our defaults above.
      #
      #               example:
      #               <html>
      #                 <head>
      #                   <script type="text/javascript">
      #                     var PhantomJSPrinting = {
      #                        header: {
      #                            height: "1cm",
      #                            contents: function(pageNum, numPages) { return pageNum + "/" + numPages; }
      #                        },
      #                        footer: {
      #                            height: "1cm",
      #                            contents: function(pageNum, numPages) { return pageNum + "/" + numPages; }
      #                        }
      #                     };
      #                   </script>
      #                 </head>
      #                 <body><h1>asdfadsf</h1><p>asdfadsfycvx</p></body>
      #              </html>
      #            
      if page.evaluate(->
        typeof PhantomJSPrinting is "object"
      )
        paperSize = page.paperSize
        paperSize.header.height = page.evaluate(->
          PhantomJSPrinting.header.height
        )
        paperSize.header.contents = phantom.callback((pageNum, numPages) ->
          page.evaluate ((pageNum, numPages) ->
            PhantomJSPrinting.header.contents pageNum, numPages
          ), pageNum, numPages
        )
        paperSize.footer.height = page.evaluate(->
          PhantomJSPrinting.footer.height
        )
        paperSize.footer.contents = phantom.callback((pageNum, numPages) ->
          page.evaluate ((pageNum, numPages) ->
            PhantomJSPrinting.footer.contents pageNum, numPages
          ), pageNum, numPages
        )
        page.paperSize = paperSize
        console.log page.paperSize.header.height
        console.log page.paperSize.footer.height
      window.setTimeout (->
        page.render output
        phantom.exit()
      ), 200