HTML to PDF Conversion java.awt.HeadlessException

Hello we receive the following exception when trying to convert an html file to pdf using the Aspose.HTML library.

Aspose.HTML version 18.5

java.awt.HeadlessException
at sun.awt.HeadlessToolkit.getScreenSize(HeadlessToolkit.java:284)
at com.aspose.html.internal.p230.z8.(Unknown Source)
at com.aspose.html.Configuration.initializeServices(Unknown Source)
at com.aspose.html.Configuration.(Unknown Source)
at com.aspose.html.HTMLDocument.(Unknown Source)
at com.aspose.html.HTMLDocument.(Unknown Source)…

Conversion Code:
com.aspose.html.rendering.pdf.PdfRenderingOptions pdfOptions = null;
com.aspose.html.rendering.pdf.PdfDevice pdfDevice = null;
com.aspose.html.rendering.HtmlRenderer htmlRenderer = null;
com.aspose.html.HTMLDocument htmlDocument = null;

	try {
		
		pdfOptions = new com.aspose.html.rendering.pdf.PdfRenderingOptions();

		Margin m = new Margin(9, 17, 9, 9);
		Size s = new Size(816, 1056);  
				
		Page p = new Page(s,m);

		pdfOptions.getPageSetup().setAnyPage(p);

		pdfDevice = new com.aspose.html.rendering.pdf.PdfDevice(pdfOptions, newPdfFile.getPath());

		htmlRenderer = new com.aspose.html.rendering.HtmlRenderer();
		
		htmlDocument = new com.aspose.html.HTMLDocument(newHtmlFile.getPath());

		htmlRenderer.render(pdfDevice, htmlDocument);

	} catch(Exception e) {
		//Exception code
		
	} finally {
		if (htmlDocument != null)
			htmlDocument.dispose();
		if (htmlRenderer != null)
			htmlRenderer.dispose();
		if (pdfDevice != null)
			pdfDevice.dispose();
	}

The war file that performs this conversion is deployed on a Linux wildfly 22 server.

Sample htmlhtml.zip (738 Bytes)

The same conversion code works fine when deployed on a windows tomcat server.

@Brandon9595

Would you please try to use 21.6 version of the API as it is the latest one and a lot of improvements have been made in it. In case you still face any issue, please share the sample .war file with us as well so that we can further proceed to assist you accordingly.

@asad.ali

We upgraded to the 21.6 Apose.HTML jar and it does get rid of the exception. We noticed some of the formatting is still slightly different but may still work for us. One thing I was curious about is do you know where the fonts need to be located on the server in order for Aspose.HTML to use those fonts or is there a specific method within the api that allows you to set the font folder location?

Thanks in advance,
Brandon

@Brandon9595

The fonts directories in Linux are usually as follows:

  • /usr/share/fonts or
  • /usr/local/share/fonts
  • ~/.fonts

Aspose.HTML finds the fonts in the above directories. Furthermore, you can check more information on the link below:

We would also like to share with you that we have added new Class i.e. FontSettings which can be used to set/get the font folders.

Awesome, thank you @asad.ali! The new FontSettings class worked great. What version of the api was the FontSettings class made available in?

@Brandon9595

Thanks for your feedback. The mentioned class was added in the 21.6 version of the API.