Mht to pdf - exception - com.aspose.html.dom.DOMException: The string contains invalid characters

language java 21

library
aspose-html 25.2.2

During conversion from mht to pdf, exception is thrown “com.aspose.html.dom.DOMException: The string contains invalid characters.”

input file
137.zip (33.7 KB)

@divine1

The exception “com.aspose.html.dom.DOMException: The string contains invalid characters” during the conversion from MHT to PDF typically indicates that there are characters in your MHT file that are not valid in the context of HTML or the PDF format. This can happen due to various reasons, such as unsupported encoding or special characters that are not properly escaped.

To resolve this issue, you can follow these steps:

  1. Check the MHT File: Open the MHT file in a text editor and look for any unusual characters or encoding issues. Ensure that the file is properly formatted and does not contain any invalid characters.

  2. Use PdfSaveOptions: When converting, make sure you are using the PdfSaveOptions class to specify any necessary options for the conversion. This can help in managing how the content is processed.

  3. Example Code: Here is a simple example of how to convert an MHT file to PDF using Aspose.HTML in Java:

    package com.aspose.html.examples;
    
    import com.aspose.html.converters.Converter;
    import com.aspose.html.saving.PdfSaveOptions;
    
    public class MHTMLToPDFConverter {
        public static void main(String[] args) {
            try (java.io.FileInputStream fileInputStream = new java.io.FileInputStream("path/to/your/file.mht")) {
                PdfSaveOptions options = new PdfSaveOptions();
                options.setJpegQuality(100); // Set JPEG quality if needed
                String outputFile = "path/to/output/file.pdf";
    
                // Convert MHTML to PDF
                Converter.convertMHTML(fileInputStream, options, outputFile);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
    
  4. Update Library: Ensure that you are using the latest version of the Aspose.HTML library, as updates may include fixes for known issues.

If the problem persists after checking the file and using the correct options, consider reaching out to Aspose support with the specific MHT file that is causing the issue for further assistance.

For more detailed information on converting MHT to PDF, you can refer to the official documentation here.

Sources:
[1]: Convertir MHTML a PDF en Java - Aspose.HTML for Java

@divine1

We have opened the following new ticket(s) in our internal issue tracking system and will deliver their fixes according to the terms mentioned in Free Support Policies.

Issue ID(s): HTMLJAVA-2006

You can obtain Paid Support Services if you need support on a priority basis, along with the direct access to our Paid Support management team.

this issue is not fixed in aspose-html 25.3. please fix this.

@divine1

The ticket is sadly not yet resolved. As soon as we fix the issue, we will send you a notification. Please spare us some time.

We apologize for the inconvenience.