Document sources
Open PDFs from a URL, a File the user picked, or bytes you already have, without uploading anything.
source accepts everything a web app usually has at hand when it wants to show a PDF:
- a URL string or
URLobject, fetched by the viewer; - a
FileorBlob, for example from a file input or drag and drop; - an
ArrayBufferorUint8Arrayyou fetched, generated or decrypted yourself.
Basics
Pass a URL and the viewer fetches, parses and renders the document.
null, undefined and an empty string show the empty state. Changing source aborts the previous request and destroys the previous document, so switching documents quickly never shows a stale page.
Local files
A File is read in the browser and never leaves the user’s device.
Bytes
ArrayBuffer and Uint8Array sources are copied before PDF.js takes ownership of them, so the buffer you pass stays usable afterwards.
Download file name
The download button uses fileName when you pass one; otherwise a File’s own name, then the last .pdf segment of the URL, then document.pdf. See Download.
Customization
renderEmpty replaces the view shown while there is no document, for example with a call to action.
The loading and error views have their own slots; see Loading states, Error states and the customization guide.
Advanced / escape hatch
To load documents without the viewer, for example to count pages or render thumbnails in your own layout, use usePdfDocument directly. It accepts the same sources and request options.
Limitations
- Cross-origin URLs need CORS. The browser blocks reading a PDF from another origin unless the server sends
Access-Control-Allow-Origin. The viewer reports aNETWORK_ERROR. Workaround: enable CORS on the server, or proxy the file through your own origin. See Authenticated requests. - Large files are loaded in full. URL sources are fetched completely before rendering starts (range requests are not used), so a very large PDF takes as long as its download. Workaround: split huge documents server-side, or show your own progress UI with
renderLoading. - Streams are not accepted. A
ReadableStreamis not aPdfSource. Workaround: read it into aBloborArrayBufferfirst (await new Response(stream).blob()).