Skip to content

Commit 737b33d

Browse files
fix: add try catch to convert pdf array buffer
1 parent ac214d0 commit 737b33d

File tree

4 files changed

+40
-9
lines changed

4 files changed

+40
-9
lines changed

apps/OpenSign/src/constant/Utils.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1895,3 +1895,19 @@ export const copytoData = (text) => {
18951895
document.body.removeChild(textArea);
18961896
}
18971897
};
1898+
1899+
export const convertPdfArrayBuffer = async (url) => {
1900+
try {
1901+
const response = await fetch(url);
1902+
// Check if the response was successful (status 200)
1903+
if (!response.ok) {
1904+
return "Error";
1905+
}
1906+
// Convert the response to ArrayBuffer
1907+
const arrayBuffer = await response.arrayBuffer();
1908+
return arrayBuffer;
1909+
} catch (error) {
1910+
console.error("Error fetching data:", error);
1911+
return "Error";
1912+
}
1913+
};

apps/OpenSign/src/pages/PdfRequestFiles.js

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,8 @@ import {
2323
addDefaultSignatureImg,
2424
radioButtonWidget,
2525
replaceMailVaribles,
26-
fetchSubscription
26+
fetchSubscription,
27+
convertPdfArrayBuffer
2728
} from "../constant/Utils";
2829
import Loader from "../primitives/LoaderWithMsg";
2930
import HandleError from "../primitives/HandleError";
@@ -170,8 +171,12 @@ function PdfRequestFiles() {
170171
if (documentData && documentData.length > 0) {
171172
const url = documentData[0] && documentData[0]?.URL;
172173
//convert document url in array buffer format to use embed widgets in pdf using pdf-lib
173-
const arrayBuffer = await fetch(url).then((res) => res.arrayBuffer());
174-
setPdfArrayBuffer(arrayBuffer);
174+
const arrayBuffer = await convertPdfArrayBuffer(url);
175+
if (arrayBuffer === "Error") {
176+
setHandleError("Error: Something went wrong!");
177+
} else {
178+
setPdfArrayBuffer(arrayBuffer);
179+
}
175180
if (isEnableSubscription) {
176181
await checkIsSubscribed(documentData[0]?.ExtUserPtr?.Email);
177182
}

apps/OpenSign/src/pages/PlaceHolderSign.js

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,8 @@ import {
3333
getTenantDetails,
3434
replaceMailVaribles,
3535
copytoData,
36-
fetchSubscription
36+
fetchSubscription,
37+
convertPdfArrayBuffer
3738
} from "../constant/Utils";
3839
import RenderPdf from "../components/pdf/RenderPdf";
3940
import { useNavigate } from "react-router-dom";
@@ -266,8 +267,12 @@ function PlaceHolderSign() {
266267
if (documentData && documentData.length > 0) {
267268
const url = documentData[0] && documentData[0]?.URL;
268269
//convert document url in array buffer format to use embed widgets in pdf using pdf-lib
269-
const arrayBuffer = await fetch(url).then((res) => res.arrayBuffer());
270-
setPdfArrayBuffer(arrayBuffer);
270+
const arrayBuffer = await convertPdfArrayBuffer(url);
271+
if (arrayBuffer === "Error") {
272+
setHandleError("Error: Something went wrong!");
273+
} else {
274+
setPdfArrayBuffer(arrayBuffer);
275+
}
271276
setExtUserId(documentData[0]?.ExtUserPtr?.objectId);
272277
if (isEnableSubscription) {
273278
checkIsSubscribed(documentData[0]?.ExtUserPtr?.Email);

apps/OpenSign/src/pages/SignyourselfPdf.js

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,8 @@ import {
2828
getDate,
2929
textWidget,
3030
getTenantDetails,
31-
checkIsSubscribed
31+
checkIsSubscribed,
32+
convertPdfArrayBuffer
3233
} from "../constant/Utils";
3334
import { useParams } from "react-router-dom";
3435
import Tour from "reactour";
@@ -193,8 +194,12 @@ function SignYourSelf() {
193194
setExtUserId(documentData[0]?.ExtUserPtr?.objectId);
194195
const url = documentData[0] && documentData[0]?.URL;
195196
//convert document url in array buffer format to use embed widgets in pdf using pdf-lib
196-
const arrayBuffer = await fetch(url).then((res) => res.arrayBuffer());
197-
setPdfArrayBuffer(arrayBuffer);
197+
const arrayBuffer = await convertPdfArrayBuffer(url);
198+
if (arrayBuffer === "Error") {
199+
setHandleError("Error: Something went wrong!");
200+
} else {
201+
setPdfArrayBuffer(arrayBuffer);
202+
}
198203
isCompleted = documentData[0].IsCompleted && documentData[0].IsCompleted;
199204
if (isCompleted) {
200205
setIsCompleted(true);

0 commit comments

Comments
 (0)