DocxWorkshop JS · 0.105.0
JavaScript quickstart
Add a paginated editor to a web app. This example opens a DOCX from the file picker and saves editable DOCX and searchable PDF copies from the browser.
Install the npm package
Use Node.js 22.12 or newer. In a new project directory, place the supplied docxworkshop-js-0.105.0.tgz next to the files you’ll create. The app uses Vite 7.3.7.
npm init -y
npm install ./docxworkshop-js-0.105.0.tgz
npm install -D vite@7.3.7
Add the editor
Create the page with a toolbar, an editor mount, and controls for opening and downloading files.
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>DocxWorkshop example</title>
<style>#editor { height: 70vh; }</style>
</head>
<body>
<label for="word-file">Open a Word file</label>
<input id="word-file" type="file" accept=".docx">
<button id="save-docx" type="button" disabled>Save DOCX</button>
<button id="export-pdf" type="button" disabled>Export PDF</button>
<p id="status" role="status"></p>
<div id="toolbar"></div>
<div id="editor"></div>
<script type="module" src="./main.js"></script>
</body>
</html>
Mount the SDK and wire up import and export. The font registry loads the bundled fonts used by the editor and the searchable PDF renderer.
import { createDocxWorkshop, createDefaultLayoutFontRegistry } from 'docxworkshop-js';
import 'docxworkshop-js/style.css';
const input = document.querySelector('#word-file');
const save = document.querySelector('#save-docx');
const pdf = document.querySelector('#export-pdf');
const status = document.querySelector('#status');
const studio = createDocxWorkshop({
mount: document.querySelector('#editor'),
toolbar: document.querySelector('#toolbar'),
fontRegistry: await createDefaultLayoutFontRegistry(),
});
let name;
async function run(action) {
input.disabled = save.disabled = pdf.disabled = true;
try {
status.textContent = await action();
} catch (error) {
status.textContent = error.message;
} finally {
input.disabled = false;
save.disabled = pdf.disabled = !name;
}
}
input.addEventListener('change', () => {
const [file] = input.files;
input.value = '';
if (!file) return;
void run(async () => {
await studio.importFile(file);
name = file.name.replace(/\.docx$/i, '');
return `Opened ${file.name}`;
});
});
async function download(format, options) {
const filename = `${name}-edited.${format}`;
const blob = await studio.exportBlob(format, { ...options, filename });
const url = URL.createObjectURL(blob);
const link = document.createElement('a');
link.href = url;
link.download = filename;
link.click();
setTimeout(() => URL.revokeObjectURL(url), 30_000);
return `Downloaded ${filename}`;
}
save.addEventListener('click', () => void run(() =>
download('docx', { docxPreservation: 'preserve' })));
pdf.addEventListener('click', () => void run(() =>
download('pdf', { pdfLayout: 'shared-text' })));
Run and verify
npx vite --host 127.0.0.1
- Open the URL printed by Vite and choose a DOCX file.
- Edit a line, select Save DOCX, then reopen the downloaded file.
- Select Export PDF and search for the edited text in a PDF viewer.
API reference
createDocxWorkshop({ mount, toolbar, fontRegistry })- Mounts the editor. The toolbar and font registry are optional. The npm package includes TypeScript declarations.
studio.importFile(file)- Loads a browser
Fileinto the editor. studio.exportBlob(format, options)- Returns a
Blobfor download or storage. Theshared-textPDF layout uses the registered font files to render searchable text. fontRegistry.register({ family, data, weight, style })- Registers the font bytes for a particular face. If the template uses additional fonts, register the matching files before PDF export.
studio.destroy()- Releases the editor instance when its host view is removed.