A React component to view a PDF document
Function | Destination |
---|---|
jumpToNextDestination |
Jump to the next destination |
jumpToPreviousDestination |
Jump to the previous destination |
Viewer
component adds a new property to disable smooth scroll:<Viewer enableSmoothScroll={false} />
import type { SetRenderRange, VisiblePagesRange } from '@react-pdf-viewer/core';
const setRenderRange: SetRenderRange = React.useCallback((visiblePagesRange: VisiblePagesRange) => {
return {
startPage: visiblePagesRange.startPage - 10,
endPage: visiblePagesRange.endPage + 10,
};
}, []);
// Another usage: render the first 20 pages initially
const setRenderRange: SetRenderRange = React.useCallback((visiblePagesRange: VisiblePagesRange) => {
return {
startPage: visiblePagesRange.endPage <= 20 ? 0 : visiblePagesRange.startPage - 5,
endPage:
visiblePagesRange.startPage <= 20
? Math.max(20, visiblePagesRange.endPage + 5)
: visiblePagesRange.endPage + 5,
};
}, []);
<Viewer setRenderRange={setRenderRange} />;
Cover
component adds new width
property:const { Cover } = thumbnailPluginInstance;
// Render the cover of second page
<Cover getPageIndex={() => 1} width={300} />;
Shortcut | Operating System | Action |
---|---|---|
Command + ArrowLeft |
macOS | Jump to the previous bookmark destination |
Alt + ArrowLeft |
Windows | Same as above |
Command + ArrowRight |
macOS | Jump to next bookmark destination |
Alt + ArrowRight |
Windows | Same as above |
Viewer
component scrolls to the initialPage
page automatically after resizing the container or browserjumpToDestination
function now has only a single property. You don't have to change anything if your application doesn't have any custom plugin using the jumpToDestination
function.// Before
import type { DestinationOffsetFromViewport } from '@react-pdf-viewer/core';
import { SpecialZoomLevel } from '@react-pdf-viewer/core';
jumpToDestination(
pageIndex: number,
bottomOffset: number | DestinationOffsetFromViewport,
leftOffset: number | DestinationOffsetFromViewport,
scaleTo?: number | SpecialZoomLevel
);
// After
import type { Destination } from '@react-pdf-viewer/core';
jumpToDestination(destination: Destination);
pagesContainerRef
to RenderViewer
, so the plugin can access the pages container more easilyViewer
adds new property to customize the view of a protected document:import { RenderProtectedViewProps, Viewer } from '@react-pdf-viewer/core';
const ProtectedView: React.FC<RenderProtectedViewProps> = ({ passwordStatus, verifyPassword }) => {
...
};
<Viewer renderProtectedView={ProtectedView} />
import { pageNavigationPlugin } from '@react-pdf-viewer/page-navigation';
const pageNavigationPluginInstance = pageNavigationPlugin();
const { jumpToNextPage, jumpToPreviousPage } = pageNavigationPluginInstance;
Thumbnails
component provides new property to display the thumbnails in the given direction:import { ThumbnailDirection } from '@react-pdf-viewer/thumbnail';
// Display thumbnails horizontally
<Thumbnails thumbnailDirection={ThumbnailDirection.Horizontal} />
// Display thumbnails vertically
<Thumbnails thumbnailDirection={ThumbnailDirection.Vertical} />
Shortcut | Operating System | Action |
---|---|---|
Command + ArrowLeft |
macOS | Jump to previous clicked link annotation |
Alt + ArrowLeft |
Windows | Same as above |
initialPage
) optiontranformSize
property is changed to transformSize
import { ScrollMode, Viewer } from '@react-pdf-viewer/core';
<Viewer scrollMode={ScrollMode.Page} />;
You can switch to the page scrolling mode from the toolbar.
import { ViewMode, Viewer } from '@react-pdf-viewer/core';
<Viewer viewMode={ViewMode.DualPage} />;
Mode | Description |
---|---|
ViewMode.SinglePage |
The default mode. View single page continuously |
ViewMode.DualPage |
View two pages each time |
ViewMode.DualPageWithCover |
View two pages each time. The first page is displayed as a cover |
PluginFunctions
provides new jumpToPreviousPage
and jumpToNextPage
to jump to the previous and next pages
The scroll-mode
plugin provides new DualPageCoverViewModeIcon
, DualPageViewModeIcon
and PageScrollingIcon
icons
pageLayout
option to customize the layout of each page.
The following code adds margin between pages, and center the page in its container:import { type PageLayout, Viewer } from '@react-pdf-viewer/core';
const pageLayout: PageLayout = {
buildPageStyles: () => ({
alignItems: 'center',
display: 'flex',
justifyContent: 'center',
}),
tranformSize: ({ size }) => ({ height: size.height + 30, width: size.width + 30 }),
};
<Viewer pageLayout={pageLayout} />;
Fix bugs that might happen with a document whose pages have different dimensions
initialPage
optionpageHeight
and pageWidth
properties in RenderViewer
are replaced with the pageSizes
property that are the sizes of pages.
You don't have to do anything if you don't develope your own plugin using those properties.initialRotation
parameter:<Viewer initialRotation={90} />
const highlightPluginInstance = highlightPlugin();
const { switchTrigger } = highlightPluginInstance;
// Switch to None
switchTrigger(Trigger.None);
// Switch to Selection mode
switchTrigger(Trigger.TextSelection);
aria-label
attributes for link annotations without using the Bookmark pluginRenderBookmarkItemProps
includes new path
property that indicates the path from each bookmark item to the rootSelectionData
provides more information about the selected text including:Property | Type | Description |
---|---|---|
selectedText |
string |
The selected text |
divTexts |
DivText[] |
List of text items contain the selected text |
A DivText
item consists of
Property | Type | Description |
---|---|---|
divIndex |
number |
The zero-based text element rendered by the text layer |
pageIndex |
number |
The zero-based page index |
textContent |
string |
The text content of text element |
keyword
option:const searchPluginInstance = searchPlugin({
keyword: '...',
});
RenderSearchProps
adds new isDocumentLoaded
property that indicates if the document is loaded.
You can use it to perform searching for a keyword in a sidebar:import type { Match, RenderSearchProps } from '@react-pdf-viewer/search';
const SearchSidebarInner: React.FC<{
renderSearchProps: RenderSearchProps
}> = ({ renderSearchProps }) => {
const [matches, setMatches] = React.useState<Match[]>([]);
const { isDocumentLoaded, keyword, search } = renderSearchProps;
React.useEffect(() => {
if (isDocumentLoaded && keyword) {
search().then((matches) => {
setMatches(matches);
});
}
}, [isDocumentLoaded]);
return (
// Display matches ...
);
};
Viewer
is rendered inside ShadowDOMcontents
and title
properties of annotations with corresponding objectsCover
component doesn't rotate the corresponding rotated pagejumpToHighlightArea
function does not work properly with some documentsstartPageIndex
and endPageIndex
properties of SelectionData
aren't correctSelectionData
data.
The SelectionData
property in RenderHighlightContentProps
and RenderHighlightTargetProps
turn to optional properties.interface RenderHighlightContentProps {
selectionData?: SelectionData;
}
interface RenderHighlightTargetProps {
selectionData?: SelectionData;
}
You have to check if it exists before using it:
if (renderHighlightContentProps.selectionData) {
// Do something ...
}
if (renderHighlightTargetProps.selectionData) {
// Do something ...
}
renderBookmarkItem
properly:<Bookmark renderBookmarkItem={renderBookmarkItem} />
toggleTab
function to toggle a given tab in the sidebar:const defaultLayoutPluginInstance = defaultLayoutPlugin();
const { toggleTab } = defaultLayoutPluginInstance;
// Toggle the second tab
toggleTab(1);
Command
+ ArrowUp
(on macOS) or Ctrl
+ ArrowUp
(on Windows) will bring users to the previous clicked link annotation.
You can disable that shortcuts via the enableShortcuts
option:// Use the standalone page navigation plugin
const pageNavigationPluginInstance = pageNavigationPlugin({
enableShortcuts: false,
});
// Use the default layout plugin
const defaultLayoutPluginInstance = defaultLayoutPlugin({
toolbarPlugin: {
pageNavigationPlugin: {
enableShortcuts: false,
},
},
});
CharacterMap
type isn't availableonPageChange
callback does not trigger if the current page equals to the initial pagesetPages
option. Is is a function that takes the current document and returns the list of zero-based index of pages you want to print.import type { PdfJs } from '@react-pdf-viewer/core';
import { printPlugin } from '@react-pdf-viewer/print';
const printPluginInstance = printPlugin({
setPages: (doc: PdfJs.PdfDocument) => number[],
});
Here are some examples:
// Only print the even pages
const printPluginInstance = printPlugin({
setPages: (doc) =>
Array(doc.numPages)
.fill(0)
.map((_, i) => i)
.filter((i) => (i + 1) % 2 === 0),
});
// Only print the odd pages
const printPluginInstance = printPlugin({
setPages: (doc) =>
Array(doc.numPages)
.fill(0)
.map((_, i) => i)
.filter((i) => (i + 1) % 2 === 1),
});
The option is also available when using the default layout plugin:
const defaultLayoutPluginInstance = defaultLayoutPlugin({
toolbarPlugin: {
printPlugin: {
setPages: ...,
},
},
});
You don't have to implement functions that return popular pages numbers. The print plugin provides useful functions for most popular cases:
import {
getAllPagesNumbers,
getCustomPagesNumbers,
getEvenPagesNumbers,
getOddPagesNumbers,
} from '@react-pdf-viewer/print';
const printPluginInstance = printPlugin({
setPages: getEvenPagesNumbers,
});
Function | Description |
---|---|
getAllPagesNumbers |
Returns all pages numbers of the document |
getCustomPagesNumbers |
Returns the custom pages numbers. The input is a string consists of given pages or ranges of pages. For example: |
1, 2, 3 | |
1-3 | |
1-3, 5, 8-11 | |
getEvenPagesNumbers |
Returns even pages numbers |
getOddPagesNumbers |
Returns odd pages numbers |
import { getEvenPagesNumbers } from '@react-pdf-viewer/print';
const printPluginInstance = printPlugin();
const { setPages } = printPluginInstance;
// Show UI for users to choose pages
const handleChooseEvenPages = () => setPages(getEvenPagesNumbers);
<label>
<input type="radio" onChange={handleChooseEvenPages} />
Print even pages
</label>;
print
function:import { printPlugin } from '@react-pdf-viewer/print';
const printPluginInstance = printPlugin();
const { print } = printPluginInstance;
The print
function is also available if you use the default layout plugin:
import { defaultLayoutPlugin } from '@react-pdf-viewer/default-layout';
const defaultLayoutPluginInstance = defaultLayoutPlugin();
const { print } = defaultLayoutPluginInstance.toolbarPluginInstance.printPluginInstance;
const printPluginInstance = printPlugin({
renderProgressBar: (numLoadedPages: number, numPages: number, onCancel: () => void) => (
// Render the progress bar
),
});
import { DownloadIcon } from '@react-pdf-viewer/get-file';
import { OpenFileIcon } from '@react-pdf-viewer/open';
const searchPluginInstance = searchPlugin({
renderHighlights: (highlightPositions: HighlightPosition[]) => (
// Your custom highlight elements
),
});
The highlight plugin supports double click. Users can double click to select the entire text of a given element
The page navigation plugin allows to jump to the previous and next pages with shortcuts.
Shortcut | Action |
---|---|
PageUp or Alt + ArrowUp |
Go to the previous page |
PageDown or Alt + ArrowDown |
Go to the next page |
The shortcuts are enabled by default. It's possible to disable them, for example:
// Use the standalone page navigation plugin
const pageNavigationPluginInstance = pageNavigationPlugin({
enableShortcuts: false,
});
// Use the default layout plugin
const defaultLayoutPluginInstance = defaultLayoutPlugin({
toolbarPlugin: {
pageNavigationPlugin: {
enableShortcuts: false,
},
},
});
setInitialTabFromPageMode
function returns a Promise
which resolves an invalid tab indeximport { defaultLayoutPlugin } from '@react-pdf-viewer/default-layout';
const defaultLayoutPluginInstance = defaultLayoutPlugin({
// The `Bookmark` tab is active initially
setInitialTab: () => Promise.resolve(1),
});
According to the PDF specifications, the initial tab can be determined based on the document's page mode.
If you want to follow that behaviour, then you can use the setInitialTabFromPageMode
function:
import { defaultLayoutPlugin, setInitialTabFromPageMode } from '@react-pdf-viewer/default-layout';
const defaultLayoutPluginInstance = defaultLayoutPlugin({
setInitialTab: setInitialTabFromPageMode,
});
Enter
automatically jumps to the next match when being focused on the keyword fieldrenderPageLayer
method