Set the target pages when printing a document

By default, the print plugin prints all pages of the current document. However, it is possible for you to indicate which pages will be printed by using the `setPages` option.
It is a function that takes the current document and returns the list of zero-based indexes 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[],
});
The following sample code indicates that only even pages are printed:
const printPluginInstance = printPlugin({
setPages: (doc) =>
Array(doc.numPages)
.fill(0)
.map((_, i) => i)
.filter((i) => (i + 1) % 2 === 0),
});
If you want to print odd pages, then here it is:
const printPluginInstance = printPlugin({
setPages: (doc) =>
Array(doc.numPages)
.fill(0)
.map((_, i) => i)
.filter((i) => (i + 1) % 2 === 1),
});
You don't have to implement functions that return popular page 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,
});
FunctionDescription
`getAllPagesNumbers`Returns all pages numbers of the document
`getCustomPagesNumbers`Returns the custom page numbers. The input is a string consisting 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
The option is also available when using the default layout plugin:
const defaultLayoutPluginInstance = defaultLayoutPlugin({
toolbarPlugin: {
printPlugin: {
setPages: ...,
},
},
});
Click the Print icon in the toolbar (The sample code)

See also