com.lowagie.text.ExceptionConverter: Index 4100 out of bounds for length 1068

22 views
Skip to first unread message

Aleksandar Vidaković

unread,
Feb 26, 2025, 10:21:50 AMFeb 26
to Flying Saucer Users
Hi all,

I have recently started using Flyingsaucer with Thymeleaf.

I have troubles adding Raleway font with something like:

try (ByteArrayOutputStream outputStream = new ByteArrayOutputStream()) {
ITextRenderer renderer = new ITextRenderer();

renderer.getFontResolver().addFont("fonts/Raleway-Regular.ttf", BaseFont.IDENTITY_H, true);

renderer.setDocumentFromString(htmlContent);
renderer.layout();
renderer.createPDF(outputStream, true);
return outputStream.toByteArray();
} catch (DocumentException e) {
throw new RuntimeException(e);
}

Locally it seems it works, but once I build docker image I get error:

com.lowagie.text.ExceptionConverter: Index 4100 out of bounds for length 1068

I figured it might be that Maven is messing around with ttf, and I tried adding:

<plugin>
<artifactId>maven-resources-plugin</artifactId>
<configuration>
<encoding>UTF-8</encoding>
<nonFilteredFileExtensions>
<nonFilteredFileExtension>ttf</nonFilteredFileExtension>
</nonFilteredFileExtensions>
</configuration>
</plugin>

but this does not help.
I am using flying-saucer-pdf:9.11.4

Does anyone have any idea what could be the cause?

Thanks,
Aleksandar






Andrei Solntsev

unread,
Feb 26, 2025, 10:36:03 AMFeb 26
to flying-sa...@googlegroups.com
Hi Aleksandar!

At least, please show the full stack trace.

Andrei Solntsev


--
You received this message because you are subscribed to the Google Groups "Flying Saucer Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to flying-saucer-u...@googlegroups.com.
To view this discussion visit https://groups.google.com/d/msgid/flying-saucer-users/00b9f7ff-7755-4b90-970e-c815a4eede5cn%40googlegroups.com.

Aleksandar Vidaković

unread,
Feb 26, 2025, 2:21:53 PMFeb 26
to Flying Saucer Users
Hi Andrei,

I would gladly, but there is not much more to it.

Screenshot 2025-02-26 at 20.18.26.png


Method:

ublic byte[] generatePdfForReport(Long reportId)
throws IOException {
var report = reportService.getById(reportId);
ClassLoaderTemplateResolver templateResolver = new ClassLoaderTemplateResolver();
templateResolver.setSuffix(".html");
templateResolver.setTemplateMode(TemplateMode.HTML);
templateResolver.setCharacterEncoding("UTF-8");

var context getContext(report);

String htmlContent = templateEngine.process("thyme", context);


try (ByteArrayOutputStream outputStream = new ByteArrayOutputStream()) {
ITextRenderer renderer = new ITextRenderer();

renderer.getFontResolver().addFont("fonts/Raleway-Regular.ttf", true);
renderer.getFontResolver().addFont("fonts/Raleway-Bold.ttf", BaseFont.IDENTITY_H, true);


renderer.setDocumentFromString(htmlContent);
renderer.layout();
renderer.createPDF(outputStream, true);
return outputStream.toByteArray();
}
}

Thanks,
Aleks

Andrei Solntsev

unread,
Feb 27, 2025, 2:38:35 PMFeb 27
to flying-sa...@googlegroups.com
Sorry Aleks, but we also can't do anything without seeing this stacktrace.
It's your web application who hides the stackrace.

You need to configure your web app to properly log the error with stacktrace.

Andrei Solntsev


Aleksandar Vidaković

unread,
Feb 27, 2025, 3:53:55 PMFeb 27
to Flying Saucer Users
Hi Andrei,

Is this helpful?

Screenshot 2025-02-27 at 21.51.26.png

this is my latest version of the code which generates pdf:

@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);
resolver.setCharacterEncoding("UTF-8");

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);
}
}
}
Message has been deleted

Aleksandar Vidaković

unread,
Feb 27, 2025, 4:28:35 PMFeb 27
to Flying Saucer Users
I am not sure if this is specific to Flyingsaucer lib or LibrePDF, but for reference, if I try another font, e.g. https://fonts.google.com/noto/specimen/Noto+Serif, it works. But https://fonts.google.com/specimen/Raleway does not.

Aleksandar Vidaković

unread,
Feb 27, 2025, 4:39:37 PMFeb 27
to Flying Saucer Users
Reply all
Reply to author
Forward
0 new messages