Skip to footer content
USE CASES

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.

Frequently Asked Questions

Why are self-contained PDFs important in cloud and serverless environments?

Self-contained PDFs are crucial in cloud and serverless environments because they eliminate dependencies on external image paths or URLs. This ensures that the PDF renders consistently, even when the document is moved to a different machine or stored for long-term archiving, where such references may no longer be valid.

How does IronPDF help in creating self-contained PDFs?

IronPDF helps create self-contained PDFs by allowing users to embed images directly into the HTML as base64 data URIs. This method removes the dependency on external file paths and URLs, ensuring the PDF is fully self-contained.

What is a base64 data URI and how is it used in IronPDF?

A base64 data URI is a way to encode image data directly within an HTML document. In IronPDF, you can convert image bytes to a base64 string and embed it in an ` related to What is a base64 data URI and how is it used in IronPDF?` tag in the HTML. This allows the image to be included directly in the PDF, eliminating the need for external file references.

What are the advantages of using data URIs in PDFs?

Using data URIs in PDFs ensures that images are embedded directly within the document, reducing the risk of broken links if the document is moved or archived. This approach also improves performance by reducing the number of external calls needed during rendering.

Can IronPDF handle different image formats when embedding images as data URIs?

Yes, IronPDF supports multiple image formats when embedding images as data URIs. Formats such as PNG, JPEG, GIF, SVG, and WebP can be used as long as the data URI specifies the correct MIME type.

Does embedding images as data URIs affect the size of a PDF?

Yes, encoding images as base64 data URIs increases the size of each image by about a third. This increase affects both the HTML and the resulting PDF, so it's advisable to compress or resize images before encoding and apply PDF compression afterward.

Is there any preparation needed before embedding images as data URIs in IronPDF?

Before embedding images as data URIs in IronPDF, ensure that images are compressed or resized to manage file size. Additionally, verify that the data URI prefix matches the image's MIME type to ensure correct rendering.

What is the process of embedding images as data URIs in IronPDF?

The process involves three steps: reading the image bytes, converting them to a base64 string, and embedding the result in an ` related to What is the process of embedding images as data URIs in IronPDF?` tag with the correct MIME type. This allows the image to be included within the PDF file itself.

How does using data URIs impact HTTP or file-system calls during PDF rendering with IronPDF?

By using data URIs, IronPDF embeds images directly in the document, eliminating the need for additional HTTP or file-system calls during rendering. This improves throughput, especially when the document contains many small images.

What is a common mistake to avoid when embedding images as data URIs in IronPDF?

A common mistake is not matching the MIME type in the data URI prefix with the actual image format. For example, a JPEG image should have a prefix of `data:image/jpeg;base64,`. If the MIME type is incorrect, the image will not render properly.

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