diff --git a/src/dbp-pdf-preview.js b/src/dbp-pdf-preview.js index 301ad03e55f18d6f222d1ad432926e8264fa7094..d89e0a3376bd9033b176373c008a89df47d737b3 100644 --- a/src/dbp-pdf-preview.js +++ b/src/dbp-pdf-preview.js @@ -10,7 +10,7 @@ import * as commonStyles from '@dbp-toolkit/common/styles'; import pdfjs from 'pdfjs-dist/es5/build/pdf.js'; import buildinfo from 'consts:buildinfo'; import {name as pkgName} from './../package.json'; -import {getPDFSignatureCount} from './utils.js'; +import {getPDFSignatureCount, readBinaryFileContent} from './utils.js'; const i18n = createI18nInstance(); @@ -209,36 +209,29 @@ export class PdfPreview extends ScopedElementsMixin(DBPLitElement) { this.isShowPlacement = isShowPlacement; this.isShowPage = true; - let reader = new FileReader(); - reader.onload = async () => { - //this.isPageLoaded = false; - const data = reader.result; + const data = await readBinaryFileContent(file); - // get handle of pdf document - try { - this.pdfDoc = await pdfjs.getDocument({data: data}).promise; - } catch (error) { - console.error(error); - - return; - } - - // total pages in pdf - this.totalPages = this.pdfDoc.numPages; - const page = placementData.currentPage || 1; + // get handle of pdf document + try { + this.pdfDoc = await pdfjs.getDocument({data: data}).promise; + } catch (error) { + console.error(error); + return; + } - // show the first page - // if the placementData has no values we want to initialize the signature position - await this.showPage(page, placementData["scaleX"] === undefined); + // total pages in pdf + this.totalPages = this.pdfDoc.numPages; + const page = placementData.currentPage || 1; - this.isPageLoaded = true; + // show the first page + // if the placementData has no values we want to initialize the signature position + await this.showPage(page, placementData["scaleX"] === undefined); - // fix width adaption after "this.isPageLoaded = true" - await this.showPage(page); - }; + this.isPageLoaded = true; - reader.readAsBinaryString(file); + // fix width adaption after "this.isPageLoaded = true" + await this.showPage(page); console.log(`Signature count: ${await getPDFSignatureCount(file)}`); } diff --git a/src/utils.js b/src/utils.js index ea75b14f9e677368382b8dfc06694a196604029b..98796e24871cff232beec70b5380f5ea78dd19c9 100644 --- a/src/utils.js +++ b/src/utils.js @@ -81,6 +81,25 @@ export const fabricjs2pdfasPosition = (data) => { }; }; +/** + * Returns the content of the file + * + * @param {File} file The file to read + * @returns {string} The content + */ +export const readBinaryFileContent = async (file) => { + return new Promise((resolve, reject) => { + let reader = new FileReader(); + reader.onload = () => { + resolve(reader.result); + }; + reader.onerror = () => { + reject(reader.error); + }; + reader.readAsBinaryString(file); + }); +}; + /** * Given a PDF file returns the amount of signatures found in it. * @@ -94,22 +113,10 @@ export const getPDFSignatureCount = async (file) => { const sigRegex = new RegExp( "/Type\\s*/Sig\\s*/Filter\\s*/Adobe.PPKLite\\s*/SubFilter\\s*(/ETSI\\.CAdES\\.detached|/adbe\\.pkcs7\\.detached)", "g"); - - const promise = new Promise((resolve, reject) => { - let reader = new FileReader(); - reader.onload = async () => { - let result = reader.result; - let matches = 0; - while (sigRegex.exec(result) !== null) { - matches++; - } - resolve(matches); - }; - reader.onerror = async () => { - reject(reader.error); - }; - reader.readAsBinaryString(file); - }); - - return promise; + const content = await readBinaryFileContent(file); + let matches = 0; + while (sigRegex.exec(content) !== null) { + matches++; + } + return matches; }; \ No newline at end of file