How to use ExternalResourcesCredentials for PDF java

Hey there!

So I’ve been looking into using Aspose.pdf java to implement an html to java implementation. Unfortunately, I’m running into timeout issues regarding how HTMLLoadOptions pulls/loads embedded css/js links. My assumption is that it doesn’t carry the basic java SSL authentication I provide it and because of lacking the creds it times out on the relative css/js paths within the html.

I tried setting the ExternalResourcesCredentials property as well but seems this was carried over from the .NET version of your library which uses NetworkCredential from C#/.NET that’s essentially optimized for credentials carried through the lifecycle of windows systems (I am on unix based system).

NetworkCredential link: NetworkCredential Class (System.Net) | Microsoft Learn

I also saw on the .NET docs you have a working example of ExternalResourcesCredentials available but not for the java version of the library:

Can you provide any insight on how to implement the ExternalResourcesCredentials for java using keystore/jks authentication?

Unfortunately I can’t share the document versions I have tested on because they’re private, but below is sample code I used:

            // Setting aspose Temp License
            ClassLoader cl = Thread.currentThread().getContextClassLoader();
            InputStream iStreamLic = cl.getResourceAsStream("<license>");
            License license = new License();
            license.setLicense(iStreamLic);

            // temp ssl configuration using java.net.*
            //disableSslVerification();
            setSSL();

            // Converting resource to PDF
            // https://blog.aspose.com/2020/01/30/convert-html-to-pdf-in-java/
            String urlString = new String(resource.getByteArray(), StandardCharsets.UTF_8);
            URL url = new URL(urlString);
            String baseUrl = url.getProtocol() + "://" + url.getHost();
            URLConnection conn = url.openConnection();

            HtmlLoadOptions htmlOptions = new HtmlLoadOptions(baseUrl);
            htmlOptions.ExternalResourcesCredentials = new NetworkCredential(
                    "<username>",
                    "<password>",
                    "<domain>"
            ); // this is the statement I'm having issue with
            htmlOptions.getPageInfo().setLandscape(true);
            htmlOptions.getPageInfo().setHeight(PageSize.getA4().getHeight());
            htmlOptions.getPageInfo().setWidth(PageSize.getA4().getWidth());

            Document doc = new Document(conn.getInputStream(), htmlOptions);
            doc.save(os);

The output pdf excludes any styling generated from external css/js files in the html. Look forward to your response, thanks!

@noelcortes

Thanks for contacting support.

We need to check the feasibility to provide a working code sample as per your requirements. For the purpose, we have logged an investigation ticket as PDFJAVA-40118 in our issue tracking system. We will further look into its details and keep you posted as soon as the ticket is resolved. Please give us some time.

We are sorry for the inconvenience.

Thank you for looking into this. I just want to make clear since it may be confusing from my initial post, but I am looking to convert an HTML page into a PDF file.

Looking forward to your reply!

@noelcortes

Thanks for writing to us.

Yes, we have logged the ticket from perspective of HTML page to PDF conversion and will investigate it accordingly. We will inform you once it is resolved.