UNPKG

htmlpdfconversion

Version:

This Package is being used for HTML-PDF Conversion.

112 lines (107 loc) 4.05 kB
var express = require('express'); var app = express(); var fs = require('fs'); var Promise = require('bluebird'); var Request = require("request"); var JSPath = require("jspath"); var pdf = require('html-pdf'); var bodyParser = require("body-parser"); app.use(bodyParser.json()); app.use(bodyParser.urlencoded({ extended: false })); app.post('/convertPdf', function (req, resp) { fs.writeFileSync('app.html', req.body.html); fs.readFile('app.html', 'utf8', function (err, contents) { fs.writeFileSync('File.html', contents); var html = fs.readFileSync('File.html', 'utf8'); var options = { "format": 'A4', "border": { "top": "40px", // default is 0, units: mm, cm, in, px "right": "30px", "bottom": "40px", "left": "30px" }, "header": { "height": "45mm", //"contents": '<div style="text-align: center;">Author:' + req.body.author ? req.body.author : "null" + '</div>' }, "footer": { "height": "20px", "contents": { default: '<span style="color: #444;">{{page}}</span>-<span>{{pages}}</span>', // fallback value } } }; pdf.create(html, options).toFile('sample.pdf', function (err, res) { console.log("response" + res); if (err) return console.log(err); fs.readFile('sample.pdf', function (err, data) { if (err) return console.log(err); resp.contentType("application/octet-stream"); resp.send(data); }); }); }) }); exports.convertPdfWithAuthorNameInHeader = function(req,resp){ fs.writeFileSync('app.html', req.html); fs.readFile('app.html', 'utf8', function (err, contents) { fs.writeFileSync('File.html', contents); var html = fs.readFileSync('File.html', 'utf8'); var options = { "format": 'A4', "border": { "top": "40px", // default is 0, units: mm, cm, in, px "right": "30px", "bottom": "40px", "left": "30px" }, "header": { "height": "45mm", "contents": '<div style="text-align: center;">Author:' + req.author + '</div>' }, "footer": { "height": "20px", "contents": { default: '<span style="color: #444;">{{page}}</span>-<span>{{pages}}</span>', // fallback value } } }; pdf.create(html, options).toFile('sample.pdf', function (err, res) { console.log("Success in Converting the File" + res); if (err) return console.log(err); }); }) } exports.convertPdf = function(req,resp){ fs.writeFileSync('app.html', req.html); fs.readFile('app.html', 'utf8', function (err, contents) { fs.writeFileSync('File.html', contents); var html = fs.readFileSync('File.html', 'utf8'); var options = { "format": 'A4', "border": { "top": "40px", // default is 0, units: mm, cm, in, px "right": "30px", "bottom": "40px", "left": "30px" }, "header": { "height": "45mm" }, "footer": { "height": "20px", "contents": { default: '<span style="color: #444;">{{page}}</span>-<span>{{pages}}</span>', // fallback value } } }; pdf.create(html, options).toFile('sample.pdf', function (err, res) { console.log("Success in Converting the File" + res); if (err) return console.log(err); }); }) } // app.listen(8080, function () { // console.log("listening on 8080"); // });