-
Notifications
You must be signed in to change notification settings - Fork 361
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
NullPointerException when using PDF/A conformance #517
Comments
Hi @ThorbenKuck, Are those fonts being used when no PDF/A conformance? It looks like it can't load any of them. |
I am uncertain. What exactly makes a ttf font PFD/A conform? |
If this little sample (taken from here) works, it is a bug in this project, otherwise, the font file may be at fault (or PDFBOX's font loading): import java.io.IOException;
import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.pdmodel.PDPage;
import org.apache.pdfbox.pdmodel.PDPageContentStream;
import org.apache.pdfbox.pdmodel.font.PDFont;
import org.apache.pdfbox.pdmodel.font.PDType0Font;
public class CreatePdf {
public static void main(String[] args) throws IOException {
String filename = "whatever.pdf";
String message = "This font works.";
PDDocument doc = new PDDocument();
try {
PDPage page = new PDPage();
doc.addPage(page);
PDFont font = PDType0Font.load(doc, yourInputStreamFont, true);
PDPageContentStream contents = new PDPageContentStream(doc, page);
contents.beginText();
contents.setFont(font, 30);
contents.newLineAtOffset(50, 700);
contents.showText(message);
contents.endText();
contents.close();
doc.save(filename);
}
finally {
doc.close();
}
}
} |
I adjusted the example to Kotlin code like this: fun main() {
val filename = "whatever.pdf"
val message = "This font works."
val file = "/fonts/first-font.ttf"
val inputStream = PdfRenderer::class.java.getResourceAsStream(file) ?: throw IllegalArgumentException("Null")
val byteArrayInputStream = ByteArrayInputStream(inputStream.readAllBytes())
val doc = PDDocument()
doc.use {
val page = PDPage()
it.addPage(page)
val font: PDFont = PDType0Font.load(it, byteArrayInputStream, true)
val contents = PDPageContentStream(it, page)
contents.beginText()
contents.setFont(font, 30f)
contents.newLineAtOffset(50f, 700f)
contents.showText(message)
contents.endText()
contents.close()
it.save(filename)
}
} This example fails with this Exception upon loading the font:
I tried to verify this with different fonts and according to this even OpenSans is not PDF/A conform. To make sure, that it is not an encoding issue, i added the resources filter to the pom
I also checked in java, where the same behaviour occurs. I must be clearly doing something wrong, right? Because i doubt that OpenSans is not PDF/A conform. |
hi @ThorbenKuck you don't want to filter the ttf font, as they will be mangled by the maven-resources-plugin (being binary files), see #360 (comment). You need to exclude them. |
Sorry. You are right. I tried so many different combinations, that is also changed this before copying. Normally filtering is set to false. |
Sometimes when running, i also get the following exception:
|
this is most likely another issue (?). I think it would be simpler if you could provide a repository with a minimal example that reproduce the issue. From the stack trace I'm unfortunately not able to find a culprit. |
the last stack trace is due to: public void drawString(String s, float x, float y, JustificationInfo info) {
PDFont firstFont = _font.getFontDescription().get(0).getFont(); So most likely related to the font issue too. |
I'll have a look, if i can provide some repository with reduced information, which still keeps the gist of it, in my free time. Is this not checked prior to rendering? Because i expected that before calling |
The stack trace suggests there is not a valid font for one of the page margin areas such |
I am facing an issue, while creating a PDF with PDF/A conformance. Creating a normal PDF works fine, but once PDF/A conformance is enabled, the application exits with a NullPointerException.
The PDF i am trying to create has to be PDF/A conform.
My process of creating a PDF starts with an HTML template i process using Thymeleaf in Spring Boot. The application is written in Kotlin. The rendered PDF is at last returned as a ByteArray and provided by a controller as a Blob-Download. This part is working fine. The code generating the PDF looks something like this:
The code has been slightly altered, but the logic is kept the same.
The html contains css, which includes the fonts like this:
With
PdfAConformance.NONE
, everything works fine and the PDF is rendered as expected.However, once i change it to any PDF/A conformance, it no longer works. There is a very long list of logs to System.err, containing the following two lines. Repeated over and over again.
And finally, the process ends with the following three info logs and NullPointerException
This Exception is thrown after calling run on the Builder.
I have tried a lot of possible combinations using the conformance. All 3 PDF/A conformance settings, in combination with and without the fast mode, because this post suggested it might be an issue. I made sure to
[...] [use] a name that is not sans-serif, serif or monospace
as this question might have suggested. Also, adding the "-fs-font-subset: complete-font", as well as the url additionally in the css entries did not help, as it apparently did in this question. If possible, i want to include the fonts in code not in css, since resolving those urls was tricky and finicky in the past.However, those answers and all other answers to issues as well as on stack overflow did not help me in my case. I am lost. I have also tried to use different fonts, maybe they where broken, but every font i tried resulted in this exception.
Is there anything i am missing? Do i have to do something different to get PDF/A conformance? Maybe there is something special i have to bend for PDF/A conformance?
The text was updated successfully, but these errors were encountered: