@docx-editor.dev/docx-to-pdf

v2.22.0 · 1 published subpath with full TypeScript signatures and JSDoc.

npmv2.22.0licenseEigenPal Prosourcegithub

Package root

Functions (5)

createFontSourcefunctionSource ↗

Turn font bytes you ALREADY hold into a FontSource — a file input, IndexedDB, a bundler import. loadFonts covers URLs; this covers everything else, so no caller has to hand-assemble the record or reach for a hashing helper.

Returns a typed failure instead of throwing when the descriptor or the bytes are unusable, matching loadFonts: one bad face degrades itself, never its neighbours.

declare function createFontSource(bytes: Uint8Array, request: FontFaceRequest & {
    readonly faceIndex?: number;
}, options?: {
    readonly id?: string;
    readonly maxFontBytes?: number;
}): {
    readonly source: FontSource;
} | {
    readonly failure: FontLoadFailure;
};

defineFontResolverfunctionSource ↗

Mark a function as an on-demand [FontResolver](FontResolver).

Wrap every resolver you put in a fonts list — useFonts(...), useDocxSource's fonts option, composeFontOrigins. The fonts PROP needs no marking, because a function there is always a resolver; a list also accepts the older zero-argument loader form ({ fonts: defaultFonts }), and this is what keeps the two apart.

Returns the same function object, mutated, so the mark survives being passed around and a marked resolver stays === to itself. It does NOT survive .bind() or being wrapped: both make a new function object. Re-mark the result.

ts const brandFonts = defineFontResolver(async ({ families }) => ({ sources: await loadMine(families), }));

Throws a TypeError on a frozen or sealed function, which cannot take the mark. That is deliberate: returning it unmarked would compile — the return TYPE says marked — and then lose every font at runtime.

declare function defineFontResolver<T extends FontResolver>(resolve: T): MarkedFontResolver<T>;

exportPdffunctionSource ↗

Convert DOCX bytes. Disposes its session after success or failure.

declare function exportPdf(source: Uint8Array, options?: PdfExportOptions): Promise<PdfExportResult>;

exportPdfFromfunctionSource ↗

Encode a caller-owned font-backed session without reopening the document. Does not dispose the session. The caller must dispose it after all exports. Cancellation stops this export; the session's signal controls shared resource work.

declare function exportPdfFrom(session: PdfExportSession, options?: PdfProjectionOptions): Promise<PdfExportResult>;

openDocumentForExportfunctionSource ↗

Open a reusable PDF session over immutable DOCX bytes. Pass the session to exportPdfFrom or Markdown's exportMarkdownFrom. The caller must dispose the session. Its signal controls the entire session lifetime.

declare function openDocumentForExport(source: Uint8Array, options?: OpenPdfDocumentForExportOptions): Promise<OpenPdfDocumentForExportResult>;

Classes (7)

ExportResourceErrorclassSource ↗

Bounded failure from a headless export session.

declare class ExportResourceError extends Error
MemberTypeSummary
(constructor)Constructs a new instance of the `ExportResourceError` class
code'aborted' | 'timedOut' | 'nonConvergent' | 'disposed' | 'layoutInvariant' | 'layoutFailed'

PdfDocumentOpenErrorclassSource ↗

Core refused the input package.

declare class PdfDocumentOpenError extends Error
MemberTypeSummary
(constructor)Constructs a new instance of the `PdfDocumentOpenError` class
code
detail?string | undefined
reasonHeadlessDocumentRejection | 'aborted'

PdfEncodingErrorclassSource ↗

PDF encoding failed after a document was opened.

declare class PdfEncodingError extends Error
MemberTypeSummary
(constructor)Constructs a new instance of the `PdfEncodingError` class
code'encodingFailed' | 'outputTooLarge'

PdfFidelityErrorclassSource ↗

Requested content could not be represented faithfully.

declare class PdfFidelityError extends Error
MemberTypeSummary
(constructor)Constructs a new instance of the `PdfFidelityError` class
code
diagnosticsreadonly PdfDiagnostic[]

PdfOutputLimitErrorclassSource ↗

