improve performance of pdf to image function
This commit is contained in:
parent
c86ee45f11
commit
d173c7728c
@ -4,8 +4,7 @@ import whiteboard from "./whiteboard";
|
|||||||
import keybinds from "./keybinds";
|
import keybinds from "./keybinds";
|
||||||
import Picker from "vanilla-picker";
|
import Picker from "vanilla-picker";
|
||||||
import { dom } from "@fortawesome/fontawesome-svg-core";
|
import { dom } from "@fortawesome/fontawesome-svg-core";
|
||||||
import pdfjsLib from "pdfjs-dist/build/pdf";
|
import pdfjsLib from "pdfjs-dist/webpack";
|
||||||
import pdfjsLibWorker from "pdfjs-dist/build/pdf.worker";
|
|
||||||
|
|
||||||
function main() {
|
function main() {
|
||||||
|
|
||||||
@ -477,18 +476,21 @@ function main() {
|
|||||||
const base64data = reader.result;
|
const base64data = reader.result;
|
||||||
uploadImgAndAddToWhiteboard(base64data);
|
uploadImgAndAddToWhiteboard(base64data);
|
||||||
}
|
}
|
||||||
} else if (isPDFFileName(filename)) {
|
} else if (isPDFFileName(filename)) { //Handle PDF Files
|
||||||
var blob = e.originalEvent.dataTransfer.files[0];
|
var blob = e.originalEvent.dataTransfer.files[0];
|
||||||
pdfjsLib.GlobalWorkerOptions.workerSrc = pdfjsLibWorker;
|
|
||||||
|
|
||||||
var reader = new window.FileReader();
|
var reader = new window.FileReader();
|
||||||
reader.onloadend = function () {
|
reader.onloadend = function () {
|
||||||
var pdfData = new Uint8Array(this.result);
|
var pdfData = new Uint8Array(this.result);
|
||||||
|
|
||||||
|
var loadingTask = pdfjsLib.getDocument({ data: pdfData });
|
||||||
|
loadingTask.promise.then(function (pdf) {
|
||||||
|
console.log('PDF loaded');
|
||||||
|
|
||||||
var currentDataUrl = null;
|
var currentDataUrl = null;
|
||||||
var modalDiv = $('<div>' +
|
var modalDiv = $('<div>' +
|
||||||
'Page: <select></select>' +
|
'Page: <select></select> ' +
|
||||||
'<br><button>Upload to Whiteboard</button>' +
|
'<button style="margin-bottom: 3px;" class="modalBtn"><i class="fas fa-upload"></i> Upload to Whiteboard</button>' +
|
||||||
'<img style="width:100%;" src=""/>' +
|
'<img style="width:100%;" src=""/>' +
|
||||||
'</div>')
|
'</div>')
|
||||||
|
|
||||||
@ -503,6 +505,10 @@ function main() {
|
|||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
|
for (var i = 1; i < pdf.numPages + 1; i++) {
|
||||||
|
modalDiv.find("select").append('<option value="' + i + '">' + i + '</option>')
|
||||||
|
}
|
||||||
|
|
||||||
showBasicAlert(modalDiv, {
|
showBasicAlert(modalDiv, {
|
||||||
header: "Pdf to Image",
|
header: "Pdf to Image",
|
||||||
okBtnText: "cancel",
|
okBtnText: "cancel",
|
||||||
@ -511,15 +517,6 @@ function main() {
|
|||||||
|
|
||||||
showPDFPageAsImage(1);
|
showPDFPageAsImage(1);
|
||||||
function showPDFPageAsImage(pageNumber) {
|
function showPDFPageAsImage(pageNumber) {
|
||||||
var loadingTask = pdfjsLib.getDocument({ data: pdfData });
|
|
||||||
loadingTask.promise.then(function (pdf) {
|
|
||||||
console.log('PDF loaded');
|
|
||||||
|
|
||||||
if (!currentDataUrl) { //First load
|
|
||||||
for (var i = 1; i < pdf.numPages + 1; i++) {
|
|
||||||
modalDiv.find("select").append('<option value="' + i + '">' + i + '</option>')
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Fetch the page
|
// Fetch the page
|
||||||
pdf.getPage(pageNumber).then(function (page) {
|
pdf.getPage(pageNumber).then(function (page) {
|
||||||
@ -549,6 +546,7 @@ function main() {
|
|||||||
|
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
}
|
||||||
|
|
||||||
}, function (reason) {
|
}, function (reason) {
|
||||||
// PDF loading error
|
// PDF loading error
|
||||||
@ -556,7 +554,7 @@ function main() {
|
|||||||
showBasicAlert("Error loading pdf as image! Check that this is a vaild pdf file!");
|
showBasicAlert("Error loading pdf as image! Check that this is a vaild pdf file!");
|
||||||
console.error(reason);
|
console.error(reason);
|
||||||
});
|
});
|
||||||
}
|
|
||||||
}
|
}
|
||||||
reader.readAsArrayBuffer(blob);
|
reader.readAsArrayBuffer(blob);
|
||||||
} else {
|
} else {
|
||||||
|
Loading…
Reference in New Issue
Block a user