diff --git a/src/utils.js b/src/utils.js index e6e26ee6181b3d62351db980eddb5aa2594b5fbc..276dfa92fc24a75f57cb77761fa888b5d019242e 100644 --- a/src/utils.js +++ b/src/utils.js @@ -55,3 +55,28 @@ export const baseName = (str) => return base; }; + + +export const fabricjs2pdfasPosition = (data) => { + let angle = -(data.angle - 360) % 360; + let bottom = data.bottom; + let left = data.left; + + if (data.angle === 90) { + bottom += data.height; + left -= data.height; + } else if (data.angle === 180) { + bottom += data.height * 2; + } else if (data.angle === 270) { + bottom += data.height; + left += data.height; + } + + return { + y: Math.round(bottom), + x: Math.round(left), + r: angle, + w: Math.round(data.width), // only width, no "height" allowed in PDF-AS + p: data.currentPage + }; +} \ No newline at end of file diff --git a/src/vpu-official-signature-pdf-upload.js b/src/vpu-official-signature-pdf-upload.js index b833d3c43219f277cb5ad428c69de19e99799e1f..3fe423385383fb8f875decb1ba3213c83be9edd5 100644 --- a/src/vpu-official-signature-pdf-upload.js +++ b/src/vpu-official-signature-pdf-upload.js @@ -129,42 +129,8 @@ class OfficialSignaturePdfUpload extends ScopedElementsMixin(VPUSignatureLitElem // prepare parameters to tell PDF-AS where and how the signature should be placed if (this.queuedFilesPlacementModes[key] === "manual") { const data = this.queuedFilesSignaturePlacements[key]; - if (data !== undefined) { - let angle = data.angle; - let bottom = data.bottom; - let left = data.left; - - if (angle !== 0) { - // attempt to adapt positioning in the rotated states to fit PDF-AS - switch (angle) { - case 90: - // 321 / 118; - bottom += data.width / 2.72034; - left -= data.width / 2.72034; - break; - case 180: - // 321 / 237; - bottom += data.width / 1.3544; - break; - case 270: - left += data.height; - bottom += data.height; - break; - } - - // adapt rotation to fit PDF-AS - const rotations = {0: 0, 90: 270, 180: 180, 270: 90}; - angle = rotations[data.angle]; - } - - params = { - y: Math.round(bottom), - x: Math.round(left), - r: angle, - w: Math.round(data.width), // only width, no "height" allowed in PDF-AS - p: data.currentPage - }; + params = utils.fabricjs2pdfasPosition(data); } } @@ -922,8 +888,8 @@ class OfficialSignaturePdfUpload extends ScopedElementsMixin(VPUSignatureLitElem </div> <vpu-pdf-preview lang="${this.lang}" signature-placeholder-image="official-signature-placeholder.png" - signature-width="146" - signature-height="42" + signature-width="145" + signature-height="45" @vpu-pdf-preview-accept="${this.storePDFData}" @vpu-pdf-preview-cancel="${this.hidePDF}"></vpu-pdf-preview> </div> diff --git a/src/vpu-pdf-preview.js b/src/vpu-pdf-preview.js index e8b4c228e0106233682076e4ee28c638762af22d..eaafc5756a34cae59a72e653825d0fe44682271e 100644 --- a/src/vpu-pdf-preview.js +++ b/src/vpu-pdf-preview.js @@ -31,8 +31,9 @@ export class PdfPreview extends ScopedElementsMixin(VPULitElement) { this.canvasToPdfScale = 1.0; this.currentPageOriginalHeight = 0; this.placeholder = 'signature-placeholder.png'; - this.signature_width = 80; - this.signature_height = 29; + this.signature_width = 42; + this.signature_height = 42; + this.border_width = 2; this._onWindowResize = this._onWindowResize.bind(this); } @@ -108,7 +109,7 @@ export class PdfPreview extends ScopedElementsMixin(VPULitElement) { // add signature image fabric.Image.fromURL(commonUtils.getAssetURL('local/vpu-signature/' + this.placeholder), function(image) { // add a red border around the signature placeholder - image.set({stroke: "#e4154b", strokeWidth: 8}); + image.set({stroke: "#e4154b", strokeWidth: that.border_width, strokeUniform: true}); // disable controls, we currently don't want resizing and do rotation with a button image.hasControls = false; @@ -353,13 +354,34 @@ export class PdfPreview extends ScopedElementsMixin(VPULitElement) { sendAcceptEvent() { const item = this.getSignatureRect(); + let left = item.get("left"); + let top = item.get("top"); + const angle = item.get("angle"); + + // fabricjs includes the stroke in the image position + // and we have to remove it + const border_offset = (this.border_width / 2); + if (angle === 0) { + left += border_offset; + top += border_offset; + } else if (angle === 90) { + left -= border_offset; + top += border_offset; + } else if (angle === 180) { + left -= border_offset; + top -= border_offset; + } else if (angle === 270) { + left += border_offset; + top -= border_offset; + } + const data = { "currentPage": this.currentPage, "width": item.get("width") * item.get("scaleX") / this.canvasToPdfScale, "height": item.get("height") * item.get("scaleY") / this.canvasToPdfScale, - "left": item.get("left") / this.canvasToPdfScale, - "top": item.get("top") / this.canvasToPdfScale, - "bottom": this.currentPageOriginalHeight - (item.get("top") / this.canvasToPdfScale), + "left": left / this.canvasToPdfScale, + "top": top / this.canvasToPdfScale, + "bottom": this.currentPageOriginalHeight - (top / this.canvasToPdfScale), "angle": item.get("angle") }; const event = new CustomEvent("vpu-pdf-preview-accept", diff --git a/src/vpu-qualified-signature-pdf-upload.js b/src/vpu-qualified-signature-pdf-upload.js index 29ec41d86bfaaefc61229699960da1d69f5c968e..8cec57e516ce7d5ae53f113439c683323ba48f9a 100644 --- a/src/vpu-qualified-signature-pdf-upload.js +++ b/src/vpu-qualified-signature-pdf-upload.js @@ -156,42 +156,8 @@ class QualifiedSignaturePdfUpload extends ScopedElementsMixin(VPUSignatureLitEle // prepare parameters to tell PDF-AS where and how the signature should be placed if (this.queuedFilesPlacementModes[key] === "manual") { const data = this.queuedFilesSignaturePlacements[key]; - if (data !== undefined) { - let angle = data.angle; - let bottom = data.bottom; - let left = data.left; - - if (angle !== 0) { - // attempt to adapt positioning in the rotated states to fit PDF-AS - switch (angle) { - case 90: - // 321 / 118; - bottom += data.width / 2.72034; - left -= data.width / 2.72034; - break; - case 180: - // 321 / 237; - bottom += data.width / 1.3544; - break; - case 270: - left += data.height; - bottom += data.height; - break; - } - - // adapt rotation to fit PDF-AS - const rotations = {0: 0, 90: 270, 180: 180, 270: 90}; - angle = rotations[data.angle]; - } - - params = { - y: Math.round(bottom), - x: Math.round(left), - r: angle, - w: Math.round(data.width), // only width, no "height" allowed in PDF-AS - p: data.currentPage - }; + params = utils.fabricjs2pdfasPosition(data); } } @@ -1068,6 +1034,8 @@ class QualifiedSignaturePdfUpload extends ScopedElementsMixin(VPUSignatureLitEle @click="${this.hidePDF}"><vpu-icon name="close"></vpu-icon></button> </div> <vpu-pdf-preview lang="${this.lang}" + signature-width="80" + signature-height="29" @vpu-pdf-preview-accept="${this.storePDFData}" @vpu-pdf-preview-cancel="${this.hidePDF}"></vpu-pdf-preview> </div>