Search Docs
Loading...
Skip to content

To WebP

Export CE.SDK designs to WebP format for compact image files that preserve transparency, with one quality option for both lossy and lossless modes.

5 mins
estimated time
GitHub

WebP files are usually smaller than PNG files and keep transparency. At comparable quality, they are usually also smaller than JPEG files. Use WebP when bandwidth or storage matters and the app or service that shows the file can read WebP.

This guide covers exporting a block to WebP, configuring quality, controlling output dimensions, and writing the exported bytes to app storage.

Export to WebP#

Use engine.block.export(...) with MimeType.WEBP to export a page, scene, group, or any other design block. The method returns a ByteBuffer containing the encoded WebP data. The webpQuality option controls compression. A value of 0.8 is a good starting point for web delivery.

val webpData = engine.block.export(
block = page,
mimeType = MimeType.WEBP,
options = ExportOptions(webpQuality = 0.8F),
)

Pass the page block when you want to export the current page, or pass another block ID to export a specific element.

Export Options#

WebP export reads these ExportOptions fields:

Option Description
webpQuality Quality from just above 0 to 1.0. 1.0 selects lossless compression. Defaults to 1.0.
targetWidth Target output width in pixels. Without targetHeight, the height follows from the block’s aspect ratio. Defaults to null.
targetHeight Target output height in pixels. Without targetWidth, the width follows from the block’s aspect ratio. Defaults to null.
allowTextOverhang When true, text blocks export with glyphs that extend beyond their frame still visible. Defaults to false.

Lossless Compression#

Set webpQuality to 1.0 to select lossless compression. Lossless WebP keeps every pixel and usually produces smaller files than PNG. Because 1.0 is the default, an export without ExportOptions is also lossless.

val losslessWebpData = engine.block.export(
block = page,
mimeType = MimeType.WEBP,
options = ExportOptions(webpQuality = 1.0F),
)

Values from 0.8 to 0.95 keep visual quality high and make the file much smaller. Lower values make the file smaller still, but compression artifacts become more visible.

Target Dimensions#

Set targetWidth and targetHeight together to request a target size. CE.SDK preserves the block’s aspect ratio, scales the block until it fills the requested target, and derives the WebP dimensions from that scaled block. Leave both values unset to export the block at its native size.

val options = ExportOptions(
webpQuality = 0.85F,
targetWidth = 1920F,
targetHeight = 1080F,
)
val sizedWebpData = engine.block.export(
block = page,
mimeType = MimeType.WEBP,
options = options,
)

If the target aspect ratio differs from the block aspect ratio, one exported dimension can be larger than the requested value.

Save to App Storage#

After export, write the returned ByteBuffer to a file in your app-specific storage or pass it to your upload pipeline. The sample writes a read-only view of the buffer, so the original export data stays readable.

suspend fun saveWebpExportToFile(
webpData: ByteBuffer,
outputFile: File,
): File = withContext(Dispatchers.IO) {
outputFile.parentFile?.mkdirs()
FileOutputStream(outputFile).channel.use { channel ->
val readableData = webpData.asReadOnlyBuffer()
while (readableData.hasRemaining()) {
channel.write(readableData)
}
}
check(outputFile.length() > 0L) { "saved WebP file is empty" }
outputFile
}

Use an Android app-owned directory such as context.cacheDir or context.filesDir for local exports, then share or upload the file according to your app’s storage policy.

Troubleshooting#

Issue Fix
File is still too large Lower webpQuality toward 0.5 to 0.7, or reduce dimensions with targetWidth and targetHeight. Photographic content compresses well at lower quality.
Transparent areas appear opaque Check the exported page or block background. WebP preserves alpha only when the source content is transparent.
Output looks degraded Set webpQuality to 1.0. Lossless WebP keeps sharp edges and text clean, where lossy compression can blur them.
Output dimensions are unexpected Check the block aspect ratio against targetWidth and targetHeight. CE.SDK preserves the block aspect ratio and can export one dimension larger than the requested target when the ratios differ.
Quality value is outside the range Keep webpQuality above 0 and at most 1.0.

API Reference#

API Description
engine.block.export(block=_, mimeType=MimeType.WEBP, options=_) Export a block as WebP and return the encoded data as a ByteBuffer.
ExportOptions(webpQuality=_, targetWidth=_, targetHeight=_, allowTextOverhang=_) Configure WebP quality, output dimensions, and text-overhang handling.

Next Steps#

  • Export Overview - Compare all supported export formats
  • Export to PDF - Generate print-ready PDF documents from your designs
  • Partial Export - Export specific blocks, groups, or page elements instead of entire scenes
  • Size Limits - Check device export limits before exporting large designs