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

Free Support Forum - aspose.com

java.awt.HeadlessException thrown when converting HTML to PDF

Updating to Aspose PDF for Java 17.3.0 and higher throws an exception while trying to convert an HTML file to PDF under headless environments:

java.awt.HeadlessException
at sun.awt.HeadlessToolkit.getScreenSize(HeadlessToolkit.java:284)
at com.aspose.pdf.internal.p340.z5.(Unknown Source)
at com.aspose.pdf.internal.p312.z5.m1(Unknown Source)
at com.aspose.pdf.internal.p296.z3.m1(Unknown Source)
at com.aspose.pdf.internal.p312.z5.(Unknown Source)
at com.aspose.pdf.internal.p296.z3.(Unknown Source)
at com.aspose.pdf.z88.m1(Unknown Source)
at com.aspose.pdf.z88.m1(Unknown Source)
at com.aspose.pdf.z88.m1(Unknown Source)
at com.aspose.pdf.z88.m1(Unknown Source)
at com.aspose.pdf.ADocument.m1(Unknown Source)
at com.aspose.pdf.ADocument.(Unknown Source)
at com.aspose.pdf.Document.(Unknown Source)

I'm attaching a maven project with a test case to demonstrate the problem.

Hi Sid,


Thanks for contacting support.

I have tested the scenario with following simplified code while using Aspose.Pdf for .NET 17.4 in Eclipse Juno project with JDK 1.8, running over Windows 7 (x64) and I am unable to notice any issue. As per my observations, the HTML file is properly being converted to PDF and text is also being extracted from resultant PDF file.

Can you please share some further details regarding your working environment, so that we can further investigate this scenario in our environment. We are sorry for this inconvenience.

[Java]

com.aspose.pdf.Document
document = new com.aspose.pdf.Document(“C:\pdftest\test.html”, new
com.aspose.pdf.HtmlLoadOptions());<o:p></o:p>

try {

document.save("C:\\pdftest\\test.pdf");

} finally {

document.close();

}

extractText("C:\\pdftest\\test.pdf");

protected static void extractText(String input) {

com.aspose.pdf.Document document = new com.aspose.pdf.Document(input);

try {

TextAbsorber absorber = new TextAbsorber();

document.getPages().accept(absorber);

System.out.println(absorber.getText());

} finally {

document.close();

}

}

Hi Sid,


Thank you for contacting support. In your sample project, commons-io-2.5.jar does not resolve the reference of IOUtils class. We have used a previous version 2.4 of this Jar and when we executed your code, it throws a comparison failure error. We are unable to reproduce the same error (as you shared) in our working environment. We have added a new class and executed your test case as below:

[Java]
<span style=“font-family: “Courier New”, Consolas, Courier, monospace; font-size: small; white-space: pre; background-color: rgb(255, 255, 255);”>HeadlessExceptionTest exptest = <span class=“kwrd” style=“color: rgb(0, 0, 255); font-family: “Courier New”, Consolas, Courier, monospace; font-size: small; white-space: pre;”>new<span style=“font-family: “Courier New”, Consolas, Courier, monospace; font-size: small; white-space: pre; background-color: rgb(255, 255, 255);”> HeadlessExceptionTest();<span style=“font-family: “Courier New”, Consolas, Courier, monospace; font-size: small; white-space: pre; background-color: rgb(255, 255, 255);”>
exptest.convertDocument();
<span style=“font-family: “Courier New”, Consolas, Courier, monospace; font-size: small; white-space: pre; background-color: rgb(255, 255, 255);”>
Please share the complete steps of how to reproduce this error.

I’m not sure why you can’t locate the commons-io 2.5 jar but it should be available in the central repository:

http://repo1.maven.org/maven2/commons-io/commons-io/2.5/

The comparison failure is probably due to line separator differences. I’m running these tests on Mac OS (10.12.4) and Ubuntu Linux (14.04 server).

Please make sure you are executing the test as written with the @BeforeClass method in place:

@BeforeClass

public static void initLicense() throws Exception {

new License().setLicense("/tmp/Aspose.Total.Java.lic");

System.setProperty(“java.awt.headless”, “true”);

}

The system property (java.awt.headless) must be true to emulate how the code would run on a server environment without a UI running.

Including this thread for reference:


java.awt.HeadlessException

It looks like this may be a regression bug which was fixed in PDFNEWJAVA-34641 via version 10.3.0.

Hi Sid,


Thank you for the details. We managed to replicate the said error in our working environment using the latest version 17.4 of Aspose.Pdf for Java API and commons io 2.5 Jar. It has been logged under ticket ID PDFJAVA-36737 in our bug tracking system. We have linked your post to this ticket and will keep you informed regarding any available updates. We are sorry for the inconvenience caused.
snewby:
It looks like this may be a regression bug which was fixed in PDFNEWJAVA-34641 via version 10.3.0.
We have not marked the recent ticket ID PDFJAVA-36737 as regression because the code of test cases varies. However, we have added reference of this old ticket (PDFNEWJAVA-34641).

Thanks for the update

I have a similar problem with 17.3, 17.4 (cannot use 17.5 as maintenance expired at end of May.

Note that 17.2 works OK, so regression introduced in 17.3 and continued to at least 17.4.

sdxmessaging:
I have a similar problem with 17.3, 17.4 (cannot use 17.5 as maintenance expired at end of May.

Note that 17.2 works OK, so regression introduced in 17.3 and continued to at least 17.4.
Hi Andrew,

Thanks for contacting support.

Can you please confirm if you are facing the problem with all the documents or it is appearing for certain documents. Can you please share the documents causing this problem and details regarding your working environment, so that we can further look into this matter. We are sorry for this inconvenience.

We have replaced Itext with aspose-pdf and were running the 17.3 version. Once completed we ran into the HeadlessException when generating PDFs with HTML. Based on the comments we moved back to 17.2 and are able to move forward at this time. I hope this issue is high on the list to correct as it impacts our ability to use newer versions of aspose-pdf. Especially since this seems to be a regression of a fix from 10.3.

Throughout our project to remove Itext and replace it with aspose-pdf we ran into a number if issues where Itext handled things fine and the aspose-pdf implementation resulted in exceptions.

@SciQuest_Development,
We have logged this information under the same linked ticket ID PDFJAVA-36737. We will let you know once it is fixed.

We recommend our clients to post each their problem in the form of a use case, including source documents and code snippets along with the details of each problem. If you have any other problem which has not been reported yet, then share details with us. We will investigate and let you know about our findings.

Best Regards,
Imran Rafique