BlueGit

blue-star danique-bot

Files

danique-bot / node_modules / phantomjs-prebuilt / lib / phantom / examples / printheaderfooter.js

printheaderfooter.js

Import template
timfa Tim · pushed by timfa · 4 years ago
c788ad20 History
printheaderfooter.js 3.6 KiB Download Raw
1"use strict";
2var page = require('webpage').create(),
3 system = require('system');
4
5function someCallback(pageNum, numPages) {
6 return "<h1> someCallback: " + pageNum + " / " + numPages + "</h1>";
7}
8
9if (system.args.length < 3) {
10 console.log('Usage: printheaderfooter.js URL filename');
11 phantom.exit(1);
12} else {
13 var address = system.args[1];
14 var output = system.args[2];
15 page.viewportSize = { width: 600, height: 600 };
16 page.paperSize = {
17 format: 'A4',
18 margin: "1cm",
19 /* default header/footer for pages that don't have custom overwrites (see below) */
20 header: {
21 height: "1cm",
22 contents: phantom.callback(function(pageNum, numPages) {
23 if (pageNum == 1) {
24 return "";
25 }
26 return "<h1>Header <span style='float:right'>" + pageNum + " / " + numPages + "</span></h1>";
27 })
28 },
29 footer: {
30 height: "1cm",
31 contents: phantom.callback(function(pageNum, numPages) {
32 if (pageNum == numPages) {
33 return "";
34 }
35 return "<h1>Footer <span style='float:right'>" + pageNum + " / " + numPages + "</span></h1>";
36 })
37 }
38 };
39 page.open(address, function (status) {
40 if (status !== 'success') {
41 console.log('Unable to load the address!');
42 } else {
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.
46
47 example:
48 <html>
49 <head>
50 <script type="text/javascript">
51 var PhantomJSPrinting = {
52 header: {
53 height: "1cm",
54 contents: function(pageNum, numPages) { return pageNum + "/" + numPages; }
55 },
56 footer: {
57 height: "1cm",
58 contents: function(pageNum, numPages) { return pageNum + "/" + numPages; }
59 }
60 };
61 </script>
62 </head>
63 <body><h1>asdfadsf</h1><p>asdfadsfycvx</p></body>
64 </html>
65 */
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;
70 });
71 paperSize.header.contents = phantom.callback(function(pageNum, numPages) {
72 return page.evaluate(function(pageNum, numPages){return PhantomJSPrinting.header.contents(pageNum, numPages);}, pageNum, numPages);
73 });
74 paperSize.footer.height = page.evaluate(function() {
75 return PhantomJSPrinting.footer.height;
76 });
77 paperSize.footer.contents = phantom.callback(function(pageNum, numPages) {
78 return page.evaluate(function(pageNum, numPages){return PhantomJSPrinting.footer.contents(pageNum, numPages);}, pageNum, numPages);
79 });
80 page.paperSize = paperSize;
81 console.log(page.paperSize.header.height);
82 console.log(page.paperSize.footer.height);
83 }
84 window.setTimeout(function () {
85 page.render(output);
86 phantom.exit();
87 }, 200);
88 }
89 });
90}