diff --git a/src/dbp-signature-lit-element.js b/src/dbp-signature-lit-element.js index fce8150780010c67d69e1f9e5865d09e523166e0..9cbcf55536fd39917661fe912ad777d51dd6a776 100644 --- a/src/dbp-signature-lit-element.js +++ b/src/dbp-signature-lit-element.js @@ -2,6 +2,7 @@ import * as utils from "./utils"; import {AdapterLitElement} from "@dbp-toolkit/provider/src/adapter-lit-element"; import JSONLD from "@dbp-toolkit/common/jsonld"; import * as commonUtils from "@dbp-toolkit/common/utils"; +import {getAnnotationFactoryFromFile} from "./utils"; export class DBPSignatureBaseLitElement extends AdapterLitElement { constructor() { @@ -134,9 +135,11 @@ export default class DBPSignatureLitElement extends DBPSignatureBaseLitElement { } async addAnnotationsToFile(file, annotations, i18n) { + // We need to work with the AnnotationFactory because the pdf file is broken if + // we add the multiple annotations to the file itself + let pdfFactory = await utils.getAnnotationFactoryFromFile(file); + await commonUtils.asyncObjectForEach(annotations, async (annotation) => { - console.log("annotation", annotation); - console.log("file before", file); const key1 = annotation.key1.trim(); const key2 = annotation.key2.trim(); const value = annotation.value.trim(); @@ -146,13 +149,12 @@ export default class DBPSignatureLitElement extends DBPSignatureBaseLitElement { } const annotationKey = key1 + '-' + key2; - file = await utils.addKeyValuePdfAnnotation(file, i18n, 'AppName', this.auth['user-full-name'], annotationKey, value); - console.log("file after", file); + pdfFactory = await utils.addKeyValuePdfAnnotationToAnnotationFactory( + pdfFactory, i18n, 'AppName', this.auth['user-full-name'], annotationKey, value); }); - console.log("addAnnotationsToFile file", file); - - return file; + // output the AnnotationFactory as File again + return utils.writeAnnotationFactoryToFile(pdfFactory, file); } /** diff --git a/src/utils.js b/src/utils.js index 63326b4feeabf26a3f1ef5c45c0ff0057a23a914..194e9574db46ba9931c98f5c9b46dc6d90e94357 100644 --- a/src/utils.js +++ b/src/utils.js @@ -142,6 +142,17 @@ export const getPDFSignatureCount = async (file) => { return matches; }; +/** + * Adds an annotation to a PDF file + * + * @param file + * @param i18n + * @param appName + * @param personName + * @param key + * @param value + * @returns {File} + */ export const addKeyValuePdfAnnotation = async (file, i18n, appName, personName, key, value) => { key = key.trim(); value = value.trim(); @@ -151,10 +162,58 @@ export const addKeyValuePdfAnnotation = async (file, i18n, appName, personName, return file; } + let annotationFactory = await getAnnotationFactoryFromFile(file); + annotationFactory = addKeyValuePdfAnnotationToAnnotationFactory(annotationFactory, i18n, appName, personName, key, value); + + return writeAnnotationFactoryToFile(annotationFactory, file); +}; + +/** + * Returns a File from an AnnotationFactory + * + * @param annotationFactory + * @param file + * @returns {File} + */ +export const writeAnnotationFactoryToFile = (annotationFactory, file) => { + const blob = annotationFactory.write(); + + return new File([blob], file.name, { type: file.type }); +} + +/** + * Creates an AnnotationFactory from a File + * + * @param file + * @returns AnnotationFactory + */ +export const getAnnotationFactoryFromFile = async (file) => { const data = await readArrayBufferFileContent(file); - let pdfFactory = new AnnotationFactory(data); - // console.log("pdfFactory.getAnnotations() before", pdfFactory.getAnnotations()); + return new AnnotationFactory(data); +}; + +/** + * Adds a key/value annotation to a AnnotationFactory and returns the AnnotationFactory + * + * @param annotationFactory + * @param i18n + * @param appName + * @param personName + * @param key + * @param value + * @returns PdfFactory + */ +export const addKeyValuePdfAnnotationToAnnotationFactory = (annotationFactory, i18n, appName, personName, key, value) => { + key = key.trim(); + value = value.trim(); + + // don't annotate if key or value are empty + if (key === '' || value === '') { + return annotationFactory; + } + + // console.log("annotationFactory.getAnnotations() before", annotationFactory.getAnnotations()); const page = 0; const rect = [1, 1, 1, 1]; @@ -164,19 +223,17 @@ export const addKeyValuePdfAnnotation = async (file, i18n, appName, personName, }); const contents = 'dbp-annotation-' + key + '=' + value; - // pdfFactory.checkRect(4, rect); + // annotationFactory.checkRect(4, rect); // Create single free text annotation with print flag and 0 font size - let annot = Object.assign(pdfFactory.createBaseAnnotation(page, rect, contents, author), { + let annotation = Object.assign(annotationFactory.createBaseAnnotation(page, rect, contents, author), { annotation_flag: 4, // enable print to be PDF/A conform color: {r: 1, g: 1, b: 1}, // white to (maybe) hide it better opacity: 0.001, // we can't set to 0 because of "if (opacity) {" defaultAppearance: "/Invalid_font 0 Tf" // font size 0 to (maybe) hide it better }); - annot.type = "/FreeText"; - pdfFactory.annotations.push(annot); - - const blob = pdfFactory.write(); + annotation.type = "/FreeText"; + annotationFactory.annotations.push(annotation); - return new File([blob], file.name, { type: file.type }); + return annotationFactory; };