Commit 4fb7f746 by ramdayalmunda

added creating and viewing PDF for easier testing

parent a04a308e
node_modules/ node_modules/
\ No newline at end of file temporary/
\ No newline at end of file
const router = require("express").Router()
const path = require('path')
let ADocEditor = require("./dist/assets/a-doc-editor.js")
var inProgress = false
let fileName = path.join( __dirname, "temporary", "temp.pdf" )
router.post( "/generate", async function(req, res){
if (inProgress){
console.log('server busy')
res.status(200).json({ success: false, message: "Server Busy" })
return
}
try{
inProgress = true
console.clear()
let editor = new ADocEditor()
console.log('editor', editor)
let result = await editor.generatePDF(fileName)
console.log('result:', result)
inProgress = false
res.status(200).json( { success: true, message: "Successfull", fileName: fileName } )
}catch(err){
console.log(err)
inProgress = false
res.status(200).json({ message: "Failed" })
}
} )
router.get("/display", async function(req, res){
try{
console.log('reaching')
res.sendFile( fileName )
}catch(err){
console.log(err)
res.status(200).json({ message: "Failed" })
}
})
module.exports = router
\ No newline at end of file
...@@ -621,9 +621,20 @@ var ADocEditor = function (customConfig) { ...@@ -621,9 +621,20 @@ var ADocEditor = function (customConfig) {
} }
} }
function generatePDF(file) { async function generatePDF(file) {
if (isModule) { if (isModule) {
console.log('to generate PDF on Server side:', file) try{
const { jsPDF } = require("jspdf")
console.log('to generate PDF on Server side:', file)
const doc = new jsPDF('p', 'px', [ config.pageSetup.canvasWidth, config.pageSetup.canvasHeight ]);
doc.text("Hello World", 10, 10)
doc.save(file)
return file
}catch(err){
console.log(err)
return
}
} else { } else {
console.log('to generate PDF on browser:', file) console.log('to generate PDF on browser:', file)
} }
......
let ADocEditor = require("./dist/assets/a-doc-editor.js")
var counter = 0
module.exports = async function(req, res){
try{
console.clear()
let editor = new ADocEditor()
console.log('generating PDF', ++counter, editor)
editor.generatePDF('temp.pdf')
res.status(200).json( { success: true, message: "Successfull" } )
}catch(err){
console.log(err)
res.status(200).json({ message: "Failed" })
}
}
\ No newline at end of file
...@@ -5,7 +5,7 @@ const path = require("path"); ...@@ -5,7 +5,7 @@ const path = require("path");
const app = express() const app = express()
app.use( express.json() ) app.use( express.json() )
app.post( '/generate-pdf', require("./generate-pdf") ) app.use( '/pdf', require("./custom-pdf") )
app.use( express.static( path.join( __dirname, 'dist' ) ) ) app.use( express.static( path.join( __dirname, 'dist' ) ) )
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment