Change a PDF without rewriting it
Change a PDF's title by appending to it: the original bytes stay untouched.
AGPL-3.0-onlyRuns on this page
Package: @awacloud/pdf
Append an incremental update that changes a PDF's title: a new Info dictionary and cross-reference section after the untouched original bytes.
Uses @awacloud/pdf (AGPL-3.0-only) over @awacloud/fw (Apache-2.0). This example's code is AGPL-3.0-only.
When to use it
A file that is signed, archived or audited must keep its original bytes: an incremental update adds a revision after them instead of rewriting the file, and every earlier revision stays readable.
Copy-paste steps
npm install @awacloud/pdf(it depends on@awacloud/fwand@awacloud/fonts).- Serve
node_modules/@awacloud/fw/src/,node_modules/@awacloud/pdf/src/andnode_modules/@awacloud/fonts/src/at/vendor/fw/,/vendor/pdf/and/vendor/fonts/(node serve.jsfrom the examples root does this; any static server works). - Copy
index.htmlandmain.jsnext to each other and openindex.htmlthrough that server. The../../_style/examples.csslink is optional: copy that file along, or delete the link. Keep the legal notice under</main>: copy../../LICENSES/AGPL-3.0-only.txtalong, or point its link at your copy.
What it shows
pdfIncrementalWriter.appendIncremental(bytes, { updates: [{ num, gen, value }] }): the object keeps its number, the new value lands after the original bytes, and a secondstartxrefpoints at the new cross-reference section.pdfParserObj.objto build the new Info dictionary (obj.dict,obj.string).- The check a signature relies on: the updated file starts with the original bytes, byte for byte.
- The read document's public reach:
doc.info()for the title as text,doc.resolve(ref)for the Info dictionary object the update extends.
Code
main.js
// SPDX-License-Identifier: AGPL-3.0-only
import { ModuleRuntime } from '@awacloud/fw/core/runtime.js';
import { fw_require, pkg_require, modules } from '@awacloud/pdf';
const rt = new ModuleRuntime();
rt.registerAll([...fw_require, ...pkg_require, ...modules]);
const PDF = rt.resolve('pdf');
const BUILDER = rt.resolve('pdfBuilder');
const INCREMENTAL = rt.resolve('pdfIncrementalWriter');
const { obj } = rt.resolve('pdfParserObj'); // builds typed PDF objects (dict, string, …)
const A4 = [0, 0, 595.28, 841.89];
/**
* One A4 page with a line of Helvetica and an Info dictionary.
* @param {{ title?: string, author?: string, line?: string }} [meta]
*/
export function buildSample(meta = {}) {
const { title = 'Quarterly report', author = 'Example author', line = 'Hello from the builder' } = meta;
return BUILDER.builder().addPage({ mediaBox: A4 }).addFont({ name: 'F1', baseFont: 'Helvetica' })
.addContent(`BT /F1 24 Tf 72 770 Td (${line}) Tj ET`).addMetadata({ Title: title, Author: author }).build();
}
/**
* The Info dictionary object of a read document, through its public resolver.
* @param {any} doc
*/
const infoOf = (doc) => (doc.trailer.info ? doc.resolve({ type: 'ref', ...doc.trailer.info }) : null);
/**
* Read a PDF back: header version, page count, first media box, Info title and author.
* @returns {{ version: string, pages: number, mediaBox: number[], title: string | null, author: string | null }}
*/
export function readSummary(/** @type {Uint8Array} */ bytes) {
const doc = PDF.read(bytes);
const info = doc.info(); // the Info dictionary as decoded text, or null
const [title, author] = [info?.title ?? null, info?.author ?? null];
return { version: doc.version, pages: doc.pages.length, mediaBox: doc.pages[0].mediaBox, title, author };
}
/**
* Change the title without rewriting the file: an incremental update appends a
* new Info dictionary and a new cross-reference section; the original bytes
* stay in place as a prefix, which a signature or an audit trail relies on.
*/
export function appendTitle(/** @type {Uint8Array} */ bytes, /** @type {string} */ title) {
const doc = PDF.read(bytes);
const current = infoOf(doc);
if (!current) throw new Error('the document has no Info dictionary to update');
const value = obj.dict({ ...current.entries, Title: obj.string(new TextEncoder().encode(title), 'lit') });
const { num, gen } = doc.trailer.info;
return INCREMENTAL.appendIncremental(bytes, { updates: [{ num, gen, value }] });
}
const original = buildSample();
/** @param {string} id @param {string} value */
const show = (id, value) => { /** @type {HTMLElement} */ (document.getElementById(id)).textContent = value; };
show('before', `${readSummary(original).title} (${original.length} bytes)`);
document.getElementById('append')?.addEventListener('click', () => {
const title = /** @type {HTMLInputElement} */ (document.getElementById('title')).value;
const updated = appendTitle(original, title);
const kept = updated.subarray(0, original.length).every((b, i) => b === original[i]);
show('after', `${readSummary(updated).title} (${updated.length} bytes)`);
const added = updated.length - original.length;
show('status', `+${added} bytes appended; original kept as a prefix: ${kept ? 'yes' : 'no'}`);
});index.html
<!doctype html>
<!-- SPDX-License-Identifier: AGPL-3.0-only -->
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>pdf · Change a PDF without rewriting it</title>
<link rel="stylesheet" href="../../_style/examples.css">
<script type="importmap">
{ "imports": {
"@awacloud/fw": "/vendor/fw/main.js", "@awacloud/fw/": "/vendor/fw/",
"@awacloud/pdf": "/vendor/pdf/main.js", "@awacloud/pdf/": "/vendor/pdf/",
"@awacloud/fonts": "/vendor/fonts/main.js", "@awacloud/fonts/": "/vendor/fonts/" } }
</script>
<script type="module" src="./main.js"></script>
</head>
<body>
<main>
<h1>Change a PDF without rewriting it</h1>
<p>Title of the original: <output id="before"></output></p>
<label for="title">New title</label>
<input id="title" type="text" value="Quarterly report, final edition">
<button id="append" type="button">Append an update</button>
<p>Title after the update: <output id="after">—</output></p>
<p id="status" aria-live="polite"></p>
</main>
<!-- aln:begin (AGPL-3.0 sections 0, 5d and 13; derived by _harness/contract.js legalNotice, checked by C7) -->
<footer id="legal" aria-label="Legal notices">
<p>Copyright (c) 2026 AwaCloud SAS. This page is free software under the GNU Affero General Public License,
version 3 only (AGPL-3.0-only), with ABSOLUTELY NO WARRANTY:
<a href="../../LICENSES/AGPL-3.0-only.txt">licence text</a>.</p>
<p>It runs these packages, each with its own LICENSE and NOTICE file (third-party notices included):</p>
<ul>
<li>@awacloud/fw · Apache-2.0</li>
<li>@awacloud/pdf · AGPL-3.0-only</li>
<li>@awacloud/fonts · AGPL-3.0-only</li>
</ul>
</footer>
<!-- aln:end -->
</body>
</html>