Skip to content

Commit a4d1df8

Browse files
Merge pull request #1262 from OpenSignLabs/validation
feat: add signature and png type image with transparent background in pdf
2 parents eda6113 + ba15611 commit a4d1df8

File tree

5 files changed

+37
-30
lines changed

5 files changed

+37
-30
lines changed

apps/OpenSign/src/components/pdf/Placeholder.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -725,7 +725,9 @@ function Placeholder(props) {
725725
: props?.pos?.zIndex
726726
? props.pos.zIndex
727727
: "5",
728-
background: props.data ? props.data.blockColor : "rgb(203 233 237)"
728+
background: props.data
729+
? props.data?.blockColor + "b0"
730+
: "rgba(203, 233, 237, 0.69)"
729731
}}
730732
onDrag={() => {
731733
setDraggingEnabled(true);

apps/OpenSign/src/components/pdf/SignPad.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -596,7 +596,7 @@ function SignPad({
596596
borderRadius: "2px"
597597
}
598598
}}
599-
backgroundColor="rgb(255, 255, 255)"
599+
// backgroundColor="rgb(255, 255, 255)"
600600
onEnd={() =>
601601
handleSignatureChange(canvasRef.current?.toDataURL())
602602
}

apps/OpenSign/src/constant/Utils.js

Lines changed: 3 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1350,11 +1350,6 @@ export const multiSignEmbed = async (
13501350
widgetsPositionArr.map(async (url) => {
13511351
let signUrl = url.SignUrl && url.SignUrl;
13521352
if (signUrl) {
1353-
if (url.ImageType === "image/png") {
1354-
//function for convert signature png base64 url to jpeg base64
1355-
const newUrl = await convertPNGtoJPEG(signUrl);
1356-
signUrl = newUrl;
1357-
}
13581353
const res = await fetch(signUrl);
13591354
return res.arrayBuffer();
13601355
}
@@ -1364,19 +1359,14 @@ export const multiSignEmbed = async (
13641359
widgetsPositionArr.forEach(async (position, id) => {
13651360
let img;
13661361
if (["signature", "stamp", "initials", "image"].includes(position.type)) {
1367-
if (
1368-
(position.ImageType && position.ImageType === "image/png") ||
1369-
position.ImageType === "image/jpeg"
1370-
) {
1362+
if (position.ImageType && position.ImageType === "image/jpeg") {
13711363
img = await pdfDoc.embedJpg(images[id]);
13721364
} else {
13731365
img = await pdfDoc.embedPng(images[id]);
13741366
}
13751367
} else if (!position.type) {
1376-
if (
1377-
(position.ImageType && position.ImageType === "image/png") ||
1378-
position.ImageType === "image/jpeg"
1379-
) {
1368+
// to handle old widget when only stamp and signature are exists
1369+
if (position.ImageType && position.ImageType === "image/jpeg") {
13801370
img = await pdfDoc.embedJpg(images[id]);
13811371
} else {
13821372
img = await pdfDoc.embedPng(images[id]);

apps/OpenSign/src/pages/Managesign.js

Lines changed: 30 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,13 @@ import React, { useState, useRef, useEffect } from "react";
22
import SignatureCanvas from "react-signature-canvas";
33
import "../styles/managesign.css";
44
import "../styles/signature.css";
5-
import { toDataUrl } from "../constant/Utils";
5+
import { generateTitleFromFilename, toDataUrl } from "../constant/Utils";
66
import Parse from "parse";
77
import { SaveFileSize } from "../constant/saveFileSize";
88
import Alert from "../primitives/Alert";
99
import Loader from "../primitives/Loader";
1010
import { useTranslation } from "react-i18next";
11+
import sanitizeFileName from "../primitives/sanitizeFileName";
1112
const ManageSign = () => {
1213
const { t } = useTranslation();
1314
const [penColor, setPenColor] = useState("blue");
@@ -46,14 +47,22 @@ const ManageSign = () => {
4647
if (signRes) {
4748
const res = signRes.toJSON();
4849
setId(res.objectId);
49-
setSignName(res?.SignatureName);
50-
setImage(res.ImageURL);
50+
if (res?.SignatureName) {
51+
const sanitizename = generateTitleFromFilename(res?.SignatureName);
52+
const replaceSpace = sanitizeFileName(sanitizename);
53+
setSignName(replaceSpace);
54+
}
55+
setImage(res?.ImageURL);
5156
if (res && res.Initials) {
5257
setInitials(res.Initials);
5358
setIsInitials(true);
5459
}
5560
} else {
56-
setSignName(User?.get("name") || "");
61+
if (User?.get("name")) {
62+
const sanitizename = generateTitleFromFilename(User?.get("name"));
63+
const replaceSpace = sanitizeFileName(sanitizename);
64+
setSignName(replaceSpace);
65+
}
5766
}
5867
setIsLoader(false);
5968
} catch (err) {
@@ -87,7 +96,9 @@ const ManageSign = () => {
8796
initailsRef.current.clear();
8897
}
8998
setInitials("");
90-
setIsValue(true);
99+
if (image) {
100+
setIsValue(true);
101+
}
91102
setIsInitials(false);
92103
};
93104

@@ -101,34 +112,39 @@ const ManageSign = () => {
101112
const base64Img = await toDataUrl(file);
102113
setImage(base64Img);
103114
setIsValue(true);
115+
} else {
116+
setImage("");
117+
setIsValue(false);
104118
}
105119
};
106120
const handleSubmit = async (e) => {
107121
e.preventDefault();
108-
const isUrl = image.includes("https");
122+
const isUrl = image.includes("https") || image.includes("http");
109123

110124
if (!isvalue) {
111125
setWarning(true);
112126
setTimeout(() => setWarning(false), 1000);
113127
} else {
114128
setIsLoader(true);
115-
const replaceSpace = signName.replace(/ /g, "_");
129+
const sanitizename = generateTitleFromFilename(signName);
130+
const replaceSpace = sanitizeFileName(sanitizename);
116131
let file;
117132
if (signature) {
118133
file = base64StringtoFile(signature, `${replaceSpace}_sign.png`);
119134
} else {
120-
if (!isUrl) {
135+
if (image && !isUrl) {
121136
file = base64StringtoFile(image, `${replaceSpace}__sign.png`);
122137
}
123138
}
124139
let imgUrl;
125-
if (!isUrl) {
140+
if (file && !isUrl) {
126141
imgUrl = await uploadFile(file);
127142
} else {
128143
imgUrl = image;
129144
}
130145
let initialsUrl = "";
131-
const isInitialsUrl = Initials.includes("https");
146+
const isInitialsUrl =
147+
Initials.includes("https") || Initials.includes("http");
132148
if (!isInitialsUrl && Initials) {
133149
const initialsImg = base64StringtoFile(
134150
Initials,
@@ -146,7 +162,7 @@ const ManageSign = () => {
146162
}
147163
};
148164
function base64StringtoFile(base64String, filename) {
149-
var arr = base64String.split(","),
165+
let arr = base64String.split(","),
150166
mime = arr[0].match(/:(.*?);/)[1],
151167
bstr = atob(arr[1]),
152168
n = bstr.length,
@@ -182,7 +198,7 @@ const ManageSign = () => {
182198
const updateSign = new Parse.Object(signCls);
183199
updateSign.id = id;
184200
updateSign.set("Initials", obj.initialsUrl ? obj.initialsUrl : "");
185-
updateSign.set("ImageURL", obj.url);
201+
updateSign.set("ImageURL", obj.url ? obj.url : "");
186202
updateSign.set("SignatureName", obj.name);
187203
updateSign.set("UserId", userId);
188204
const res = await updateSign.save();
@@ -281,7 +297,7 @@ const ManageSign = () => {
281297
height: "180px",
282298
className: "signatureCanvas rounded-box"
283299
}}
284-
backgroundColor="rgb(255, 255, 255)"
300+
// backgroundColor="rgb(255, 255, 255)"
285301
onEnd={() =>
286302
handleSignatureChange(canvasRef.current.toDataURL())
287303
}
@@ -376,7 +392,7 @@ const ManageSign = () => {
376392
canvasProps={{
377393
className: "intialSignature rounded-box"
378394
}}
379-
backgroundColor="rgb(255, 255, 255)"
395+
// backgroundColor="rgb(255, 255, 255)"
380396
onEnd={() =>
381397
handleInitialsChange(initailsRef.current.toDataURL())
382398
}

apps/OpenSign/src/styles/managesign.css

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
border: 1px solid black;
55
background: white;
66
border-radius: 5px;
7-
animation: inAnimation 2s ease-in-out;
87
z-index: 1;
98
}
109

0 commit comments

Comments
 (0)