Hi there,
We recently upgraded our aspose-pdf for java from version 24:9 to 24:10
Version 24:10 appears to have broken generating a pdf from html when running in headless mode.
Here is a cut down example to reproduce the example and prove that it version 24:10 that is the issue
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import com.aspose.pdf.*;
public class App {
public static void main(String[] args) {
System.setProperty("java.awt.headless", "true");
var html = " <!DOCTYPE html>" +
"<html>" +
"<body>" +
"<h1>A Heading</h1>" +
"<p>A paragraph.</p>" +
"</body>" +
"</html> ";
var options = new HtmlLoadOptions();
var pageInfo = new PageInfo();
pageInfo.setLandscape(false);
options.setPageInfo(pageInfo);
var document = new Document(new ByteArrayInputStream(html.getBytes()), options);
var outStream = new ByteArrayOutputStream();
document.save(outStream, SaveFormat.Pdf);
outStream.toByteArray();
}
}
contents of gradle.build
plugins {
id 'application'
}
repositories {
mavenCentral()
maven { url 'https://repository.aspose.com/repo/' }
}
dependencies {
implementation 'com.aspose:aspose-pdf:24.10:jdk16'
}
// Apply a specific Java toolchain to ease working on different environments.
java {
toolchain {
languageVersion = JavaLanguageVersion.of(21)
}
}
application {
// Define the main class for the application.
mainClass = 'App'
}
if you run
gradle build
then
gradle run
You’ll see the following stack trace
Exception in thread "main" java.lang.ExceptionInInitializerError
at com.aspose.pdf.internal.l52u.lk$1.lI(Unknown Source)
at com.aspose.pdf.internal.l52u.lk.getWindowSize(Unknown Source)
at com.aspose.pdf.l2f.lI(Unknown Source)
at com.aspose.pdf.l2f.lI(Unknown Source)
at com.aspose.pdf.l2f.lf(Unknown Source)
at com.aspose.pdf.l2f.lI(Unknown Source)
at com.aspose.pdf.l2f.lI(Unknown Source)
at com.aspose.pdf.ADocument.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)
at App.main(App.java:20)
Caused by: java.awt.HeadlessException
at java.desktop/sun.java2d.HeadlessGraphicsEnvironment.getScreenDevices(HeadlessGraphicsEnvironment.java:52)
at com.aspose.pdf.internal.l45u.lj.<clinit>(Unknown Source)
... 12 more
If you change the library back to com.aspose:aspose-pdf:24.9:jdk16 in gradle.build the code will run without blowing up.