Hopding / pdf-lib

Create and modify PDF documents in any JavaScript environment
https://pdf-lib.js.org
MIT License
6.77k stars 647 forks source link

Getting cyclic node warning when i run PDFDocument function imported from 'pdf-lib' #1448

Open surya-dharmakrishnan opened 1 year ago

surya-dharmakrishnan commented 1 year ago

What were you trying to do?

I am trying to merge pdf documents by importing PDFDocument from 'pdf-lib' to merge pdf files into one pdf document in my react native mobile application development. During the first pdf file merge I am getting cyclic node modules warning.

How did you attempt to do it?

I am adding the code, where I am trying to merge my pdf files. ` //This is the function that i am using to get array buffer of individual pdf documents function get(url) { return new Promise((accept, reject) => { var req = new XMLHttpRequest(); req.open("GET", url, true); req.responseType = "arraybuffer"; req.onload = function (event) { var resp = req.response; if (resp) { accept(resp); } }; req.send(null); }); }

async function mergePDFDocuments(documents, currentFile, currentFilePath) { const mergedPdf = await PDFDocument.create(); for (let doc of documents) { const arrayBuffer = await get(doc) pdfDoc = await PDFDocument.load(arrayBuffer) const copiedPages = await mergedPdf.copyPages(pdfDoc, pdfDoc.getPageIndices()); copiedPages.forEach((page) => mergedPdf.addPage(page)) } const mergedPDfFile = await mergedPdf.saveAsBase64() const fileName = uuid.v4(); var path = RNFS.DocumentDirectoryPath + /${fileName}.pdf; RNFS.writeFile(path, mergedPDfFile, 'base64') .then((res) => { props.setSetListMerged([...props.setListMerged.filter(item => item.fileFetchName !== currentFile), { listName: listName, fileFetchName: fileName, path: path, isDownloaded: true, childFiles: props.setListFiles }]) RNFS.unlink(currentFilePath) props.setSetListFiles([]) props.setBusyStatus(false) navigation.navigate("setListScreen") }) .catch((err) => { console.log(err.message); }); } `

What actually happened?

I am adding the warning message here. The cyclic node issue occurs inside the pdf-lib node modules. Require cycle: node_modules/pdf-lib/cjs/api/PDFPage.js -> node_modules/pdf-lib/cjs/api/PDFDocument.js -> node_modules/pdf-lib/cjs/api/form/PDFForm.js -> node_modules/pdf-lib/cjs/api/form/PDFTextField.js -> node_modules/pdf-lib/cjs/api/PDFPage.js

Require cycles are allowed, but can result in uninitialized values. Consider refactoring to remove the need for a cycle.

What did you expect to happen?

I don't wan't this warning to pop up, as it slows down the performance. The warning occurs only in the first PDF file merge. Help me with the solution to handle this warning.

How can we reproduce the issue?

` function get(url) { return new Promise((accept, reject) => { var req = new XMLHttpRequest(); req.open("GET", url, true); req.responseType = "arraybuffer"; req.onload = function (event) { var resp = req.response; if (resp) { accept(resp); } }; req.send(null); }); }

async function mergePDFDocuments(documents, currentFile, currentFilePath) { const mergedPdf = await PDFDocument.create(); for (let doc of documents) { const arrayBuffer = await get(doc) pdfDoc = await PDFDocument.load(arrayBuffer) const copiedPages = await mergedPdf.copyPages(pdfDoc, pdfDoc.getPageIndices()); copiedPages.forEach((page) => mergedPdf.addPage(page)) } const mergedPDfFile = await mergedPdf.saveAsBase64() const fileName = uuid.v4(); var path = RNFS.DocumentDirectoryPath + /${fileName}.pdf; RNFS.writeFile(path, mergedPDfFile, 'base64') .then((res) => { props.setSetListMerged([...props.setListMerged.filter(item => item.fileFetchName !== currentFile), { listName: listName, fileFetchName: fileName, path: path, isDownloaded: true, childFiles: props.setListFiles }]) RNFS.unlink(currentFilePath) props.setSetListFiles([]) props.setBusyStatus(false) navigation.navigate("setListScreen") }) .catch((err) => { console.log(err.message); }); } `

Version

1.17.1

What environment are you running pdf-lib in?

React Native

Checklist

Additional Notes

No response