Print documents

Connect PDF conversion to the File menu for printing in React and Vue.

Printing converts the document with your PDF export handler and opens the browser print dialog. No separate print handler exists.

Enable printing

Configure menu.exporters.pdf. This example uses the exporters.ts module from Connect export handlers:

import { DocxEditor } from '@docx-editor.dev/react';
import { exporters } from './exporters';
import '@docx-editor.dev/core/styles/editor.css';

export function Editor({ source }: { source: Uint8Array }) {
  return <DocxEditor document={source} menu={{ exporters }} />;
}

PDF conversion runs on Node.js and requires the EigenPal Pro License. For converter installation and the server endpoint, see Export Markdown and PDF.

File > Print, Ctrl+P, and Cmd+P on macOS then print. The shortcut works when focus is inside the editor.

The slot is file.print, exposed as DocxEditor.Menu.Print and DocxEditorMenu.Print. In a custom File menu:

  • If you replace the menu with preset={false}, include this part.
  • Pass hidden to remove it.
  • A generic Menu.Item does not print.

Configure print settings

Before conversion, the packaged dialog asks for Markup and Print comments. These settings change the printed view only, not the editor's view. The handler receives them in its ChromeExportView argument:

FieldValue
displayModeThe chosen markup: All Markup, Simple Markup, No Markup, or Original. Starts at the current review view.
commentsWhether comments print. Defaults to on.
revisionMarkup, revisionAuthorSlotsThe current markup preferences and author slots.

Forward all four fields to exportPdf, as in Convert PDF on the server. A handler that ignores view ignores both settings. A handler that drops comments always prints comments.

Menu optionEffect
printSettings: falsePrints the current view without asking.
printSettings: trueRequired for a custom popups.print component to show the settings step.

The packaged dialog shows settings by default. The step also does not show when popups.print is false or no PDF handler exists.

Build a custom print flow

Call runChromePrint(editor, exporters, container, settings) from @docx-editor.dev/core/editor to prepare a ChromePrintJob:

import {
  ChromePrintError,
  runChromePrint,
  type ChromeExportHandlers,
  type ChromePrintJob,
} from '@docx-editor.dev/core/editor';
import type { Editor } from '@docx-editor.dev/core';

export async function printDocument(
  editor: Editor,
  exporters: ChromeExportHandlers,
  container: Element
): Promise<ChromePrintJob> {
  const job = await runChromePrint(editor, exporters, container);
  try {
    await job.print();
  } catch (error) {
    if (!(error instanceof ChromePrintError)) {
      job.dispose();
      throw error;
    }
    // Show job.url as a link to the PDF viewer.
  }
  // Keep the job during printing. Dispose it before the next print or on unmount.
  return job;
}
Member or argumentBehavior
containerOptional Document or Element for the hidden print frame. Defaults to document. Inside a modal, pass an element in the modal.
settingsOptional ChromePrintSettings with displayMode and comments. Omitted settings keep the current view.
job.urlObject URL of the PDF. Valid until dispose().
job.print(options?)Loads the PDF and opens the browser print dialog.
job.dispose()Removes the hidden frame and revokes url.

For a settings form, initialChromePrintSettings(editor.snapshot()) returns starting values. CHROME_PRINT_DISPLAY_MODES lists the modes with label keys. isChromePrintShortcut(event) recognizes the print shortcut.

Keep the job alive while the user prints or opens its link. Removing the frame during printing can cancel the printout.

Handle failures

runChromePrint rejects with ChromeExportError when the PDF handler is missing. Conversion and PDF validation errors also reject it. Browser print failures throw ChromePrintError:

CodeMeaning
pdf-viewer-unavailableThe browser reports no built-in PDF viewer (navigator.pdfViewerEnabled is false).
pdf-load-failedThe PDF frame failed to load. Call print() again to reload it.
print-refusedThe browser refused access to PDF printing.
print-endedThe job was disposed. Create another job.

The packaged dialog offers an Open PDF link for these failures and after a 15-second load timeout. If a browser prints blank pages from a frame, export the PDF and print it from a PDF viewer.

Customize feedback

Use popups.print to replace the settings, progress, and error feedback. See Customize print feedback.

For a modal progress dialog, pass job.print({ beforePrint: closeDialog }). The callback runs after the PDF loads and before printing. Wait for the dialog to unmount before it returns: use flushSync in React, or await nextTick() in Vue.

Limits

  • Printing calls save() first, which validates form input and can refresh fields with an undo step. See Save a document.
  • Cancel during preparation discards a conversion result that arrives later.
  • The packaged print action stays unavailable during preparation and while its error dialog is open.
  • Without menu.exporters.pdf, the browser handles the shortcut, and File > Print opens a setup error.
  • macOS Ctrl+P stays available for moving to the previous line.
  • The PDF converter determines the printed layout. If a background is missing, inspect the exported PDF.

See also