We're sorry Aspose doesn't work properply without JavaScript enabled.

Free Support Forum - aspose.com

NullPointerException transforming html document to PDF

I work in a project that uses Aspose.PDF for Java to create a PDF document from a basic HTML document. This works without issue on my windows local environment and in some of our testing Linux environments. But in our preproduction Linux environment it fails with a NullPointerException when executing new Document(htmlEmail, options) without a message or a stacktrace. This makes it very difficult to diagnose what could be the cause of the issue.

Below is an extract of the class that throws the error, I added a comment to show exactly the line with the problem.

I have two questions:

  1. Does anyone know if there is some debugging option that I can turn on in Aspose.PDF for Java to get more information about the error? Right now, I only know it is a NullPointerException, no stacktrace, no message
  2. Does anyone know what the issue could be at all?

Aspose.PDF for Java version is use: com.aspose:aspose-pdf:21.5

@Service 
@RequiredArgsConstructor 
public class PdfConverterService {

    private final TemplateEngine templateEngine;
    Logger logger;

    /**
     * Method to retrieve the email converted in PDF
     *
     * @param emailDto the EmailDto
     * @return the ByteArrayOutputStream of the PDF
     */
    public ByteArrayOutputStream convertHtml(EmailDTO emailDto) {
        balLogger.info("PdfConverterService templateEngine {}", templateEngine);
        String result = null;
        ByteArrayOutputStream baos = null;
        try {
            // ... html document creation removed for irrelevant ...
            result = templateEngine.process("email-for-storage", context);
            logger.info("PdfConverterService after templateEngine.process");
            InputStream htmlEmail = new ByteArrayInputStream(result.getBytes());

            var options = new HtmlLoadOptions();
            var document = new Document(htmlEmail, options); // Here a NullPointerException is thrown
            baos = new ByteArrayOutputStream();
            document.save(baos);
            logger.info("PdfConverterService after save");
            baos.close();
            document.close();
            logger.info("PdfConverterService after close");
        } catch (Exception e) {
            logger.info("convertHtml error. result after process {}, {}, {}", result, e, e.getMessage());
        }
        return baos;
    }
}

Thanks

Finally I got the stacktrace

java.lang.NullPointerException: null
        at com.aspose.pdf.internal.l46v.lI.lI(Unknown Source)
        at com.aspose.pdf.internal.l46v.lI.lI(Unknown Source)
        at com.aspose.pdf.internal.l44v.lc.lc(Unknown Source)
        at com.aspose.pdf.internal.l44v.lt.l1n(Unknown Source)
        at com.aspose.pdf.internal.l44v.l0l.l6f(Unknown Source)
        at com.aspose.pdf.internal.l44v.l0l.lI(Unknown Source)
        at com.aspose.pdf.internal.l44v.l0l.lI(Unknown Source)
        at com.aspose.pdf.internal.l44v.lk.lI(Unknown Source)
        at com.aspose.pdf.internal.l44v.lk.lI(Unknown Source)
        at com.aspose.pdf.internal.l44v.l1l.lI(Unknown Source)
        at com.aspose.pdf.internal.l44v.l1l.lI(Unknown Source)
        at com.aspose.pdf.internal.l44v.l1l.lI(Unknown Source)
        at com.aspose.pdf.internal.l44v.l1l.lI(Unknown Source)
        at com.aspose.pdf.internal.l44v.l3n.lI(Unknown Source)
        at com.aspose.pdf.internal.l44v.lf.lI(Unknown Source)
        at com.aspose.pdf.internal.l44v.l3n.lI(Unknown Source)
        at com.aspose.pdf.internal.l44v.l3n.lj(Unknown Source)
        at com.aspose.pdf.internal.l44v.lf.lI(Unknown Source)
        at com.aspose.pdf.internal.l44v.l3n.lI(Unknown Source)
        at com.aspose.pdf.internal.l44v.l4if.l2n(Unknown Source)
        at com.aspose.pdf.internal.l40v.lb.lf(Unknown Source)
        at com.aspose.pdf.internal.l41k.lk.l0p(Unknown Source)
        at com.aspose.pdf.internal.l41v.lf.lI(Unknown Source)
        at com.aspose.pdf.internal.l41v.lf.lI(Unknown Source)
        at com.aspose.pdf.internal.l41v.lf.lI(Unknown Source)
        at com.aspose.pdf.internal.l40l.lj.lI(Unknown Source)
        at com.aspose.pdf.internal.l40t.lI.lI(Unknown Source)
        at com.aspose.pdf.internal.l48k.lI.lI(Unknown Source)
        at com.aspose.pdf.internal.l40f.lt.lI(Unknown Source)
        at com.aspose.pdf.internal.l40f.lf.lj(Unknown Source)
        at com.aspose.pdf.internal.html.collections.lj.lj(Unknown Source)
        at com.aspose.pdf.internal.html.collections.lj.hasNext(Unknown Source)
        at com.aspose.pdf.internal.l48k.lI.lI(Unknown Source)
        at com.aspose.pdf.internal.html.rendering.HtmlRenderer.render(Unknown Source)
        at com.aspose.pdf.internal.html.rendering.HtmlRenderer.render(Unknown Source)
        at com.aspose.pdf.internal.html.rendering.Renderer.render(Unknown Source)
        at com.aspose.pdf.internal.html.rendering.Renderer.render(Unknown Source)
        at com.aspose.pdf.l7if.lf(Unknown Source)
        at com.aspose.pdf.l7if.lI(Unknown Source)
        at com.aspose.pdf.l7if.lI(Unknown Source)
        at com.aspose.pdf.ADocument.lI(Unknown Source)
        at com.aspose.pdf.ADocument.<init>(Unknown Source)
        at com.aspose.pdf.Document.<init>(Unknown Source)

@fera1984

It is hard to guess the cause of the issue without replicating it in our environment. However, can you please make sure that all windows fonts are installed in the environment where you are performing the conversion? Please try to install msttcorefonts package or copy all Windows fonts in the directory “/usr/share/fonts/truetype/msttcorefonts”. Also, please try using 21.8 version of the API and share your sample HTML file if issue still persists.

1 Like

Thanks for the reply,

Currently we are working in a very restrictive environment and it may take a while before I can install the fonts in that machine. In the meantime, is there a workaround to make Aspose only use fonts that are actually available in the system, regardless of how the document looks? Because currently my main issue is that I don’t know if Aspose works at all in our environment.

@fera1984

In order to use existing fonts, you can specify the fonts directory by using the method setLocalFontPaths(List< String > newFontPathsList). Furthermore, please share your sample file along with sample docker file so that we can further investigate if issue still persists.

1 Like

Using setLocalFontPaths we were able to overcome Aspose’s error. The issue is still not fixed but now the new exception comes from the JDK instead of Aspose. Thanks for the help!

@fera1984

It is always recommended to have basic Microsoft Fonts installed in the system because API needs them to generate PDF files. You can please further check at your end and share the requested information with us. We will try to investigate the issue at our end and share our feedback with you.