Hopding / pdf-lib

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

Interactive form calculation not working in nodejs. #1258

Open MallRoy opened 2 years ago

MallRoy commented 2 years ago

What were you trying to do?

We are trying to fill the interactive form values using node js and generating a new pdf. 1655466835858.pdf

image

How did you attempt to do it?

const fs = require('fs');
const { PDFDocument } = require('pdf-lib');

const pdfBuffer = fs.readFileSync('./smartform2.pdf');

const interactiveForms = async () => {
  const pdfDoc = await PDFDocument.load(pdfBuffer);
  const form = pdfDoc.getForm();

  const fields = form.getTextField('Name');
  fields.setText('Testing');

  const fields1 = form.getTextField('num1');
  fields1.setText('5000');

  const fields2 = form.getTextField('num2');
  fields2.setText('3000');

  const fileBuffer = await pdfDoc.save({
    updateFieldAppearances: true,
  });
  const newPdfFile = `${Date.now()}.pdf`;
  fs.writeFileSync(newPdfFile, fileBuffer);
  console.log(newPdfFile);
};
interactiveForms();

What actually happened?

The Calculations are not happening as expected. The interactive form retains the old value in the total field.

What did you expect to happen?

I expect the calculations to happen while generating the PDF. I expect 8000 in the total field after the file generation.

How can we reproduce the issue?

interactive form.zip

Version

1.17.1

What environment are you running pdf-lib in?

Node

Checklist

Additional Notes

No response

dcsline commented 2 years ago

I could not found any handling of form field "total". Can you also send smartform2.pdf?

MallRoy commented 2 years ago

n you also send smartform2.pdf?

The approach i have tried and the pdf used is available here

dcsline commented 2 years ago

Thanks. How should calculate the total? I'm missing a mathematical operation like total = a + b.

MallRoy commented 2 years ago

Thanks. How should calculate the total? I'm missing a mathematical operation like total = a + b.

If you open the smartform2.pdf with chrome default pdf reader or adobe it performs the scripting. I think the scripting execution functionalities are not available in pdf-lib

dcsline commented 2 years ago

For this reason is my approach : handle the setting of the total value also in your javascript routine.

dcsline commented 2 years ago

I have checked your pdf file and found this fields: Field:::Name>>>PAGE:::1 Field:::num1>>>PAGE:::1 Field:::num2>>>PAGE:::1 Field:::Text4>>>PAGE:::1 So i believe "Text4" is your total field ???