Registry / serialization / wmf
library0.4.2jsnpmunverified

The `wmf` package, currently at stable version 1.0.2, provides a pure JavaScript parser and renderer for Windows MetaFile (WMF) image files. It enables developers to extract image dimensions and draw WMF content onto HTML Canvas elements in both browser and Node.js environments. Unlike solutions that often require server-side processing or native libraries for WMF parsing, `wmf` offers a client-side or server-side (Node.js) JavaScript-only approach. Its primary purpose is to make a legacy image format accessible within modern web and Node.js applications. The library is specialized for this niche, implying a stable but infrequent release cadence focused on maintaining compatibility and reliability for WMF processing rather than rapid feature expansion. It ships with TypeScript types, facilitating type-safe development.

npm install wmf
INSTALL
IMPORT
SIG · WMF
W
wmf
serializationjavascriptv0.4.2
Install
Import
Disk
Pass rate
0/ 6
Env Coverage0 / 6
glibc
1822
musl
1822
Install & Compatibility
Where this runs
tested against v? · npm install
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
node 18226 runs
build_error
glibc
node 18226 runs
build_error
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

WMF
import WMF from 'wmf';
import { WMF } from 'wmf';
The primary API is exported as a default. For CJS environments, use `const WMF = require('wmf');`.
WMF (CommonJS)
const WMF = require('wmf');
import WMF from 'wmf';
This is the correct way to import `wmf` in CommonJS modules, primarily for older Node.js versions or specific module setups. Type declarations should still allow ESM import syntax in TypeScript.
WMF (Browser Global)
<script src="wmf.js"></script> // WMF is now globally available
import WMF from 'wmf'; // In browser HTML directly
When included via a script tag in the browser, `wmf.js` exposes the `WMF` object directly on the global scope, making module imports unnecessary and non-functional without a module loader.

This quickstart demonstrates how to parse a WMF file in Node.js, shim the required `ImageData` global, draw its content onto a `node-canvas` instance, and save the result as a PNG image. It covers basic setup for server-side WMF processing.

import { createCanvas, createImageData } from "canvas"; import WMF from "wmf"; import fs from "fs"; // Create a dummy WMF buffer for demonstration // In a real application, replace this with actual WMF file data // This is a minimal, non-functional WMF header placeholder. // For actual use, ensure 'image.wmf' exists or generate a valid one. const dummyWmfBuffer = Buffer.from([ 0xD7, 0xCD, 0xC6, 0x9A, 0x00, 0x00, 0x00, 0x00, // Header magic 0x01, 0x00, 0x09, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 ]); // Use a real WMF file path if available, otherwise use dummy for structure const wmfFilePath = './test_image.wmf'; // Ensure this file exists for actual execution let wmfData: Buffer; try { wmfData = fs.readFileSync(wmfFilePath); console.log(`Using WMF file: ${wmfFilePath}`); } catch (error) { console.warn(`Could not read ${wmfFilePath}. Using dummy buffer. For actual rendering, provide a valid WMF file.`); wmfData = dummyWmfBuffer; } // Shim ImageData for Node.js environments, required by wmf global.ImageData = createImageData; try { // Extract image dimensions const size = WMF.image_size(wmfData); console.log(`Detected WMF size: ${size[0]}x${size[1]} pixels`); // Create a canvas with the determined dimensions const canvas = createCanvas(size[0], size[1]); const ctx = canvas.getContext('2d'); // Draw the WMF content onto the canvas WMF.draw_canvas(wmfData, canvas); // Save the canvas content to a PNG file const outputPngPath = './output.png'; const buffer = canvas.toBuffer('image/png'); fs.writeFileSync(outputPngPath, buffer); console.log(`WMF content successfully rendered and saved to ${outputPngPath}`); } catch (error) { console.error("Error processing WMF:", error); }
Debug
Known issues
gotchaThe `wmf` library depends on a global `ImageData` constructor. In Node.js environments, this browser API must be manually shimmed using a compatible implementation, such as `createImageData` from the `canvas` npm package.
fix
For Node.js, ensure `global.ImageData = require('canvas').createImageData;` is executed before calling any `wmf` functions.
affects: >=1.0.0
gotchaWhen using `OffscreenCanvas` with `wmf.draw_canvas`, some implementations (like Chrome's) require the canvas dimensions to be passed to its constructor. Failing to do so can result in an uninitialized or incorrectly sized canvas.
fix
First, use `WMF.image_size(data)` to get the WMF's dimensions, then create `const canvas = new OffscreenCanvas(width, height);` before passing it to `WMF.draw_canvas(data, canvas);`.
affects: >=1.0.0
Errors
Common errors & fixes
ReferenceError: ImageData is not defined
The `wmf` library attempts to use the `ImageData` constructor, which is a browser-specific global API, in a Node.js environment without it being shimmed.
fix
In your Node.js application, add `global.ImageData = require('canvas').createImageData;` (assuming you have `canvas` installed) before initializing or using the `wmf` library.
Upgrade
Version history
0.4.2latest on npm
Audit
Dependencies
canvasrequiredRequired for server-side WMF rendering in Node.js environments.
Agent activity
14 hits · last 30 days
node
12
OpenAI (training)
1
Resources
wmf — npm install wmf · libregistry