Html to pdf conversion - aspose-html

i used below dependency in java to convert html file to pdf.

<dependency>
            <groupId>com.aspose</groupId>
            <artifactId>aspose-imaging</artifactId>
            <version>25.5</version>
        </dependency>

i used this input file
Epiq Homepage.zip (21.4 KB)

i received below exception

Error creating file: Cookie domain not set.
Parameter name: cookie.Domain
java.lang.RuntimeException: Cookie domain not set.
Parameter name: cookie.Domain
	at com.aspose.html.utils.XQ.dispose(Unknown Source)
	at com.aspose.html.utils.XR.dispose(Unknown Source)
	at com.aspose.html.dom.Document.a(Unknown Source)
	at com.aspose.html.dom.Document.navigate(Unknown Source)
	at com.aspose.html.utils.P.a(Unknown Source)
	at com.aspose.html.HTMLIFrameElement.cx(Unknown Source)
	at com.aspose.html.HTMLIFrameElement$a.a(Unknown Source)
	at com.aspose.html.utils.apl.a(Unknown Source)
	at com.aspose.html.utils.apl.a(Unknown Source)
	at com.aspose.html.utils.amw.a(Unknown Source)
	at com.aspose.html.utils.amw.a(Unknown Source)
	at com.aspose.html.utils.amw.a(Unknown Source)
	at com.aspose.html.utils.amw.a(Unknown Source)
	at com.aspose.html.utils.amw.a(Unknown Source)
	at com.aspose.html.utils.amw.a(Unknown Source)
	at com.aspose.html.utils.amw.a(Unknown Source)
	at com.aspose.html.utils.amw.a(Unknown Source)
	at com.aspose.html.utils.amw.a(Unknown Source)
	at com.aspose.html.utils.amw.a(Unknown Source)
	at com.aspose.html.utils.amw.a(Unknown Source)
	at com.aspose.html.utils.Tq.a(Unknown Source)
	at com.aspose.html.utils.Tq.a(Unknown Source)
	at com.aspose.html.utils.To.g(Unknown Source)
	at com.aspose.html.utils.Um.agt(Unknown Source)
	at com.aspose.html.utils.Um.ahc(Unknown Source)
	at com.aspose.html.utils.abb.a(Unknown Source)
	at com.aspose.html.rendering.HtmlRenderer.render(Unknown Source)
	at com.aspose.html.rendering.HtmlRenderer.render(Unknown Source)
	at com.aspose.html.rendering.HtmlRenderer.render(Unknown Source)
	at com.aspose.html.rendering.Renderer.render(Unknown Source)
	at com.aspose.html.rendering.Renderer.render(Unknown Source)
	at com.aspose.html.utils.jO.a(Unknown Source)
	at com.aspose.html.utils.jO.a(Unknown Source)
	at com.aspose.html.utils.jO.a(Unknown Source)
	at com.aspose.html.converters.Converter.convertHTML(Unknown Source)
	at org.epiqpoc.mainhtmlwithImaging.HtmlImagingConverter.convert(HtmlImagingConverter.java:26)
	at org.epiqpoc.common.Common.lambda$doNextStep$0(Common.java:86)
	at java.base/java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:572)
	at java.base/java.util.concurrent.FutureTask.run(FutureTask.java:317)
	at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1144)
	at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:642)
	at java.base/java.lang.Thread.run(Thread.java:1583)
