[]
        
(Showing Draft Content)

README

DsPdfJS API v9.1.0


@mescius/ds-pdf

Document Solutions for PDF JS

Document Solutions for PDF JS (DsPdfJS) is a powerful PDF library for JavaScript and TypeScript that provides a rich set of APIs for fast, memory-efficient PDF and image processing in modern web and server-side environments.

Powered by WebAssembly (Wasm), DsPdfJS runs in browsers and JavaScript runtimes such as Node.js, Deno, and Bun. Its core object model follows the PDF specification, providing programmatic access to PDF elements such as document properties, pages, fonts, annotations, forms, and more.

DsPdfJS also includes high-level APIs for creating PDF documents with complex layouts including formatted text, graphics, images, forms, and advanced document workflows.

Features include:

  • Programmatically create, load, modify, save, or inspect PDF documents
  • Support for modern PDF standards
  • Export PDF pages to raster or vector image formats
  • Merge or split PDF documents
  • Draw text, shapes, paths, and images with high-level graphics APIs
  • Powerful text formatting and layout engine with wrapping, alignment, spacing, tabs, RTL text (including Kashida), and vertical writing
  • Advanced font handling with embedding and subsetting support
  • Add raster or vector images including PNG, JPEG, SVG, and SVGZ
  • Create, edit, fill, or flatten AcroForm PDF forms
  • Work with annotations, links, text markup, and rich media
  • Find and replace text with configurable search options and exact match positions
  • Programmatically create and apply redactions to permanently remove sensitive content
  • Encryption and security APIs
  • Image processing APIs for resizing, transforming, filtering, drawing, and bitmap generation
  • Designed for efficient memory usage through explicit Wasm object lifecycle management
  • TypeScript-ready with modern module support

Compatibility:

DsPdfJS is compatible with:

  • Environments:
    • Modern browsers
    • Node.js, Deno, Bun, other Node‑compatible runtimes
  • Module formats:
    • ES Modules
    • CommonJS
    • UMD
  • TypeScript (type definitions are included)

Installation:

npm install @mescius/ds-pdf

Getting Started:

1. Set the WebAssembly (Wasm) URL

DsPdfJS uses a WebAssembly module. You must point DsPdfConfig.wasmUrl to the shipped .wasm file:

import { DsPdfConfig } from "@mescius/ds-pdf";
DsPdfConfig.wasmUrl = "/node\_modules/@mescius/ds-pdf/assets/DsPdf.wasm";

2. (Optional) Apply a License Key

// await DsPdfConfig.setLicenseKey("YOUR\_LICENSE\_KEY");

3. Connect to DsPdfJS

import { connectDsPdf } from "@mescius/ds-pdf";
const connected = await connectDsPdf();
if (!connected) throw new Error("Failed to initialize DsPdfJS.");

Usage:

Browser (ES Modules)

import { DsPdfConfig, connectDsPdf, PdfDocument, pushObjectManager, popObjectManager
} from "@mescius/ds-pdf";

DsPdfConfig.wasmUrl = "/node_modules/@mescius/ds-pdf/assets/DsPdf.wasm";
// await DsPdfConfig.setLicenseKey("...");

if (!(await connectDsPdf())) throw new Error("Failed to initialize DsPdfJS.");

pushObjectManager();
try {
  const doc = new PdfDocument();
  const page = doc.pages.addNew();
  const ctx = page.context;

  ctx.drawText({ text: "Hello, World!", fontSize: 20 }, 72, 72);

  const pdfData = doc.savePdf();
  // pdfData is a Uint8Array (or equivalent binary buffer)
  return pdfData;
} finally {
  popObjectManager();
}

Browser (Pure JavaScript / UMD)

Include the UMD bundle directly:

<script src="node_modules/@mescius/ds-pdf/umd/ds-pdf.js"></script>
<script>
async function createDocument() {
  // UMD bundle exposes global 'DocSol'
  DocSol.DsPdfConfig.wasmUrl = "/node_modules/@mescius/ds-pdf/assets/DsPdf.wasm";
  // await DocSol.DsPdfConfig.setLicenseKey("...");

  const connected = await DocSol.connectDsPdf();
  if (!connected) throw new Error("Failed to initialize DsPdfJS.");

  // Use an ObjectManager to control native/Wasm-backed object lifetimes:
  const om = new DocSol.ObjectManager();
  try {
    const doc = new DocSol.PdfDocument(om);
    const page = doc.pages.addNew();
    const ctx = page.context;

    ctx.drawText({ text: "Hello, World!", fontSize: 20 }, 72, 72);

    return doc.savePdf();
  } finally {
    om.dispose();
  }
}

createDocument()
  .then(pdfData => console.log("PDF created:", pdfData))
  .catch(err => console.error(err));
</script>

Node.js (CommonJS)

const { DsPdfConfig, connectDsPdf, PdfDocument, ObjectManager } = require("@mescius/ds-pdf");

// Example: set to the package Wasm (adjust to your environment/pathing)
DsPdfConfig.wasmUrl = "./node_modules/@mescius/ds-pdf/assets/DsPdf.wasm";
// await DsPdfConfig.setLicenseKey("...");

(async () => {
  if (!(await connectDsPdf())) throw new Error("Failed to initialize DsPdfJS.");

  const om = new ObjectManager();
  try {
    const doc = new PdfDocument(om);
    const page = doc.pages.addNew();
    const ctx = page.context;

    ctx.drawText({ text: "Hello, World!", fontSize: 20 }, 72, 72);

    const pdfData = doc.savePdf();
    return pdfData;
  } finally {
    om.dispose();
  }
})();

Memory Management: ObjectManager

Many objects are backed by Wasm memory. Use one of these patterns to control memory usage:

  • Push/pop: pushObjectManager(); ... popObjectManager();
  • Explicit: const om = new ObjectManager(); ... om.dispose();
  • Decorator: @withObjectManager async pdfSample1() { ... }

This makes sure that intermediate objects are released when no longer needed.


Notes:

  • Coordinate units in PDF drawing contexts are “page units” (points) by default (72 units per inch), but can be changed as needed.
  • Coordinate units in image drawing contexts are pixels (96 units per inch).

Resources:


Other Document Solutions Products:

Document Solutions JavaScript Viewers:


License:

A commercial license is required for production use.

Each purchase includes one year of updates and support. Please contact MESCIUS Sales (us.sales@mescius.com) for pricing, trial keys, and licensing options.