Convert a HTML file to PDF programmatically using Java

Can Group docs convert a html (remote website or hosted remotely) along with css + fonts + JS to pdf?

Usually, page contains lot of dynamic data loaded by JS. We need to convert pdf of the page once all the dom (and dom loaded/mainipulated by JS) loading is complete.

@radhakrishna20,

Thank you for your inquiry.
Please note that GroupDocs.Conversion API takes a HTML file as input and then converts it to PDF or any other supported document. You can pass a local HTML file path or remote file path
String guid = "http://example.com/DOCXsample.docx";

Do you mean that you want to convert a complete remote or hosted HTML website (along-with all of its web pages) into PDF by just passing the website URL? Can you please further elaborate this use-case?

Partly correct. If we give URL, it should convert that html page into pdf (including dynamically rendered DOM by javascript), example : medium.com home page

@radhakrishna20,

No, actually API gets HTML file from the URL (if there is a file located). For instance in the given path:
String guid = "http://example.com/DOCXsample.docx";

However, we are further investigating the scenario, your investigation ticket ID is CONVERSIONJAVA-523. As we have any update, we’ll update you.

@radhakrishna20

Conversion from URL is possible when using the following code snippet:

// Create an instance of WebLoadOptions to set options for resource loading
WebLoadOptions webLoadOptions = new WebLoadOptions();
// Set the timeout for resource loading to 10,000 seconds (this seems unusually high; consider adjusting)
webLoadOptions.setResourceLoadingTimeout(TimeSpan.fromSeconds(10000));

// Create a Converter instance that will handle the downloading and converting of the content
Converter converter = new Converter(
    () -> downloadFile("https://www.medium.com"), // Lambda function to download the file from Medium
    () -> webLoadOptions // Lambda function to provide the web loading options
);

// Create PdfConvertOptions instance to specify options for PDF conversion
PdfConvertOptions options = new PdfConvertOptions();
// Convert the downloaded content to a PDF file named "out.pdf"
converter.convert("out.pdf", options);

// Method to download a file from the given URL and return it as a ByteArrayInputStream
public static ByteArrayInputStream downloadFile(String urlString) {
    try (
        // Create a default CloseableHttpClient to handle HTTP requests
        CloseableHttpClient httpClient = HttpClients.createDefault();
        // Execute a GET request to the specified URL
        CloseableHttpResponse response = httpClient.execute(new HttpGet(urlString))
    ) {
        // Convert the response entity to a byte array and return it as a ByteArrayInputStream
        byte[] bytes = EntityUtils.toByteArray(response.getEntity());
        return new ByteArrayInputStream(bytes);
    } catch (IOException e) {
        // Handle any IO exceptions that may occur during the download
        throw new RuntimeException(e);
    }
}