Skip to content

Commit 5d99a4a

Browse files
fix: multiple default signature error,sign-yourself placeholder location issue in mobile view
1 parent db1e81a commit 5d99a4a

File tree

6 files changed

+789
-947
lines changed

6 files changed

+789
-947
lines changed

microfrontends/SignDocuments/src/Component/PdfRequestFiles.js

Lines changed: 15 additions & 127 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import React, { useState, useRef, useEffect } from "react";
22
import { themeColor } from "../utils/ThemeColor/backColor";
3-
import { PDFDocument, rgb } from "pdf-lib";
3+
import { PDFDocument } from "pdf-lib";
44
import "../css/signature.css";
55
import axios from "axios";
66
import ModalHeader from "react-bootstrap/esm/ModalHeader";
@@ -16,7 +16,9 @@ import {
1616
contractDocument,
1717
getBase64FromIMG,
1818
getBase64FromUrl,
19-
urlValidator
19+
urlValidator,
20+
multiSignEmbed,
21+
embedDocId
2022
} from "../utils/Utils";
2123
import Loader from "./component/loader";
2224
import HandleError from "./component/HandleError";
@@ -319,29 +321,12 @@ function PdfRequestFiles() {
319321
if (isDocId) {
320322
pdfBase64 = await getBase64FromUrl(url);
321323
} else {
322-
for (let i = 0; i < allPages; i++) {
323-
const font = await pdfDoc.embedFont("Helvetica");
324-
325-
const fontSize = 10;
326-
const textContent =
327-
documentId && `OpenSign™ DocumentId: ${documentId}`;
328-
329-
const pages = pdfDoc.getPages();
330-
const page = pages[i];
331-
332-
page.drawText(textContent, {
333-
x: 10,
334-
y: page.getHeight() - 10,
335-
size: fontSize,
336-
font,
337-
color: rgb(0.5, 0.5, 0.5)
338-
});
339-
}
324+
//embed document's object id to all pages in pdf document
325+
await embedDocId(pdfDoc, documentId, allPages);
340326
pdfBase64 = await pdfDoc.saveAsBase64({
341327
useObjectStreams: false
342328
});
343329
}
344-
345330
for (let i = 0; i < pngUrl.length; i++) {
346331
const imgUrlList = pngUrl[i].pos;
347332
const pageNo = pngUrl[i].pageNumber;
@@ -381,112 +366,15 @@ function PdfRequestFiles() {
381366
}
382367
//else if signature is more than one then embed all sign with the use of pdf-lib
383368
else if (pngUrl.length > 0 && pngUrl[0].pos.length > 0) {
384-
for (let i = 0; i < allPages; i++) {
385-
const font = await pdfDoc.embedFont("Helvetica");
386-
387-
const fontSize = 10;
388-
const textContent =
389-
documentId && `OpenSign™ DocumentId: ${documentId} `;
390-
391-
const pages = pdfDoc.getPages();
392-
const page = pages[i];
393-
394-
page.drawText(textContent, {
395-
x: 10,
396-
y: page.getHeight() - 10,
397-
size: fontSize,
398-
font,
399-
color: rgb(0.5, 0.5, 0.5)
400-
});
401-
}
402-
403-
for (let i = 0; i < pngUrl.length; i++) {
404-
const pageNo = pngUrl[i].pageNumber;
405-
406-
const imgUrlList = pngUrl[i].pos;
407-
const pages = pdfDoc.getPages();
408-
const page = pages[pageNo - 1];
409-
410-
const images = await Promise.all(
411-
imgUrlList.map(async (url) => {
412-
let signUrl = url.SignUrl;
413-
if (url.ImageType === "image/png") {
414-
//function for convert signature png base64 url to jpeg base64
415-
const newUrl = await convertPNGtoJPEG(signUrl);
416-
signUrl = newUrl;
417-
}
418-
const checkUrl = urlValidator(signUrl);
419-
if (checkUrl) {
420-
signUrl = signUrl + "?get";
421-
}
422-
const res = await fetch(signUrl);
423-
return res.arrayBuffer();
424-
})
425-
);
426-
images.forEach(async (imgData, id) => {
427-
let img = await pdfDoc.embedJpg(imgData);
428-
const imgHeight = imgUrlList[id].Height
429-
? imgUrlList[id].Height
430-
: 60;
431-
const imgWidth = imgUrlList[id].Width
432-
? imgUrlList[id].Width
433-
: 150;
434-
const isMobile = window.innerWidth < 767;
435-
const newWidth = window.innerWidth;
436-
const scale = isMobile ? pdfOriginalWidth / newWidth : 1;
437-
const xPos = (pos) => {
438-
//checking both condition mobile and desktop view
439-
if (isMobile) {
440-
//if pos.isMobile false -- placeholder saved from desktop view then handle position in mobile view divided by scale
441-
if (pos.isMobile) {
442-
const x = pos.xPosition * (pos.scale / scale);
443-
return x * scale + 50;
444-
} else {
445-
const x = pos.xPosition / scale;
446-
return x * scale;
447-
}
448-
} else {
449-
//else if pos.isMobile true -- placeholder saved from mobile or tablet view then handle position in desktop view divide by scale
450-
if (pos.isMobile) {
451-
const x = pos.xPosition * pos.scale + 50;
452-
return x;
453-
} else {
454-
return pos.xPosition;
455-
}
456-
}
457-
};
458-
const yPos = (pos) => {
459-
//checking both condition mobile and desktop view
460-
const y = pos.yPosition / scale;
461-
if (isMobile) {
462-
//if pos.isMobile false -- placeholder saved from desktop view then handle position in mobile view divided by scale
463-
if (pos.isMobile) {
464-
const y = pos.yPosition * (pos.scale / scale);
465-
return page.getHeight() - y * scale - imgHeight;
466-
} else {
467-
return page.getHeight() - y * scale - imgHeight;
468-
}
469-
} else {
470-
//else if pos.isMobile true -- placeholder saved from mobile or tablet view then handle position in desktop view divide by scale
471-
if (pos.isMobile) {
472-
const y = pos.yPosition * pos.scale;
473-
return page.getHeight() - y - imgHeight;
474-
} else {
475-
return page.getHeight() - pos.yPosition - imgHeight;
476-
}
477-
}
478-
};
479-
page.drawImage(img, {
480-
x: xPos(imgUrlList[id]),
481-
y: yPos(imgUrlList[id]),
482-
width: imgWidth,
483-
height: imgHeight
484-
});
485-
});
486-
}
487-
const pdfBytes = await pdfDoc.saveAsBase64({
488-
useObjectStreams: false
489-
});
369+
//embed document's object id to all pages in pdf document
370+
await embedDocId(pdfDoc, documentId, allPages);
371+
//embed multi signature in pdf
372+
const pdfBytes = await multiSignEmbed(
373+
pngUrl,
374+
pdfDoc,
375+
pdfOriginalWidth,
376+
false
377+
);
490378

491379
signPdfFun(pdfBytes, documentId, pngUrl);
492380
}

microfrontends/SignDocuments/src/Component/SignYourselfPdf.js

Lines changed: 22 additions & 107 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import React, { useState, useRef, useEffect } from "react";
2-
import { PDFDocument, rgb } from "pdf-lib";
2+
import { PDFDocument } from "pdf-lib";
33
import "../css/./signature.css";
44
import sign from "../assests/sign3.png";
55
import stamp from "../assests/stamp2.png";
@@ -18,7 +18,9 @@ import ModalHeader from "react-bootstrap/esm/ModalHeader";
1818
import {
1919
convertPNGtoJPEG,
2020
contractDocument,
21-
getBase64FromIMG
21+
getBase64FromIMG,
22+
embedDocId,
23+
multiSignEmbed
2224
} from "../utils/Utils";
2325
import { useParams } from "react-router-dom";
2426
import Tour from "reactour";
@@ -30,7 +32,7 @@ import Header from "./component/header";
3032
import RenderPdf from "./component/renderPdf";
3133
import { contractUsers, contactBook, urlValidator } from "../utils/Utils";
3234
import { modalAlign } from "../utils/Utils";
33-
import { $ } from "select-dom";
35+
3436
//For signYourself inProgress section signer can add sign and complete doc sign.
3537
function SignYourSelf() {
3638
const [pdfDetails, setPdfDetails] = useState([]);
@@ -422,24 +424,7 @@ function SignYourSelf() {
422424
//checking if signature is only one then send image url in jpeg formate to server
423425
if (xyPostion.length === 1 && xyPostion[0].pos.length === 1) {
424426
//embed document's object id to all pages in pdf document
425-
for (let i = 0; i < allPages; i++) {
426-
const font = await pdfDoc.embedFont("Helvetica");
427-
428-
const fontSize = 10;
429-
const textContent =
430-
documentId && `OpenSign™ DocumentId: ${documentId}`;
431-
432-
const pages = pdfDoc.getPages();
433-
const page = pages[i];
434-
435-
page.drawText(textContent, {
436-
x: 10,
437-
y: page.getHeight() - 10,
438-
size: fontSize,
439-
font,
440-
color: rgb(0.5, 0.5, 0.5)
441-
});
442-
}
427+
await embedDocId(pdfDoc, documentId, allPages);
443428
const pdfBase64 = await pdfDoc.saveAsBase64({
444429
useObjectStreams: false
445430
});
@@ -475,85 +460,15 @@ function SignYourSelf() {
475460
}
476461
//else if signature is more than one then embed all sign with the use of pdf-lib
477462
else if (xyPostion.length > 0 && xyPostion[0].pos.length > 0) {
478-
for (let i = 0; i < allPages; i++) {
479-
const font = await pdfDoc.embedFont("Helvetica");
480-
481-
const fontSize = 10;
482-
const textContent =
483-
documentId && `OpenSign™ DocumentId: ${documentId} `;
484-
485-
const pages = pdfDoc.getPages();
486-
const page = pages[i];
487-
488-
page.drawText(textContent, {
489-
x: 10,
490-
y: page.getHeight() - 10,
491-
size: fontSize,
492-
font,
493-
color: rgb(0.5, 0.5, 0.5)
494-
});
495-
}
496-
for (let i = 0; i < pngUrl.length; i++) {
497-
const pageNo = pngUrl[i].pageNumber;
498-
499-
const imgUrlList = pngUrl[i].pos;
500-
const pages = pdfDoc.getPages();
501-
const page = pages[pageNo - 1];
502-
503-
const images = await Promise.all(
504-
imgUrlList.map(async (url) => {
505-
let signUrl = url.SignUrl;
506-
507-
if (url.ImageType === "image/png") {
508-
//function for convert signature png base64 url to jpeg base64
509-
const newUrl = await convertPNGtoJPEG(signUrl);
510-
signUrl = newUrl;
511-
}
512-
const checkUrl = urlValidator(signUrl);
513-
if (checkUrl) {
514-
signUrl = signUrl + "?get";
515-
}
516-
const res = await fetch(signUrl);
517-
518-
return res.arrayBuffer();
519-
})
520-
);
521-
images.forEach(async (imgData, id) => {
522-
let img = await pdfDoc.embedJpg(imgData);
523-
const imgHeight = imgUrlList[id].Height
524-
? imgUrlList[id].Height
525-
: 60;
526-
const imgWidth = imgUrlList[id].Width ? imgUrlList[id].Width : 150;
527-
const isMobile = window.innerWidth < 767;
528-
const newWidth = window.innerWidth;
529-
const scale = isMobile ? pdfOriginalWidth / newWidth : 1;
530-
531-
const posY = () => {
532-
if (isMobile) {
533-
if (id === 0) {
534-
return (
535-
page.getHeight() -
536-
imgUrlList[id].yPosition * scale -
537-
imgHeight
538-
);
539-
} else if (id > 0) {
540-
return page.getHeight() - imgUrlList[id].yPosition * scale;
541-
}
542-
} else {
543-
return page.getHeight() - imgUrlList[id].yPosition - imgHeight;
544-
}
545-
};
546-
page.drawImage(img, {
547-
x: isMobile
548-
? imgUrlList[id].xPosition * scale + imgWidth / 2
549-
: imgUrlList[id].xPosition,
550-
y: posY(),
551-
width: imgWidth,
552-
height: imgHeight
553-
});
554-
});
555-
}
556-
const pdfBytes = await pdfDoc.saveAsBase64({ useObjectStreams: false });
463+
//embed document's object id to all pages in pdf document
464+
await embedDocId(pdfDoc, documentId, allPages);
465+
//embed multi signature in pdf
466+
const pdfBytes = await multiSignEmbed(
467+
pngUrl,
468+
pdfDoc,
469+
pdfOriginalWidth,
470+
true
471+
);
557472
signPdfFun(pdfBytes, documentId);
558473
}
559474
setIsSignPad(false);
@@ -574,28 +489,28 @@ function SignYourSelf() {
574489
let singleSign;
575490

576491
const isMobile = window.innerWidth < 767;
577-
const newWidth = window.innerWidth;
492+
const newWidth = window.innerWidth - 32;
578493
const scale = isMobile ? pdfOriginalWidth / newWidth : 1;
579494
const imgWidth = xyPosData ? xyPosData.Width : 150;
580495
if (xyPostion.length === 1 && xyPostion[0].pos.length === 1) {
581496
const height = xyPosData.Height ? xyPosData.Height : 60;
582497
const bottomY = xyPosData.isDrag
583-
? xyPosData.yBottom * scale - height
498+
? xyPosData.yBottom * scale - height * scale
584499
: xyPosData.firstYPos
585-
? xyPosData.yBottom * scale - height + xyPosData.firstYPos
586-
: xyPosData.yBottom * scale - height;
500+
? xyPosData.yBottom * scale - height * scale + xyPosData.firstYPos
501+
: xyPosData.yBottom * scale - height * scale;
587502

588503
singleSign = {
589504
pdfFile: pdfBase64Url,
590505
docId: documentId,
591506
sign: {
592507
Base64: base64Url,
593508
Left: isMobile
594-
? xyPosData.xPosition * scale + imgWidth / 2
509+
? xyPosData.xPosition * scale + 43
595510
: xyPosData.xPosition,
596511
Bottom: bottomY,
597-
Width: xyPosData.Width ? xyPosData.Width : 150,
598-
Height: height,
512+
Width: xyPosData.Width ? xyPosData.Width * scale : 150 * scale,
513+
Height: height * scale,
599514
Page: pageNo
600515
}
601516
};

0 commit comments

Comments
 (0)