Commit c813ff8b by ramdayalmunda

removed jsPDF and cleaned pdf-lib

parent 68a69896
...@@ -811,102 +811,6 @@ var ADocEditor = function (customConfig) { ...@@ -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) { async function generatePDF(file) {
...@@ -926,10 +830,7 @@ var ADocEditor = function (customConfig) { ...@@ -926,10 +830,7 @@ var ADocEditor = function (customConfig) {
const customFont = await pdfDoc.embedFont(fontBytes) const customFont = await pdfDoc.embedFont(fontBytes)
// Add a blank page to the document // Add a blank page to the document
let pageSetup = { width: config.pageSetup.canvasWidth, height: config.pageSetup.canvasHeight } var page = pdfDoc.addPage([config.pageSetup.width, config.pageSetup.height])
var page = pdfDoc.addPage([pageSetup.width, pageSetup.height])
let canvasIndex = 0 let canvasIndex = 0
let x = 0 let x = 0
...@@ -944,7 +845,7 @@ var ADocEditor = function (customConfig) { ...@@ -944,7 +845,7 @@ var ADocEditor = function (customConfig) {
if (lines[l].blockStart && l != 0) y += lines[l].maxFontSize if (lines[l].blockStart && l != 0) y += lines[l].maxFontSize
if ((maxVericalWidth + lines[l].maxFontSize) < (y - lines[l].maxFontSize)) { if ((maxVericalWidth + lines[l].maxFontSize) < (y - lines[l].maxFontSize)) {
canvasIndex++ 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) y = lines[l].maxFontSize + (config.format.margin * config.pageSetup.canvasMultiplier)
} }
...@@ -961,9 +862,9 @@ var ADocEditor = function (customConfig) { ...@@ -961,9 +862,9 @@ var ADocEditor = function (customConfig) {
...setData.style ...setData.style
} }
page.drawText( `${lines[l].listIndex + 1}.` , { page.drawText( `${lines[l].listIndex + 1}.` , {
x: numberX, x: numberX/config.pageSetup.canvasMultiplier,
y: config.pageSetup.canvasHeight-y, y: (config.pageSetup.canvasHeight-y)/config.pageSetup.canvasMultiplier,
size: style.fontSize*config.pageSetup.fontMultiplier, size: (style.fontSize*config.pageSetup.fontMultiplier)/config.pageSetup.canvasMultiplier,
font: customFont, font: customFont,
color: rgb(0, 0, 0), color: rgb(0, 0, 0),
}) })
...@@ -978,9 +879,9 @@ var ADocEditor = function (customConfig) { ...@@ -978,9 +879,9 @@ var ADocEditor = function (customConfig) {
if (char) { if (char) {
let style = setData.formatedText[c] let style = setData.formatedText[c]
page.drawText( dataSet[lines[l].dataSetIndex].plainContent[c] , { page.drawText( dataSet[lines[l].dataSetIndex].plainContent[c] , {
x: style.x, x: style.x/config.pageSetup.canvasMultiplier,
y: config.pageSetup.canvasHeight-style.y, y:( config.pageSetup.canvasHeight-style.y)/config.pageSetup.canvasMultiplier,
size: style.fontSize*config.pageSetup.fontMultiplier, size: (style.fontSize*config.pageSetup.fontMultiplier)/config.pageSetup.canvasMultiplier,
font: customFont, font: customFont,
color: rgb(0, 0, 0), color: rgb(0, 0, 0),
}) })
...@@ -1060,6 +961,10 @@ var ADocEditor = function (customConfig) { ...@@ -1060,6 +961,10 @@ var ADocEditor = function (customConfig) {
clearInterval(caretData.interval) clearInterval(caretData.interval)
} }
function getContent(){
return JSON.parse(JSON.stringify(dataSet))
}
var returnObj = { var returnObj = {
destory, destory,
loadContent: function (data) { loadContent: function (data) {
...@@ -1069,9 +974,7 @@ var ADocEditor = function (customConfig) { ...@@ -1069,9 +974,7 @@ var ADocEditor = function (customConfig) {
caretData.activeData = dataSet[0] caretData.activeData = dataSet[0]
caretData.index = 0 caretData.index = 0
}, },
getContent: function () { getContent,
return JSON.parse(JSON.stringify(dataSet))
},
addFonts: addFonts, addFonts: addFonts,
generatePDF, generatePDF,
} }
......
This source diff could not be displayed because it is too large. You can view the blob instead.
...@@ -7,7 +7,6 @@ ...@@ -7,7 +7,6 @@
<link rel="stylesheet" href="./assets/a-doc-editor.css"> <link rel="stylesheet" href="./assets/a-doc-editor.css">
<script src="./assets/fontkit.umd.min.js"></script> <script src="./assets/fontkit.umd.min.js"></script>
<script src="./assets/pdf-lib.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> <script src="./assets/a-doc-editor.js"></script>
<style> <style>
......
...@@ -12,6 +12,6 @@ ...@@ -12,6 +12,6 @@
"dependencies": { "dependencies": {
"canvas": "^2.11.2", "canvas": "^2.11.2",
"express": "^4.18.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