Encoded output exceeds the configured byte limit.

declare class PdfOutputLimitError extends PdfEncodingError
MemberTypeSummary
(constructor)Constructs a new instance of the `PdfOutputLimitError` class
actualnumber
code
limitnumber

PdfPageLimitErrorclassSource ↗

Layout exceeds the configured page limit.

declare class PdfPageLimitError extends RangeError
MemberTypeSummary
(constructor)Constructs a new instance of the `PdfPageLimitError` class
actualnumber
code
limitnumber

PdfWorkLimitErrorclassSource ↗

Content exceeds a processing budget.

declare class PdfWorkLimitError extends Error
MemberTypeSummary
(constructor)Constructs a new instance of the `PdfWorkLimitError` class
code

Interfaces (11)

ExportDroppedEmbeddedFontinterface

One document-embedded face the export mapper refused before composition.

interface ExportDroppedEmbeddedFont
MemberTypeSummary
partNamestring
reason'overLimit' | 'malformed'
requestFontRequest

ExportFontFaceResolutioninterface

One paintable face in an export font-resolution report.

interface ExportFontFaceResolution
MemberTypeSummary
faceIndex?number
hash?string
id?string
identity?stringAdmitted face identity (`hash#faceIndex`); remains after session disposal.
sourceFamilystring
style'normal' | 'italic'
substitution?FontSubstitution | nullFull substitution evidence; `null` when the admitted face answers the request directly.
via'direct' | 'substitution'
weight400 | 700

ExportFontFamilyResolutioninterface

Coverage of one family Core layout may request.

interface ExportFontFamilyResolution
MemberTypeSummary
coverage'complete' | 'partial' | 'none'
facesreadonly ExportFontFaceResolution[]
familystring

ExportFontResolutionReportinterface

Exporter-neutral evidence for the font policy behind one layout session.

interface ExportFontResolutionReport
MemberTypeSummary
defaultFamilystring
droppedEmbeddedFonts?readonly ExportDroppedEmbeddedFont[]Bounded drop evidence from the document-embedded font origin; omitted on legacy mocks.
familiesreadonly ExportFontFamilyResolution[]
originFailuresreadonly FontOriginFailure[]
requestedFamiliesreadonly string[]

FontOriginFailureinterface

A failed font origin or a partial failure in an otherwise usable fragment.

interface FontOriginFailure
MemberTypeSummary
causeunknown
originIndexnumberZero-based position in the first-wins origin list.
originName?stringResolver function name when one is safely available.

FontRequestinterface

A face, as something asks for it: family plus the two axes this engine admits.

Only static weights and slants. Variable-font axes are deliberately outside this vocabulary — the shaper refuses variation axes, and a variable file admitted here would render bold at regular weight.

interface FontRequest
MemberTypeSummary
familystring
style'normal' | 'italic'
weightnumber

FontSubstitutioninterface

A request that was answered by a DIFFERENT face than the one asked for.

Recorded rather than silently applied, because a substitution changes measurement: it is part of the shaping fingerprint, so a cached run shaped against a substitute is never reused for the real face.

interface FontSubstitution
MemberTypeSummary
lineMetrics?{ readonly heightEm: number; readonly baselineEm: number; }
requestedFontRequest
resolvedFontRequest

PdfDiagnosticinterfaceSource ↗

A bounded explanation of content the PDF cannot reproduce.

interface PdfDiagnostic
MemberTypeSummary
codestringMachine-readable diagnostic identifier. Handle unknown codes by severity.
messagestringHuman-readable explanation. Do not use this text for program control.
originIndex?numberFailed font source index, when this diagnostic describes a source failure.
originName?stringFailed font source name, when the source provided one.
pageIndex?numberZero-based page index. Omitted for document-wide diagnostics.
pageNumber?numberOne-based page number for display, consistent with Markdown warnings.
severity'unsupported' | 'approximation' | 'information'Strict export permits only information diagnostics.

PdfExportOptionsinterfaceSource ↗

Node conversion controls. Font substitution follows Core's independent fontPolicy.

