Skip to content

Commit

Permalink
WIP Multi-page
Browse files Browse the repository at this point in the history
  • Loading branch information
gpalsingh committed Apr 1, 2024
1 parent 8f590f9 commit c77ef8b
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 2 deletions.
2 changes: 1 addition & 1 deletion packages/react-pdf/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@commutatus/react-pdf",
"version": "8.0.3",
"version": "8.0.4",
"description": "Display PDFs in your React app as easily as if they were images.",
"type": "module",
"sideEffects": [
Expand Down
27 changes: 26 additions & 1 deletion packages/react-pdf/src/Document.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ import type {
ScrollPageIntoViewArgs,
Source,
} from './shared/types.js';
import Page, { type PageProps } from './Page.js';

const { PDFDataRangeTransport } = pdfjs;

Expand Down Expand Up @@ -224,6 +225,12 @@ export type DocumentProps = {
* @example 90
*/
rotate?: number | null;
/**
* Page width. If neither `height` nor `width` are defined, page will be rendered at the size defined in PDF. If you define `width` and `height` at the same time, `height` will be ignored. If you define `width` and `scale` at the same time, the width will be multiplied by a given factor.
*
* @example 300
*/
width?: number;
} & EventProps<DocumentCallback | false | undefined>;

const defaultOnPassword: OnPassword = (callback, reason) => {
Expand Down Expand Up @@ -291,6 +298,7 @@ const Document = forwardRef(function Document(
options,
renderMode,
rotate,
width,
...otherProps
}: DocumentProps,
ref,
Expand Down Expand Up @@ -668,7 +676,24 @@ const Document = forwardRef(function Document(
const eventProps = useMemo(() => makeEventProps(otherProps, () => pdf), [otherProps, pdf]);

function renderChildren() {
return <DocumentContext.Provider value={childContext}>{children}</DocumentContext.Provider>;
const pagesList = new Array(pages.current.length).fill(0);

return (
<DocumentContext.Provider value={childContext}>
{pagesList.map((_, pageIndex) => {
const pageNumber = pageIndex + 1;
const pageProps: PageProps = {
pageNumber,
};

if (width) {
pageProps.width = width;
}

return <Page key={pageNumber} {...pageProps} />;
})}
</DocumentContext.Provider>
);
}

function renderContent() {
Expand Down

0 comments on commit c77ef8b

Please sign in to comment.