Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cannot add Raleway font with Unicode encoding #1273

Open
aleksvidak opened this issue Feb 27, 2025 · 0 comments
Open

Cannot add Raleway font with Unicode encoding #1273

aleksvidak opened this issue Feb 27, 2025 · 0 comments
Labels

Comments

@aleksvidak
Copy link

aleksvidak commented Feb 27, 2025

Describe the bug

I am trying to add Regular and Bold variants of https://fonts.google.com/specimen/Raleway font. Not directly through the this library, but by using FlyingSaucer. I think though, it might be an issue with LibrePDF.

I get exception:

java.lang.ArrayIndexOutOfBoundsException: Index 4100 out of bounds for length 1068
	at com.lowagie.text.pdf.TrueTypeFontSubSet.checkGlyphComposite(TrueTypeFontSubSet.java:365)
	at com.lowagie.text.pdf.TrueTypeFontSubSet.flatGlyphs(TrueTypeFontSubSet.java:360)
	at com.lowagie.text.pdf.TrueTypeFontSubSet.process(TrueTypeFontSubSet.java:148)
	at com.lowagie.text.pdf.TrueTypeFontUnicode.writeFont(TrueTypeFontUnicode.java:478)
	at com.lowagie.text.pdf.FontDetails.writeFont(FontDetails.java:410)
	at com.lowagie.text.pdf.PdfWriter.addSharedObjectsToBody(PdfWriter.java:1333)
	at com.lowagie.text.pdf.PdfWriter.close(PdfWriter.java:1266)
	at com.lowagie.text.pdf.PdfDocument.close(PdfDocument.java:851)
	at com.lowagie.text.Document.close(Document.java:535)
	at org.xhtmlrenderer.pdf.ITextRenderer.createPDF(ITextRenderer.java:406)
	at org.xhtmlrenderer.pdf.ITextRenderer.createPDF(ITextRenderer.java:326)
	at internal.project.package.PdfRenderer.renderPdf(PdfRenderer.java:45)

To Reproduce

Code to reproduce the issue

public class Test {
  public static void main(String[] args) {

    System.out.println("True Types (embedded)");

    // step 1: creation of a document-object
    Document document = new Document();

    try {

      // step 2: creation of the writer-object
      PdfWriter.getInstance(document, new FileOutputStream("unicode.pdf"));

      // step 3: we open the document
      document.open();

      // step 4: we add content to the document
      Font font =
          FontFactory.getFont(
              "fonts/Raleway-Regular.ttf", BaseFont.IDENTITY_H, BaseFont.EMBEDDED, 12);
      String text1 = "This is the quite popular True Type font 'Raleway'.";
      document.add(new Paragraph(text1, font));
    } catch (DocumentException | IOException de) {
      System.err.println(de.getMessage());
    }

    // step 5: we close the document
    document.close();
  }
}

Original code with Flyingsaucer:

@Service
@Slf4j
public class PdfRenderer {

  private static final List<String> FONTS =
      List.of("fonts/Raleway-Regular.ttf", "fonts/Raleway-Bold.ttf");

  private final TemplateEngine templateEngine;

  public PdfRenderer(TemplateEngine templateEngine) {
    ClassLoaderTemplateResolver resolver = new ClassLoaderTemplateResolver();
    resolver.setPrefix("templates/");
    resolver.setSuffix(".html");
    resolver.setTemplateMode(TemplateMode.HTML);

    templateEngine.setTemplateResolver(resolver);

    this.templateEngine = templateEngine;
  }

  public byte[] renderPdf(String templateName, Context context) {
    try (ByteArrayOutputStream outputStream = new ByteArrayOutputStream()) {
      ITextRenderer renderer = new ITextRenderer();

      for (String font : FONTS) {
        renderer.getFontResolver().addFont(font, BaseFont.IDENTITY_H, BaseFont.EMBEDDED);
      }

      renderer.setDocumentFromString(templateEngine.process(templateName, context));
      renderer.layout();
      renderer.createPDF(outputStream);
      return outputStream.toByteArray();
    } catch (Exception e) {
      e.printStackTrace();
      throw new PdfGenerationException("Error generating PDF", e);
    }
  }
}

Expected behavior

I would expect that Fonts are loaded and rendered. Trying test given above with direct usage of LibrePDF lib, it yields empty pdf. If I try another font, e.g. https://fonts.google.com/noto/specimen/Noto+Sans, it works.

System

@aleksvidak aleksvidak added the bug label Feb 27, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

1 participant