RebodocsDocs
Start3
What is Rebodocs
Getting started
Demos to run
Put it in your app8
React component
Headless: your own UI
A complete interface
Without a framework
Only to read: the viewer
Styling and Tailwind
Phones and touch
Accessibility
Documents8
Templates and variables
On a server: DOCX and PDF
Documents in an S3 bucket
Letterheads
Versions
Tracked changes
Fonts
Languages
Together2
Real-time collaboration
AI assistants (MCP)
Reference5
Packages
Components and hooks
Editor API
How it works
Status and roadmap
↑↓ to move↵ to openEsc to close26 pages

Documents Documents in an S3 bucket

Documents in an S3 bucket

Where documents live is your application’s business. This page shows one common answer, with a demo to run: the files in a bucket that speaks S3 (Cloudflare R2, AWS S3, MinIO, Backblaze B2…), a small backend that holds the keys, and the editor in the browser.

The browserthe editor · no keysYour backendhas the keys · signsThe bucketR2 · S3 · MinIO · B21234
  1. 1GET /api/url?key=contract.docx — An address to read this file from, please.
  2. 2{ url } — Signed with the keys, for this file and five minutes.
  3. 3GET url — The browser goes to the bucket with it: no key, only the address.
  4. 4the DOCX — The bytes, given to the editor as its docx.

The browser never has a key of the bucket. It asks the backend for an address signed for one file and five minutes, and reads from or writes to the bucket itself, so the files do not pass through your server. Only the PDF is made by the server, because that is work for one.

tip: save the contract below and watch where each request goes
Live

The requests of a save

A real editor over the same protocol as the demo, with the backend and the bucket played in this page: the addresses are signed and checked as an S3 checks them, but nothing leaves the browser.

Opening the editor…

Opening is a list, an address and a read from the bucket. A save is two addresses and two writes: the document, and a copy of it under versions/. Take the CORS rule away and the browser never reaches the bucket; a PUT of another type breaks the signature; five minutes later the address has run out.

Start here

Run it with Cloudflare R2

The demo is apps/storage. It runs on your machine, with a bucket of your own.

  1. A bucket

    In the Cloudflare dashboard, R2, Create bucket. Call it rebodocs-demo, or what you like.

  2. Keys

    R2, Manage API tokens, Create API token, with Object Read & Write for that bucket only. It shows the Access Key ID and the Secret Access Key once.

  3. The environment

    Copy apps/storage/.env.example to .env and fill it in: the generator below writes it for the S3 you pick.

  4. Let the page in

    npm run cors -w @rebodocs/storage-demo tells the bucket the page may use its addresses. Keys for objects only may not change that: it then prints the rule to paste.

  5. A document, and the demo

    npm run seed -w @rebodocs/storage-demo puts contract.docx, a template, in the bucket; npm run storage opens http://localhost:5176.

git clone https://github.com/JFranciscoRobles/rebodocs && cd rebodocs && npm install
cp apps/storage/.env.example apps/storage/.env     # and fill it in
npm run cors -w @rebodocs/storage-demo             # lets http://localhost:5176 reach the bucket
npm run seed -w @rebodocs/storage-demo             # puts contract.docx in it
npm run storage                                    # http://localhost:5176

Open contract.docx from the list, change it, save: it is written to the bucket, and a version beside it, signed with the name you give your notes. If the document is written and its version is not, the demo says so apart: the document is saved. PDF on the server… asks for the values of its variables and leaves contract.pdf beside it. New starts an empty document that goes to the bucket when it is saved.

Any S3

Another S3

The code is the same; the environment says which one. Pick yours, and copy what it writes.

…

Node

The backend

Seven routes, in server/api.ts. It is a plain Node handler, (request, response), so it goes as it is into Express, a Next.js route or a server of your own.

GET /api/files
The .docx and .pdf of the bucket, the newest first: key, size, modified. The versions are not among them.
GET /api/url?key=…
{ url }: an address to read that file from, good for five minutes.
POST /api/url?key=…
{ url, type }: an address to write a .docx to, sending that Content-Type and no other. Never one of a version: those stay as they were kept.
GET /api/variables?key=…
{ names }: the variables of a template.
POST /api/pdf?key=…
Fills the template with the JSON of the body, stores the PDF beside it, and answers { key, url, pages, missingFonts }. A server with no font says so, with a 503.
GET /api/versions?key=…
The versions kept of a document: id (its key, read with GET /api/url), date, author, label.
POST /api/versions?key=…
{ url, type, version }: an address to write a new version to. The JSON of the body names it: { label, author }. The oldest beyond the 50 kept (VERSIONS_KEPT) go first.

