Skip to content

Multiline text fields drop German umlauts/special characters #80

Description

@JohannesNekes

Description

When filling a multiline text field with a value containing a line break (\n), the second line is rendered, but German special characters such as ö, ä, ü, and ß are rendered as blanks/spaces in the output pdf.

The field value itself appears to be stored correctly. The issue seems to happen during appearance generation for multiline text fields.

Reproduction

Given a PDF with a multiline text field:

import { PDF } from "@libpdf/core";

const pdf = await PDF.load(bytes);
const form = pdf.getForm();

form.fill({
  myTextField: "First line\nFähre Öl süß äöüß",
});

const output = await pdf.save();

The text field should render both lines correctly, including German special characters. The second line is rendered, but the characters such as ä, ö, ü, and ß appear as blanks/spaces. This only happens when \n is present in the input string.

Fix

I asked the AI for a suggestion and this solution works, at least in my particular case.

In src/document/forms/appearance-utils.ts, update encodeTextForFont()

if (isExistingFont(font) && font.isCIDFont) {
  return PdfString.fromBytes(font.encodeTextToBytes(text));
}

to

if (isExistingFont(font)) {
  return PdfString.fromBytes(font.encodeTextToBytes(text));
}
  1. Add /Encoding /WinAnsiEncoding for synthesized Standard 14 fallback fonts
    In src/document/forms/appearance-utils.ts update buildFontResources():
fontDict.set("Type", PdfName.of("Font"));
fontDict.set("Subtype", PdfName.of("Type1"));
fontDict.set("BaseFont", PdfName.of(mapToStandardFont(cleanName) ?? cleanName));

to

const baseFont = mapToStandardFont(cleanName) ?? cleanName;

fontDict.set("Type", PdfName.of("Font"));
fontDict.set("Subtype", PdfName.of("Type1"));
fontDict.set("BaseFont", PdfName.of(baseFont));

if (baseFont !== "Symbol" && baseFont !== "ZapfDingbats") {
  fontDict.set("Encoding", PdfName.of("WinAnsiEncoding"));
}

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions