Gracefully handle unsupported fonts.

I have a pdf with several form fields. It is a form to fill out. Since these are just text fields, the user may enter whatever they want. Emojis, foreign characters, etc. When they do this, they PDF now cannot flatten the form fields due to the missmatch in fonts. 

I believe this could be handled more gracefully. 

Test scenario:
- Create a fillable pdf with a form field and add a character to it not supported by the fonts. For example "😊"

- export the pdf data as json
- rebuild the pdf and save the bytes to a file

  Future<File> getPdf(WidgetRef ref) async {

    final doc = PdfDocument(inputBytes: await readData(pdfAssetPath));

    await fillOutFormFields(doc, ref);


    doc.form.importData(formData?.toList() ?? utf8.encode('{}'), DataFormat.json);

    await addSignatures(doc);

    doc.form.setDefaultAppearance(false);

    doc.form.flattenAllFields();


    var pdfPath = '${(await getTemporaryDirectory()).path}/${formType.downloadName}.pdf';

    File file = File(pdfPath);

    file.writeAsBytesSync(doc.saveSync());

    return file;

  }


If there are characters that the PDF can't handle, this will fail out with the below errors:

Fatal Exception: FlutterError

Invalid argument (The character is not supported by the font.): 8702. Error thrown .
--------

          Fatal Exception: FlutterError

0 ??? 0x0 StandardWidthTable._returnValue + 115 (pdf_font_metrics.dart:115)

1 ??? 0x0 StandardWidthTable.[] + 112 (pdf_font_metrics.dart:112)

2 ??? 0x0 PdfStandardFontHelper.getCharWidthInternal + 436 (pdf_standard_font.dart:436)

3 ??? 0x0 PdfStandardFontHelper.getLineWidth + 452 (pdf_standard_font.dart:452)

4 ??? 0x0 PdfFontHelper.getLineWidth + 286 (pdf_font.dart:286)

5 ??? 0x0 PdfStringLayouter._getLineWidth + 261 (pdf_string_layouter.dart:261)

6 ??? 0x0 PdfStringLayouter._layoutLine + 114 (pdf_string_layouter.dart:114)

7 ??? 0x0 PdfStringLayouter._doLayout + 57 (pdf_string_layouter.dart:57)

8 ??? 0x0 PdfStringLayouter.layout + 45 (pdf_string_layouter.dart:45)

9 ??? 0x0 PdfFont.measureString + 131 (pdf_font.dart:131)

10 ??? 0x0 PdfTextBoxFieldHelper.getFontHeight + 685 (pdf_text_box_field.dart:685)

11 ??? 0x0 PdfFieldHelper._getFont + 1324 (pdf_field.dart:1324)

12 ??? 0x0 PdfFieldHelper.font + 980 (pdf_field.dart:980)

13 ??? 0x0 PdfTextBoxField.font + 178 (pdf_text_box_field.dart:178)

14 ??? 0x0 PdfTextFormFieldHelper.build + 188 (pdf_text_box.dart:188)

15 ??? 0x0 _FormFieldContainerState._buildFormFields + 63 (form_field_container.dart:63)

16 ??? 0x0 _FormFieldContainerState.build + 43 (form_field_container.dart:43)

17 ??? 0x0 StatefulElement.build + 5599 (framework.dart:5599)

.....




5 Replies

IJ Irfana Jaffer Sadhik Syncfusion Team October 17, 2024 11:43 AM UTC

Hi Jeremy,

We suggest you try
PdfTrueTypeFont overload with the supported Emoji character font and set to the form fields and append the character. We have prepared a similar sample for your reference, please try this on your end and let us know the result.

Sample: https://www.syncfusion.com/downloads/support/directtrac/general/ze/pdf_demo_sample-1762457661

Follow the below links for more information:

https://help.syncfusion.com/flutter/pdf/working-with-text#draw-text-using-truetype-fonts


If you are still facing an issue, we request you to share the modified sample with us. So that we can assist with you further in this.


Regards,

Irfana J.







JI Jeremy Isenburg October 18, 2024 06:05 AM UTC

Hi Irfna,


I am already aware of this, but in these form fields on a pdf, they can enter ANY font, any emoji, and any text they want. Is there a TrueFont type that allows any text? I can't predict what the users will enter, the emoji was just an example. 


Better yet, can we just have an option to remove undesired fonts, rather than crashing?


Thanks



JT Jeyalakshmi Thangamarippandian Syncfusion Team October 21, 2024 12:57 PM UTC

Hi Jeremy,

Currently, our library can only render the provided text if the font supports the specific glyphs. If the font doesn't support the text, an exception is thrown, which is the default behavior. However, we are actively working on handling this exception more gracefully on our end. We will provide you with further updates on this by October 23, 2024.

 

Note: When handling the exception, the PDF form will only display correctly if supported fonts are available. Otherwise, certain characters in the field may fail to render properly.


Regards,

Jeyalakshmi T



IJ Irfana Jaffer Sadhik Syncfusion Team October 23, 2024 01:03 PM UTC

Hi Jeremy,


After further analysis, we identified that we can handle this exception on our end when filling form fields with unsupported fonts. We have logged this as a defect and will resolve it in our weekly release, which is scheduled for November 5, 2024.


You can track the status of the issue using the following feedback link:

https://www.syncfusion.com/feedback/62486/exception-occurs-while-drawing-emoji-symbol-in-pdf-form-fields


Note: After the fix, if unsupported fonts or characters are used to fill the form fields, they may not be preserved correctly due to the nature of this behavior.


Disclaimer: “Inclusion of this solution in the weekly release may change due to other factors including but not limited to QA checks and works reprioritization.”


Regards,

Irfana J.



IJ Irfana Jaffer Sadhik Syncfusion Team November 5, 2024 09:56 AM UTC

HI Jeremy,

The fix for the reported issue "Exception occurs while drawing emoji symbol in PDF form fields" has been included in our weekly release v27.1.58.

Kindly use the following link to refer to the latest release version,

Root Cause: An exception occurs when an emoji is added to the text box with an unsupported font, which cannot render the emoji. 


Regards,

Irfana J.


Loader.
Up arrow icon