|
var fs = require('fs'); |
|
var system = require('system'); |
|
var webpage = require('webpage'); |
|
|
|
var page = webpage.create(); |
|
|
|
var output = system.stderr; |
|
|
|
page.onConsoleMessage = function(msg, lineNum, sourceId) { |
|
output.writeLine('CONSOLE: ' + msg + ' (from line #' + lineNum + ' in "' + sourceId + '")'); |
|
}; |
|
|
|
function logError(source, msg, trace) { |
|
output.writeLine(source + ' ERROR: ' + msg); |
|
trace.forEach(function(item) { |
|
output.writeLine(' ' + item.file + ':' + item.line); |
|
}); |
|
} |
|
|
|
page.onError = function(msg, trace) { |
|
logError('PAGE', msg, trace); |
|
}; |
|
|
|
phantom.onError = function(msg, trace) { |
|
logError('PHANTOM', msg, trace); |
|
phantom.exit(1); |
|
}; |
|
|
|
page.onResourceRequested = function(request) { |
|
output.writeLine('REQUEST: ' + JSON.stringify(request, undefined, 4)); |
|
}; |
|
|
|
var content = fs.read('/dev/stdin'); |
|
var url = system.args[1]; |
|
var sessionId = system.args[2]; |
|
|
|
phantom.addCookie({ |
|
'name': '_session_id', |
|
'value': sessionId, |
|
'domain': 'localhost', |
|
'path': '/', |
|
'httponly': true, |
|
'secure': false |
|
}); |
|
|
|
page.setContent(content, url); |
|
|
|
var paperSize = { |
|
format: 'A4', |
|
margin: { |
|
top: '1cm', |
|
bottom: '1cm', |
|
left: '2cm', |
|
right: '2cm' |
|
} |
|
}; |
|
|
|
// Define PDF header and footer using HTML template elements. |
|
// Example: `<template id="pdf-footer" data-height="1cm">Page <strong>%{pageNum}</strong></template>` |
|
['header', 'footer'].forEach(function(section) { |
|
var template = page.evaluate(function(s) { |
|
var element = document.querySelector('template#pdf-' + s); |
|
return element && { height: element.dataset.height, contents: element.innerHTML, style: element.getAttribute('style') }; |
|
}, section); |
|
if (!template) return; |
|
paperSize[section] = {}; |
|
paperSize[section].height = template.height; |
|
paperSize[section].contents = phantom.callback(function(pageNum, numPages) { |
|
var html = template.contents.replace(/%{pageNum}/g, pageNum).replace(/%{numPages}/g, numPages); |
|
return addPrintStyle(html, template.style); |
|
}); |
|
}); |
|
|
|
function addPrintStyle(html, bodyStyle) { |
|
return '<style media="print">\n' + |
|
'body {' + bodyStyle + '}\n' + |
|
printStyle() + |
|
'</style>\n' + |
|
html; |
|
} |
|
|
|
var cachedPrintStyle; |
|
function printStyle() { |
|
if (!cachedPrintStyle) { |
|
cachedPrintStyle = page.evaluate(function() { |
|
var p = Array.prototype; |
|
return p.filter.call(document.styleSheets, function(s) { |
|
return p.some.call(s.media, function(m) { return m === 'print'; }); |
|
}).map(function(s) { |
|
return p.map.call(s.rules, function(r) { return r.cssText; }).join('\n'); |
|
}).join('\n'); |
|
}); |
|
} |
|
return cachedPrintStyle; |
|
} |
|
|
|
page.paperSize = paperSize; |
|
|
|
function checkReadyState() { |
|
var readyState = page.evaluate(function() { return document.readyState; }); |
|
if (readyState === 'complete') { |
|
onPageReady(); |
|
} else { |
|
setTimeout(checkReadyState); |
|
} |
|
} |
|
|
|
function onPageReady() { |
|
page.render('/dev/stdout', { format: 'pdf' }); |
|
phantom.exit(); |
|
} |
|
|
|
checkReadyState(); |
Using this code with send_data works very well, but if I need to attach to a mail, how can I do that?