Open in a new tab

Convert one Markdown source (a textarea) to .docx, .odt and .pdf with oconv.fromMd; per target, the byte size, the lossy flag and the loss ledger side by side, and a download.

Uses @awacloud/oconv (AGPL-3.0-only) over @awacloud/fw (Apache-2.0). This example's code is AGPL-3.0-only.

When to use it

Letting people write once, in Markdown, and hand out the office format each recipient expects — a Word file for the client, an OpenDocument for the public body, a PDF for the archive — without a server and without a rendering engine. The three ledgers next to each other show what each container could not carry, so the author decides what to simplify.

Copy-paste steps

  1. npm install @awacloud/oconv (it depends on @awacloud/fw, md, ooxml, odf, pdf and fonts).
  2. Serve each package's src/ directory at /vendor/<name>/ — fw, pdf, fonts, oconv, md, ooxml, odf — with any static server; the import map in index.html points there.
  3. Copy index.html and main.js next to each other and open index.html through that server. The ../../_style/examples.css link is optional: copy that file along, or delete the link. Keep the legal notice under </main>: copy ../../LICENSES/AGPL-3.0-only.txt along, or point its link at your copy.
  4. Pass opts: { pdf: { pageSize, margin, … } } to fromMd for the PDF target, or register the @awacloud/oconv-fonts face pack on the runtime for real Unicode coverage (see the oconv-fonts example).

What it shows

  • Wiring: one ModuleRuntime with @awacloud/oconv's fw_require and modules arrays (the four office packages' graphs come with them) plus fw's download; rt.resolve('oconv') is the façade.
  • The conversion: oconv.fromMd({ markdown, name, target }) for each of docx, odt, pdf returns { bytes, target, lossy, losses }. The test checks the containers (PK archives with the OpenDocument media type for .odt, %PDF- for the PDF) and that a plain heading-and-paragraph source converts with no loss on all three.
  • The ledgers: the prefilled source carries YAML front matter, which no target can hold — every ledger records frontmatter/stripped (yaml), and lossy is yes on all three. A detail that is an object (the PDF writer's layout losses) is shown as JSON. An empty ledger shows "no loss recorded".
  • Downloads: one button per target, download.bytes(bytes, name, type) with the right media type.
  • Not shown: images (assets), the Web Worker entry, convert and toMd (see the sibling examples and the docx-to-markdown recipe).

Code

main.js

javascriptAGPL-3.0-only76 lines

// SPDX-License-Identifier: AGPL-3.0-only
import { ModuleRuntime } from '@awacloud/fw/core/runtime.js';
import { download } from '@awacloud/fw/dom/fs/download.js';
import { fw_require, modules } from '@awacloud/oconv';

const rt = new ModuleRuntime();
rt.registerAll([...fw_require, ...modules, download]);
const OCONV = rt.resolve('oconv');
const DOWNLOAD = rt.resolve('download');

/** The three `fromMd` targets and their media types. */
export const TARGETS = {
    docx: 'application/vnd.openxmlformats-officedocument.wordprocessingml.document',
    odt: 'application/vnd.oasis.opendocument.text',
    pdf: 'application/pdf',
};

/**
 * One Markdown source to every target. Each result carries the bytes, the
 * `lossy` flag and the loss ledger that target's path recorded.
 * @param {string} markdown
 * @returns {Promise<{ target: string, bytes: Uint8Array, lossy: boolean,
 *   losses: { code: string, detail: unknown }[] }[]>}
 */
export async function convertAll(markdown) {
    return Promise.all(Object.keys(TARGETS).map(async (target) => {
        const { bytes, lossy, losses } = await OCONV.fromMd({ markdown, name: `document.${target}`, target });
        return { target, bytes, lossy, losses };
    }));
}

/** A loss as one readable line: `code (detail)`. @param {{ code: string, detail: unknown }} loss */
export const describeLoss = ({ code, detail }) =>
    `${code} (${typeof detail === 'string' ? detail : JSON.stringify(detail)})`;

const source = /** @type {HTMLTextAreaElement} */ (document.getElementById('source'));
const rows = /** @type {HTMLElement} */ (document.getElementById('rows'));
const status = /** @type {HTMLElement} */ (document.getElementById('status'));

/**
 * @param {{ target: string, bytes: Uint8Array, lossy: boolean, losses: { code: string, detail: unknown }[] }} result
 */
function row({ target, bytes, lossy, losses }) {
    const tr = document.createElement('tr');
    const cell = (/** @type {Node | string} */ content) => {
        const td = tr.appendChild(document.createElement('td'));
        td.append(content);
        return td;
    };
    cell(`.${target}`);
    cell(String(bytes.length));
    cell(lossy ? 'yes' : 'no');
    const list = document.createElement('ul');
    for (const text of losses.length ? losses.map(describeLoss) : ['no loss recorded']) {
        list.appendChild(document.createElement('li')).textContent = text;
    }
    cell(list);
    const button = document.createElement('button');
    button.type = 'button';
    button.textContent = `Download .${target}`;
    button.addEventListener('click', () => DOWNLOAD.bytes(bytes, `document.${target}`, TARGETS[target]));
    cell(button);
    return tr;
}

document.getElementById('convert')?.addEventListener('click', async () => {
    status.textContent = 'converting…';
    try {
        const results = await convertAll(source.value);
        rows.replaceChildren(...results.map(row));
        status.textContent = `${results.length} targets written from ${source.value.length} characters of Markdown`;
    } catch (err) {
        rows.replaceChildren();
        status.textContent = `error: ${/** @type {Error} */ (err).message}`;
    }
});

index.html

htmlAGPL-3.0-only72 lines

<!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>oconv · One Markdown source to .docx, .odt and .pdf</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/",
    "@awacloud/oconv": "/vendor/oconv/main.js", "@awacloud/oconv/": "/vendor/oconv/",
    "@awacloud/md": "/vendor/md/main.js", "@awacloud/md/": "/vendor/md/",
    "@awacloud/ooxml": "/vendor/ooxml/main.js", "@awacloud/ooxml/": "/vendor/ooxml/",
    "@awacloud/odf": "/vendor/odf/main.js", "@awacloud/odf/": "/vendor/odf/" } }
  </script>
  <script type="module" src="./main.js"></script>
</head>
<body>
  <main>
    <h1>One Markdown source to .docx, .odt and .pdf</h1>
    <p><label for="source">Markdown</label></p>
    <textarea id="source" rows="14" cols="72">---
title: Release notes
author: The team
---

# Release notes

The **2.4** release ships three changes:

- a faster importer
- a new *export* panel
- fixes, see the table

| Area | Change | Status |
|---|---|---|
| Import | 2× faster | done |
| Export | new panel | done |
</textarea>
    <p>
      <button id="convert" type="button">Convert</button>
      <span id="status" aria-live="polite"></span>
    </p>
    <table>
      <caption>Targets</caption>
      <thead><tr><th scope="col">Target</th><th scope="col">Bytes</th><th scope="col">Lossy</th>
        <th scope="col">Loss ledger</th><th scope="col"></th></tr></thead>
      <tbody id="rows"></tbody>
    </table>
  </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>
      <li>@awacloud/oconv · AGPL-3.0-only</li>
      <li>@awacloud/md · AGPL-3.0-only</li>
      <li>@awacloud/ooxml · AGPL-3.0-only</li>
      <li>@awacloud/odf · AGPL-3.0-only</li>
    </ul>
  </footer>
  <!-- aln:end -->
</body>
</html>