Skip to footer content
USING IRONPDF

Self-Contained PDF Generation for Cloud and Serverless Apps

A PDF that references images by file path or URL carries a hidden dependency. The moment the document leaves the machine that made it, runs in a stateless cloud function, or gets archived for years, those references can break and leave blank boxes where the logo or chart should be. Embedding images directly in the HTML as base64 data URIs removes that dependency, and IronPDF renders the result into a fully self-contained PDF.

The Business Problem

An invoicing service moves to Azure or AWS Lambda, where file-system access is limited and ephemeral, so the image paths that worked locally no longer resolve. A reporting tool emails documents that must still display correctly years later, long after the original asset server has changed. A pipeline assembles documents from images generated at runtime that were never saved to disk. In each case, external image references are a liability.

Embedding Images as Data URIs

The pattern is three steps: read the image bytes, convert them to base64, and place the result in an <img> tag with the matching MIME type. The rendered PDF then holds the image inside the file.

using IronPdf;
using System;

byte[] imageBytes = System.IO.File.ReadAllBytes("logo.png");
string dataUri = "data:image/png;base64," + Convert.ToBase64String(imageBytes);
string html = $"<img src='{dataUri}'>";

var renderer = new ChromePdfRenderer();
var pdf = renderer.RenderHtmlAsPdf(html);
pdf.SaveAs("self-contained.pdf");
using IronPdf;
using System;

byte[] imageBytes = System.IO.File.ReadAllBytes("logo.png");
string dataUri = "data:image/png;base64," + Convert.ToBase64String(imageBytes);
string html = $"<img src='{dataUri}'>";

var renderer = new ChromePdfRenderer();
var pdf = renderer.RenderHtmlAsPdf(html);
pdf.SaveAs("self-contained.pdf");
Imports IronPdf
Imports System

Dim imageBytes As Byte() = System.IO.File.ReadAllBytes("logo.png")
Dim dataUri As String = "data:image/png;base64," & Convert.ToBase64String(imageBytes)
Dim html As String = $"<img src='{dataUri}'>"

Dim renderer As New ChromePdfRenderer()
Dim pdf = renderer.RenderHtmlAsPdf(html)
pdf.SaveAs("self-contained.pdf")
$vbLabelText   $csharpLabel

The same approach works for runtime-generated images, such as a chart or barcode produced in memory, by encoding the bytes directly without writing a temporary file. Multiple formats are supported as long as the data URI declares the right type: PNG, JPEG, GIF, SVG (image/svg+xml), and WebP.

Points to Plan For

  • Base64 adds size: Encoding increases each image's byte size by roughly a third, which lands in both the HTML and the PDF. For image-heavy documents, compress or resize before encoding and apply PDF compression afterward.
  • Match the MIME type: The prefix must match the format, such as data:image/jpeg;base64, for a JPEG, or the image will not render.
  • No file-system dependency: Because the image travels inside the document, the render does not rely on a reachable path or asset server, which is what makes it dependable in cloud and container deployments.
  • Fewer fetches: Inline images avoid extra HTTP or file-system calls during rendering, which helps throughput when a document contains many small images.

Result

By embedding images as data URIs, teams generate PDFs that render identically whether they run on a laptop or in a serverless function, and that keep every image intact when emailed or archived. Full format details and examples are in the embed images with DataURIs guide.

Curtis Chau
Technical Writer

Curtis Chau holds a Bachelor’s degree in Computer Science (Carleton University) and specializes in front-end development with expertise in Node.js, TypeScript, JavaScript, and React. Passionate about crafting intuitive and aesthetically pleasing user interfaces, Curtis enjoys working with modern frameworks and creating well-structured, visually appealing manuals.

...

Read More

Iron Support Team

We're online 24 hours, 5 days a week.
Chat
Email
Call Me