Compare PDF and Markdown conversion

Share conversion settings and reuse one document session for PDF and Markdown output.

Export both formats from one session

Open the document through the PDF package to get one font-backed layout for both formats. The following example exports Markdown with images and a PDF, then disposes the session:

import { readFile } from 'node:fs/promises';
import { openDocumentForExport, exportPdfFrom } from '@docx-editor.dev/docx-to-pdf';
import { exportMarkdownFrom } from '@docx-editor.dev/docx-to-markdown';

const opened = await openDocumentForExport(await readFile('document.docx'), {
  displayMode: 'proposed',
  useSystemFonts: false,
});
if (!opened.ok) throw new Error(opened.reason);

try {
  const markdown = await exportMarkdownFrom(opened.session, { images: true });
  const pdf = await exportPdfFrom(opened.session);
  console.log(markdown.pages.length, pdf.pageCount);
} finally {
  opened.session.dispose();
}

You own the session. Dispose it in finally, because a failed export never disposes it. Both exporters read the same cached layout, font evidence, and PDF shaping policy.

Choose shared settings

If you convert each format separately with exportPdf and exportMarkdown, pass matching settings:

SettingGuidance
fonts, fallbackFontsPass the same sources to both. Both packages export createFontSource and defineFontResolver.
fontPolicy, onFontResolutionSame meaning in both. Strict checks coverage and source failures, not author font selection.
displayModeBoth default to 'all-markup'. Set it explicitly to match.
signal, resourceTimeoutMsSame meaning in both.
useSystemFonts: falsePDF only. Matches the Markdown default, which skips installed fonts.
documentLigatures: falsePDF only. Matches the Markdown default, which leaves optional ligatures off.

Matching settings do not guarantee identical pagination. Missing glyphs, fallback faces, and unsupported features can still differ.

Compare format behavior

AreaMarkdownPDF
SourceDOCX bytes or supported live viewsDOCX bytes only
Primary outputresult.markdownresult.bytes
Page dataresult.pagesresult.pageCount
Page metadataresult.pagination.layoutRevision and .displayModeresult.layoutRevision and .displayMode
Content noticesresult.warningsresult.diagnostics, with severity
Notice page locationOptional one-based pageNumberOne-based pageNumber and zero-based pageIndex
Open refusalDocumentOpenErrorPdfDocumentOpenError
Font discoveryPackaged and embedded sourcesInstalled, packaged, and embedded sources
Optional ligaturesOffOn unless documentLigatures is false
Unsupported contentWarnings for omissionsStrict refusal by default; fidelityPolicy: 'best-effort'
CommentsStructured review dataNative PDF annotations, unless comments is false
ChartsSaved fallback pictureDrawn by the drawing module, or the saved fallback
ReuseExport sessions and detached layoutsFont-backed export sessions only
PDF-only controlsNonefidelityPolicy, timeoutMs, maxPages, maxOutputBytes

Limits

  • Markdown image output needs images: true, also from a shared session.
  • Markdown-opened sessions use other font and ligature settings. Strict PDF conversion can refuse them for missing font identities or unshaped text.
  • exportPdfFrom accepts displayMode for another cached projection. The session default stays the same.
  • Add one to a PDF pageIndex before you compare it to a Markdown pageNumber.
  • Layout revisions are not persistent document identifiers. Store the input version with page references.
  • For cancellation and session ownership, see Reuse a session. For chart rendering, see Use the module without an editor.

Next steps