Pagecraft

docx-to-pdf

Word to PDF

Set a Word document on paper, with its headings, lists, tables and pictures.

Reads
a Word document — application/vnd.openxmlformats-officedocument.wordprocessingml.document
Writes
one file — PDFs
Tool page
/word-to-pdf

Example

Send a Word document to somebody who should not be able to edit it.

Given

  • letter.docx — a Word document

From code

            import { loadDocument, runOperation } from '@pagecraft/core';

const letter = await loadDocument({ bytes, name: 'letter.docx' });

const [output] = await runOperation('docx-to-pdf', [letter], {
  size: 'a4',
});
          

From a shell

            pagecraft docx-to-pdf letter.docx --size a4
          

What comes back

One PDF, containing “Quarterly report”.

This example is run by the test suite. If it stopped being true, the build would fail.

Options

filename--filename

string

Filename

What the PDF will be called.

Default: ''

size--size

string, one of

Page size

Default: 'source'

  • source — The size and margins in the file, or A4 with 20mm where it names none.
  • a3 — A3
  • a4 — A4
  • a5 — A5
  • a6 — A6
  • b5 — B5
  • letter — Letter
  • legal — Legal
  • tabloid — Tabloid
  • executive — Executive
  • custom — Custom size

width--width

number

Width

Only when size is 'custom'.

Default: 210

height--height

number

Height

Only when size is 'custom'.

Default: 297

orientation--orientation

string, one of

Orientation

Default: 'portrait'

  • portrait — Portrait
  • landscape — Landscape

margin--margin

number

Margin

Default: 20

unit--unit

string, one of

Measured in

Default: 'mm'

  • mm — Millimetres
  • cm — Centimetres
  • in — Inches
  • pt — Points

fontSize--font-size

number (pt)

Text size

Default: 11

lineHeight--line-height

number

Line spacing

A multiple of the text size. 1.4 is what a book uses.

Default: 1.4

align--align

string, one of

Alignment

Default: 'left'

  • left — Even word spacing; an uneven right edge.
  • justify — Both edges flush; the word spacing stretches to do it.

font--font

string, one of

Typeface

Default: 'sans'

  • sans — Helvetica.
  • serif — Times.
  • mono — Courier.

fontFile--font-file

Uint8Array

Font file

A TrueType or OpenType font to set the text in. Needed for scripts the built-in fonts do not cover, unless the host supplies one.

What it cannot do

  • This converts the document rather than reproducing Word's layout of it: the text is set again from scratch, so the lines and the page breaks fall where this engine puts them, not where Word put them. It is not a facsimile and the page count may differ.
  • What is read is what the document says: headings, paragraphs, bold and italic runs, lists, tables, embedded pictures, hyperlinks, and the page size and margins it was set up for.
  • What is not read is what Word would have drawn: styles beyond the heading levels, tabs and their stops, columns, text boxes, shapes, charts, headers and footers stored in the document, footnotes, endnotes, comments and tracked changes.
  • A cell that spans several rows is set as a cell in the row it starts in; only spans across columns survive.
  • A `.doc` from before 2007 is a different format altogether and cannot be read. Save it as `.docx` first.
  • Text is set in the standard fonts every reader already carries, so the file renders identically on a machine that has none of your fonts installed. Anything they cannot draw needs a font file, which is embedded and subsetted into the PDF with it.
  • A font file you supply is used for everything: bold, italic and monospaced text are all drawn in that one face rather than in a bold, italic or fixed-width cut of it. Code blocks and columns of figures lose their alignment as a result, so supply one only when the built-in fonts cannot draw your text.
  • Shaping and joining come from the font itself, and nothing reorders a line: text is drawn in the order it is given, so a line mixing right-to-left and left-to-right writing comes out in that order rather than in reading order.