Hopding / pdf-lib

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

Edited Form not able to save in React native #1555

Open vipulparmarmt opened 9 months ago

vipulparmarmt commented 9 months ago

What were you trying to do?

I want to save the form input field into pdf so that whenever user open the pdf file then he only able to read that not able to edit.

How did you attempt to do it?

I have read this solution on some github issue but its not working on react native to save the edited field so can some one please Help to how can ww do ?.

What actually happened?

I have tried above solution to save the edited pdf and use readonly but it actually not working.

What did you expect to happen?

The Input field edited by user will save the answer into pdf and the other user acess pdf as read only not able to edit it.

How can we reproduce the issue?

` import { PDFArray, PDFNumber, PDFBool, PDFName, } from 'pdf-lib';

const getAcroFields = (pdfDoc: any) => { const acroForm = getAcroForm(pdfDoc); if (!acroForm) return [];

const fieldRefs = acroForm.lookupMaybe(PDFName.of('Fields'), PDFArray);
if (!fieldRefs) return [];

const fields = new Array(fieldRefs.size());
for (let idx = 0, len = fieldRefs.size(); idx < len; idx++) {
  fields[idx] = fieldRefs.lookup(idx);
}
return fields;

};

const lockField = (acroField: any) => { acroField.set(PDFName.of('Ff'), PDFNumber.of(1 << 0 / Read Only /)); };

const saveFormField = async (url) => {

const pdfDoc = await PDFDocument.load(pdfSource);
const form = pdfDoc.getForm();
const fields = form.getFields();
fields.forEach((field) => {
  const type = field.constructor.name;
  const name = field.getName();
  console.log(`${type}: ${name}`);
});
const acroForm = getAcroForm(pdfDoc);
acroForm.set(PDFName.of('NeedAppearances'), PDFBool.True);

const acroFields = getAcroFields(pdfDoc);
acroFields.forEach((field) => lockField(field));

const pdfBytes = await pdfDoc.save();

};`

Version

1.17.1

What environment are you running pdf-lib in?

React Native

Checklist

Additional Notes

Please Help if anyone know how can we save the edited input fileds and save that as read only pdf. @Hopding Thanks