|
| 1 | +var content, exit, json, options, page, paper, setContent, system, totalPages, type, vp, webpage, _i, _len, _ref, _ref1, _ref2, _ref3; |
| 2 | + |
| 3 | +system = require('system'); |
| 4 | + |
| 5 | +webpage = require('webpage'); |
| 6 | + |
| 7 | +exit = function(error) { |
| 8 | + var message; |
| 9 | + if (typeof error === 'string') { |
| 10 | + message = error; |
| 11 | + } |
| 12 | + if (error) { |
| 13 | + system.stderr.write("html-pdf: " + (message || ("Unknown Error " + error)) + "\n"); |
| 14 | + } |
| 15 | + return phantom.exit(error ? 1 : 0); |
| 16 | +}; |
| 17 | + |
| 18 | +setTimeout(function() { |
| 19 | + return exit('Force timeout'); |
| 20 | +}, 120000); |
| 21 | + |
| 22 | +json = JSON.parse(system.stdin.readLine()); |
| 23 | + |
| 24 | +if (!((_ref = json.html) != null ? _ref.trim() : void 0)) { |
| 25 | + exit('Did not receive any html'); |
| 26 | +} |
| 27 | + |
| 28 | +options = json.options; |
| 29 | + |
| 30 | +page = webpage.create(); |
| 31 | + |
| 32 | +page.content = json.html; |
| 33 | + |
| 34 | +if (vp = options.viewportSize) { |
| 35 | + page.viewportSize = vp; |
| 36 | +} |
| 37 | + |
| 38 | +totalPages = 0; |
| 39 | + |
| 40 | +content = page.evaluate(function() { |
| 41 | + var $body, $footer, $header, body, footer, header, styles, _ref1; |
| 42 | + styles = ((_ref1 = document.querySelector('head style')) != null ? _ref1.outerHTML : void 0) || ''; |
| 43 | + if ($header = document.getElementById('pageHeader')) { |
| 44 | + header = $header.outerHTML; |
| 45 | + $header.parentNode.removeChild($header); |
| 46 | + } |
| 47 | + if ($footer = document.getElementById('pageFooter')) { |
| 48 | + footer = $footer.outerHTML; |
| 49 | + $footer.parentNode.removeChild($footer); |
| 50 | + } |
| 51 | + if ($body = document.getElementById('pageContent')) { |
| 52 | + body = $body.outerHTML; |
| 53 | + } else { |
| 54 | + body = document.body.outerHTML; |
| 55 | + } |
| 56 | + return { |
| 57 | + styles: styles, |
| 58 | + header: header, |
| 59 | + body: body, |
| 60 | + footer: footer |
| 61 | + }; |
| 62 | +}); |
| 63 | + |
| 64 | +paper = { |
| 65 | + border: options.border || '0' |
| 66 | +}; |
| 67 | + |
| 68 | +if (options.height && options.width) { |
| 69 | + paper.width = options.width; |
| 70 | + paper.height = options.height; |
| 71 | +} else { |
| 72 | + paper.format = options.format || 'A4'; |
| 73 | + paper.orientation = options.orientation || 'portrait'; |
| 74 | +} |
| 75 | + |
| 76 | +setContent = function(type) { |
| 77 | + var _ref1; |
| 78 | + return paper[type] = { |
| 79 | + height: (_ref1 = options[type]) != null ? _ref1.height : void 0, |
| 80 | + contents: phantom.callback(function(pageNum, numPages) { |
| 81 | + var _ref2; |
| 82 | + return (((_ref2 = options[type]) != null ? _ref2.contents : void 0) || content[type] || '').replace('{{page}}', pageNum).replace('{{pages}}', numPages) + content.styles; |
| 83 | + }) |
| 84 | + }; |
| 85 | +}; |
| 86 | + |
| 87 | +_ref1 = ['header', 'footer']; |
| 88 | +for (_i = 0, _len = _ref1.length; _i < _len; _i++) { |
| 89 | + type = _ref1[_i]; |
| 90 | + if (options[type] || content[type]) { |
| 91 | + setContent(type); |
| 92 | + } |
| 93 | +} |
| 94 | + |
| 95 | +if ((_ref2 = paper.header) != null) { |
| 96 | + if (_ref2.height == null) { |
| 97 | + _ref2.height = '45mm'; |
| 98 | + } |
| 99 | +} |
| 100 | + |
| 101 | +if ((_ref3 = paper.footer) != null) { |
| 102 | + if (_ref3.height == null) { |
| 103 | + _ref3.height = '28mm'; |
| 104 | + } |
| 105 | +} |
| 106 | + |
| 107 | +page.paperSize = paper; |
| 108 | + |
| 109 | +page.onLoadFinished = function(status) { |
| 110 | + var fileOptions, filename; |
| 111 | + fileOptions = { |
| 112 | + type: options.type || 'pdf', |
| 113 | + quality: options.quality || 75 |
| 114 | + }; |
| 115 | + if (!options.buffer) { |
| 116 | + filename = options.filename || ("" + (options.directory || '/tmp') + "/html-pdf-" + system.pid + "." + fileOptions.type); |
| 117 | + page.render(filename, fileOptions); |
| 118 | + system.stdout.write(filename); |
| 119 | + } else { |
| 120 | + system.stderr.write('html-pdf: options.buffer is deprecated. Because of compatibility issues this method is longer supported.\n'); |
| 121 | + page.render('/dev/stdout', fileOptions); |
| 122 | + } |
| 123 | + return exit(null); |
| 124 | +}; |
0 commit comments