bdfwasm

command
v0.1.3 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 9, 2026 License: MIT Imports: 43 Imported by: 0

Documentation

Rendered for js/wasm

Overview

Command bdfwasm is the converters built for the browser: a page hands it the bytes of a file and gets a BDF document back, so that the file never leaves the browser. Build it with

GOOS=js GOARCH=wasm go build -tags bdf_noconv -o bdf.wasm ./cmd/bdfwasm

and run it with Go's wasm_exec.js (in a Worker: a conversion keeps the thread busy). -tags pdfonly, officeonly, webonly or imageonly builds a smaller module of the PDF converters (PDF, and Illustrator, whose files are PDFs), the Office converters (Word, PowerPoint, Excel, CSV, Parquet, Visio, draw.io, DXF, Jw_cad, SXF, CGM, Gerber, KiCad, metafiles, Photoshop, the music formats MML, MIDI and MusicXML, font files, and images), the HTML, Markdown and EPUB converters, or the images browsers display by themselves (PNG, JPEG, SVG …, stored as they are) only; previewonly builds one without converters that makes the thumbnail and the search text of a bdf document (see the end of this comment). bdf_noconv leaves out the image and WOFF2 encoders: a document drawn where it is converted gains nothing from them. It leaves out Brotli altogether, whose tables alone would add 1.7 MB to the Office module: the Parquet converter reads no Brotli in any js build (converter/parquet/brotli_js.go), and the font converter no WOFF2 file. The demo site (examples/viewer/site.mjs) builds it with a copy of goldmark (the Markdown converter's parser) whose linkify patterns it rewrites: compiled when the program starts, they overflow the stack of a worker in Safari.

The program sets globalThis.bdfConverter and waits for calls:

bdfConverter.formats: [{name, description, extensions}]
bdfConverter.convert(data: Uint8Array, options?: {
  format?: string,   // a format name; detected from the content when absent
  password?: string, // the open password of an encrypted input
  fonts?: string,    // URL of a font directory (see below)
  name?: string,     // the file name, whose extension tells what the content does not (Markdown, a CSV file of one line)
  pages?: string,    // the pages (slides, sheets) to convert, as bdf generate -pages takes them ("1" for a thumbnail); all when absent
}): Promise<{bdf: Uint8Array, format: string, summary: string, warnings: string[], protected: boolean}>
bdfConverter.open(data, options?): Promise<{bdf, format, pages: number, warnings, stream?}>

convert converts the input whole into a single-file bdf. open starts a conversion done a page at a time (converter.OpenStream), for a viewer that draws the pages as they are converted: with pages > 0, bdf is the outline (every page sized, the pages of the first view without layers) and stream converts the rest:

stream.page(i: number): Promise<{bdf: Uint8Array, warnings: string[]}>
stream.finish(): Promise<{bdf, format, summary, warnings, protected}>
stream.close(): void

page converts page i of the first view and returns a single-file bdf whose only page is that page, with the parts no earlier call returned; warnings are the new ones. finish returns the whole document, as convert makes it, with every warning. close lets the stream go (call it after finish too). A format that is not converted a page at a time comes back whole from open, with pages 0, a summary and no stream.

A failed conversion rejects with an Error whose code is "password-required", "wrong-password" or "unknown-format" when it is one of those. The document is not encrypted even when the input was.

The Office converters lay text out with the fonts of the font directory: index.json there lists its files as [{"name", "size", "scan"}], where scan holds the [offset, length] ranges of the tables the font scan and fallback coverage checks read (the table directories, name, OS/2, post and cmap), fetched ahead in parallel. Fallback search checks cmap first; a font's whole file is fetched only when it can draw a needed character.

The preview module (-tags previewonly) makes of a single-file bdf what a server makes with packages thumbnail and bdf (Document.SearchText):

bdfConverter.thumbnail(bdf: Uint8Array, options?: {
  size?: number,     // pixels: the side of a cropped thumbnail, the longer side of a fitted one (default 256, at most 2048)
  mode?: string,     // "auto" (from the kind of document, as thumbnail.Auto), "crop" or "fit"
  format?: string,   // "png" (default), "jpeg", or "webp" (not with bdf_noconv)
  view?: string,     // the id of the view to draw (default: the first)
  password?: string, // the password of an encrypted document
  fonts?: string,    // URL of a font directory, for text in fonts referred to by name
}): Promise<{image: Uint8Array, format: string, width: number, height: number, mode: "crop" | "fit", warnings: string[]}>
bdfConverter.text(bdf: Uint8Array, options?: {password?: string}): Promise<{json: string}>

text returns what bdf text writes: the metadata and the text of each page, as JSON. Neither the thumbnail nor the text is encrypted when the document is; an encrypted document without its password rejects with "password-required".

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL