Skip loading of external resources

What are external resources?

External resources refer to various types of content or files that are not directly embedded within the document but are instead referenced and loaded from external locations. These resources enhance the functionality, appearance, and interactivity of the document. Common external resources include images, audio, video, fonts, CSS, scripts, frameworks, and so on. GroupDocs.Conversion considers external any resource that a document is trying to load from an external URL.

Utilizing external resources optimizes the performance, maintainability, and scalability of web pages and other documents. These resources are typically cached by browsers, which can reduce the load time of subsequent visits to a website. However, relying too heavily on external resources can also introduce dependencies and potential points of failure if the external sources become unavailable.

Restricting all external resources

While loading web, presentations and word-processing documents, the GroupDocs.Conversion allows skipping of loading the external resources.

To restrict the loading of external resources during the conversion, use the getSkipExternalResources() boolean property of the respective WebLoadOptions, PresentationLoadOptions, or WordProcessingLoadOptions class.

The following code snippet shows how to skip loading of external resources while loading an HTML document:


    WebLoadOptions loadOptions = new WebLoadOptions();
    loadOptions.setSkipExternalResources(true);

    Converter converter = new Converter("sample.html", () -> loadOptions);
    PdfConvertOptions options = new PdfConvertOptions();
    converter.convert("converted.pdf", options);

Allowlisting some of the external resources

Sometimes you may want to skip loading most of the external resources, but still load some particular resources. To allow-list specific resources during the conversion, use the WhitelistedResources property of the respective WebLoadOptions, PresentationLoadOptions, or WordProcessingLoadOptions class. The property is effective when the SkipExternalResources property is enabled. The WhitelistedResources property accepts the string list containing the portions of URLs to be loaded while restricting the loading of other external resources.

The following code snippet shows how to load the JPG and JPEG images and any resources from the example.com domain while restricting any other external resources:


    WebLoadOptions loadOptions = new WebLoadOptions();
    loadOptions.setSkipExternalResources(true);
    loadOptions.setWhitelistedResources(Arrays.asList( "jpg", "jpeg", "example.com" ));

    Converter converter = new Converter("sample.html", () -> loadOptions);

    PdfConvertOptions options = new PdfConvertOptions();
    converter.convert("converted.pdf", options);