A version is a file under versions/<key>/. Its key holds when it was kept and, encoded, who kept it and its name, so that one listing request gives them all; the editor’s side panel lists them, compares them and restores them. It is written once and never again: the backend signs no address to write over one. Fifty are kept of each document, or what VERSIONS_KEPT says, and the oldest go as new ones come. See versions. Signing an address is two lines with the AWS SDK:

import { GetObjectCommand, PutObjectCommand, S3Client } from '@aws-sdk/client-s3';
import { getSignedUrl } from '@aws-sdk/s3-request-presigner';

const client = new S3Client({
  endpoint: process.env.S3_ENDPOINT,
  region: process.env.S3_REGION ?? 'auto',
  credentials: { accessKeyId: process.env.S3_ACCESS_KEY_ID!, secretAccessKey: process.env.S3_SECRET_ACCESS_KEY! },
  requestChecksumCalculation: 'WHEN_REQUIRED',
  responseChecksumValidation: 'WHEN_REQUIRED',
});

const readUrl = (Key: string) => getSignedUrl(client, new GetObjectCommand({ Bucket, Key }), { expiresIn: 300 });
const writeUrl = (Key: string, ContentType: string) =>
  getSignedUrl(client, new PutObjectCommand({ Bucket, Key, ContentType }), { expiresIn: 300, signableHeaders: new Set(['content-type']) });
React

The page

Opening is a file given to the component, and saving is what onSave hands back.

import { DocsEditor } from '@rebodocs/react';

async function read(key: string) {
  const { url } = await (await fetch(`/api/url?key=${encodeURIComponent(key)}`)).json();
  return new Uint8Array(await (await fetch(url)).arrayBuffer());
}

async function write(key: string, docx: Uint8Array) {
  const { url, type } = await (await fetch(`/api/url?key=${encodeURIComponent(key)}`, { method: 'POST' })).json();
  await fetch(url, { method: 'PUT', headers: { 'content-type': type }, body: docx });
}

<DocsEditor key={key} docx={file} name={key} onSave={(docx) => write(key, docx)} />
@rebodocs/core

A PDF on the server

The server reads the template from the bucket, fills it, lays it out with font files and stores the result. See On a server for what each call does.

import { fillVariables, importDocx, renderPdf } from '@rebodocs/core';

const template = importDocx(await bucket.get('contract.docx'));
const { pdf, missingFonts } = renderPdf(fillVariables(template, values), fonts, 'contract');
await bucket.put('contract.pdf', pdf, 'application/pdf');

The demo looks for .ttf and .otf files in the fonts of the system, or in the folder FONTS_DIR names. A server of yours has no fonts unless you put them there: ship the ones your documents use, and read missingFonts to know when one was stood in for.

Limits

What to know

Check who is asking before signing.

The demo signs for anyone who reaches it, which is right for a machine of your own and wrong anywhere else. In your application the routes know the person and decide which keys they may read and write: a prefix for each account, for one.

Sign the type too.

With signableHeaders, an address to write takes one Content-Type and refuses a PUT with another. Without it, the bucket stores whatever type the browser says.

No checksums the browser cannot send.

Recent versions of the AWS SDK add a checksum to what they sign, as headers a browser does not send, and not every S3 takes them. The two WHEN_REQUIRED lines turn that off.

The origin has to be allowed.

A request the browser refuses before it is answered is, nearly always, the CORS rule: the port and the protocol are part of the origin.

The last one to save wins.

Two people on the same file overwrite each other. For several at once, see collaboration: its demo can keep its documents in this same bucket.

The PDF is made from what is stored,

not from what is on screen: save first. The button waits until the document is in the bucket.

How it has been checked. The backend has automated tests against an S3 kept in memory: listing, signed addresses, writing as a browser does, the variables, the PDF (on a machine with fonts), versions kept, pruned and never written over, and the rule of origins. Against a bucket of Cloudflare R2 the same was run by hand from Node, with the origin rule in place.