PDF editor: the JavaScript library

PDF editing on PDFium (WebAssembly): change text in place keeping the layout, add text, pictures, highlights and drawings, hide fragments, rotate, delete, reorder and merge pages.

  • @openlibsdev/pdf-editor
  • v1.0.0
  • ES module
  • MIT

Installation

With a bundler or Node-style imports:

npm install @openlibsdev/pdf-editor
import PdfEditor from "@openlibsdev/pdf-editor";

Or straight in a page, without a build step (jsDelivr resolves the dependencies):

<script type="module">
import PdfEditor from "https://cdn.jsdelivr.net/npm/@openlibsdev/pdf-editor@1.0.0/+esm";
</script>

Usage

import PdfEditor from "@openlibsdev/pdf-editor";

const doc = await PdfEditor.create();               // or await PdfEditor.open(bytes)
await doc.addText(0, 72, 770, "Hello, PDF", { family: "DejaVu Sans", size: 18 });
const page = doc.render(0, 1.5);                    // { width, height, data: RGBA }
const bytes = doc.save();                           // Uint8Array
doc.close();

API

PdfEditor.open(bytes)

Opens a PDF from bytes and returns a document.

PdfEditor.create(width, height)

Creates an empty document (A4 by default).

PdfEditor.ready()

Loads PDFium in advance.

PdfEditor.setup(o)

Sets the addresses of the engine and fonts.

PdfEditor.addFontBytes(name, bytes)

Adds a font from bytes for new text.

PdfEditor.pdfFamilies()

Font families available for text.

PdfEditor.A4

A4 page size in points.

PdfEditor.APP

Name, version and release date of the library: {name, version, created}.

PdfEditor.PdfDocument()

The class of an open document (what open and create return).

PdfEditor.SUBSTITUTE

Fonts used instead of the document's own when they cannot be edited: sans, serif, mono → DejaVu.

PdfEditor.findNamedColor(text, lang)

Finds a named color by its name in the given language ("dark blue" → #00008b).

PdfEditor.fontFor(family, bold, italic)

Font for new text in a family and style, loaded and ready to embed in the PDF.

PdfEditor.listNamedColors(lang)

Lists the named colors with their names in the given language.

PdfEditor.namedColorTexts(lang)

Labels of the color field in the given language (the texts of the color picker), English for a missing language.

PdfEditor.pdfTextFontData

Data for the text and font block of the PDF editor (font-forge's, limited to fonts that can be embedded in a PDF).

PdfEditor.textFontData

Data for a text and font block: the font list, loading a font style, adding a font file, the block's labels in a language and the default values.

Instance: doc = await PdfEditor.open(bytes)

doc.pageCount

Number of pages.

doc.pageSize(i)

Page width and height in points.

doc.render(i, scale)

Draws a page at a scale: {width, height, data}.

doc.paragraphs(i)

Paragraphs of text on a page with their position and font.

doc.paragraphAt(i, x, y)

Paragraph under a point of the page.

doc.editParagraph(i, para, newText, style)

Replaces the text of a paragraph, keeping its place and line breaks.

doc.addText(i, x, yTop, text, style)

Adds text at a point.

doc.insertImage(i, img, r)

Inserts a picture into a rectangle.

doc.highlight(i, r, color)

Highlights a rectangle.

doc.addInk(i, strokes, color, width)

Adds a freehand drawing.

doc.redact(i, r, fill)

Paints over a rectangle and removes what was under it.

doc.rotatePage(i, quarterTurns)

Rotates a page by quarter turns.

doc.deletePage(i)

Deletes a page.

doc.movePage(from, to)

Moves a page.

doc.insertBlankPage(at, width, height)

Inserts an empty page.

doc.merge(bytes, at)

Adds the pages of another PDF.

doc.save()

Returns the document as bytes.

doc.close()

Frees the memory.

doc.annotationCount(i)

Number of annotations (highlights, drawings) on a page.

doc.duplicatePage(i)

Inserts a copy of a page right after it.

doc.importPages(src, at, indexes)

Copies pages of another open document (all, or the given indexes) to a position.

doc.rectToView(i, view, r)

Page-space rectangle {x0, y0, x1, y1} → rectangle on the rendered page {left, top, width, height}.

doc.textObjects(i)

Every text object of a page with its text, position, size and font.

doc.toPage(i, view, x, y)

Point on the rendered page → point in page space (points, y up).

doc.toView(i, view, x, y)

Point in page space → point on the rendered page.

doc.wrap(font, size, text, maxWidth)

Splits text into lines that fit a width, using the real glyph widths of the font.

Dependencies

What the library loads, with the licenses (all are listed on the Licenses page).

NameVersionLicenseFiles
@openlibsdev/font-forge1.0.0MITGitHub
@openlibsdev/image-forge1.0.0MITGitHub
@embedpdf/pdfium2.15.1MIT License · Source code
@embedpdf/pdfium2.15.1MIT License · Source code
@imagemagick/magick-wasm0.0.35Apache-2.0 License · Source code
@imagemagick/magick-wasm0.0.35Apache-2.0 License · Source code
@simonwep/pickr1.10.2MIT License · Source code
@simonwep/pickr1.10.2MIT License · Source code
dejavu-fonts-ttf2.37.3Bitstream Vera Fonts License (free, redistributable); DejaVu changes are public domain License · Source code
imagetracerjs1.2.6Unlicense License · Source code
jspdf2.5.2MIT License · Source code