( widgets, pdfDoc, signyourself, scale, prefillImg )
| 2044 | |
| 2045 | //function for embed all type widgets in document using pdf-lib |
| 2046 | export const embedWidgetsToDoc = async ( |
| 2047 | widgets, |
| 2048 | pdfDoc, |
| 2049 | signyourself, |
| 2050 | scale, |
| 2051 | prefillImg |
| 2052 | ) => { |
| 2053 | // `fontBytes` is used to embed custom font in pdf |
| 2054 | const fontBytes = await fileasbytes( |
| 2055 | "https://cdn.opensignlabs.com/webfonts/times.ttf" |
| 2056 | ); |
| 2057 | pdfDoc.registerFontkit(fontkit); |
| 2058 | const font = await pdfDoc.embedFont(fontBytes, { subset: true }); |
| 2059 | let hasError = false; |
| 2060 | for (let item of widgets) { |
| 2061 | if (hasError) break; // Stop the outer loop if an error occurred |
| 2062 | const typeExist = item.pos.some((data) => data?.type); |
| 2063 | let updateItem; |
| 2064 | if (typeExist) { |
| 2065 | if (signyourself) { |
| 2066 | updateItem = item.pos; |
| 2067 | } else { |
| 2068 | // Checking required and optional widget types |
| 2069 | // For both required and optional widgets, handle signurl, defaultValue, and response as the widget's data |
| 2070 | // If the widget type is checkbox or radio (whether required or optional), we don't need to validate its value. |
| 2071 | // Instead, add an empty checkbox/radio, or if a value exists, mark the checkbox/radio as checked. |
| 2072 | updateItem = item.pos.filter( |
| 2073 | (data) => |
| 2074 | data?.options?.SignUrl || |
| 2075 | !isEmptyValue(data?.options?.defaultValue) || |
| 2076 | !isEmptyValue(data?.options?.response) || |
| 2077 | data?.type === "checkbox" || |
| 2078 | data?.type === radioButtonWidget |
| 2079 | ); |
| 2080 | } |
| 2081 | } else { |
| 2082 | updateItem = item.pos; |
| 2083 | } |
| 2084 | const ImgTypeWidget = [ |
| 2085 | "signature", |
| 2086 | "stamp", |
| 2087 | "initials", |
| 2088 | "image", |
| 2089 | drawWidget |
| 2090 | ]; |
| 2091 | const pageNo = item.pageNumber; |
| 2092 | const widgetsPositionArr = updateItem; |
| 2093 | const pages = pdfDoc.getPages(); |
| 2094 | const form = pdfDoc.getForm(); |
| 2095 | const page = pages[pageNo - 1]; |
| 2096 | // `page.getCropBox()` returns the visible area of the PDF page. |
| 2097 | // It provides an object with the following properties: |
| 2098 | // - x: the x-coordinate of the lower-left corner of the visible area |
| 2099 | // - y: the y-coordinate of the lower-left corner of the visible area |
| 2100 | // - width: the width of the visible area (x1 - x0) |
| 2101 | // - height: the height of the visible area (y1 - y0) |
| 2102 | // If the page does not explicitly define a CropBox, this method returns |
| 2103 | // the MediaBox instead, which represents the full physical size of the page. |
no test coverage detected