I want to insert the image inside the existing pdf. So i use the pdf-lib libary in react.When i use this code i got maximum call stack size exceeded error in mobile. how to solve this. Anyone can help with this?
const createpdf1 = async (signurl) => {
try {
const pdfData = new Uint8Array(await fetch(samplePdf).then((response) =>
response.arrayBuffer()));
const imageBlob = await fetch(ur).then((response) => response.arrayBuffer());
const pdfDoc = await PDFDocument.load(pdfData);
const pages = pdfDoc.getPages();
const fifthpage = pages[5];
const image = await pdfDoc.embedPng(imageBlob);
const { width, height } = image.scale(0.2); // Adjust the scale as needed
fifthpage.drawImage(image, {
x: 500, // Adjust the X and Y coordinates as needed
y: 180,
width,
height,
opacity: 1,
});
const modifiedPdfBytes = await pdfDoc.save();
const modifiedPdfDataUri = `data:application/pdf;base64,${btoa(
String.fromCharCode(...modifiedPdfBytes)
)}`;
setPdfDataUri(modifiedPdfDataUri)
return modifiedPdfDataUri;
} catch (error) {
console.error('Error loading or manipulating the PDF:', error);
throw error;
}
};