interface PdfExportOptions extends Omit<OpenFontBackedDocumentForExportOptions, 'fonts' | 'measurer' | 'producer' | 'reuseAcrossRevisions'>
MemberTypeSummary
comments?booleanInclude native PDF comment annotations. Defaults to true.
displayMode?RevisionDisplayModeRevision display mode. Defaults to proposed.
documentLigatures?booleanApply optional document ligatures during measurement and output. Defaults to true.
fallbackFonts?PdfFontsSourceFont sources after packaged fonts and before embedded fonts.
fidelityPolicy?'strict' | 'best-effort'Reject unsupported or approximate content by default.
fonts?PdfFontsSourceFont sources before installed and packaged fonts.
lastResortFonts?PdfFontsSourceSources after embedded fonts and before the built-in generic substitutes.
maxOutputBytes?numberMaximum encoded bytes, from 1 to 67108864. Defaults to 67108864.
maxPages?numberMaximum output pages, from 1 to 10000. Checked after layout. Defaults to 10000.
timeoutMs?numberCooperative conversion deadline. Defaults to 60000 milliseconds.
useSystemFonts?booleanUse installed Word fonts before packaged substitutes. Defaults to true.

PdfExportResultinterfaceSource ↗

Result owns its bytes; mutating them does not affect a session or subsequent export.

interface PdfExportResult
MemberTypeSummary
bytesUint8ArrayOwned, mutable PDF bytes. Use application/pdf when serving them.
diagnosticsreadonly PdfDiagnostic[]Immutable output limitations and informational notices.
displayModeRevisionDisplayModeRevision display mode applied to the document.
fontResolutionExportFontResolutionReportSelected faces, substitutions, and failed font sources.
layoutRevisionnumberLayout revision for this conversion; not a persistent document identifier.
pageCountnumberPhysical PDF page count.
timingsPdfExportTimingsWall-clock milliseconds spent opening and resolving fonts, laying out, painting, and saving.

PdfExportTimingsinterfaceSource ↗

Elapsed milliseconds for each conversion phase.

interface PdfExportTimings
MemberTypeSummary
layoutMsnumber
openMsnumber
paintMsnumber
saveMsnumber

Type aliases (8)

HeadlessDocumentRejectiontype

A bad package is data, not an exceptional control path.

type HeadlessDocumentRejection = OoxmlPackageRejection | 'no-main-document-tree';

OpenPdfDocumentForExportOptionstypeSource ↗

Font and layout controls for a reusable immutable-byte session.

type OpenPdfDocumentForExportOptions = Omit<PdfExportOptions, 'comments' | 'fidelityPolicy' | 'timeoutMs' | 'maxOutputBytes' | 'maxPages'>;

OpenPdfDocumentForExportResulttypeSource ↗

Open result with the same success/refusal shape as Markdown sessions.

type OpenPdfDocumentForExportResult = OpenFontBackedDocumentForExportResult;

PdfExportSessiontypeSource ↗

Session that retains the admitted fonts and glyph capabilities needed by PDF output.

type PdfExportSession = FontBackedExportCapabilities;

PdfFontOrigintypeSource ↗

A font configuration or document-aware resolver.

type PdfFontOrigin = FontOrigin;

PdfFontsSourcetypeSource ↗

Ordered font configurations or resolvers. Earlier sources take priority.

type PdfFontsSource = PdfFontOrigin | readonly PdfFontOrigin[];

PdfProjectionOptionstypeSource ↗

Output controls for a caller-owned PDF session.

type PdfProjectionOptions = Pick<PdfExportOptions, 'comments' | 'fidelityPolicy' | 'timeoutMs' | 'maxOutputBytes' | 'maxPages' | 'signal' | 'displayMode'>;

RevisionDisplayModetype

Which revisions layout resolves before producing pages.

- all-markup shows both halves of every change. - proposed shows what the document becomes if every change is accepted. - original shows what it was before any of them.

The last two are specified as equal to accept-all and reject-all OUTPUT, which is what makes them testable, without either applying an op.

type RevisionDisplayMode = 'all-markup' | 'proposed' | 'original';