file_data_uri()

Return a stored file (from a file field) as a data: URI — data:{mime};base64,{...}. This is how you embed a stored image directly in a generated PDF.

Syntax

file_data_uri(file)

Parameters

Name Type Required Description
file object | array yes A file object {name, path, type, size, host}, or a whole file-field value (an array of file objects — the first is used).

Returns

A data:{mime};base64,... string. Throws if the value isn't a file, the file doesn't belong to your workspace, or it exceeds the 25 MB limit.

Why this exists

A PDF is rendered on our servers, which have no access to your file storage — so <img src="/some/path.png"> (or a download link) won't load. A data: URI carries the image bytes inline, so it renders without any fetch.

Example: image in a PDF

The Generate PDF brick's content is HTML with {{tokens}}, not code — so build the <img> tag in a Set Variable brick (code), then pass that variable as the PDF content.

Set variable html:

$img  = file_data_uri($record["photo"][0]);
$html = "<h1>" . $record["title"] . "</h1>"
      . "<img style='width:300px' src='" . $img . "'>";

Then in the Generate PDF brick, set Content to {{html}}.

In pure code mode the same thing in one step:

$img = file_data_uri($record["photo"][0]);
pdf_generate("orders", $record["_meta"]["id"], "document",
    "<h1>" . $record["title"] . "</h1><img src='" . $img . "'>");

Notes

  • File fields are arrays$record["photo"][0] is the first file; passing the whole array uses the first.
  • Limited to files 25 MB or smaller.
  • Works for any file type, but only images are useful as an <img> src.

See also: pdf_generate(), file_base64(), file_url()