Getting started
emf-converter converts Windows metafiles into PNG, JPEG, WebP, SVG or PDF:
| Format | Description | Coordinate system |
|---|---|---|
| WMF | Windows Metafile (16-bit) | Window/viewport mapping |
| EMF | Enhanced Metafile (32-bit GDI) | Bounds-based scaling |
| EMF+ | GDI+ extension embedded in EMF | World transform matrix |
Metafiles are recorded GDI and GDI+ drawing calls. They are common in Office documents and on the Windows clipboard. The library replays those calls in the same way Windows does. PNG output is compared pixel by pixel with images rendered by Windows (the fixtures are in src/__fixtures__/gdi, generated by scripts/gdi-fixtures). SVG output keeps paths, text and gradients as vectors.
Installation
npm install emf-converterpnpm add emf-converteryarn add emf-converterbun add emf-converterThe package has no required dependencies.
Browser and Web Workers
OffscreenCanvas or HTMLCanvasElement is used automatically for PNG, JPEG and WebP output. SVG and PDF output do not need a canvas.
Node.js
SVG and PDF output work without a canvas. PNG, JPEG and WebP output of drawings without text uses the built-in JavaScript rasteriser.
For PNG, JPEG or WebP output of drawings that contain text, do one of the following:
Pass font files with the
fontsoption. See Exact text.Install the optional
@napi-rs/canvaspackage. It ships prebuilt binaries and does not neednode-gyp.shnpm install @napi-rs/canvas
If neither is available, PNG, JPEG or WebP conversion of a drawing with text returns null instead of an image without its text.
First conversion
import { convertMetafileToDataUrl } from 'emf-converter';
const buffer: ArrayBuffer = /* contents of an .emf or .wmf file */;
const png = await convertMetafileToDataUrl(buffer);
// "data:image/png;base64,iVBORw0KGgo..."The function returns Promise<string | null>. The result is null when the buffer is not a valid metafile, or, in plain Node.js, when the drawing has text and neither fonts nor @napi-rs/canvas is available.
Reading a file in Node.js. A Buffer can be passed straight to any conversion function:
import { readFile } from 'node:fs/promises';
import { convertMetafileToSvg } from 'emf-converter';
const bytes = await readFile('drawing.emf');
const svg = await convertMetafileToSvg(bytes);To write the converted file itself, use convertMetafile, which returns the bytes and their MIME type:
import { readFile, writeFile } from 'node:fs/promises';
import { convertMetafile } from 'emf-converter';
const image = await convertMetafile(await readFile('drawing.emf'), { format: 'webp' });
if (image) {
await writeFile('drawing.webp', image.data);
}The package also installs an emf-converter command line tool. See Usage.
Next: Usage.