mirror of
https://github.com/stolksdorf/homebrewery.git
synced 2025-12-13 23:05:57 +00:00
Added pdf endpoint
This commit is contained in:
@@ -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);
|
||||
});
|
||||
|
||||
|
||||
})
|
||||
});
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user