parallax / jsPDF

Client-side JavaScript PDF generation for everyone.
https://parall.ax/products/jspdf
MIT License
29.21k stars 4.67k forks source link

Add support for AcroForm textFields with same fieldName #3361

Open imjosh opened 2 years ago

imjosh commented 2 years ago

I have read and understood the contribution guidelines. Version 2.5.0

The document created by this code appears and behaves correctly in Chrome's built-in pdf viewer. Two text fields both containing the value 'bar' should be displayed, and updating the value in one should change the value displayed in the other. A use case for this feature is multi-page documents with fields that are duplicated on each page (e.g. name and date on medical forms).

However, Adobe Acrobat does not display textField2. The PDF should contain one field element named 'foo' with two Kids. Instead it contains two field elements both named 'foo', one with the value 'bar' and the other with no value (my understanding is that violates ISO-32000-1 section 12.7.3.2 "Field names" creating an invalid PDF):

It is possible for different field dictionaries to have the same fully qualified field name if they are descendants of a common ancestor with that name and have no partial field names (T entries) of their own. Such field dictionaries are different representations of the same underlying field; they should differ only in properties that specify their visual appearance. In particular, field dictionaries with the same fully qualified field name shall have the same field type (FT), value (V), and default value (DV).

/* global jsPDF */
var doc = new jsPDF();
var {
  TextField,
} = jsPDF.AcroForm;

var textField = new TextField();
textField.Rect = [10, 10, 50, 10];
textField.fieldName = 'foo';
textField.value = 'bar';
doc.addField(textField);

var textField2 = new TextField();
textField2.Rect = [10, 30, 50, 10];
textField2.fieldName = 'foo';
doc.addField(textField2);

Document created by this code: jsPdf example.pdf

Correctly working document created with Adobe Acrobat: acrobat example.pdf

HackbrettXXX commented 2 years ago

Please provide a pull request and I'll merge it.