Skip to content

Commit c231303

Browse files
authored
Merge pull request #1 from PSPDFKit/ritesh/webpack
Implement Webpack example for PSPDFKit integration
1 parent 6e4346a commit c231303

File tree

15 files changed

+5309
-3
lines changed

15 files changed

+5309
-3
lines changed

examples/webpack/.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
dist
2+
node_modules

examples/webpack/LICENSE

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
The PSPDFKit Sample applications are licensed with a modified BSD
2+
license. In plain language: you're allowed to do whatever you wish
3+
with the code, modify, redistribute, embed in your products (free or
4+
commercial), but you must include copyright, terms of usage and
5+
disclaimer as stated in the license.
6+
7+
You will require a commercial PSPDFKit License to run these examples
8+
in non-demo mode. Please refer to [email protected] for details.
9+
10+
Copyright © 2017-present PSPDFKit GmbH.
11+
All rights reserved.
12+
13+
Redistribution and use in source or binary forms,
14+
with or without modification, are permitted provided
15+
that the following conditions are met:
16+
17+
- Redistributions of source code must retain the above copyright
18+
notice, this list of conditions and the following disclaimer.
19+
20+
- Redistributions in binary form must reproduce the above copyright
21+
notice, this list of conditions and the following disclaimer in the
22+
documentation and/or other materials provided with the
23+
distribution.
24+
25+
- Redistributions of PSPDFKit Samples must include attribution to
26+
PSPDFKit, either in documentation or other appropriate media.
27+
28+
- Neither the name of the PSPDFKit, PSPDFKit GmbH, nor its developers
29+
may be used to endorse or promote products derived from
30+
this software without specific prior written permission.
31+
32+
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
33+
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
34+
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
35+
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
36+
HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
37+
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
38+
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
39+
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
40+
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
41+
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
42+
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

