1
0
mirror of https://github.com/stolksdorf/homebrewery.git synced 2025-12-14 11:45:57 +00:00

Added pdf endpoint

This commit is contained in:
Scott Tolksdorf
2016-01-13 16:03:57 -05:00
parent 963e282d9e
commit 13f68c872e
3 changed files with 59 additions and 16 deletions

View File

@@ -122,6 +122,44 @@ module.exports = function(app){
}) })
}); });
//PDF download
var pdf = require('html-pdf');
app.get('/homebrew/pdf/:id', function(req, res){
HomebrewModel.find({shareId : req.params.id}, function(err, objs){
if(err) return res.status(404).send();
var resObj = null;
var errObj = {text: "# oops\nCould not find the homebrew."}
if(objs.length){
resObj = objs[0];
}
var content = _.map(resObj.text.split('\\page'), function(pageText){
return '<div class="phb">' + Markdown(pageText) + '</div>';
}).join('\n');
var title = '<title>' + resObj.text.split('\n')[0] + '</title>';
var page = '<html><head>' + title + PHBStyle + '</head><body>' + content +'</body></html>'
var config = {
"height": (279.4 - 56) + "mm",
"width": (215.9 - 43) + "mm",
"border": "0",
}
pdf.create(html, config).toStream(function(err, stream){
res.attachment('pdfname.pdf');
return stream.pipe(res);
});
})
});

View File

@@ -1,26 +1,31 @@
var pdf = require('html-pdf'); var pdf = require('html-pdf');
var Markdown = require('marked'); var Markdown = require('marked');
var style = require('fs').readFileSync('../build/phbPage/bundle.css', 'utf8'); var PHBStyle = '<style>' + require('fs').readFileSync('../phb.standalone.css', 'utf8') + '</style>'
style = "<style>html, body {margin: 0;padding: 0;-webkit-print-color-adjust: exact;box-sizing: border-box;}\n"+
style + "</style>";
function replaceAll(str, find, replace) {
return str.replace(new RegExp(find, 'g'), replace);
}
style = replaceAll(style, '/assets/homebrew/assets/', 'http://www.naturalcrit.com/assets/homebrew/assets/');
var content = Markdown('# oh hey \n welcome! ##### test');
var html = "<html><head>" + style + "</head><body><div class='phb'>"+ content +"</div></body></html>" var content = Markdown('# oh hey \n welcome! isnt this neat \n \\page ##### test');
console.log(html);
pdf.create(html).toFile('./pdfs/businesscard.pdf', function(err, res){ var html = "<html><head>" + PHBStyle + "</head><body><div class='phb'>"+ content +"</div></body></html>"
//var h = 279.4 - 20*2.8;
var h = 279.4 - 56;
//var w = 215.9 - 56*1.7
var w = 215.9 - 43;
var config = {
"height": (279.4 - 56) + "mm",
"width": (215.9 - 43) + "mm",
"border": "0",
}
pdf.create(html, config).toFile('./temp.pdf', function(err, res){
console.log(err); console.log(err);
console.log(res.filename); console.log(res.filename);
}); });

BIN
server/temp.pdf Normal file

Binary file not shown.