com.aspose.pdf.internal.ms.System.lh: Invalid operation

We keep getting this error while converting HTML documents to PDF documents
com.aspose.pdf.internal.ms.System.lh: Invalid operation
at com.aspose.pdf.internal.l108t.l6if.lI(Unknown Source)
at com.aspose.pdf.internal.l108h.l1j.lI(Unknown Source)
at com.aspose.pdf.internal.l108h.ld.lf(Unknown Source)

Gradle dependency in use:
implementation (group: ‘com.aspose’, name: ‘aspose-pdf’, version: ‘25.4’)

Code:

public void convertHtmlStringToPdf(String htmlContent, ByteArrayOutputStream outputStream) throws Exception {
// Use Aspose.PDF’s HtmlLoadOptions to load HTML content
long asposeProcessingTime= Instant.now().toEpochMilli();
HtmlFragment htmlFragment = new HtmlFragment(htmlContent);
HtmlLoadOptions options = new HtmlLoadOptions();
htmlFragment.setHtmlLoadOptions(options);
options.setHtmlMediaType(HtmlMediaType.Screen);
options.setInputEncoding(ENCODING_UTF);
Document pdfDoc = new Document();
TextState textState = getDefaultFont();
pdfDoc.getPageInfo().setDefaultTextState(textState);
pdfDoc.getPages().add().getParagraphs().add(htmlFragment);
pdfDoc.save(outputStream);
pdfDoc.close();
}

Steps taken to try and resolve this error:

  1. We have added necessary fonts like Microsoft core fonts and symbol.ttf to the container , but we still keep getting this error.
  2. Added fallback font:
    private static @NotNull TextState getDefaultFont() {
    //Setting Default Font
    FontRepository.getSources().add(new FolderFontSource(“/usr/local/share/fonts”));
    FontRepository.getSources().add(new FolderFontSource(“src/main/resources/static/fonts”)); //For passing the test case in local environment
    Font font = FontRepository.findFont(“NotoSans-Light”);
    TextState tstate = new TextState();
    tstate.setFont(font);
    return tstate;
    }

Despite taken above steps , the error keeps happening.

Can some help determine the root cause of this error and help us resolve this error.
Any help in this regards will be highly appreciated.

@prateek.saxena01

The error you’re encountering, com.aspose.pdf.internal.ms.System.lh: Invalid operation, typically indicates an issue with the input HTML content or the way it’s being processed. Here’s a structured approach to diagnose and resolve the issue:

Cause:

The error may stem from invalid HTML content, incorrect font configurations, or outdated library versions.

Solution:

  1. Check HTML Content: Ensure that the HTML content you are trying to convert is well-formed and valid. Invalid HTML can lead to unexpected behavior during conversion.

  2. Update Aspose.PDF Version: You’re currently using version 25.4. Consider updating to the latest version of Aspose.PDF, as there may have been bug fixes or improvements related to HTML to PDF conversion.

  3. Review Font Configuration: Since you’ve already added necessary fonts, double-check that the font names used in your HTML match the fonts available in your environment. You can also try using a different font to see if the issue persists.

  4. Set HtmlLoadOptions: Experiment with different settings in HtmlLoadOptions. For example, try setting options.setHtmlMediaType(HtmlMediaType.Print) instead of HtmlMediaType.Screen to see if it makes a difference.

  5. Error Handling: Implement error handling to catch exceptions and log more details about the HTML content being processed. This can help identify specific issues with the input.

Next Steps:

Here’s a slight modification to your existing code to include error handling:

public void convertHtmlStringToPdf(String htmlContent, ByteArrayOutputStream outputStream) {
    Document pdfDoc = null; // Initialize pdfDoc here
    try {
        HtmlFragment htmlFragment = new HtmlFragment(htmlContent);
        HtmlLoadOptions options = new HtmlLoadOptions();
        options.setHtmlMediaType(HtmlMediaType.Print); // Try changing this
        options.setInputEncoding(ENCODING_UTF);
        htmlFragment.setHtmlLoadOptions(options);
        
        pdfDoc = new Document();
        TextState textState = getDefaultFont();
        pdfDoc.getPageInfo().setDefaultTextState(textState);
        pdfDoc.getPages().add().getParagraphs().add(htmlFragment);
        pdfDoc.save(outputStream);
    } catch (Exception e) {
        e.printStackTrace(); // Log the exception for debugging
    } finally {
        if (pdfDoc != null) {
            pdfDoc.close();
        }
    }
}

Please try these steps and let me know if they help resolve the issue or if you need further assistance!