Caused by: class com.aspose.html.utils.ms.System.ArgumentException: Cookie domain not set.
Parameter name: cookie.Domain
com.aspose.html.utils.net.CookieContainer.a(Unknown Source)
com.aspose.html.utils.net.CookieContainer.add(Unknown Source)
com.aspose.html.utils.net.CookieContainer.add(Unknown Source)
com.aspose.html.dom.Document$d$1.invoke(Unknown Source)
com.aspose.html.utils.XK.ang(Unknown Source)
com.aspose.html.utils.XL.amQ(Unknown Source)
com.aspose.html.utils.XL.b(Unknown Source)
com.aspose.html.utils.XL$1.invoke(Unknown Source)
com.aspose.html.utils.XC$a$1.invoke(Unknown Source)
com.aspose.html.utils.ms.System.Threading.Thread$3.invoke(Unknown Source)
com.aspose.html.utils.ms.System.Threading.Thread$6.run(Unknown Source)
java.base/java.lang.Thread.run(Thread.java:1583)
	at com.aspose.html.utils.net.CookieContainer.a(Unknown Source)
	at com.aspose.html.utils.net.CookieContainer.add(Unknown Source)
	at com.aspose.html.utils.net.CookieContainer.add(Unknown Source)
	at com.aspose.html.dom.Document$d$1.invoke(Unknown Source)
	at com.aspose.html.utils.XK.ang(Unknown Source)
	at com.aspose.html.utils.XL.amQ(Unknown Source)
	at com.aspose.html.utils.XL.b(Unknown Source)
	at com.aspose.html.utils.XL$1.invoke(Unknown Source)
	at com.aspose.html.utils.XC$a$1.invoke(Unknown Source)
	at com.aspose.html.utils.ms.System.Threading.Thread$3.invoke(Unknown Source)
	at com.aspose.html.utils.ms.System.Threading.Thread$6.run(Unknown Source)
	... 1 more

please help to fix this issue

@divine1

Cause

The error message “Cookie domain not set” indicates that the HTML rendering process is attempting to handle cookies, but the domain for those cookies has not been specified. This can occur when the HTML content being processed includes elements that require cookie handling, such as iframes or external resources.

Solution

To resolve this issue, you need to ensure that the HTML conversion process is aware of the base URI. This can be achieved by specifying the base URI when calling the conversion method. Here’s how you can do that:

  1. Set the base URI to the location of your HTML content.
  2. Use the convertHTML() method with the appropriate parameters.

Code

Here’s a sample code snippet demonstrating how to convert HTML to PDF while specifying the base URI:

import com.aspose.html.converters.Converter;
import com.aspose.html.saving.PdfSaveOptions;
import com.aspose.html.HTMLDocument;

public class HtmlToPdfConverter {
    public static void main(String[] args) {
        // Specify the path to your HTML file
        String htmlFilePath = "path/to/your/input.html";
        // Specify the output PDF file path
        String outputPdfPath = "path/to/your/output.pdf";
        // Specify the base URI
        String baseUri = "file:///path/to/your/";

        // Initialize an HTML document from the file
        HTMLDocument document = new HTMLDocument(htmlFilePath);
        // Initialize PdfSaveOptions
        PdfSaveOptions options = new PdfSaveOptions();

        // Convert HTML to PDF
        Converter.convertHTML(document, options, outputPdfPath, baseUri);
    }
}

Make sure to replace "path/to/your/input.html" and "path/to/your/output.pdf" with the actual paths.

Next Steps

  1. Ensure that the base URI is correctly set to the directory containing your HTML file.
  2. Test the conversion again after making these changes.
  3. If you continue to experience issues, check the HTML content for any elements that might require additional cookie handling.

Please implement these steps and let me know if you need further assistance!

the proposed method doesnot exist in aspose-imaging 25.5 (java)

The provided code is using Aspose.HTML, not Aspose.Imaging, so I redirected your issue to the Aspose.HTML branch

@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-2132

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.

@evgeniy.sidenko @asad.ali my original question is related to aspose-imaging. i opened this ticket to report a problem i experienced with aspose-imaging.

could you please provide a solution for aspose-imaging?

@divine1

Looks like the exception you are facing is causing due to Aspose.HTML. It will be helpful for us if you can please share your sample code snippet for our reference so that we can further proceed accordingly.

@asad.ali please close this ticket. i will create a new one with correct info. thanks

@divine1

Sure, we are closing it now.