import PSPDFKitType, { AnnotationsUnion } from "./index"; import { Instance, WidgetAnnotation, ToolbarItem } from "./index"; declare const PSPDFKit: typeof PSPDFKitType const { List } = PSPDFKit.Immutable; const { Rect } = PSPDFKit.Geometry; const { SignatureFormField } = PSPDFKit.FormFields; const { DRAW, TYPE } = PSPDFKit.ElectronicSignatureCreationMode; const { DISABLED } = PSPDFKit.AutoSaveMode; interface EnvelopeResponse { receiverId: number; envelope: Envelope; } interface Envelope { id: number; userId: number; title: string; contractType: number; status: number; uuid: string; } class Settings { public static allowedToolbarItems: string[] = [ "sidebar-thumbnails", "sidebar-document-ouline", "sidebar-bookmarks", "pager", "pan", "zoom-out", "zoom-in", "zoom-mode", "spacer", "search" ] } export class App { public static Instance: Instance; // This function will be called in the ShowEnvelope.razor page // and will trigger loading of the Editor Interface public static async loadPDFFromUrl (container: string, envelopeKey: string) { console.debug("Loading PSPDFKit.."); const arrayBuffer = await App.loadDocument(`/api/download/${envelopeKey}`); const envelopeObject: EnvelopeResponse = await App.loadData(`/api/get-data/${envelopeKey}`); App.Instance = await App.loadPSPDFKit(arrayBuffer, container) App.configurePSPDFKit(this.Instance) console.debug(envelopeObject.envelope.id); console.debug("PSPDFKit configured!"); const id = PSPDFKit.generateInstantId() const annotation: WidgetAnnotation = App.createSignatureAnnotation(id, 150, 50, 50, 50, 0) const formField = new SignatureFormField({ name: id, annotationIds: List([annotation.id]) }) console.debug("Annotation created.") const [createdAnnotation] = await App.Instance.create([annotation, formField]) console.debug(createdAnnotation) } // Makes a call to the supplied url and fetches the binary response as an array buffer private static loadDocument(url: string): Promise { return fetch(url, { credentials: "include" }) .then(res => res.arrayBuffer()); } // Makes a call to the supplied url and fetches the json response as an object private static loadData(url: string): Promise { return fetch(url, { credentials: "include" }) .then(res => res.json()); } // Load the PSPDFKit UI by setting a target element as the container to render in // and a arraybuffer which represents the document that should be displayed. private static loadPSPDFKit(arrayBuffer: ArrayBuffer, container: string): Promise { const annotationPresets = PSPDFKit.defaultAnnotationPresets; console.log(annotationPresets) annotationPresets.ink = { lineWidth: 10 }; return PSPDFKit.load({ container: container, document: arrayBuffer, autoSaveMode: DISABLED, annotationPresets, electronicSignatures: { creationModes: [DRAW] } }) } private static configurePSPDFKit(instance: Instance) { instance.addEventListener("annotations.load", (loadedAnnotations) => { console.log("annotations loaded", loadedAnnotations.toJS()); }) instance.addEventListener("annotations.change", () => { console.log("annotations changed") }) instance.addEventListener("annotations.create", (createdAnnotations) => { const annotation: AnnotationsUnion = createdAnnotations[0]; console.log("annotations created", createdAnnotations.toJS()); }) const filteredItems: Array = instance.toolbarItems .filter((item) => Settings.allowedToolbarItems.includes(item.type)) const customItems: ToolbarItem[] = [ { type: "custom", id: "button-finish", title: "Abschließen", icon: ` `, onPress: this.handleFinish } ] instance.setToolbarItems(filteredItems.concat(customItems)) } private static async handleFinish(event: any) { await App.Instance.applyOperations([{ type: "flattenAnnotations" }]) //await downloadDocument(); } private static async downloadDocument() { const buffer = await App.Instance.exportPDF({ flatten: true }); const supportsDownloadAttribute = HTMLAnchorElement.prototype.hasOwnProperty("download"); const blob = new Blob([buffer], { type: "application/pdf" }); if (!supportsDownloadAttribute) { const reader = new FileReader(); reader.onloadend = function () { const dataUrl = reader.result; downloadPdf(dataUrl); }; reader.readAsDataURL(blob); } else { const objectUrl = window.URL.createObjectURL(blob); downloadPdf(objectUrl); window.URL.revokeObjectURL(objectUrl); } function downloadPdf(blob) { const a = document.createElement("a"); a.href = blob; a.style.display = "none"; a.download = "download.pdf"; a.setAttribute("download", "download.pdf"); document.body.appendChild(a); a.click(); document.body.removeChild(a); } } private static createSignatureAnnotation(id: string, x: number, y: number, top: number, left: number, pageIndex: number): WidgetAnnotation { const annotation = new PSPDFKit.Annotations.WidgetAnnotation({ id: id, pageIndex: pageIndex, formFieldName: id, boundingBox: new Rect({ width: x, height: y, top: top, left: left }) }) return annotation } }