-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
70 lines (59 loc) · 2.02 KB
/
app.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
const express = require('express');
const path = require('path');
// const bodyParser = require('body-parser');
const pdf = require('html-pdf');
const morgan = require('morgan');
const fs =require('fs');
const formidable = require('formidable');
const app = express()
//app-locals
app.set('port',5678)
app.use(morgan('tiny'))
// app.use(bodyParser.json({extended:true}));
// app.use(bodyParser.urlencoded({extended:true}))
//static routes
app.use(express.static(path.join(__dirname,'./public')));
app.use('/cropper',express.static(path.join(__dirname,'./node_modules/cropperjs/dist')));
app.use('/font-awesome',express.static(path.join(__dirname,'./node_modules/font-awesome/')));
app.use('/jspdf',express.static(path.join(__dirname,'./node_modules/jspdf/dist')));
app.post('/html2pdf',(req,res)=>{
form = new formidable.IncomingForm();
form.parse(req,(err,fields,files)=>{
let html = `<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Image</title>
<style>${fields['style']}</style>
</head>
<body>${fields['html']}</body></html>`;
fs.writeFileSync('convert.html',html)
//todo - name the file by getting name of image with the request
pdf.create(html).toFile('converted.pdf',(err,result)=>{
if(err)throw err;
console.log(result)
// res.setHeader('content-disposition','attachment;filename=converted.pdf');
// res.download(__dirname+'/converted.pdf');
// res.redirect('/downloadPdf')
res.json({url:'/downloadPdf'});
// res.end('see the file?')
})
});
//---- form.parse -----
});
app.get('/downloadPdf',(req,res)=>{
res.download(__dirname+'/converted.pdf');
// try{
// fs.unlink(__dirname+'/converted.pdf');
// }catch(err){
// console.log(err)
// }
})
app.listen(app.get('port'),()=>{
console.log('app listening on localhost:'+app.get('port'))
});
app.use((err,req,res,next)=>{
console.log(err);
})