useLoadPdf

Loads a PDF document and reports the load state.

const [loadState, loadPdf] = useLoadPdf()

Load state

loadState.status is one of:

StatusMeaning
'initial'No document is loaded, no loading in progress, no error.
'loading'A document is loading.
'ready'A document is loaded; the PDF application UI can be rendered.

loadPdf

loadPdf(pdfFileContent: Uint8Array, options?: { password?: string }): Promise<void>
  • The document reads from the given buffer for its whole lifetime, and anything else touching the buffer can detach its ArrayBuffer (another loadPdf call, a transfer to a worker), making every later document read throw. If you intend to reuse the buffer, pass a copy: loadPdf(data.slice()).
  • Loading replaces the current document: it's unloaded as soon as loading starts, together with everything derived from it (see useResetPdf). When loading fails, the state returns to 'initial'.
  • For an encrypted document, pass password. Without one, the empty password is tried, as the PDF specification prescribes (some documents open with it despite being encrypted). When the password is missing or wrong, the promise rejects with InvalidPasswordError: catch it and ask the user, then call loadPdf again.

Example

See Getting started for a complete loading flow with a file input, the loading state, and the password prompt.

On this page