pdf-lib icon indicating copy to clipboard operation
pdf-lib copied to clipboard

setText method removes border & combing

Open krzyswiatkowski opened this issue 4 years ago • 4 comments

What were you trying to do?

Trying to use pdfTextField.setText method to fill pdf-form-field and leave combing & border visible

I have PDF file with prepared form in it. Most of text-fields in this PDF-file has combing & border visible. I want to fill those fields using setText method and leave combing & border visible.

Why were you trying to do this?

I want to fill fields inside pdf-form and leave combing & border visible (border-boxes for letters).

How did you attempt to do it?

Example of how I'm filling the field and how I've tried to restore combing &border after calling setText:

const pdfDoc = await PDFDocument.load(fileBuffer); // fileBuffer loaded before
const myFontBytes = await someUtils.getBytes('myfont.ttf')...  // returning font-file buffer from utility function

pdfDoc.registerFontkit(fontkit);
const font = await this.pdfDoc.embedFont(myFontBytes);

const pdfForm = pdfDoc.getForm();
const textField = pdfForm.getTextField('name');

textField.setText('SomeRandomName');

const [ widget ]= textField.acroField.getWidgets();
widget.getOrCreateBorderStyle().setWidth(1); // trying to restore border
textField.enableCombing(); // trying to restore combing
textField.updateAppearances(font); // trying to force update appearance

pdfForm.updateFieldAppearances(this.font);
pdfForm.flatten({ updateFieldAppearances: true });

const updatedBytes = await this.pdfDoc.save();
await saveToFile(updatedBytes) // utility function
 

What actually happened?

After using setText method, particular field loses combing & border. setting additonal border doesn't help. calling enableCombing doesn't help.

What did you expect to happen?

after using setText combing with border are still present particular field.

How can we reproduce the issue?

  1. Create simple PDF-form with atleast 1 text field with enabled combing & border
  2. Try to load it with pdf-lib and fill the field with setText method
  3. Save filled pdf-form to new file
  4. open file and check if form-field is populated and combing+border for that field remains

Version

1.17.0

What environment are you running pdf-lib in?

Node

Required Reading

Additional Notes

No response

krzyswiatkowski avatar Nov 05 '21 14:11 krzyswiatkowski

Would you please share a file that can be used to reproduce the issue?

Hopding avatar Nov 05 '21 19:11 Hopding

Hey @Hopding sorry for delayed response. I'm attaching ZIP archive with 5 files in it:

  • example-single-field.pdf - this is the template which should be used to fill text-field
  • pdf-test.js - JS file which allowed me to reproduce the issue (you might be interested in comments inside)
  • example-single-field-NOTflatten.pdf - result file when I DID NOT use form.flatten() method
  • example-single-field-flatten.pdf - result file when I used form.flatten() method before saving the doc
  • ubuntu-font.ttf - font used in example

reproduce-files.zip

So it looks like not the setText method is responsible for removing the border of combed fields - flatten method is.

Edit: Versions of the packages I've used:

pdf-lib: 1.17.0
@pdf-lib/fontkit: 1.1.1
node: v14.15.5
npm: 6.14.11

krzyswiatkowski avatar Nov 09 '21 16:11 krzyswiatkowski

I have tested your shown example - it's worked. Here my example code.

const {PDFDocument} = require('pdf-lib');
const fontkit = require('@pdf-lib/fontkit');
const fs = require('fs');
const ubuntuFontBytes = fs.readFileSync('ubuntu-font.ttf');

async function runTest() {  
  var templ= 'example-single-field.pdf';
  const pdfDoc = await PDFDocument.load(fs.readFileSync(templ)); 
  pdfDoc.registerFontkit(fontkit);
  const font = await pdfDoc.embedFont(ubuntuFontBytes);
  const form = pdfDoc.getForm();
  const combedField = form.getTextField('combedField');
  //console.log('Text field contents:', combedField.getText);
  //if (combedField.isCombed()) console.log('Combing is enabled');
  combedField.setText('Filled by script');
  //console.log("-2-" +combedField.isCombed());
  const [ widget ]= combedField.acroField.getWidgets();
  //console.log("W:::"+widget.getOrCreateBorderStyle);
  widget.getOrCreateBorderStyle().setWidth(1); // trying to restore border
  combedField.enableCombing(); // trying to restore combing
  combedField.updateAppearances(font); // trying to force update appearance
  form.updateFieldAppearances(this.font);
  form.flatten({ updateFieldAppearances: true });
  const fillpdfBytes = await pdfDoc.save({ useObjectStreams: true });
  fs.writeFileSync('test.pdf', fillpdfBytes);  //await pdfDoc.save());
}

runTest();

dcsline avatar Nov 16 '21 10:11 dcsline

@krzyswiatkowski thanks for sharing the files! I'll dig into this when I get a chance 👍

Hopding avatar Nov 28 '21 00:11 Hopding