Publisher Import
Import Microsoft Publisher (.pub) files into your web editor as editable elements with the @polotno/pub-import JavaScript package, in the browser or Node.js
The @polotno/pub-import package converts a Microsoft Publisher .pub file into Polotno design JSON, in the browser or in Node.js. Text boxes, linked text frames, tables, shapes and pictures arrive as separate, editable elements, never as a flattened page image, and supported WordArt arrives as vector outlines by default. Each publication page becomes one design page, blank pages included. The file is read locally.
Importing other formats? See PDF Import, PSD Import, PPTX Import, IDML Import and SVG Import for the equivalent packages.
Try it without code: The PUB file viewer, PUB to PDF and PUB to PPTX tools run this package in the browser on your own files.
Installation
npm install @polotno/pub-importIt runs in browsers with WebAssembly and in Node.js 22.13 or newer, from the same ESM entry point. The first call to pubToJson loads the reader and the converter; publications with text frames or tables also load the text layout code. Browser bundlers keep these loading boundaries with ESM code splitting.
Basic Usage
Browser – read bytes from a file input:
import { pubToJson } from '@polotno/pub-import';
const file = input.files[0];
const json = await pubToJson({
pub: await file.arrayBuffer(),
onWarning: ({ message, details }) => console.warn(message, details),
});
store.loadJSON(json);Node.js – read bytes from disk:
import { readFile, writeFile } from 'node:fs/promises';
import { pubToJson } from '@polotno/pub-import';
const json = await pubToJson({ pub: await readFile('newsletter.pub') });
await writeFile('newsletter.json', JSON.stringify(json));Render the result with Polotno or polotno-node.
For large publications, run the importer in a Web Worker or a Node worker thread: the parser and the text layout use the calling thread. A Content Security Policy that restricts WebAssembly must allow compilation for the worker or page that imports the file, and a connect-src policy must allow Google Fonts for the substitutes below.
Options
| Option | Default | Meaning |
|---|---|---|
pub | required | The bytes of one publication, as an ArrayBuffer or Uint8Array. Node buffers and bounded byte views work |
fonts | Polotno font definitions. Use the family names from the publication and font sources you may distribute | |
wordArt | "vector" | "vector" draws supported WordArt as outlines, in an eligible font or a substitute. "editable" keeps native text throughout |
onWarning | Receives each feature that the importer approximated or omitted |
pubToJson returns a Promise<Design>. Coordinates use CSS pixels at 96 dpi, and embedded pictures become data URLs.
Fonts
Text keeps its family name, or uses a metric-compatible substitute when there is one. A matching entry in fonts takes precedence over the font embedded in the publication. The importer uses an embedded face for editable designs when its OpenType permissions allow it, and includes only the font families that editable text needs.
To measure glyph advances, pair kerning and line spacing during composition, and to outline supported WordArt, give it an inline TTF, OTF or WOFF2 data URL. External URLs are loaded by the renderer when it draws the design.
const face = await readFile('fonts/BrochureTitle.ttf');
const json = await pubToJson({
pub: await readFile('brochure.pub'),
fonts: [
{
fontFamily: 'Brochure Title',
url: `data:font/ttf;base64,${face.toString('base64')}`,
},
],
});Use the font definition's styles array to supply bold and italic faces.
Desktop families get open-licensed substitutes:
- Served by Google Fonts, such as Arimo, Gelasio, Jost, Anton, Archivo Black and UnifrakturMaguntia. The importer fetches them from
fonts.googleapis.comandfonts.gstatic.comduring the import, to compose linked text and to outline WordArt, and the design names them so the renderer loads them the same way. Without network access, WordArt in those families stays editable, and their text is composed with estimated letter widths and afont-fetchwarning. - Shipped with Polotno, and carried in the design when editable text uses them, so they render offline: Almanac, Bindery and Mangonel for Century, Bookman Old Style and Trebuchet MS; Compass and Colines for Century Gothic and Garamond; Quarry Nova Light for Rockwell Nova Light; and Selawik for Segoe UI.
Body text and WordArt in one family use the same substitute. Editable Lucida Console and Consolas keep their family names and prefer the installed originals, with Prism Console and Polotno Mono as their bundled fallbacks. Editable Segoe UI and Segoe UI Symbol keep their family names. design.custom.publisher.fontSubstitutions records each original and replacement family, and design.custom.publisher.fontFallbacks the bundled face a font's src names after the installed original. Other scripts and symbols need matching glyphs on the rendering host or a supplied font.
What Arrives in the Editor
| Publisher content | Result |
|---|---|
| Pages and master-page content | Pages at their document size, in publication order. Master-page artwork is repeated on each page that uses it |
| Text boxes | Editable rich text with character formatting, paragraph alignment, margins and rotation. Page-number fields show the current page |
| Linked text and columns | Separate editable text frames in reading order, with automatic US and British English hyphenation. Text wraps on the selected side of an object, follows its contour, or resumes below it. The last frame shows its printed text, and its hidden remainder is stored in element.custom.publisher.overset |
| Tables | Native tables with editable text, tracks, merges, solid cell fills, shared grid borders and uniform cell insets |
| Shapes and pictures | Simple shapes and lines become editable elements; straight lines keep triangle, open, stealth and oval arrowheads. Other paths stay vectors. Raster pictures keep their original bytes where the format allows |
| WordArt | Outlines for supported envelopes, with the original wording and family in element.custom.publisher.wordArt. Arabic and Hebrew titles keep joined lettering. Gradients and projected shadows are kept on supported envelopes. Unsupported lettering stays editable text |
Warnings
onWarning receives { code, message, details }, where details holds the feature and, when available, the zero-based pageIndex.
| Code | Meaning |
|---|---|
FEATURE_APPROXIMATED | A feature is drawn approximately |
FEATURE_OMITTED | A feature is not imported |
Some Publisher records have no representation in the reader, and those produce no warning.
Limitations
- Text layout: Publisher and the Polotno Canvas use different text layout engines. Font substitution, hyphenation, wrapped tabbed lines, tab leaders and linked-frame breaks can change the rendered page.
- Tables: a merged cell side carries one border style.
- Arrowheads: diamond heads, and heads on paths other than straight lines, are omitted with a warning.
- 3D WordArt is approximated.
- Pictures: solid-fill EMF+ paths stay vector images. A WMF picture holding a supported 24-bit bitmap is decoded to its original pixels, with its Publisher crop. Other metafile operations and restricted fonts can need separately supplied assets.
- Publisher 97 and 98: Japanese and Chinese text needs a
TextDecoderfor Shift_JIS, GBK and Big5, which browsers and the official Node.js builds provide.
Check a reference render before you rely on a particular publication format or template.
Error Handling
A file that cannot be imported rejects with code: 'IMPORT_FAILED' and details: { format: 'pub', reason }:
reason | Meaning |
|---|---|
unsupported | Not a compound binary Publisher file, or one that holds no supported Publisher document |
corrupt | The document holds data the reader cannot decode |
truncated | An empty file, or one that ends early, such as a copy cut short |
too-large | The file, or the document decoded from it, does not fit in the reader's memory (files of 2 GiB or more are refused) |
An error without a reason is not the file's fault, for example a host that blocks WebAssembly. A file in another compound Office format can carry the Publisher container signature. See Error Handling for the codes every package shares.
License
Development, testing and evaluation are free. Production use, including internal tools, requires a Polotno subscription. The WebAssembly reader includes libmspub and librevenge under MPL 2.0, and the package ships their notices and build recipe. The bundled fallback fonts use the SIL Open Font License 1.1: when you distribute designs that carry them, include the notices from the package's src/emit/wordart-fonts/notices.txt.