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