Skip to content

Commit 8c1330f

Browse files
committed
Initial commit
1 parent 09c24b7 commit 8c1330f

File tree

10 files changed

+2186
-2
lines changed

10 files changed

+2186
-2
lines changed

.codesandbox/workspace.json

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
{
2+
"responsive-preview": {
3+
"Mobile": [
4+
320,
5+
675
6+
],
7+
"Tablet": [
8+
1024,
9+
765
10+
],
11+
"Desktop": [
12+
1400,
13+
800
14+
],
15+
"Desktop HD": [
16+
1920,
17+
1080
18+
]
19+
}
20+
}

.gitignore

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
# See https://help.github.com/articles/ignoring-files/ for more about ignoring files.
2+
3+
# dependencies
4+
/node_modules
5+
/.pnp
6+
.pnp.js
7+
8+
# testing
9+
/coverage
10+
11+
# next.js
12+
/.next/
13+
/out/
14+
15+
# production
16+
/build
17+
18+
# misc
19+
.DS_Store
20+
*.pem
21+
22+
# debug
23+
npm-debug.log*
24+
yarn-debug.log*
25+
yarn-error.log*
26+
27+
# local env files
28+
.env.local
29+
.env.development.local
30+
.env.test.local
31+
.env.production.local
32+
33+
# vercel
34+
.vercel

README.md

+1-2
Original file line numberDiff line numberDiff line change
@@ -1,2 +1 @@
1-
# nextjs-react-pdf-sandbox
2-
Created with CodeSandbox
1+
# react-pdf + next js example

components/pdf-viewer.js

+41
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
import { useState } from "react";
2+
// import default react-pdf entry
3+
import { Document, Page, pdfjs } from "react-pdf";
4+
// import pdf worker as a url, see `next.config.js` and `pdf-worker.js`
5+
import workerSrc from "../pdf-worker";
6+
7+
pdfjs.GlobalWorkerOptions.workerSrc = workerSrc;
8+
9+
export default function PDFViewer() {
10+
const [file, setFile] = useState("./sample.pdf");
11+
const [numPages, setNumPages] = useState(null);
12+
13+
function onFileChange(event) {
14+
setFile(event.target.files[0]);
15+
}
16+
17+
function onDocumentLoadSuccess({ numPages: nextNumPages }) {
18+
setNumPages(nextNumPages);
19+
}
20+
21+
return (
22+
<div>
23+
<div>
24+
<label htmlFor="file">Load from file:</label>{" "}
25+
<input onChange={onFileChange} type="file" />
26+
</div>
27+
<div>
28+
<Document file={file} onLoadSuccess={onDocumentLoadSuccess}>
29+
{Array.from({ length: numPages }, (_, index) => (
30+
<Page
31+
key={`page_${index + 1}`}
32+
pageNumber={index + 1}
33+
renderAnnotationLayer={false}
34+
renderTextLayer={false}
35+
/>
36+
))}
37+
</Document>
38+
</div>
39+
</div>
40+
);
41+
}

next.config.js

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
module.exports = {
2+
future: {
3+
webpack5: true
4+
},
5+
webpack: (config) => {
6+
// load worker files as a urls with `file-loader`
7+
config.module.rules.unshift({
8+
test: /pdf\.worker\.(min\.)?js/,
9+
use: [
10+
{
11+
loader: "file-loader",
12+
options: {
13+
name: "[contenthash].[ext]",
14+
publicPath: "_next/static/worker",
15+
outputPath: "static/worker"
16+
}
17+
}
18+
]
19+
});
20+
21+
return config;
22+
}
23+
};

package.json

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
{
2+
"name": "react-pdf-next-js",
3+
"version": "1.0.0",
4+
"scripts": {
5+
"dev": "next",
6+
"build": "next build",
7+
"start": "next start",
8+
"post-update": "yarn upgrade --latest"
9+
},
10+
"dependencies": {
11+
"next": "latest",
12+
"react": "^16.13.1",
13+
"react-dom": "^16.13.1",
14+
"react-pdf": "^5.2.0"
15+
},
16+
"devDependencies": {
17+
"file-loader": "^6.2.0"
18+
},
19+
"license": "MIT",
20+
"keywords": [],
21+
"description": ""
22+
}

pages/index.js

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import dynamic from "next/dynamic";
2+
3+
const PDFViewer = dynamic(() => import("../components/pdf-viewer"), {
4+
ssr: false
5+
});
6+
7+
export default function PDF() {
8+
return <PDFViewer />;
9+
}

pdf-worker.js

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
if (process.env.NODE_ENV === "production") {
2+
// use minified verion for production
3+
module.exports = require("pdfjs-dist/build/pdf.worker.min.js");
4+
} else {
5+
module.exports = require("pdfjs-dist/build/pdf.worker.js");
6+
}

public/sample.pdf

524 KB
Binary file not shown.

0 commit comments

Comments
 (0)