Export Presentations to HTML with Externally Linked Images in Python

Background

By default, HTML export embeds all resources directly in the HTML using Base64 encoding. This produces a single, self-contained HTML file that’s convenient for viewing and distribution. However, this approach has drawbacks:

  • The resulting file is significantly larger than the original resources because of Base64 overhead.
  • Embedded images and other assets are difficult to update or replace.

Alternative Approach

An alternative approach using ILinkEmbedController avoids these limitations.

The LinkController class below implements ILinkEmbedController and is passed to the HtmlOptions constructor. The interface exposes three methods that control how resources are embedded or linked during HTML export:

get_object_storing_location(id, entity_data, semantic_name, content_type, recommended_extension): Called when the exporter encounters a resource and must decide where to store it. The most important parameters are id (the resource’s unique identifier for this export run) and content_type (the resource MIME type). Return LinkEmbedDecision.LINK to link the resource, or LinkEmbedDecision.EMBED to embed it.

get_url(id, referrer): Returns the URL that will appear in the resulting HTML for the resource identified by id (optionally considering the referrer object).

save_external(id, entity_data): Called when a resource selected for linking needs to be written externally. Because the identifier and contents are provided (as a byte array), you can persist the resource however you like.

The Python LinkController implementation of ILinkEmbedController follows below.

# [TODO[not_supported_yet]: python implementation of .NET interfaces]

After implementing the LinkController class, you can use it with the HtmlOptions class to export the presentation to HTML with externally linked images, as shown below:

# [TODO[not_supported_yet]: python implementation of .NET interfaces]

We assigned SlideImageFormat.SVG to the slide_image_format property so that the resulting HTML file will contain SVG data to render the presentation’s contents.

Content types: If the presentation contains raster bitmaps, then the class code must be prepared to process both image/jpeg and image/png content types. The content of the exported bitmap images may not match what was stored in the presentation. Aspose.Slides’ internal algorithms perform size optimization and use either the JPEG or PNG codec (depending on which produces a smaller file size). Images containing an alpha channel (transparency) are always encoded as PNG.