mirror of
https://github.com/stolksdorf/homebrewery.git
synced 2025-12-14 05:35:56 +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);
|
||||
});
|
||||
|
||||
|
||||
})
|
||||
});
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
@@ -1,26 +1,31 @@
|
||||
var pdf = require('html-pdf');
|
||||
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);
|
||||
var content = Markdown('# oh hey \n welcome! isnt this neat \n \\page ##### test');
|
||||
|
||||
|
||||
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",
|
||||
}
|
||||
|
||||
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>"
|
||||
|
||||
console.log(html);
|
||||
|
||||
|
||||
pdf.create(html).toFile('./pdfs/businesscard.pdf', function(err, res){
|
||||
pdf.create(html, config).toFile('./temp.pdf', function(err, res){
|
||||
console.log(err);
|
||||
console.log(res.filename);
|
||||
});
|
||||
BIN
server/temp.pdf
Normal file
BIN
server/temp.pdf
Normal file
Binary file not shown.
Reference in New Issue
Block a user