Hopding / pdf-lib

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

Can we set the font size (to auto) in the new form filling API? #767

Closed Sjoerd82 closed 3 years ago

Sjoerd82 commented 3 years ago

In recent versions of pdf-lib we now have the form-filling API, no longer do we need to work directly with those AcroForms. As mentioned in #48:

pdf-lib now has form creation and filling APIs that should be used instead of the above example(s). See the form filling JSFiddle for a working example. Additional information is available in the README and API docs. The PDFTextField.setText method is of particular relevance to this issue.

However, at this point we may seem have lost the ability to set the font size to Auto? When I now use setText(), it reverts the size from auto (as specified in the PDF) to 12. Preventing the text to scale down. Is there a solution for this?

leapdev1 commented 3 years ago

Having the same issue wit the new form-filling API as well

Hopding commented 3 years ago

https://github.com/Hopding/pdf-lib/pull/983 might help here. If not, please fill out the new bug report form and we can pick things up there.

khanhubgit commented 1 year ago

You can also set form fields font size to Auto with JavaScript pdfDoc.addJavaScript("customJS", "for(var i = 0; i < this.numFields; i++) { this.getField(this.getNthFieldName(i)).textSize = 'Auto' }" )

zacwellmer commented 1 year ago

@Hopding I don't think #983 covers it since it sets the fontsize based on the field size. However, Acrobat has a way to set the font size to Auto, which will set the font size based on the content.

image

One work around is to avoid the assertPositive by calling

// @ts-expect-error
textField.acroField.setFontSize('Auto')
Dude29 commented 1 year ago

I'm having some trouble using the workaround provided by @zacwellmer.

It seems that the font size set to auto that way is not propagated to the PDF and it ends up still being overwritten with a numeric font size 😕

I've tried marking the fields as dirty after setting the size but that didn't seem to make a difference. I even tried setting the DA string with the Auto value in there following the suggestions from this comment but the problem persisted.

@Hopding Could you reopen this issue?

Filipe-QuickFlip commented 11 months ago

@Dude29 I'll start by apologizing for my English, but I imagine that what could solve your problem with dynamically sized text would be the solution I found, I defined textField.acroField.setFontSize(0), the text is being changed based on the number of characters, However, if the text is too short and the textField is large, the text will be too large and the opposite also happens, this solution solved my problem, I hope it helps

gbryant3000 commented 11 months ago

@Dude29

Issue We have had an issue where we have PDFs with multiple of the same named field in a form all set to auto font size, some fields have less room, some have more, so the filled size should vary, but pdf-lib was running the auto size logic once and setting all widgets of the field to the same font size.

Solution Comment out the call to updateDefaultAppearance in src/api/form/appearances.ts around line number 487:

  if (widgetColor || widgetFontSize !== undefined) {
    updateDefaultAppearance(widget, textColor, font, fontSize);
  } else {
    // updateDefaultAppearance(textField.acroField, textColor, font, fontSize);
  }

This prevents the source textField from having its font size set, allowing all display widgets to get autosize logic, this works for me, but could break something for you, good luck.

psamaan commented 2 weeks ago

I'm having the same issue as @Dude29, and unfortunately @Filipe-QuickFlip 's answer doesn't solve it.

Setting textField.acroField.setFontSize(0) leads to the size of the font to change at PDF creation time, but it is set to a number still, not to "Auto". The only way I've managed to achieve a setting of "Auto" so far has been following @khanhubgit 's suggestion.

@Hopding is there a way to achieve this without runtime javascript? guidance would be much appreciated, or if you see this as a bug I'm happy to open a bug report as well.