Hi Team,
Recently my license upgraded to 24.10, after that the excelToPdf conversion code is giving below exception. Please check and provide steps to resolve the same. And code to savePdf is as follows,
public void savePdfDocument(Document pdfDocument, String outputFile) {
// Optimize resources in the PDF document
Document.OptimizationOptions optimizationOptions = new Document.OptimizationOptions();
// Save the optimized PDF
pdfDocument.optimizeResources(optimizationOptions);
pdfDocument.save(outputFile);
pdfDocument.close();
}
Error message getting is :-
java.awt.HeadlessException: null
at java.desktop/sun.java2d.HeadlessGraphicsEnvironment.getScreenDevices(HeadlessGraphicsEnvironment.java:53)
at com.aspose.pdf.internal.l45u.lj.(Unknown Source)
@koteswaragunda
The java.awt.HeadlessException
you are encountering during the Excel to PDF conversion typically occurs when your application is running in an environment that does not support a display, such as a server without a graphical user interface (GUI). This can happen when using libraries that rely on AWT (Abstract Window Toolkit) for rendering graphics.
To resolve this issue, you can try the following steps:
-
Run in Headless Mode: You can set the Java system property to run in headless mode. This can be done by adding the following line to your code before any AWT-related classes are used:
System.setProperty("java.awt.headless", "true");
-
Check Your Environment: Ensure that your application is running in an environment that supports headless operations. If you are running on a server, make sure it is configured correctly to handle headless operations.
-
Update Your Code: If you are using the Aspose.Cells library for the conversion, ensure that you are following the correct implementation. Here is a sample code snippet for converting an Excel file to PDF:
import com.aspose.cells.PdfSaveOptions;
import com.aspose.cells.Workbook;
import AsposeCellsExamples.Utils;
public class ExcelToPDF {
public static void main(String[] args) throws Exception {
// Set headless mode
System.setProperty("java.awt.headless", "true");
// The path to the documents directory.
String dataDir = Utils.getSharedDataDir(ExcelToPDF.class) + "TechnicalArticles/";
// Initialize a new Workbook
Workbook workbook = new Workbook(dataDir + "Mybook.xls");
// Implement one page per worksheet option
PdfSaveOptions pdfSaveOptions = new PdfSaveOptions();
pdfSaveOptions.setOnePagePerSheet(true);
// Save the PDF file
workbook.save(dataDir + "ExceltoPDF_out.pdf", pdfSaveOptions);
}
}
By setting the headless property and ensuring your code is correctly implemented, you should be able to resolve the HeadlessException
and successfully convert your Excel files to PDF.
If you continue to experience issues, please provide additional details about your environment and any other error messages you may encounter.
Sources:
[1]: ExceltoPDF.java
@koteswaragunda,
Moreover, since your code segment and error suggest that you are using Aspose.PDF for Java, I am moving your thread to the respective forum where one of our colleagues from the Aspose.PDF team will evaluate your issue and assist you accordingly.
@amjad.sahi seems it got resolved after setting the awt system property. But there are other issues opened. I will open a new ticket in pdf forum. thank you for taking care.
@koteswaragunda,
You’re welcome to create new threads for your issues in the respective Aspose product forums, such as Aspose.Cells category or Aspose.PDF category. If you encounter any issues with converting Excel to PDF using Aspose.Cells API only, please feel free to post in the Aspose.Cells forum, and we’ll look into it promptly.
Hi @amjad.sahi
I have observed that even i have set the System.setProperty(“java.awt.headless”, “true”); i am getting HeadLessException, When i further debug found that it was caused due to HtmlFragment class used in the code, i.e. hrCell.getParagraphs().add(new HtmlFragment("<HR>"));
When i commented, the image got rendered. Even i also tried alternative approach to set the html by following below code, but getting compilation issue saying that the setHtmlTagSupported method not found.
TextFragment textFragment = new TextFragment("<hr>");
textFragment.getTextState().setHtmlTagSupported(true);
Can you please check and suggest on this issue. FYI, i tested with 24.9 and 24.10 versions.
@koteswaragunda
Sorry for the confusion but such property does not exist in the API. Furthermore, would you kind share sample files along with minimal code sample to reproduce the issue that you have faced with HtmlFragment? We will test the scenario in our environment and address it accordingly.