Commit c813ff8b by ramdayalmunda

removed jsPDF and cleaned pdf-lib

parent 68a69896
......@@ -811,102 +811,6 @@ var ADocEditor = function (customConfig) {
}
}
async function generatePDFJsPDF(file) {
const customFontDataURI = await getFontDataURI("./assets/fonts/customfont.ttf")
const { jsPDF } = jspdf;
// Default export is a4 paper, portrait, using millimeters for units
const doc = new jsPDF({
orientation: "p",
unit: "px",
hotfixes: ["px_scaling"],
format: [config.pageSetup.canvasWidth, config.pageSetup.canvasHeight]
});
let customFontName = "customfont"
doc.addFileToVFS('customfont.ttf', customFontDataURI); // Add the font to the Virtual File System
doc.addFont('customfont.ttf', customFontName, 'normal');
let canvasIndex = 0
let x = 0
let y = (config.format.margin * config.pageSetup.canvasMultiplier)
let maxVericalWidth = (config.pageSetup.canvasHeight - config.format.margin * config.pageSetup.canvasMultiplier * 2)
for (let l = 0; l < lines.length; l++) {
x = (config.format.margin * config.pageSetup.canvasMultiplier);
y += (lines[l].maxFontSize * config.pageSetup.fontMultiplier)
if (lines[l].blockStart && l != 0) y += lines[l].maxFontSize
if ((maxVericalWidth + lines[l].maxFontSize) < (y - lines[l].maxFontSize)) {
canvasIndex++
doc.addPage()
y = lines[l].maxFontSize + (config.format.margin * config.pageSetup.canvasMultiplier)
}
let setData = dataSet[lines[l].dataSetIndex]
lines[l].y = y
if (lines[l].listIndex >= 0) {
let totalTabDistance = config.format.tabWidth * config.pageSetup.canvasMultiplier * (lines[l].tabCount + 1)
x += totalTabDistance;
let numberX = x - config.format.tabWidth * config.pageSetup.canvasMultiplier / 2
if (lines[l].blockStart) {
let style = {
...config.style,
...setData.style
}
doc.setFont(style.fontFamily)
doc.setFontSize(style.fontSize * 3 / 2)
doc.text(`${lines[l].listIndex + 1}.`, numberX, y)
}
}
lines[l].x = x
lines[l].canvasIndex = canvasIndex
for (let c = lines[l].charStartIndex; c <= lines[l].charEndIndex; c++) {
let char = setData?.plainContent[c]
if (char) {
let style = setData.formatedText[c]
doc.setFont(style.fontFamily)
doc.setFontSize(style.fontSize * 3 / 2)
doc.text(char, x, y)
setData.formatedText[c].x = x
setData.formatedText[c].y = y
if (setData.formatedText[c]?.width) {
x += setData.formatedText[c]?.width
}
}
}
}
doc.save(file)
async function getFontDataURI(url) {
return new Promise(async (res, rej) => {
fetch('./assets/fonts/customfont.ttf')
.then(response => response.blob())
.then(blob => {
const reader = new FileReader();
reader.readAsDataURL(blob);
reader.onloadend = () => {
res(reader.result.split(',')[1])
}
})
.catch((err) => { rej(err) })
})
}
}
async function generatePDF(file) {
......@@ -926,10 +830,7 @@ var ADocEditor = function (customConfig) {
const customFont = await pdfDoc.embedFont(fontBytes)
// Add a blank page to the document
let pageSetup = { width: config.pageSetup.canvasWidth, height: config.pageSetup.canvasHeight }
var page = pdfDoc.addPage([pageSetup.width, pageSetup.height])
var page = pdfDoc.addPage([config.pageSetup.width, config.pageSetup.height])
let canvasIndex = 0
let x = 0
......@@ -944,7 +845,7 @@ var ADocEditor = function (customConfig) {
if (lines[l].blockStart && l != 0) y += lines[l].maxFontSize
if ((maxVericalWidth + lines[l].maxFontSize) < (y - lines[l].maxFontSize)) {
canvasIndex++
page = pdfDoc.addPage([pageSetup.width, pageSetup.height])
page = pdfDoc.addPage([config.pageSetup.width, config.pageSetup.height])
y = lines[l].maxFontSize + (config.format.margin * config.pageSetup.canvasMultiplier)
}
......@@ -961,9 +862,9 @@ var ADocEditor = function (customConfig) {
...setData.style
}
page.drawText( `${lines[l].listIndex + 1}.` , {
x: numberX,
y: config.pageSetup.canvasHeight-y,
size: style.fontSize*config.pageSetup.fontMultiplier,
x: numberX/config.pageSetup.canvasMultiplier,
y: (config.pageSetup.canvasHeight-y)/config.pageSetup.canvasMultiplier,
size: (style.fontSize*config.pageSetup.fontMultiplier)/config.pageSetup.canvasMultiplier,
font: customFont,
color: rgb(0, 0, 0),
})
......@@ -978,9 +879,9 @@ var ADocEditor = function (customConfig) {
if (char) {
let style = setData.formatedText[c]
page.drawText( dataSet[lines[l].dataSetIndex].plainContent[c] , {
x: style.x,
y: config.pageSetup.canvasHeight-style.y,
size: style.fontSize*config.pageSetup.fontMultiplier,
x: style.x/config.pageSetup.canvasMultiplier,
y:( config.pageSetup.canvasHeight-style.y)/config.pageSetup.canvasMultiplier,
size: (style.fontSize*config.pageSetup.fontMultiplier)/config.pageSetup.canvasMultiplier,
font: customFont,
color: rgb(0, 0, 0),
})
......@@ -1060,6 +961,10 @@ var ADocEditor = function (customConfig) {
clearInterval(caretData.interval)
}
function getContent(){
return JSON.parse(JSON.stringify(dataSet))
}
var returnObj = {
destory,
loadContent: function (data) {
......@@ -1069,9 +974,7 @@ var ADocEditor = function (customConfig) {
caretData.activeData = dataSet[0]
caretData.index = 0
},
getContent: function () {
return JSON.parse(JSON.stringify(dataSet))
},
getContent,
addFonts: addFonts,
generatePDF,
}
......
This source diff could not be displayed because it is too large. You can view the blob instead.
......@@ -7,7 +7,6 @@
<link rel="stylesheet" href="./assets/a-doc-editor.css">
<script src="./assets/fontkit.umd.min.js"></script>
<script src="./assets/pdf-lib.min.js" ></script>
<script src="./assets/jspdf.umd.min.js"></script>
<script src="./assets/a-doc-editor.js"></script>
<style>
......
......@@ -12,6 +12,6 @@
"dependencies": {
"canvas": "^2.11.2",
"express": "^4.18.2",
"jspdf": "^2.5.1"
"pdf-lib": "^1.17.1"
}
}
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