examples/webpack/README.md

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
# Nutrient Web SDK Example – Webpack
2+
3+
This example shows how to build a [PSPDFKit for Web](https://www.nutrient.io/web/) web application with
4+
[webpack](https://webpack.js.org/).
5+
6+
This example uses the Standalone version of [PSPDFKit for Web](https://www.nutrient.io/web/)
7+
distributed as an npm package.
8+
9+
## Prerequisites
10+
11+
- [Node.js](http://nodejs.org/)
12+
13+
## Support, Issues and License Questions
14+
15+
PSPDFKit offers support for customers with an active SDK license via https://www.nutrient.io/support/request/
16+
17+
Are you [evaluating our SDK](https://www.nutrient.io/try/)? That's great, we're happy to help out! To make sure this is fast, please use a work email and have someone from your company fill out our sales form: https://www.nutrient.io/sales/
18+
19+
## Getting Started
20+
21+
Clone the repo:
22+
23+
```bash
24+
git clone https://github.com/PSPDFKit/pspdfkit-web-example-webpack.git
25+
cd pspdfkit-web-example-webpack
26+
```
27+
28+
Install the project dependencies with `npm`:
29+
30+
```bash
31+
npm install
32+
```
33+
34+
## Running the Example
35+
36+
We are ready to launch the app! 🎉
37+
38+
```bash
39+
npm run start
40+
```
41+
42+
You can now open http://localhost:8080 in your browser and enjoy!
43+
44+
Upload a PDF either via the `Select File` button at top-left or by dropping a PDF into the page.
45+
46+
We put a sample PDF document in the `assets` folder of this project for you to try!
47+
48+
For further instructions please refer to our online guide available at
49+
https://www.nutrient.io/guides/web/current/standalone/adding-to-your-project#toc_install-with-npm
50+
51+
### Development mode
52+
53+
To run the app in development mode run
54+
55+
```bash
56+
npm run start:dev
57+
```
58+
59+
## webpack configuration file
60+
61+
The `webpack` configuration file is located at [./config/webpack.js](config/webpack.js).
62+
63+
## License
64+
65+
This software is licensed under a [modified BSD license](LICENSE).
66+
67+
## Contributing
68+
69+
Please ensure
70+
[you signed our CLA](https://www.nutrient.io/guides/web/current/miscellaneous/contributing/) so we can
71+
accept your contributions.
Lines changed: 4 additions & 0 deletions
Loading
234 KB
Binary file not shown.
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
try {
2+
require("pspdfkit");
3+
} catch (error) {
4+
if (!/cannot find module/i.test(error.message)) {
5+
process.exit(0);
6+
}
7+
8+
console.log(
9+
`This application requires you to install PSPDFKit for Web manually using your unique customer or trial url.
10+
For further instructions please refer to our online guide available at:
11+
12+
https://www.nutrient.io/guides/web/current/standalone/adding-to-your-project#toc_install-with-npm`,
13+
);
14+
process.exit(1);
15+
}

examples/webpack/config/webpack.js

Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
/**
2+
* webpack configuration file used to build both a development and production
3+
* version of the app.
4+
*
5+
* The production version is built in the `./dist` folder. When building the
6+
* development mode it also starts a web server at http://localhost:8080
7+
*
8+
* This configuration file creates two main bundles:
9+
*
10+
* - vendor.js - contains external libraries (including pspdfkit.js).
11+
* - app.js - contains the application code.
12+
*
13+
* It also copies the WASM/ASM and CSS files from the npm package folder, since
14+
* `PSPDFKit.load` loads them relative to the application execution path.
15+
*/
16+
17+
const path = require("node:path");
18+
const HtmlWebpackPlugin = require("html-webpack-plugin");
19+
const CopyWebpackPlugin = require("copy-webpack-plugin");
20+
21+
/**
22+
* Determine whether we are in development mode.
23+
*
24+
* $ NODE_ENV=development webpack --config config/webpack.js
25+
*/
26+
const isDev = process.env.NODE_ENV === "development";
27+
28+
const filesToCopy = [
29+
// PSPDFKit files.
30+
{
31+
from: "./node_modules/pspdfkit/dist/pspdfkit-lib",
32+
to: "./pspdfkit-lib",
33+
},
34+
// Application CSS.
35+
{
36+
from: "./src/index.css",
37+
to: "./index.css",
38+
},
39+
// Assets directory.
40+
{
41+
from: "./assets",
42+
to: "./assets",
43+
},
44+
];
45+
46+
/**
47+
* webpack main configuration object.
48+
*/
49+
const config = {
50+
entry: {
51+
// Creates an `app.js` bundle which contains the application code.
52+
app: path.resolve("./src/index.js"),
53+
},
54+
55+
// Configure Compilation Mode
56+
mode: isDev ? "development" : "production",
57+
58+
output: {
59+
path: path.resolve("./dist"),
60+
publicPath: "/",
61+
// [name] is the bundle name from above.
62+
filename: "[name].js",
63+
},
64+
65+
resolve: {
66+
modules: [path.resolve("./src"), path.resolve("./node_modules")],
67+
},
68+
69+
plugins: [
70+
// Automatically insert <script src="[name].js"><script> to the page.
71+
new HtmlWebpackPlugin({
72+
template: path.resolve("./src/index.html"),
73+
chunks: ["vendor", "app"],
74+
}),
75+
76+
// Copy the WASM/ASM and CSS files to the `output.path`.
77+
new CopyWebpackPlugin({
78+
patterns: filesToCopy,
79+
}),
80+
],
81+
82+
optimization: {
83+
splitChunks: {
84+
cacheGroups: {
85+
// Creates a `vendor.js` bundle which contains external libraries (including pspdfkit.js).
86+
vendor: {
87+
test: /node_modules/,
88+
chunks: "initial",
89+
name: "vendor",
90+
priority: 10,
91+
enforce: true,
92+
},
93+
},
94+
},
95+
},
96+
97+
devServer: {
98+
port: 3000,
99+
},
100+
};
101+
102+
module.exports = config;

0 commit comments

Comments
 (0)