Programming

Inject Base64 Avatar into Carbone Template Properly

Learn how to inject base64-encoded avatar images into Carbone templates using dummy placeholders and full data URIs. Fix Java code issues and use with third-party Carbone providers for dynamic PDFs and DOCX.

1 answer 1 view

How can I properly inject variable data for an avatar image into a Carbone template using base64 encoding? The current implementation, which fetches avatar data and encodes it to base64, isn’t working as expected. Is there a specific approach required for Carbone templates, especially when using a third-party Carbone service provider?

Here’s the code used to fetch and encode the avatar data:

java
String avatarId = result.getAvatarId();
if(avatarId != null){
    Optional<AttachmentDTO> attachmentOptional = attachmentService.findOne(avatarId);
    if(attachmentOptional.isPresent()){
        AttachmentDTO attachmentDTO = attachmentOptional.get();
        byte[] avatarData = minioFileService.downloadAvatar(attachmentDTO.getAttachPath());
        String base64 = Base64.getEncoder().encodeToString(avatarData);
        partyMember.setAvatar(base64);
    }
}

To inject a base64-encoded avatar image into a Carbone template, start by adding a dummy placeholder image in your template file (like DOCX or ODS) and set its alt text to your variable tag, such as {d.avatarBase64}. In your JSON data, supply the full data URI string like "data:image/png;base64,iVBORw0KGgo..."—not just the raw base64 payload—then Carbone swaps the placeholder during rendering. This exact approach works seamlessly with third-party Carbone service providers, as they follow the same replacement logic when you send the complete JSON payload.

Contents

How Carbone Processes Dynamic Images

Carbone templates shine when handling dynamic content like avatar images because they treat images as replaceable elements tied to your data variables. You can’t just drop a base64 string into a text field—Carbone expects a specific setup with a visual placeholder. The official Carbone pictures documentation outlines that dynamic images come as either public URLs or base64 data URIs, but both require a temporary image in the template with the variable tag in its alt text.

During rendering, Carbone scans for these alt text tags, matches them to your JSON data, and overlays the new image exactly in place. This keeps your layout intact, whether you’re generating PDFs, DOCX files, or spreadsheets. Why does this matter for avatars? User-generated images vary in size and format (PNG, JPEG, SVG), and base64 embedding ensures everything stays self-contained—no external fetches that could fail.

Step-by-Step: Preparing Your Base64 Avatar Data

First, grab your avatar bytes from storage, as you’re doing with MinIO. Encode them to base64, but crucially, prefix the string with the MIME type: data:image/png;base64, (swap PNG for JPEG or whatever matches your file). Tools like online converters confirm this format is standard for embedding images directly.

Here’s why the prefix is non-negotiable: Without it, Carbone (and most renderers) won’t recognize the string as an image source. Your current Java snippet stops at raw base64, which is the root issue—Carbone needs the full data URI to trigger the swap.

Setting Up the Template with a Dummy Image

Open your Carbone template in LibreOffice or Word. Insert any small image where the avatar should appear—say, a 100x100 pixel gray square. Right-click it, select “Edit Alt Text” (or “Alternative Text” in some versions), and enter {d.avatarBase64} or whatever path matches your JSON key, like {d.partyMember.avatar}.

The Carbone gallery example shows this in action for multiple images: placeholder in a table cell, alt text with the tag, and boom—dynamic swap. Pro tip: Match the dummy image’s dimensions to your expected avatar size to avoid layout shifts post-render.

JSON Structure for Carbone Templates

Your data payload decides everything. Structure it like this:

json
{
  "d": {
    "partyMember": {
      "avatar": "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAA...your_base64_here"
    }
  }
}

The d. prefix is Carbone’s default namespace for safe variable scoping. The conditional image hiding guide even uses a similar setup, injecting a 1x1 transparent base64 pixel as a fallback (imageError).

Fixing Your Java Code for Proper Data URI

Your code fetches and encodes correctly but misses the data URI wrapper. Update it like this:

java
String avatarId = result.getAvatarId();
if (avatarId != null) {
    Optional<AttachmentDTO> attachmentOptional = attachmentService.findOne(avatarId);
    if (attachmentOptional.isPresent()) {
        AttachmentDTO attachmentDTO = attachmentOptional.get();
        byte[] avatarData = minioFileService.downloadAvatar(attachmentDTO.getAttachPath());
        
        // Detect MIME type dynamically (or hardcode based on extension)
        String mimeType = "image/png"; // Adjust: use Tika or filename extension
        String base64 = Base64.getEncoder().encodeToString(avatarData);
        String dataUri = "data:" + mimeType + ";base64," + base64;
        
        partyMember.setAvatar(dataUri);
    }
}

This generates the full string Carbone craves. Test MIME detection with Apache Tika for robustness—PNG, JPEG, and WebP all work fine in Carbone.

Using Third-Party Carbone Providers

Switching to a hosted service? No sweat—the process mirrors self-hosted. The Carbone cheat sheet explicitly calls this out: Keep the dummy image and alt text in your template, then POST the JSON with the exact base64 data URI. Providers like Carbone Cloud handle the replacement server-side, so your payload must be complete—no partial strings.

We’ve seen teams hit snags here because they trimmed the prefix thinking the server adds it. Nope—send it fully formed. Latency tip: Base64 bloats data by 33%, so compress avatars client-side if generating bulk reports.

Common Pitfalls and Troubleshooting

  • Blank images? Check alt text spelling matches JSON key exactly (case-sensitive).
  • Layout breaks? Lock the dummy image’s size/position in the template.
  • Invalid base64? Validate with a browser: Paste the data URI into an <img src="..."> tag.
  • Third-party fails? Inspect the API response—providers log replacement errors.
  • Large files? Cap at 5MB; Carbone chokes on bigger embeds.

The HTML rendering docs reinforce that <img src="data:image/jpeg;base64,..."> works identically in rich text blocks.

Real-World Example

Imagine a party invite template. Dummy avatar at the top, alt text {d.member.avatar}. JSON:

json
{
  "d": {
    "member": {
      "name": "John Doe",
      "avatar": "data:image/jpeg;base64,/9j/4AAQSkZJRgABAQAAAQABAAD...truncated"
    }
  }
}

Render it—John’s face pops in perfectly. Scale this to 1000 invites, and your Java loop cranks out personalized PDFs via the provider’s API.

Sources

Conclusion

Mastering base64 avatar injection in Carbone templates boils down to the dummy image + alt text combo and full data URIs in JSON—your Java tweak seals it. Whether self-hosting or using third-party providers, this method delivers crisp, dynamic images without external dependencies. Test one template cycle, and you’ll crank out scalable reports effortlessly.

Authors
Verified by moderation
Moderation
Inject Base64 Avatar into Carbone Template Properly