Skip to content
React PDF Viewer

Search

Error states

Show a clear message with Retry when a document can't load or render, and receive a typed error for your own handling.

Basics

When a document fails to load, the viewer shows an alert with a Retry button and keeps the toolbar usable.

The toolbar stays so that fullscreen can always be left. Aborted loads and canceled renders are never reported as errors.

Failure types

Every failure reaches onError as a PdfViewerError with a machine-readable code, a readable message, the HTTP status where there is one, and the original cause.

CodeWhen
PDFJS_LOAD_FAILEDThe pdfjs-dist module or its worker failed to load.
NETWORK_ERRORfetch rejected: offline, CORS, DNS, …
HTTP_ERRORThe server answered with a non-2xx status (status is set).
INVALID_PDFThe bytes are not a valid PDF.
PASSWORD_REQUIREDThe document is encrypted and no password was supplied.
INCORRECT_PASSWORDThe supplied password is wrong.
RENDER_FAILEDA page failed to render.
DOWNLOAD_FAILEDSaving the document failed.
PRINT_FAILEDPrinting failed.
UNKNOWNAnything else.

Generated from the source; see the PdfViewerErrorCode reference.

RENDER_FAILED replaces only the page that failed; the password codes are covered in Password protection.

Reporting errors

<PdfViewer
  source={url}
  onError={(error) =>
    logger.warn('pdf-viewer', error.code, { status: error.status, cause: error.cause })
  }
/>

HTTP errors that onHttpError handles (for example a 401 that redirects to sign-in) are not reported; see Authenticated requests.

Customization

renderError(error, { retry }) replaces the error view for document and page errors.

Colors of the default view are CSS variables (--rpv-error-bg, --rpv-error-fg, --rpv-error-icon); see the customization guide.

Advanced / escape hatch

reload() on the imperative API retries from anywhere in your app. With usePdfDocument, error and reload are returned directly.

Limitations

  • Messages are in English by default. HTTP_ERROR messages come from getHttpErrorMessage, which reads RFC 7807 bodies; other codes carry English messages. Workaround: map error.code to your own text in renderError.
  • CORS failures are indistinguishable from offline. Browsers do not tell scripts why a cross-origin request failed, so both appear as NETWORK_ERROR. Workaround: explain both possibilities in your error view, as the demo above does.

API

  • PdfViewerError — A typed viewer error. Aborted loads and canceled renders are never reported as errors.
  • PdfViewerErrorCode — Machine-readable category of a PdfViewerError, for handling failures such as a missing password or an HTTP error in code.