Compare commits
406 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
39ac823b05 | ||
|
|
548ae4dba3 | ||
|
|
f69d593649 | ||
|
|
423af5f077 | ||
|
|
76e5e1ad00 | ||
|
|
420caa4d8d | ||
|
|
fdeeb68a6d | ||
|
|
9fe803a0b1 | ||
|
|
dd9ba90a03 | ||
|
|
2bc739316a | ||
|
|
766cb4410b | ||
|
|
78fe6d6ea8 | ||
|
|
6b99decb56 | ||
|
|
22c19670e9 | ||
|
|
cfba9681c4 | ||
|
|
a019b8b5ca | ||
|
|
bd9b267562 | ||
|
|
b0f8f56650 | ||
|
|
a71e813e82 | ||
|
|
75e7665d6e | ||
|
|
12293f5297 | ||
|
|
4899ee0bee | ||
|
|
563c17c84e | ||
|
|
d94eca4ee7 | ||
|
|
f4a01884bd | ||
|
|
105d7f12ac | ||
|
|
30c115a7de | ||
|
|
88a90f22a3 | ||
|
|
c72c712c1b | ||
|
|
7205801e76 | ||
|
|
64f4f54b9d | ||
|
|
0287d88895 | ||
|
|
cdc075b27c | ||
|
|
604d9827c5 | ||
|
|
bdeb6bf188 | ||
|
|
19e122be99 | ||
|
|
f3ddf18a23 | ||
|
|
51f863e1e4 | ||
|
|
0a9381d538 | ||
|
|
a9514b54eb | ||
|
|
d647cb196f | ||
|
|
b69973f614 | ||
|
|
fb9c42f4a1 | ||
|
|
b873e3cdf8 | ||
|
|
e9fc024332 | ||
|
|
d4956fad8c | ||
|
|
9d1dfe742e | ||
|
|
e32c092af8 | ||
|
|
18a2664b54 | ||
|
|
954e46c5ec | ||
|
|
e0f306d3f7 | ||
|
|
09db6618d6 | ||
|
|
c5ea254945 | ||
|
|
1fc1ecbaa6 | ||
|
|
bc4640c3f0 | ||
|
|
1e2eb9b07a | ||
|
|
ece00956d9 | ||
|
|
af5bbd8838 | ||
|
|
3a8f2495ea | ||
|
|
993f5e5097 | ||
|
|
1f99c26e78 | ||
|
|
05ebf3a6b4 | ||
|
|
1be3046d26 | ||
|
|
5b3858ba29 | ||
|
|
a1f388e524 | ||
|
|
cf14ff1540 | ||
|
|
a0ac2bc02a | ||
|
|
ed82c492ab | ||
|
|
fc2d71d120 | ||
|
|
e5a7a0631b | ||
|
|
315cba07f0 | ||
|
|
cd5bd92a41 | ||
|
|
10126ce979 | ||
|
|
f6c4f08254 | ||
|
|
fc4feb2096 | ||
|
|
82b641458f | ||
|
|
2c0fb33548 | ||
|
|
24e665bfd5 | ||
|
|
1404e33386 | ||
|
|
0b1fd61188 | ||
|
|
80b4c67e35 | ||
|
|
1f4aae9249 | ||
|
|
145e8006f0 | ||
|
|
872f562aad | ||
|
|
db70d67180 | ||
|
|
b1fd798a5c | ||
|
|
42907ade21 | ||
|
|
b3bc0b4e5a | ||
|
|
12e24f3ec1 | ||
|
|
fefa8347da | ||
|
|
cd7ca09a3f | ||
|
|
3960b2d6f9 | ||
|
|
264969e80c | ||
|
|
4e2911f648 | ||
|
|
42610b2645 | ||
|
|
7b07a706e1 | ||
|
|
8fef9dc8a8 | ||
|
|
83936bf4c8 | ||
|
|
65f89e283e | ||
|
|
1aa65bd3d1 | ||
|
|
0547ec3c49 | ||
|
|
7e1cbe572d | ||
|
|
7a98f30d05 | ||
|
|
c565bd4400 | ||
|
|
d039c8e62e | ||
|
|
da7f0561cb | ||
|
|
00210b75c7 | ||
|
|
3fe8973ae6 | ||
|
|
87c4b42683 | ||
|
|
0901eaac45 | ||
|
|
c7c81a7243 | ||
|
|
ac019ac196 | ||
|
|
b12c1305ea | ||
|
|
1876c2afae | ||
|
|
4575375ea9 | ||
|
|
aac7836dce | ||
|
|
df5de2d60d | ||
|
|
b026283ef2 | ||
|
|
548e5d108c | ||
|
|
4d31152a02 | ||
|
|
fd08513212 | ||
|
|
3175ac16d1 | ||
|
|
0cb5a6c7ad | ||
|
|
0bb2df135b | ||
|
|
adadf7428c | ||
|
|
146dd3c00b | ||
|
|
78bfa84afd | ||
|
|
07512c7e2c | ||
|
|
9fe96bec40 | ||
|
|
18172aa33a | ||
|
|
9ece6dacbd | ||
|
|
ef07963d79 | ||
|
|
862086eae5 | ||
|
|
277aa0c7e1 | ||
|
|
fc52741435 | ||
|
|
a7cd6bfd2e | ||
|
|
648df7b3d3 | ||
|
|
01f7f1f59c | ||
|
|
ff7f089f69 | ||
|
|
4e06e8c0c0 | ||
|
|
ca5ce905c7 | ||
|
|
0fc29de02c | ||
|
|
8509a16d6e | ||
|
|
0ed021357b | ||
|
|
58ad7a1e8a | ||
|
|
622ee29dd8 | ||
|
|
5cbb4c6223 | ||
|
|
5299859385 | ||
|
|
c57b308909 | ||
|
|
6409274f83 | ||
|
|
bc534c12a5 | ||
|
|
b58fd2022a | ||
|
|
d850d026ed | ||
|
|
83627686d4 | ||
|
|
4e7d01c72c | ||
|
|
0f8ab20db7 | ||
|
|
88cc90786d | ||
|
|
fb66717b43 | ||
|
|
1d60433fcf | ||
|
|
0f3df6e92b | ||
|
|
ca7c63c7d7 | ||
|
|
135f9611df | ||
|
|
cfaaeebd4a | ||
|
|
09a0779180 | ||
|
|
7f7d09bc85 | ||
|
|
0c454a08dc | ||
|
|
d749b63549 | ||
|
|
2053a6950d | ||
|
|
41bd801e0d | ||
|
|
cd0e1a3962 | ||
|
|
7c2f482b3b | ||
|
|
7741d60afd | ||
|
|
8aac0c0327 | ||
|
|
7c26c56210 | ||
|
|
e88a780efe | ||
|
|
363fb5dc02 | ||
|
|
39a187b6da | ||
|
|
b4cc34a522 | ||
|
|
af94ef3d49 | ||
|
|
505855a53c | ||
|
|
87ac245341 | ||
|
|
1670a09d04 | ||
|
|
620b954336 | ||
|
|
cfd51e9b84 | ||
|
|
6c797f8216 | ||
|
|
40e208152a | ||
|
|
cf7bfa62ef | ||
|
|
a1086b9a04 | ||
|
|
9bc4bbd2c8 | ||
|
|
73156012e9 | ||
|
|
91cc3d77d4 | ||
|
|
3fc55a9e9f | ||
|
|
b666aa3f26 | ||
|
|
53e7dbe12f | ||
|
|
9d8ff6856b | ||
|
|
3fb38376b0 | ||
|
|
15c73d9dd3 | ||
|
|
86f71ffb93 | ||
|
|
eb928d3369 | ||
|
|
d7307665b3 | ||
|
|
2836f0ab5a | ||
|
|
91b7f3980c | ||
|
|
5053432c2d | ||
|
|
563c612395 | ||
|
|
95dced6455 | ||
|
|
989f0bbbfb | ||
|
|
e5eec28bfd | ||
|
|
bfc402f307 | ||
|
|
35a998b934 | ||
|
|
cadc8e499d | ||
|
|
7f7ea6da9f | ||
|
|
ab9a22d8e7 | ||
|
|
cd2728105e | ||
|
|
d75e84bdff | ||
|
|
e791fee38b | ||
|
|
ad5f057733 | ||
|
|
6f325b5fdb | ||
|
|
b73aeee18d | ||
|
|
8a54035a9f | ||
|
|
af28e30e4c | ||
|
|
492513306c | ||
|
|
bc554ff4a7 | ||
|
|
b7a0d1ece8 | ||
|
|
ca384218dc | ||
|
|
e9550fd6b2 | ||
|
|
232a305d51 | ||
|
|
a6ab448eeb | ||
|
|
f9aa157c6c | ||
|
|
b7df24acaa | ||
|
|
ad4ca1b2d7 | ||
|
|
c562d197e7 | ||
|
|
83ba1899b7 | ||
|
|
3420adc7c9 | ||
|
|
fd39f28e46 | ||
|
|
710125852a | ||
|
|
e8ec208390 | ||
|
|
4584562607 | ||
|
|
2c1412a088 | ||
|
|
0a65382979 | ||
|
|
4f404f66e5 | ||
|
|
89505ada00 | ||
|
|
374a30ac5a | ||
|
|
cee4ee4128 | ||
|
|
58fc5e2ffa | ||
|
|
951ea43f8b | ||
|
|
ca16ecef24 | ||
|
|
bb025dc2a1 | ||
|
|
d797169bd0 | ||
|
|
891f9e2252 | ||
|
|
4a579c00ce | ||
|
|
9cb4d8e088 | ||
|
|
5d3ee7755a | ||
|
|
c047c46587 | ||
|
|
54f53be5b5 | ||
|
|
8bb9e5b22f | ||
|
|
379791a326 | ||
|
|
38ec68b303 | ||
|
|
a5095b04ad | ||
|
|
a27ddb40be | ||
|
|
bc36be8a5e | ||
|
|
1e35556034 | ||
|
|
882cd41d4b | ||
|
|
724fb4bf8f | ||
|
|
b07437dbfa | ||
|
|
96f05cd518 | ||
|
|
77411e94a4 | ||
|
|
0da9c62ef8 | ||
|
|
52a7885f3c | ||
|
|
f98f089d63 | ||
|
|
6b618f3abe | ||
|
|
0732ffa76e | ||
|
|
b5b4636e56 | ||
|
|
ca12d040e1 | ||
|
|
3388b9fafa | ||
|
|
954b36e14c | ||
|
|
4d43814220 | ||
|
|
f6262c82e1 | ||
|
|
7ead12922f | ||
|
|
33a6a7869c | ||
|
|
bf995f989c | ||
|
|
21de6c6520 | ||
|
|
c14aa6851e | ||
|
|
8260eced2d | ||
|
|
d028465dc5 | ||
|
|
29dab5e47d | ||
|
|
9e655631b4 | ||
|
|
179c7b80bb | ||
|
|
349bf29122 | ||
|
|
295357f12b | ||
|
|
940f8d999e | ||
|
|
5605d53a5f | ||
|
|
116d103119 | ||
|
|
2fd8c643af | ||
|
|
4367ae7934 | ||
|
|
749461334d | ||
|
|
e83a027023 | ||
|
|
1883b477a3 | ||
|
|
37e2cd40da | ||
|
|
81a9329975 | ||
|
|
0eb019fc3c | ||
|
|
4129c75475 | ||
|
|
3d66f03f58 | ||
|
|
7b83104fd6 | ||
|
|
794aede27f | ||
|
|
08eb39b206 | ||
|
|
2566c7f3d7 | ||
|
|
a8522bb3b5 | ||
|
|
92b9142902 | ||
|
|
d07e3e6522 | ||
|
|
29aabdfba8 | ||
|
|
9af1b0cfdc | ||
|
|
6e32c7fe85 | ||
|
|
ddf5915c6a | ||
|
|
cdbf1fa73a | ||
|
|
5d926b022b | ||
|
|
50bcca10e2 | ||
|
|
a5528c06ee | ||
|
|
94526de04b | ||
|
|
1ddf7abe6f | ||
|
|
a742c1b034 | ||
|
|
6e726ac2a6 | ||
|
|
5877b40be5 | ||
|
|
a3c7f5aa46 | ||
|
|
4e28bf03bd | ||
|
|
f92482d89e | ||
|
|
3c54429fe0 | ||
|
|
4c4c22e861 | ||
|
|
00e27d9327 | ||
|
|
f082278041 | ||
|
|
09dde64c57 | ||
|
|
b52a6357f6 | ||
|
|
f14a566d06 | ||
|
|
b352ec6888 | ||
|
|
4ec1bad03d | ||
|
|
f20bbc119d | ||
|
|
8555c3d422 | ||
|
|
71f9d03b19 | ||
|
|
0b31379078 | ||
|
|
29c204b2c2 | ||
|
|
657e881963 | ||
|
|
ef6bdc70a4 | ||
|
|
e4a36115a2 | ||
|
|
325f86832c | ||
|
|
e3dbdd6b09 | ||
|
|
919041e879 | ||
|
|
182231a183 | ||
|
|
46d4ae8fc5 | ||
|
|
73ab1936a3 | ||
|
|
59c72527b5 | ||
|
|
5ea3bcc1dd | ||
|
|
c140052822 | ||
|
|
f7832774d9 | ||
|
|
279d25c03a | ||
|
|
f1984047a8 | ||
|
|
eae7c1bd60 | ||
|
|
d7f592ebda | ||
|
|
1798ce002a | ||
|
|
57a0cca595 | ||
|
|
b26fbd7693 | ||
|
|
43b0e25bdb | ||
|
|
3377af1305 | ||
|
|
c81c1006b7 | ||
|
|
f313857f96 | ||
|
|
d5fbe02149 | ||
|
|
159cee0b39 | ||
|
|
5da4dd6cca | ||
|
|
e9daf05f16 | ||
|
|
a12643194a | ||
|
|
25d8fc08f7 | ||
|
|
a72378dd4d | ||
|
|
9aed70408b | ||
|
|
5ae2c71c3a | ||
|
|
4edce515b8 | ||
|
|
67ff664eb8 | ||
|
|
71c1a4f102 | ||
|
|
ba4ba1b9fc | ||
|
|
59f10f06ca | ||
|
|
f7953cbc37 | ||
|
|
9a74e81754 | ||
|
|
420e4b6766 | ||
|
|
aed48ffc93 | ||
|
|
0cebe69ff8 | ||
|
|
e5990dba81 | ||
|
|
c9b0d01250 | ||
|
|
4918ed3f3c | ||
|
|
b176ce4251 | ||
|
|
518ff5409e | ||
|
|
803bd3c5b2 | ||
|
|
03d4e73304 | ||
|
|
55a820b09f | ||
|
|
f2a65dc360 | ||
|
|
7b4a889ea7 | ||
|
|
f627d251c3 | ||
|
|
d5b7125415 | ||
|
|
67dd3cf0e3 | ||
|
|
b8b62bb5af | ||
|
|
b4a9d1ac18 | ||
|
|
8aae651c2c | ||
|
|
fc9465b324 | ||
|
|
579a50be2c | ||
|
|
9c5b967e4c | ||
|
|
d41deb729b | ||
|
|
a93a89f3f0 | ||
|
|
11d642a25f | ||
|
|
67448498ea | ||
|
|
489b8da713 |
4
.github/pull_request_template.md
vendored
Normal file
4
.github/pull_request_template.md
vendored
Normal file
@@ -0,0 +1,4 @@
|
|||||||
|
# License Agreement for Contributions
|
||||||
|
By submitting this pull request, I acknowledge and agree that my contributions will be included in Stirling-PDF and that they can be relicensed in the future under MPL 2.0 (Mozilla Public License Version 2.0) license.
|
||||||
|
|
||||||
|
(This does not change the general open-source nature of Stirling-PDF, simply moving from one license to another license)
|
||||||
3
.github/workflows/pull_request_template.md
vendored
Normal file
3
.github/workflows/pull_request_template.md
vendored
Normal file
@@ -0,0 +1,3 @@
|
|||||||
|
# License Agreement for Contributions
|
||||||
|
By submitting this pull request, I acknowledge and agree that my contributions will be included in Stirling-PDF and that they can be relicensed in the future under MPL 2.0 (Mozilla Public License Version 2.0) license.
|
||||||
|
(This does not change the open-source nature of Stirling-PDF, simply moving from one license to another license)
|
||||||
94
.github/workflows/push-docker.yml
vendored
94
.github/workflows/push-docker.yml
vendored
@@ -9,13 +9,6 @@ on:
|
|||||||
jobs:
|
jobs:
|
||||||
push:
|
push:
|
||||||
runs-on: ubuntu-latest
|
runs-on: ubuntu-latest
|
||||||
strategy:
|
|
||||||
matrix:
|
|
||||||
config: [
|
|
||||||
{ dockerfile: "./Dockerfile", tagSuffix: "" },
|
|
||||||
{ dockerfile: "./Dockerfile-ultra-lite", tagSuffix: "-ultra-lite" },
|
|
||||||
{ dockerfile: "./Dockerfile-lite", tagSuffix: "-lite" }
|
|
||||||
]
|
|
||||||
steps:
|
steps:
|
||||||
|
|
||||||
- uses: actions/checkout@v3.5.2
|
- uses: actions/checkout@v3.5.2
|
||||||
@@ -28,6 +21,8 @@ jobs:
|
|||||||
|
|
||||||
|
|
||||||
- uses: gradle/gradle-build-action@v2.4.2
|
- uses: gradle/gradle-build-action@v2.4.2
|
||||||
|
env:
|
||||||
|
DOCKER_ENABLE_SECURITY: false
|
||||||
with:
|
with:
|
||||||
gradle-version: 7.6
|
gradle-version: 7.6
|
||||||
arguments: clean build
|
arguments: clean build
|
||||||
@@ -56,14 +51,6 @@ jobs:
|
|||||||
id: repoowner
|
id: repoowner
|
||||||
run: echo "::set-output name=lowercase::$(echo ${{ github.repository_owner }} | awk '{print tolower($0)}')"
|
run: echo "::set-output name=lowercase::$(echo ${{ github.repository_owner }} | awk '{print tolower($0)}')"
|
||||||
|
|
||||||
|
|
||||||
- name: Set up QEMU
|
|
||||||
uses: docker/setup-qemu-action@v2.1.0
|
|
||||||
|
|
||||||
- name: Set up Docker Buildx
|
|
||||||
uses: docker/setup-buildx-action@v2.5.0
|
|
||||||
|
|
||||||
|
|
||||||
- name: Generate tags
|
- name: Generate tags
|
||||||
id: meta
|
id: meta
|
||||||
uses: docker/metadata-action@v4.4.0
|
uses: docker/metadata-action@v4.4.0
|
||||||
@@ -72,19 +59,84 @@ jobs:
|
|||||||
${{ secrets.DOCKER_HUB_USERNAME }}/s-pdf
|
${{ secrets.DOCKER_HUB_USERNAME }}/s-pdf
|
||||||
ghcr.io/${{ steps.repoowner.outputs.lowercase }}/s-pdf
|
ghcr.io/${{ steps.repoowner.outputs.lowercase }}/s-pdf
|
||||||
tags: |
|
tags: |
|
||||||
type=raw,value=${{ steps.versionNumber.outputs.versionNumber }}${{ matrix.config.tagSuffix }},enable=${{ github.ref == 'refs/heads/master' }}
|
type=raw,value=${{ steps.versionNumber.outputs.versionNumber }},enable=${{ github.ref == 'refs/heads/master' }}
|
||||||
type=raw,value=latest${{ matrix.config.tagSuffix }},enable=${{ github.ref == 'refs/heads/master' }}
|
type=raw,value=latest,enable=${{ github.ref == 'refs/heads/master' }}
|
||||||
type=raw,value=alpha,enable=${{ github.ref == 'refs/heads/main' }}
|
type=raw,value=alpha,enable=${{ github.ref == 'refs/heads/main' }}
|
||||||
|
|
||||||
- name: Build and push Dockerfile
|
- name: Set up QEMU
|
||||||
|
uses: docker/setup-qemu-action@v2.1.0
|
||||||
|
|
||||||
|
- name: Set up Docker Buildx
|
||||||
|
uses: docker/setup-buildx-action@v2.5.0
|
||||||
|
|
||||||
|
- name: Build and push main Dockerfile
|
||||||
uses: docker/build-push-action@v4.0.0
|
uses: docker/build-push-action@v4.0.0
|
||||||
with:
|
with:
|
||||||
context: .
|
context: .
|
||||||
dockerfile: ${{ matrix.config.dockerfile }}
|
dockerfile: ./Dockerfile
|
||||||
push: true
|
push: true
|
||||||
cache-from: type=gha
|
cache-from: type=gha
|
||||||
cache-to: type=gha,mode=max
|
cache-to: type=gha,mode=max
|
||||||
tags: ${{ steps.meta.outputs.tags }}
|
tags: ${{ steps.meta.outputs.tags }}
|
||||||
labels: ${{ steps.meta.outputs.labels }}
|
labels: ${{ steps.meta.outputs.labels }}
|
||||||
|
build-args:
|
||||||
|
VERSION_TAG=${{ steps.versionNumber.outputs.versionNumber }}
|
||||||
|
platforms: linux/amd64,linux/arm64/v8
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
- name: Generate tags ultra-lite
|
||||||
|
id: meta2
|
||||||
|
uses: docker/metadata-action@v4.4.0
|
||||||
|
if: github.ref != 'refs/heads/main'
|
||||||
|
with:
|
||||||
|
images: |
|
||||||
|
${{ secrets.DOCKER_HUB_USERNAME }}/s-pdf
|
||||||
|
ghcr.io/${{ steps.repoowner.outputs.lowercase }}/s-pdf
|
||||||
|
tags: |
|
||||||
|
type=raw,value=${{ steps.versionNumber.outputs.versionNumber }}-ultra-lite,enable=${{ github.ref == 'refs/heads/master' }}
|
||||||
|
type=raw,value=latest-ultra-lite,enable=${{ github.ref == 'refs/heads/master' }}
|
||||||
|
|
||||||
|
|
||||||
|
- name: Build and push Dockerfile-ultra-lite
|
||||||
|
uses: docker/build-push-action@v4.0.0
|
||||||
|
if: github.ref != 'refs/heads/main'
|
||||||
|
with:
|
||||||
|
context: .
|
||||||
|
file: ./Dockerfile-ultra-lite
|
||||||
|
push: true
|
||||||
|
cache-from: type=gha
|
||||||
|
cache-to: type=gha,mode=max
|
||||||
|
tags: ${{ steps.meta2.outputs.tags }}
|
||||||
|
labels: ${{ steps.meta2.outputs.labels }}
|
||||||
|
build-args:
|
||||||
|
VERSION_TAG=${{ steps.versionNumber.outputs.versionNumber }}
|
||||||
|
platforms: linux/amd64,linux/arm64/v8
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
- name: Generate tags lite
|
||||||
|
id: meta3
|
||||||
|
uses: docker/metadata-action@v4.4.0
|
||||||
|
if: github.ref != 'refs/heads/main'
|
||||||
|
with:
|
||||||
|
images: |
|
||||||
|
${{ secrets.DOCKER_HUB_USERNAME }}/s-pdf
|
||||||
|
ghcr.io/${{ steps.repoowner.outputs.lowercase }}/s-pdf
|
||||||
|
tags: |
|
||||||
|
type=raw,value=${{ steps.versionNumber.outputs.versionNumber }}-lite,enable=${{ github.ref == 'refs/heads/master' }}
|
||||||
|
type=raw,value=latest-lite,enable=${{ github.ref == 'refs/heads/master' }}
|
||||||
|
|
||||||
|
|
||||||
|
- name: Build and push Dockerfile-lite
|
||||||
|
uses: docker/build-push-action@v4.0.0
|
||||||
|
if: github.ref != 'refs/heads/main'
|
||||||
|
with:
|
||||||
|
context: .
|
||||||
|
file: ./Dockerfile-lite
|
||||||
|
push: true
|
||||||
|
cache-from: type=gha
|
||||||
|
cache-to: type=gha,mode=max
|
||||||
|
tags: ${{ steps.meta3.outputs.tags }}
|
||||||
|
labels: ${{ steps.meta3.outputs.labels }}
|
||||||
platforms: linux/amd64,linux/arm64/v8
|
platforms: linux/amd64,linux/arm64/v8
|
||||||
|
|
||||||
55
.github/workflows/releaseArtifacts.yml
vendored
Normal file
55
.github/workflows/releaseArtifacts.yml
vendored
Normal file
@@ -0,0 +1,55 @@
|
|||||||
|
name: Release Artifacts
|
||||||
|
|
||||||
|
on:
|
||||||
|
release:
|
||||||
|
types: [created]
|
||||||
|
|
||||||
|
jobs:
|
||||||
|
push:
|
||||||
|
runs-on: ubuntu-latest
|
||||||
|
strategy:
|
||||||
|
matrix:
|
||||||
|
enable_security: [true, false]
|
||||||
|
include:
|
||||||
|
- enable_security: true
|
||||||
|
file_suffix: '-with-login'
|
||||||
|
- enable_security: false
|
||||||
|
file_suffix: ''
|
||||||
|
steps:
|
||||||
|
- uses: actions/checkout@v3.5.2
|
||||||
|
|
||||||
|
- name: Set up JDK 17
|
||||||
|
uses: actions/setup-java@v3.11.0
|
||||||
|
with:
|
||||||
|
java-version: '17'
|
||||||
|
distribution: 'temurin'
|
||||||
|
|
||||||
|
- name: Grant execute permission for gradlew
|
||||||
|
run: chmod +x gradlew
|
||||||
|
|
||||||
|
- name: Generate jar (With Security=${{ matrix.enable_security }})
|
||||||
|
run: ./gradlew clean createExe
|
||||||
|
env:
|
||||||
|
DOCKER_ENABLE_SECURITY: ${{ matrix.enable_security }}
|
||||||
|
|
||||||
|
- name: Upload binaries to release
|
||||||
|
uses: svenstaro/upload-release-action@v2
|
||||||
|
with:
|
||||||
|
repo_token: ${{ secrets.GITHUB_TOKEN }}
|
||||||
|
file: ./build/launch4j/Stirling-PDF.exe
|
||||||
|
asset_name: Stirling-PDF${{ matrix.file_suffix }}.exe
|
||||||
|
tag: ${{ github.ref }}
|
||||||
|
overwrite: true
|
||||||
|
|
||||||
|
- name: Get version number
|
||||||
|
id: versionNumber
|
||||||
|
run: echo "::set-output name=versionNumber::$(./gradlew printVersion --quiet | tail -1)"
|
||||||
|
|
||||||
|
- name: Upload jar binaries to release
|
||||||
|
uses: svenstaro/upload-release-action@v2
|
||||||
|
with:
|
||||||
|
repo_token: ${{ secrets.GITHUB_TOKEN }}
|
||||||
|
file: ./build/libs/Stirling-PDF-${{ steps.versionNumber.outputs.versionNumber }}.jar
|
||||||
|
asset_name: Stirling-PDF${{ matrix.file_suffix }}.jar
|
||||||
|
tag: ${{ github.ref }}
|
||||||
|
overwrite: true
|
||||||
235
.gitignore
vendored
235
.gitignore
vendored
@@ -1,115 +1,122 @@
|
|||||||
|
|
||||||
|
|
||||||
### Eclipse ###
|
### Eclipse ###
|
||||||
.metadata
|
.metadata
|
||||||
bin/
|
bin/
|
||||||
tmp/
|
tmp/
|
||||||
*.tmp
|
*.tmp
|
||||||
*.bak
|
*.bak
|
||||||
*.swp
|
*.swp
|
||||||
*~.nib
|
*~.nib
|
||||||
local.properties
|
local.properties
|
||||||
.settings/
|
.settings/
|
||||||
.loadpath
|
.loadpath
|
||||||
.recommenders
|
.recommenders
|
||||||
.classpath
|
.classpath
|
||||||
.project
|
.project
|
||||||
version.properties
|
version.properties
|
||||||
|
pipeline/
|
||||||
# Gradle
|
|
||||||
.gradle
|
#### Stirling-PDF Files ###
|
||||||
.lock
|
customFiles/
|
||||||
|
configs/
|
||||||
# External tool builders
|
watchedFolders/
|
||||||
.externalToolBuilders/
|
|
||||||
|
|
||||||
# Locally stored "Eclipse launch configurations"
|
# Gradle
|
||||||
*.launch
|
.gradle
|
||||||
|
.lock
|
||||||
# PyDev specific (Python IDE for Eclipse)
|
|
||||||
*.pydevproject
|
# External tool builders
|
||||||
|
.externalToolBuilders/
|
||||||
# CDT-specific (C/C++ Development Tooling)
|
|
||||||
.cproject
|
# Locally stored "Eclipse launch configurations"
|
||||||
|
*.launch
|
||||||
# CDT- autotools
|
|
||||||
.autotools
|
# PyDev specific (Python IDE for Eclipse)
|
||||||
|
*.pydevproject
|
||||||
# Java annotation processor (APT)
|
|
||||||
.factorypath
|
# CDT-specific (C/C++ Development Tooling)
|
||||||
|
.cproject
|
||||||
# PDT-specific (PHP Development Tools)
|
|
||||||
.buildpath
|
# CDT- autotools
|
||||||
|
.autotools
|
||||||
# sbteclipse plugin
|
|
||||||
.target
|
# Java annotation processor (APT)
|
||||||
|
.factorypath
|
||||||
# Tern plugin
|
|
||||||
.tern-project
|
# PDT-specific (PHP Development Tools)
|
||||||
|
.buildpath
|
||||||
# TeXlipse plugin
|
|
||||||
.texlipse
|
# sbteclipse plugin
|
||||||
|
.target
|
||||||
# STS (Spring Tool Suite)
|
|
||||||
.springBeans
|
# Tern plugin
|
||||||
|
.tern-project
|
||||||
# Code Recommenders
|
|
||||||
.recommenders/
|
# TeXlipse plugin
|
||||||
|
.texlipse
|
||||||
# Annotation Processing
|
|
||||||
.apt_generated/
|
# STS (Spring Tool Suite)
|
||||||
.apt_generated_test/
|
.springBeans
|
||||||
|
|
||||||
# Scala IDE specific (Scala & Java development for Eclipse)
|
# Code Recommenders
|
||||||
.cache-main
|
.recommenders/
|
||||||
.scala_dependencies
|
|
||||||
.worksheet
|
# Annotation Processing
|
||||||
|
.apt_generated/
|
||||||
# Uncomment this line if you wish to ignore the project description file.
|
.apt_generated_test/
|
||||||
# Typically, this file would be tracked if it contains build/dependency configurations:
|
|
||||||
#.project
|
# Scala IDE specific (Scala & Java development for Eclipse)
|
||||||
|
.cache-main
|
||||||
### Eclipse Patch ###
|
.scala_dependencies
|
||||||
# Spring Boot Tooling
|
.worksheet
|
||||||
.sts4-cache/
|
|
||||||
|
# Uncomment this line if you wish to ignore the project description file.
|
||||||
### Git ###
|
# Typically, this file would be tracked if it contains build/dependency configurations:
|
||||||
# Created by git for backups. To disable backups in Git:
|
#.project
|
||||||
# $ git config --global mergetool.keepBackup false
|
|
||||||
*.orig
|
### Eclipse Patch ###
|
||||||
|
# Spring Boot Tooling
|
||||||
# Created by git when using merge tools for conflicts
|
.sts4-cache/
|
||||||
*.BACKUP.*
|
|
||||||
*.BASE.*
|
### Git ###
|
||||||
*.LOCAL.*
|
# Created by git for backups. To disable backups in Git:
|
||||||
*.REMOTE.*
|
# $ git config --global mergetool.keepBackup false
|
||||||
*_BACKUP_*.txt
|
*.orig
|
||||||
*_BASE_*.txt
|
|
||||||
*_LOCAL_*.txt
|
# Created by git when using merge tools for conflicts
|
||||||
*_REMOTE_*.txt
|
*.BACKUP.*
|
||||||
|
*.BASE.*
|
||||||
### Java ###
|
*.LOCAL.*
|
||||||
# Compiled class file
|
*.REMOTE.*
|
||||||
*.class
|
*_BACKUP_*.txt
|
||||||
|
*_BASE_*.txt
|
||||||
# Log file
|
*_LOCAL_*.txt
|
||||||
*.log
|
*_REMOTE_*.txt
|
||||||
|
|
||||||
# BlueJ files
|
### Java ###
|
||||||
*.ctxt
|
# Compiled class file
|
||||||
|
*.class
|
||||||
# Mobile Tools for Java (J2ME)
|
|
||||||
.mtj.tmp/
|
# Log file
|
||||||
|
*.log
|
||||||
# Package Files #
|
|
||||||
*.jar
|
# BlueJ files
|
||||||
*.war
|
*.ctxt
|
||||||
*.nar
|
|
||||||
*.ear
|
# Mobile Tools for Java (J2ME)
|
||||||
*.zip
|
.mtj.tmp/
|
||||||
*.tar.gz
|
|
||||||
*.rar
|
# Package Files #
|
||||||
|
*.jar
|
||||||
/build
|
*.war
|
||||||
|
*.nar
|
||||||
|
*.ear
|
||||||
|
*.zip
|
||||||
|
*.tar.gz
|
||||||
|
*.rar
|
||||||
|
*.db
|
||||||
|
/build
|
||||||
|
|
||||||
/.vscode
|
/.vscode
|
||||||
51
Dockerfile
51
Dockerfile
@@ -1,22 +1,45 @@
|
|||||||
# Build jbig2enc in a separate stage
|
# Use the base image
|
||||||
FROM frooodle/stirling-pdf-base:latest
|
FROM frooodle/stirling-pdf-base:beta4
|
||||||
|
|
||||||
# Create scripts folder and copy local scripts
|
ARG VERSION_TAG
|
||||||
RUN mkdir /scripts
|
|
||||||
|
# Set Environment Variables
|
||||||
|
ENV DOCKER_ENABLE_SECURITY=false \
|
||||||
|
HOME=/home/stirlingpdfuser \
|
||||||
|
VERSION_TAG=$VERSION_TAG
|
||||||
|
# PUID=1000 \
|
||||||
|
# PGID=1000 \
|
||||||
|
# UMASK=022 \
|
||||||
|
|
||||||
|
|
||||||
|
# Create user and group
|
||||||
|
##RUN groupadd -g $PGID stirlingpdfgroup && \
|
||||||
|
## useradd -u $PUID -g stirlingpdfgroup -s /bin/sh stirlingpdfuser && \
|
||||||
|
## mkdir -p $HOME && chown stirlingpdfuser:stirlingpdfgroup $HOME
|
||||||
|
|
||||||
|
# Set up necessary directories and permissions
|
||||||
|
RUN mkdir -p /scripts /usr/share/fonts/opentype/noto /usr/share/tesseract-ocr /configs /customFiles
|
||||||
|
##&& \
|
||||||
|
## chown -R stirlingpdfuser:stirlingpdfgroup /scripts /usr/share/fonts/opentype/noto /usr/share/tesseract-ocr /configs /customFiles && \
|
||||||
|
## chown -R stirlingpdfuser:stirlingpdfgroup /usr/share/tesseract-ocr-original
|
||||||
|
|
||||||
|
# Copy necessary files
|
||||||
COPY ./scripts/* /scripts/
|
COPY ./scripts/* /scripts/
|
||||||
|
COPY src/main/resources/static/fonts/*.ttf /usr/share/fonts/opentype/noto/
|
||||||
# Copy the application JAR file
|
COPY src/main/resources/static/fonts/*.otf /usr/share/fonts/opentype/noto/
|
||||||
COPY build/libs/*.jar app.jar
|
COPY build/libs/*.jar app.jar
|
||||||
|
|
||||||
# Expose the application port
|
# Set font cache and permissions
|
||||||
|
RUN fc-cache -f -v && chmod +x /scripts/init.sh
|
||||||
|
|
||||||
|
##&& \
|
||||||
|
## chown stirlingpdfuser:stirlingpdfgroup /app.jar && \
|
||||||
|
## chmod +x /scripts/init.sh
|
||||||
|
|
||||||
|
# Expose necessary ports
|
||||||
EXPOSE 8080
|
EXPOSE 8080
|
||||||
|
|
||||||
# Set environment variables
|
# Set user and run command
|
||||||
ENV APP_HOME_NAME="Stirling PDF"
|
##USER stirlingpdfuser
|
||||||
#ENV APP_HOME_DESCRIPTION="Personal PDF Website!"
|
|
||||||
#ENV APP_NAVBAR_NAME="Stirling PDF"
|
|
||||||
|
|
||||||
# Run the application
|
|
||||||
RUN chmod +x /scripts/init.sh
|
|
||||||
ENTRYPOINT ["/scripts/init.sh"]
|
ENTRYPOINT ["/scripts/init.sh"]
|
||||||
CMD ["java", "-jar", "/app.jar"]
|
CMD ["java", "-jar", "/app.jar"]
|
||||||
|
|||||||
@@ -10,14 +10,45 @@ RUN apt-get update && \
|
|||||||
unoconv && \
|
unoconv && \
|
||||||
rm -rf /var/lib/apt/lists/*
|
rm -rf /var/lib/apt/lists/*
|
||||||
|
|
||||||
# Copy the application JAR file
|
|
||||||
|
# Set Environment Variables
|
||||||
|
ENV DOCKER_ENABLE_SECURITY=false \
|
||||||
|
HOME=/home/stirlingpdfuser \
|
||||||
|
VERSION_TAG=$VERSION_TAG
|
||||||
|
# PUID=1000 \
|
||||||
|
# PGID=1000 \
|
||||||
|
# UMASK=022 \
|
||||||
|
|
||||||
|
# Create user and group
|
||||||
|
#RUN groupadd -g $PGID stirlingpdfgroup && \
|
||||||
|
# useradd -u $PUID -g stirlingpdfgroup -s /bin/sh stirlingpdfuser && \
|
||||||
|
# mkdir -p $HOME && chown stirlingpdfuser:stirlingpdfgroup $HOME
|
||||||
|
|
||||||
|
# Set up necessary directories and permissions
|
||||||
|
RUN mkdir -p /scripts /usr/share/fonts/opentype/noto /configs /customFiles
|
||||||
|
|
||||||
|
# chown -R stirlingpdfuser:stirlingpdfgroup /usr/share/fonts/opentype/noto /configs /customFiles
|
||||||
|
|
||||||
|
# Copy necessary files
|
||||||
|
COPY src/main/resources/static/fonts/*.ttf /usr/share/fonts/opentype/noto/
|
||||||
|
COPY src/main/resources/static/fonts/*.otf /usr/share/fonts/opentype/noto/
|
||||||
COPY build/libs/*.jar app.jar
|
COPY build/libs/*.jar app.jar
|
||||||
|
|
||||||
|
# Set font cache and permissions
|
||||||
|
RUN fc-cache -f -v
|
||||||
|
# chown stirlingpdfuser:stirlingpdfgroup /app.jar
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
# Expose the application port
|
# Expose the application port
|
||||||
EXPOSE 8080
|
EXPOSE 8080
|
||||||
|
|
||||||
# Set environment variables
|
# Set environment variables
|
||||||
ENV GROUPS_TO_REMOVE=Python,OpenCV,OCRmyPDF
|
ENV ENDPOINTS_GROUPS_TO_REMOVE=Python,OpenCV,OCRmyPDF
|
||||||
|
ENV DOCKER_ENABLE_SECURITY=false
|
||||||
|
|
||||||
# Run the application
|
# Run the application
|
||||||
|
#USER stirlingpdfuser
|
||||||
|
|
||||||
CMD ["java", "-jar", "/app.jar"]
|
CMD ["java", "-jar", "/app.jar"]
|
||||||
|
|||||||
@@ -1,14 +1,34 @@
|
|||||||
# Build jbig2enc in a separate stage
|
# Build jbig2enc in a separate stage
|
||||||
FROM bellsoft/liberica-openjdk-alpine:17
|
FROM bellsoft/liberica-openjdk-alpine:17
|
||||||
|
|
||||||
# Copy the application JAR file
|
# Set Environment Variables
|
||||||
|
ENV PUID=1000 \
|
||||||
|
PGID=1000 \
|
||||||
|
UMASK=022 \
|
||||||
|
DOCKER_ENABLE_SECURITY=false \
|
||||||
|
HOME=/home/stirlingpdfuser \
|
||||||
|
VERSION_TAG=$VERSION_TAG
|
||||||
|
|
||||||
|
# Create user and group using Alpine's addgroup and adduser
|
||||||
|
RUN addgroup -g $PGID stirlingpdfgroup && \
|
||||||
|
adduser -u $PUID -G stirlingpdfgroup -s /bin/sh -D stirlingpdfuser && \
|
||||||
|
mkdir -p $HOME && chown stirlingpdfuser:stirlingpdfgroup $HOME
|
||||||
|
|
||||||
|
# Set up necessary directories and permissions
|
||||||
|
RUN mkdir -p /scripts /configs /customFiles && \
|
||||||
|
chown -R stirlingpdfuser:stirlingpdfgroup /scripts /configs /customFiles
|
||||||
|
|
||||||
COPY build/libs/*.jar app.jar
|
COPY build/libs/*.jar app.jar
|
||||||
|
|
||||||
|
# Set font cache and permissions
|
||||||
|
RUN chown stirlingpdfuser:stirlingpdfgroup /app.jar
|
||||||
|
|
||||||
# Expose the application port
|
# Expose the application port
|
||||||
EXPOSE 8080
|
EXPOSE 8080
|
||||||
|
|
||||||
# Set environment variables
|
# Set environment variables
|
||||||
ENV GROUPS_TO_REMOVE=LibreOffice,CLI
|
ENV ENDPOINTS_GROUPS_TO_REMOVE=CLI
|
||||||
|
ENV DOCKER_ENABLE_SECURITY=false
|
||||||
|
|
||||||
# Run the application
|
# Run the application
|
||||||
CMD ["java", "-jar", "/app.jar"]
|
CMD ["java", "-jar", "/app.jar"]
|
||||||
|
|||||||
@@ -29,7 +29,7 @@ RUN apt-get update && \
|
|||||||
libjpeg-dev && \
|
libjpeg-dev && \
|
||||||
pip install --upgrade pip && \
|
pip install --upgrade pip && \
|
||||||
pip install --no-cache-dir \
|
pip install --no-cache-dir \
|
||||||
opencv-python-headless && \
|
opencv-python-headless WeasyPrint && \
|
||||||
rm -rf /var/lib/apt/lists/*
|
rm -rf /var/lib/apt/lists/*
|
||||||
|
|
||||||
# Final stage: Copy necessary files from the previous stage
|
# Final stage: Copy necessary files from the previous stage
|
||||||
|
|||||||
46
Endpoint-groups.md
Normal file
46
Endpoint-groups.md
Normal file
@@ -0,0 +1,46 @@
|
|||||||
|
| Operation | PageOps | Convert | Security | Other | CLI | Python | OpenCV | LibreOffice | OCRmyPDF | Java | Javascript |
|
||||||
|
|---------------------|---------|---------|----------|-------|------|--------|--------|-------------|----------|----------|------------|
|
||||||
|
| adjust-contrast | ✔️ | | | | | | | | | | ✔️ |
|
||||||
|
| auto-split-pdf | ✔️ | | | | | | | | | ✔️ | |
|
||||||
|
| crop | ✔️ | | | | | | | | | ✔️ | |
|
||||||
|
| extract-page | ✔️ | | | | | | | | | ✔️ | |
|
||||||
|
| merge-pdfs | ✔️ | | | | | | | | | ✔️ | |
|
||||||
|
| multi-page-layout | ✔️ | | | | | | | | | ✔️ | |
|
||||||
|
| pdf-organizer | ✔️ | | | | | | | | | ✔️ | ✔️ |
|
||||||
|
| pdf-to-single-page | ✔️ | | | | | | | | | ✔️ | |
|
||||||
|
| remove-pages | ✔️ | | | | | | | | | ✔️ | |
|
||||||
|
| rotate-pdf | ✔️ | | | | | | | | | ✔️ | |
|
||||||
|
| scale-pages | ✔️ | | | | | | | | | ✔️ | |
|
||||||
|
| split-pdfs | ✔️ | | | | | | | | | ✔️ | |
|
||||||
|
| file-to-pdf | | ✔️ | | | ✔️ | | | ✔️ | | | |
|
||||||
|
| img-to-pdf | | ✔️ | | | | | | | | ✔️ | |
|
||||||
|
| pdf-to-html | | ✔️ | | | ✔️ | | | ✔️ | | | |
|
||||||
|
| pdf-to-img | | ✔️ | | | | | | | | ✔️ | |
|
||||||
|
| pdf-to-pdfa | | ✔️ | | | ✔️ | | | | ✔️ | | |
|
||||||
|
| pdf-to-markdown | | ✔️ | | | | | | | | ✔️ | |
|
||||||
|
| pdf-to-presentation | | ✔️ | | | ✔️ | | | ✔️ | | | |
|
||||||
|
| pdf-to-text | | ✔️ | | | ✔️ | | | ✔️ | | | |
|
||||||
|
| pdf-to-word | | ✔️ | | | ✔️ | | | ✔️ | | | |
|
||||||
|
| pdf-to-xml | | ✔️ | | | ✔️ | | | ✔️ | | | |
|
||||||
|
| xlsx-to-pdf | | ✔️ | | | ✔️ | | | ✔️ | | | |
|
||||||
|
| add-password | | | ✔️ | | | | | | | ✔️ | |
|
||||||
|
| add-watermark | | | ✔️ | | | | | | | ✔️ | |
|
||||||
|
| cert-sign | | | ✔️ | | | | | | | ✔️ | |
|
||||||
|
| change-permissions | | | ✔️ | | | | | | | ✔️ | |
|
||||||
|
| remove-password | | | ✔️ | | | | | | | ✔️ | |
|
||||||
|
| sanitize-pdf | | | ✔️ | | | | | | | ✔️ | |
|
||||||
|
| add-image | | | | ✔️ | | | | | | ✔️ | |
|
||||||
|
| add-page-numbers | | | | ✔️ | | | | | | ✔️ | |
|
||||||
|
| auto-rename | | | | ✔️ | | | | | | ✔️ | |
|
||||||
|
| change-metadata | | | | ✔️ | | | | | | ✔️ | |
|
||||||
|
| compare | | | | ✔️ | | | | | | | ✔️ |
|
||||||
|
| compress-pdf | | | | ✔️ | ✔️ | | | | ✔️ | | |
|
||||||
|
| extract-image-scans | | | | ✔️ | ✔️ | ✔️ | ✔️ | | | | |
|
||||||
|
| extract-images | | | | ✔️ | | | | | | ✔️ | |
|
||||||
|
| flatten | | | | ✔️ | | | | | | | ✔️ |
|
||||||
|
| get-info-on-pdf | | | | ✔️ | | | | | | ✔️ | |
|
||||||
|
| ocr-pdf | | | | ✔️ | ✔️ | | | | ✔️ | | |
|
||||||
|
| remove-blanks | | | | ✔️ | ✔️ | ✔️ | ✔️ | | | | |
|
||||||
|
| repair | | | | ✔️ | ✔️ | | | ✔️ | | | |
|
||||||
|
| show-javascript | | | | ✔️ | | | | | | | ✔️ |
|
||||||
|
| sign | | | | ✔️ | | | | | | | ✔️ |
|
||||||
@@ -8,7 +8,7 @@ Fork Stirling-PDF and make a new branch out of Main
|
|||||||
|
|
||||||
Then add reference to the language in the navbar by adding a new language entry to the dropdown
|
Then add reference to the language in the navbar by adding a new language entry to the dropdown
|
||||||
|
|
||||||
https://github.com/Frooodle/Stirling-PDF/blob/main/src/main/resources/templates/fragments/navbar.html#L306
|
https://github.com/Frooodle/Stirling-PDF/blob/main/src/main/resources/templates/fragments/languages.html
|
||||||
and add a flag svg file to
|
and add a flag svg file to
|
||||||
https://github.com/Frooodle/Stirling-PDF/tree/main/src/main/resources/static/images/flags
|
https://github.com/Frooodle/Stirling-PDF/tree/main/src/main/resources/static/images/flags
|
||||||
Any SVG flags are fine, i got most of mine from [here](https://flagicons.lipis.dev/)
|
Any SVG flags are fine, i got most of mine from [here](https://flagicons.lipis.dev/)
|
||||||
@@ -25,7 +25,7 @@ The data-language-code is the code used to reference the file in the next step.
|
|||||||
|
|
||||||
Start by copying the existing english property file
|
Start by copying the existing english property file
|
||||||
|
|
||||||
[https://github.com/Frooodle/Stirling-PDF/tree/langSetup/src/main/resources/messages_en_GB.properties](https://github.com/Frooodle/Stirling-PDF/blob/main/src/main/resources/messages_en_US.properties)
|
[https://github.com/Frooodle/Stirling-PDF/blob/main/src/main/resources/messages_en_GB.properties](https://github.com/Frooodle/Stirling-PDF/blob/main/src/main/resources/messages_en_GB.properties)
|
||||||
|
|
||||||
Copy and rename it to messages_{your data-language-code here}.properties, in the polish example you would set the name to messages_pl_PL.properties
|
Copy and rename it to messages_{your data-language-code here}.properties, in the polish example you would set the name to messages_pl_PL.properties
|
||||||
|
|
||||||
|
|||||||
@@ -3,7 +3,7 @@
|
|||||||
This document provides instructions on how to add additional language packs for the OCR tab in Stirling-PDF, both inside and outside of Docker.
|
This document provides instructions on how to add additional language packs for the OCR tab in Stirling-PDF, both inside and outside of Docker.
|
||||||
|
|
||||||
## How does the OCR Work
|
## How does the OCR Work
|
||||||
Stirling-PDF uses OCRmyPDF which in turn uses tesseract for its text recognition.
|
Stirling-PDF uses [OCRmyPDF](https://github.com/ocrmypdf/OCRmyPDF) which in turn uses tesseract for its text recognition.
|
||||||
All credit goes to them for this awesome work!
|
All credit goes to them for this awesome work!
|
||||||
|
|
||||||
## Language Packs
|
## Language Packs
|
||||||
|
|||||||
@@ -123,7 +123,7 @@ This folder is required for the python scripts using OpenCV
|
|||||||
|
|
||||||
```bash
|
```bash
|
||||||
sudo mkdir /opt/Stirling-PDF &&\
|
sudo mkdir /opt/Stirling-PDF &&\
|
||||||
sudo mv ./build/libs/S-PDF-*.jar /opt/Stirling-PDF/ &&\
|
sudo mv ./build/libs/Stirling-PDF-*.jar /opt/Stirling-PDF/ &&\
|
||||||
sudo mv scripts /opt/Stirling-PDF/ &&\
|
sudo mv scripts /opt/Stirling-PDF/ &&\
|
||||||
echo "Scripts installed."
|
echo "Scripts installed."
|
||||||
```
|
```
|
||||||
|
|||||||
221
README.md
221
README.md
@@ -8,6 +8,8 @@
|
|||||||
[](https://www.paypal.com/paypalme/froodleplex)
|
[](https://www.paypal.com/paypalme/froodleplex)
|
||||||
[](https://github.com/sponsors/Frooodle)
|
[](https://github.com/sponsors/Frooodle)
|
||||||
|
|
||||||
|
[](https://cloud.digitalocean.com/apps/new?repo=https://github.com/Frooodle/Stirling-PDF/tree/digitalOcean&refcode=c3210994b1af)
|
||||||
|
|
||||||
This is a powerful locally hosted web based PDF manipulation tool using docker that allows you to perform various operations on PDF files, such as splitting merging, converting, reorganizing, adding images, rotating, compressing, and more. This locally hosted web application started as a 100% ChatGPT-made application and has evolved to include a wide range of features to handle all your PDF needs.
|
This is a powerful locally hosted web based PDF manipulation tool using docker that allows you to perform various operations on PDF files, such as splitting merging, converting, reorganizing, adding images, rotating, compressing, and more. This locally hosted web application started as a 100% ChatGPT-made application and has evolved to include a wide range of features to handle all your PDF needs.
|
||||||
|
|
||||||
Stirling PDF makes no outbound calls for any record keeping or tracking.
|
Stirling PDF makes no outbound calls for any record keeping or tracking.
|
||||||
@@ -17,35 +19,66 @@ Any file which has been downloaded by the user will have already been deleted fr
|
|||||||
|
|
||||||
Feel free to request any features or bug fixes either in github issues or our [Discord](https://discord.gg/Cn8pWhQRxZ)
|
Feel free to request any features or bug fixes either in github issues or our [Discord](https://discord.gg/Cn8pWhQRxZ)
|
||||||
|
|
||||||
|
|
||||||

|

|
||||||
|
|
||||||
## Features
|
## Features
|
||||||
- Full interactive GUI for merging/splitting/rotating/moving PDFs and their pages.
|
|
||||||
- Split PDFs into multiple files at specified page numbers or extract all pages as individual files.
|
|
||||||
- Merge multiple PDFs together into a single resultant file
|
|
||||||
- Convert PDFs to and from images
|
|
||||||
- Reorganize PDF pages into different orders.
|
|
||||||
- Add/Generate signatures
|
|
||||||
- Flatten PDFs
|
|
||||||
- Repair PDFs
|
|
||||||
- Detect and remove blank pages
|
|
||||||
- Compare 2 PDFs and show differences in text
|
|
||||||
- Add images to PDFs
|
|
||||||
- Rotating PDFs in 90 degree increments.
|
|
||||||
- Compressing PDFs to decrease their filesize. (Using OCRMyPDF)
|
|
||||||
- Add and remove passwords
|
|
||||||
- Set PDF Permissions
|
|
||||||
- Add watermark(s)
|
|
||||||
- Convert Any common file to PDF (using LibreOffice)
|
|
||||||
- Convert PDF to Word/Powerpoint/Others (using LibreOffice)
|
|
||||||
- Extract images from PDF
|
|
||||||
- OCR on PDF (Using OCRMyPDF)
|
|
||||||
- Edit metadata
|
|
||||||
- Dark mode support.
|
- Dark mode support.
|
||||||
- Custom download options (see [here](https://github.com/Frooodle/Stirling-PDF/blob/main/images/settings.png) for example)
|
- Custom download options (see [here](https://github.com/Frooodle/Stirling-PDF/blob/main/images/settings.png) for example)
|
||||||
- Parallel file processing and downloads
|
- Parallel file processing and downloads
|
||||||
- API for integration with external scripts
|
- API for integration with external scripts
|
||||||
|
- Optional Login and Authentication support (see [here](https://github.com/Frooodle/Stirling-PDF/tree/main#login-authentication) for documentation)
|
||||||
|
|
||||||
|
|
||||||
|
## **PDF Features**
|
||||||
|
|
||||||
|
### **Page Operations**
|
||||||
|
- Full interactive GUI for merging/splitting/rotating/moving PDFs and their pages.
|
||||||
|
- Merge multiple PDFs together into a single resultant file.
|
||||||
|
- Split PDFs into multiple files at specified page numbers or extract all pages as individual files.
|
||||||
|
- Reorganize PDF pages into different orders.
|
||||||
|
- Rotate PDFs in 90-degree increments.
|
||||||
|
- Remove pages.
|
||||||
|
- Multi-page layout (Format PDFs into a multi-paged page).
|
||||||
|
- Scale page contents size by set %.
|
||||||
|
- Adjust Contrast.
|
||||||
|
- Crop PDF.
|
||||||
|
- Auto Split PDF (With physically scanned page dividers).
|
||||||
|
- Extract page(s).
|
||||||
|
- Convert PDF to a single page.
|
||||||
|
|
||||||
|
### **Conversion Operations**
|
||||||
|
- Convert PDFs to and from images.
|
||||||
|
- Convert any common file to PDF (using LibreOffice).
|
||||||
|
- Convert PDF to Word/Powerpoint/Others (using LibreOffice).
|
||||||
|
- Convert HTML to PDF.
|
||||||
|
- URL to PDF.
|
||||||
|
- Markdown to PDF.
|
||||||
|
|
||||||
|
### **Security & Permissions**
|
||||||
|
- Add and remove passwords.
|
||||||
|
- Change/set PDF Permissions.
|
||||||
|
- Add watermark(s).
|
||||||
|
- Certify/sign PDFs.
|
||||||
|
- Sanitize PDFs.
|
||||||
|
- Auto-redact text.
|
||||||
|
|
||||||
|
### **Other Operations**
|
||||||
|
- Add/Generate/Write signatures.
|
||||||
|
- Repair PDFs.
|
||||||
|
- Detect and remove blank pages.
|
||||||
|
- Compare 2 PDFs and show differences in text.
|
||||||
|
- Add images to PDFs.
|
||||||
|
- Compress PDFs to decrease their filesize (Using OCRMyPDF).
|
||||||
|
- Extract images from PDF.
|
||||||
|
- Extract images from Scans.
|
||||||
|
- Add page numbers.
|
||||||
|
- Auto rename file by detecting PDF header text.
|
||||||
|
- OCR on PDF (Using OCRMyPDF).
|
||||||
|
- PDF/A conversion (Using OCRMyPDF).
|
||||||
|
- Edit metadata.
|
||||||
|
- Flatten PDFs.
|
||||||
|
- Get all information on a PDF to view or export as JSON.
|
||||||
|
|
||||||
|
|
||||||
For a overview of the tasks and the technology each uses please view [groups.md](https://github.com/Frooodle/Stirling-PDF/blob/main/Groups.md)
|
For a overview of the tasks and the technology each uses please view [groups.md](https://github.com/Frooodle/Stirling-PDF/blob/main/Groups.md)
|
||||||
Hosted instance/demo of the app can be seen [here](https://pdf.adminforge.de/) hosted by the team at adminforge.de
|
Hosted instance/demo of the app can be seen [here](https://pdf.adminforge.de/) hosted by the team at adminforge.de
|
||||||
@@ -68,42 +101,42 @@ Please view https://github.com/Frooodle/Stirling-PDF/blob/main/LocalRunGuide.md
|
|||||||
### Docker
|
### Docker
|
||||||
https://hub.docker.com/r/frooodle/s-pdf
|
https://hub.docker.com/r/frooodle/s-pdf
|
||||||
|
|
||||||
|
Stirling PDF has 3 different versions, a Full version, Lite, and ultra-Lite. Depending on the types of features you use you may want a smaller image to save on space.
|
||||||
|
To see what the different versions offer please look at our [version mapping](https://github.com/Frooodle/Stirling-PDF/blob/main/Version-groups.md)
|
||||||
|
For people that don't mind about space optimization just use the latest tag.
|
||||||
|

|
||||||
|

|
||||||
|

|
||||||
|
|
||||||
Docker Run
|
Docker Run
|
||||||
```
|
```
|
||||||
docker run -d \
|
docker run -d \
|
||||||
-p 8080:8080 \
|
-p 8080:8080 \
|
||||||
-v /location/of/trainingData:/usr/share/tesseract-ocr/4.00/tessdata \
|
-v /location/of/trainingData:/usr/share/tesseract-ocr/4.00/tessdata \
|
||||||
|
-v /location/of/extraConfigs:/configs \
|
||||||
|
-e DOCKER_ENABLE_SECURITY=false \
|
||||||
--name stirling-pdf \
|
--name stirling-pdf \
|
||||||
frooodle/s-pdf
|
frooodle/s-pdf:latest
|
||||||
|
|
||||||
|
|
||||||
Can also add these for customisation but are not required
|
Can also add these for customisation but are not required
|
||||||
-e APP_HOME_NAME="Stirling PDF" \
|
|
||||||
-e APP_HOME_DESCRIPTION="Your locally hosted one-stop-shop for all your PDF needs." \
|
-v /location/of/customFiles:/customFiles \
|
||||||
-e APP_NAVBAR_NAME="Stirling PDF" \
|
|
||||||
-e ALLOW_GOOGLE_VISIBILITY="true" \
|
|
||||||
-e APP_ROOT_PATH="/" \
|
|
||||||
-e APP_LOCALE="en_GB" \
|
|
||||||
```
|
```
|
||||||
Docker Compose
|
Docker Compose
|
||||||
```
|
```
|
||||||
version: '3.3'
|
version: '3.3'
|
||||||
services:
|
services:
|
||||||
stirling-pdf:
|
stirling-pdf:
|
||||||
image: frooodle/s-pdf
|
image: frooodle/s-pdf:latest
|
||||||
ports:
|
ports:
|
||||||
- '8080:8080'
|
- '8080:8080'
|
||||||
volumes:
|
volumes:
|
||||||
- /location/of/trainingData:/usr/share/tesseract-ocr/4.00/tessdata #Required for extra OCR languages
|
- /location/of/trainingData:/usr/share/tesseract-ocr/4.00/tessdata #Required for extra OCR languages
|
||||||
# - /location/of/extraConfigs:/configs
|
- /location/of/extraConfigs:/configs
|
||||||
# environment:
|
# - /location/of/customFiles:/customFiles/
|
||||||
# APP_LOCALE: en_GB
|
environment:
|
||||||
# APP_HOME_NAME: Stirling PDF
|
- DOCKER_ENABLE_SECURITY=false
|
||||||
# APP_HOME_DESCRIPTION: Your locally hosted one-stop-shop for all your PDF needs.
|
|
||||||
# APP_NAVBAR_NAME: Stirling PDF
|
|
||||||
# APP_ROOT_PATH: /
|
|
||||||
# ALLOW_GOOGLE_VISIBILITY: true
|
|
||||||
|
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
@@ -111,8 +144,9 @@ services:
|
|||||||
Please view https://github.com/Frooodle/Stirling-PDF/blob/main/HowToUseOCR.md
|
Please view https://github.com/Frooodle/Stirling-PDF/blob/main/HowToUseOCR.md
|
||||||
|
|
||||||
## Want to add your own language?
|
## Want to add your own language?
|
||||||
Stirling PDF currently supports
|
Stirling PDF currently supports 18!
|
||||||
- English (English) (en_GB)
|
- English (English) (en_GB)
|
||||||
|
- English (US) (en_US)
|
||||||
- Arabic (العربية) (ar_AR)
|
- Arabic (العربية) (ar_AR)
|
||||||
- German (Deutsch) (de_DE)
|
- German (Deutsch) (de_DE)
|
||||||
- French (Français) (fr_FR)
|
- French (Français) (fr_FR)
|
||||||
@@ -124,52 +158,111 @@ Stirling PDF currently supports
|
|||||||
- Polish (Polski) (pl_PL)
|
- Polish (Polski) (pl_PL)
|
||||||
- Romanian (Română) (ro_RO)
|
- Romanian (Română) (ro_RO)
|
||||||
- Korean (한국어) (ko_KR)
|
- Korean (한국어) (ko_KR)
|
||||||
|
- Portuguese Brazilian (Português) (pt_BR)
|
||||||
|
- Russian (Русский) (ru_RU)
|
||||||
|
- Basque (Euskara) (eu_ES)
|
||||||
|
- Japanese (日本語) (ja_JP)
|
||||||
|
- Dutch (Nederlands) (nl_NL)
|
||||||
|
|
||||||
If you want to add your own language to Stirling-PDF please refer
|
If you want to add your own language to Stirling-PDF please refer
|
||||||
https://github.com/Frooodle/Stirling-PDF/blob/main/HowToAddNewLanguage.md
|
https://github.com/Frooodle/Stirling-PDF/blob/main/HowToAddNewLanguage.md
|
||||||
|
|
||||||
And please create a PR to merge it back in so others can use it!
|
And please create a PR to merge it back in so others can use it!
|
||||||
|
|
||||||
Also please note as i add new features i will google translate existing languages so that they dont lose support. This could mean that new features need grammer corrections as added.
|
|
||||||
|
|
||||||
## How to View
|
## How to View
|
||||||
1. Open a web browser and navigate to `http://localhost:8080/`
|
1. Open a web browser and navigate to `http://localhost:8080/`
|
||||||
2. Use the application by following the instructions on the website.
|
2. Use the application by following the instructions on the website.
|
||||||
|
|
||||||
|
|
||||||
## Customize App
|
## Customisation
|
||||||
Stirling PDF allows easy customization of the visible application name.
|
Stirling PDF allows easy customization of the app.
|
||||||
Simply use environment variables APP_HOME_NAME, APP_HOME_DESCRIPTION and APP_NAVBAR_NAME with Docker or Java.
|
Includes things like
|
||||||
If running Java directly, you can also pass these as properties using -D arguments.
|
- Custom application name
|
||||||
|
- Custom slogans, icons, images, and even custom HTML (via file overrides)
|
||||||
|
|
||||||
Using the same method you can also change
|
|
||||||
|
|
||||||
- The default language by providing APP_LOCALE with values like de-DE fr-FR or ar-AR (Note the - character not _ ) to select your default language (Will always default to English on invalid locale) Current accepted locales can be seen above in the Want to add your own language section
|
There are two options for this, either using the generated settings file ``settings.yml``
|
||||||
- Enable/Disable search engine visiblility with ALLOW_GOOGLE_VISIBILITY with true / false values. Default disable visiblility.
|
This file is located in the ``/configs`` directory and follows standard YAML formatting
|
||||||
- Change root URI for Stirling-PDF ie change server.com/ to server.com/pdf-app by running APP_ROOT_PATH as pdf-app
|
|
||||||
- Disable and remove endpoints and functionality from Stirling-PDF. Currently the endpoints ENDPOINTS_TO_REMOVE and GROUPS_TO_REMOVE can include comma seperated lists of endpoints and groups to disable as example ENDPOINTS_TO_REMOVE=img-to-pdf,remove-pages would disable both image to pdf and remove pages, GROUPS_TO_REMOVE=LibreOffice Would disable all things that use LibreOffice. You can see a list of all endpoints and groups [here](https://github.com/Frooodle/Stirling-PDF/blob/main/groups.md)
|
|
||||||
|
|
||||||
|
Environment variables are also supported and would override the settings file
|
||||||
|
For example in the settings.yml you have
|
||||||
|
```
|
||||||
|
system:
|
||||||
|
defaultLocale: 'en-US'
|
||||||
|
```
|
||||||
|
|
||||||
|
To have this via an environment variable you would have ``SYSTEM_DEFAULTLOCALE``
|
||||||
|
|
||||||
|
The Current list of settings is
|
||||||
|
```
|
||||||
|
security:
|
||||||
|
enableLogin: false # set to 'true' to enable login
|
||||||
|
csrfDisabled: true
|
||||||
|
|
||||||
|
system:
|
||||||
|
defaultLocale: 'en-US' # Set the default language (e.g. 'de-DE', 'fr-FR', etc)
|
||||||
|
googlevisibility: false # 'true' to allow Google visibility (via robots.txt), 'false' to disallow
|
||||||
|
customStaticFilePath: '/customFiles/static/' # Directory path for custom static files
|
||||||
|
|
||||||
|
#ui:
|
||||||
|
# appName: exampleAppName # Application's visible name
|
||||||
|
# homeDescription: I am a description # Short description or tagline shown on homepage.
|
||||||
|
# appNameNavbar: navbarName # Name displayed on the navigation bar
|
||||||
|
|
||||||
|
endpoints:
|
||||||
|
toRemove: [] # List endpoints to disable (e.g. ['img-to-pdf', 'remove-pages'])
|
||||||
|
groupsToRemove: [] # List groups to disable (e.g. ['LibreOffice'])
|
||||||
|
|
||||||
|
metrics:
|
||||||
|
enabled: true # 'true' to enable Info APIs endpoints (view http://localhost:8080/swagger-ui/index.html#/API to learn more), 'false' to disable
|
||||||
|
```
|
||||||
|
### Extra notes
|
||||||
|
- Endpoints. Currently, the endpoints ENDPOINTS_TO_REMOVE and GROUPS_TO_REMOVE can include comma separate lists of endpoints and groups to disable as example ENDPOINTS_TO_REMOVE=img-to-pdf,remove-pages would disable both image-to-pdf and remove pages, GROUPS_TO_REMOVE=LibreOffice Would disable all things that use LibreOffice. You can see a list of all endpoints and groups [here](https://github.com/Frooodle/Stirling-PDF/blob/main/groups.md)
|
||||||
|
- customStaticFilePath. Customise static files such as the app logo by placing files in the /customFiles/static/ directory. An example of customising app logo is placing a /customFiles/static/favicon.svg to override current SVG. This can be used to change any images/icons/css/fonts/js etc in Stirling-PDF
|
||||||
|
|
||||||
|
### Environment only parameters
|
||||||
|
- ``SYSTEM_ROOTURIPATH`` ie set to ``/pdf-app`` to Set the application's root URI to ``localhost:8080/pdf-app``
|
||||||
|
- ``SYSTEM_CONNECTIONTIMEOUTMINUTES`` to set custom connection timeout values
|
||||||
|
- ``DOCKER_ENABLE_SECURITY`` to tell docker to download security jar (required as true for auth login)
|
||||||
|
|
||||||
## API
|
## API
|
||||||
For those wanting to use Stirling-PDFs backend API to link with their own custom scripting to edit PDFs you can view all existing API documentation
|
For those wanting to use Stirling-PDFs backend API to link with their own custom scripting to edit PDFs you can view all existing API documentation
|
||||||
[here](https://app.swaggerhub.com/apis-docs/Frooodle/Stirling-PDF/) or navigate to /swagger-ui/index.html of your stirling-pdf instance for your versions documentation
|
[here](https://app.swaggerhub.com/apis-docs/Frooodle/Stirling-PDF/) or navigate to /swagger-ui/index.html of your stirling-pdf instance for your versions documentation (Or by following the API button in your settings of Stirling-PDF)
|
||||||
|
|
||||||
|
|
||||||
|
## Login authentication
|
||||||
|

|
||||||
|
### Prerequisites:
|
||||||
|
- User must have the folder ./configs volumed within docker so that it is retained during updates.
|
||||||
|
- Docker uses must download the security jar version by setting ``DOCKER_ENABLE_SECURITY`` to ``true`` in environment variables.
|
||||||
|
- Then either enable login via the settings.yml file or via setting ``SECURITY_ENABLE_LOGIN`` to ``true``
|
||||||
|
- Now the initial user will be generated with username ``admin`` and password ``stirling``. On login you will be forced to change the password to a new one. You can also use the environment variables ``SECURITY_INITIALLOGIN_USERNAME`` and ``SECURITY_INITIALLOGIN_PASSWORD`` to set your own straight away (Recommended to remove them after user creation).
|
||||||
|
|
||||||
|
Once the above has been done, on restart, a new stirling-pdf-DB.mv.db will show if everything worked.
|
||||||
|
|
||||||
|
When you login to Stirling PDF you will be redirected to /login page to login with those default credentials. After login everything should function as normal
|
||||||
|
|
||||||
|
To access your account settings go to Account settings in the settings cog menu (top right in navbar) This Account settings menu is also where you find your API key.
|
||||||
|
|
||||||
|
To add new users go to the bottom of Account settings and hit 'Admin Settings', here you can add new users. The different roles mentioned within this are for rate limiting. This is a Work in progress which will be expanding on more in future
|
||||||
|
|
||||||
|
For API usage you must provide a header with 'X-API-Key' and the associated API key for that user.
|
||||||
|
|
||||||
|
|
||||||
## FAQ
|
## FAQ
|
||||||
|
|
||||||
### Q1: Can you add authentication in Stirling PDF?
|
### Q1: What are your planned features?
|
||||||
There is no Auth within Stirling PDF and there is none planned. This feature will not be added. Instead we recommended you use trusted and secure authentication software like Authentik or Authelia.
|
|
||||||
|
|
||||||
### Q2: What are your planned features?
|
|
||||||
- Crop
|
|
||||||
- Progress bar/Tracking
|
- Progress bar/Tracking
|
||||||
- Full custom logic pipelines to combine multiple operations together.
|
- Full custom logic pipelines to combine multiple operations together.
|
||||||
- Folder support with auto scanning to perform operations on
|
- Folder support with auto scanning to perform operations on
|
||||||
- Redact sections of pages
|
- Redact text (Via UI not just automated way)
|
||||||
- Add page numbers
|
- Add Forms
|
||||||
- Auto rename (Renames file based on file title text)
|
- Annotations
|
||||||
- URL to PDF
|
- Multi page layout (Stich PDF pages together) support x rows y columns and custom page sizing
|
||||||
- Change contrast
|
- Fill forms mannual and automatic
|
||||||
|
|
||||||
### Q3: Why is my application downloading .htm files?
|
### Q2: Why is my application downloading .htm files?
|
||||||
This is a issue caused commonly by your NGINX congifuration. The default file upload size for NGINX is 1MB, you need to add the following in your Nginx sites-available file. client_max_body_size SIZE; Where "SIZE" is 50M for example for 50MB files.
|
This is a issue caused commonly by your NGINX congifuration. The default file upload size for NGINX is 1MB, you need to add the following in your Nginx sites-available file. ``client_max_body_size SIZE;`` Where "SIZE" is 50M for example for 50MB files.
|
||||||
|
|
||||||
|
### Q3: Why is my download timing out
|
||||||
|
NGINX has timeout values by default so if you are running Stirling-PDF behind NGINX you may need to set a timeout value such as adding the config ``proxy_read_timeout 3600;``
|
||||||
|
|||||||
59
Version-groups.md
Normal file
59
Version-groups.md
Normal file
@@ -0,0 +1,59 @@
|
|||||||
|
|Technology | Ultra-Lite | Lite | Full |
|
||||||
|
|----------------|:----------:|:----:|:----:|
|
||||||
|
| Java | ✔️ | ✔️ | ✔️ |
|
||||||
|
| JavaScript | ✔️ | ✔️ | ✔️ |
|
||||||
|
| Libre | | ✔️ | ✔️ |
|
||||||
|
| Python | | | ✔️ |
|
||||||
|
| OpenCV | | | ✔️ |
|
||||||
|
| OCRmyPDF | | | ✔️ |
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
Operation | Ultra-Lite | Lite | Full
|
||||||
|
--------------------|------------|------|-----
|
||||||
|
add-page-numbers | ✔️ | ✔️ | ✔️
|
||||||
|
add-password | ✔️ | ✔️ | ✔️
|
||||||
|
add-image | ✔️ | ✔️ | ✔️
|
||||||
|
add-watermark | ✔️ | ✔️ | ✔️
|
||||||
|
adjust-contrast | ✔️ | ✔️ | ✔️
|
||||||
|
auto-split-pdf | ✔️ | ✔️ | ✔️
|
||||||
|
auto-rename | ✔️ | ✔️ | ✔️
|
||||||
|
cert-sign | ✔️ | ✔️ | ✔️
|
||||||
|
crop | ✔️ | ✔️ | ✔️
|
||||||
|
change-metadata | ✔️ | ✔️ | ✔️
|
||||||
|
change-permissions | ✔️ | ✔️ | ✔️
|
||||||
|
compare | ✔️ | ✔️ | ✔️
|
||||||
|
extract-page | ✔️ | ✔️ | ✔️
|
||||||
|
extract-images | ✔️ | ✔️ | ✔️
|
||||||
|
flatten | ✔️ | ✔️ | ✔️
|
||||||
|
get-info-on-pdf | ✔️ | ✔️ | ✔️
|
||||||
|
img-to-pdf | ✔️ | ✔️ | ✔️
|
||||||
|
markdown-to-pdf | ✔️ | ✔️ | ✔️
|
||||||
|
merge-pdfs | ✔️ | ✔️ | ✔️
|
||||||
|
multi-page-layout | ✔️ | ✔️ | ✔️
|
||||||
|
pdf-organizer | ✔️ | ✔️ | ✔️
|
||||||
|
pdf-to-img | ✔️ | ✔️ | ✔️
|
||||||
|
pdf-to-single-page | ✔️ | ✔️ | ✔️
|
||||||
|
remove-pages | ✔️ | ✔️ | ✔️
|
||||||
|
remove-password | ✔️ | ✔️ | ✔️
|
||||||
|
rotate-pdf | ✔️ | ✔️ | ✔️
|
||||||
|
sanitize-pdf | ✔️ | ✔️ | ✔️
|
||||||
|
scale-pages | ✔️ | ✔️ | ✔️
|
||||||
|
sign | ✔️ | ✔️ | ✔️
|
||||||
|
show-javascript | ✔️ | ✔️ | ✔️
|
||||||
|
split-pdfs | ✔️ | ✔️ | ✔️
|
||||||
|
file-to-pdf | | ✔️ | ✔️
|
||||||
|
pdf-to-html | | ✔️ | ✔️
|
||||||
|
pdf-to-presentation | | ✔️ | ✔️
|
||||||
|
pdf-to-text | | ✔️ | ✔️
|
||||||
|
pdf-to-word | | ✔️ | ✔️
|
||||||
|
pdf-to-xml | | ✔️ | ✔️
|
||||||
|
repair | | ✔️ | ✔️
|
||||||
|
xlsx-to-pdf | | ✔️ | ✔️
|
||||||
|
compress-pdf | | | ✔️
|
||||||
|
extract-image-scans | | | ✔️
|
||||||
|
ocr-pdf | | | ✔️
|
||||||
|
pdf-to-pdfa | | | ✔️
|
||||||
|
remove-blanks | | | ✔️
|
||||||
85
build.gradle
85
build.gradle
@@ -1,44 +1,105 @@
|
|||||||
plugins {
|
plugins {
|
||||||
id 'java'
|
id 'java'
|
||||||
id 'org.springframework.boot' version '3.1.0'
|
id 'org.springframework.boot' version '3.1.2'
|
||||||
id 'io.spring.dependency-management' version '1.1.0'
|
id 'io.spring.dependency-management' version '1.1.3'
|
||||||
id 'org.springdoc.openapi-gradle-plugin' version '1.6.0'
|
id 'org.springdoc.openapi-gradle-plugin' version '1.6.0'
|
||||||
id "io.swagger.swaggerhub" version "1.1.0"
|
id "io.swagger.swaggerhub" version "1.2.0"
|
||||||
|
id 'edu.sc.seis.launch4j' version '3.0.5'
|
||||||
}
|
}
|
||||||
|
|
||||||
group = 'stirling.software'
|
group = 'stirling.software'
|
||||||
version = '0.10.1'
|
version = '0.14.5'
|
||||||
sourceCompatibility = '17'
|
sourceCompatibility = '17'
|
||||||
|
|
||||||
repositories {
|
repositories {
|
||||||
mavenCentral()
|
mavenCentral()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
sourceSets {
|
||||||
|
main {
|
||||||
|
java {
|
||||||
|
if (System.getenv('DOCKER_ENABLE_SECURITY') == 'false') {
|
||||||
|
exclude 'stirling/software/SPDF/config/security/**'
|
||||||
|
exclude 'stirling/software/SPDF/controller/api/UserController.java'
|
||||||
|
exclude 'stirling/software/SPDF/controller/web/AccountWebController.java'
|
||||||
|
exclude 'stirling/software/SPDF/model/ApiKeyAuthenticationToken.java'
|
||||||
|
exclude 'stirling/software/SPDF/model/Authority.java'
|
||||||
|
exclude 'stirling/software/SPDF/model/PersistentLogin.java'
|
||||||
|
exclude 'stirling/software/SPDF/model/User.java'
|
||||||
|
exclude 'stirling/software/SPDF/repository/**'
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
openApi {
|
openApi {
|
||||||
apiDocsUrl = "http://localhost:8080/v3/api-docs"
|
apiDocsUrl = "http://localhost:8080/v1/api-docs"
|
||||||
outputDir = file("$projectDir")
|
outputDir = file("$projectDir")
|
||||||
outputFileName = "SwaggerDoc.json"
|
outputFileName = "SwaggerDoc.json"
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
launch4j {
|
||||||
|
icon = "${projectDir}/src/main/resources/static/favicon.ico"
|
||||||
|
|
||||||
|
outfile="Stirling-PDF.exe"
|
||||||
|
headerType="console"
|
||||||
|
jarTask = tasks.bootJar
|
||||||
|
|
||||||
|
errTitle="Encountered error, Do you have Java 17?"
|
||||||
|
downloadUrl="https://download.oracle.com/java/17/latest/jdk-17_windows-x64_bin.exe"
|
||||||
|
variables=["BROWSER_OPEN=true"]
|
||||||
|
jreMinVersion="17"
|
||||||
|
|
||||||
|
mutexName="Stirling-PDF"
|
||||||
|
windowTitle="Stirling-PDF"
|
||||||
|
|
||||||
|
messagesStartupError="An error occurred while starting Stirling-PDF"
|
||||||
|
//messagesJreNotFoundError="This application requires a Java Runtime Environment, Please download Java 17."
|
||||||
|
messagesJreVersionError="You are running the wrong version of Java, Please download Java 17."
|
||||||
|
messagesLauncherError="Java is corrupted. Please uninstall and then install Java 17."
|
||||||
|
messagesInstanceAlreadyExists="Stirling-PDF is already running."
|
||||||
|
}
|
||||||
|
|
||||||
dependencies {
|
dependencies {
|
||||||
implementation 'org.springframework.boot:spring-boot-starter-web:3.1.0'
|
implementation 'org.yaml:snakeyaml:2.1'
|
||||||
implementation 'org.springframework.boot:spring-boot-starter-thymeleaf:3.1.0'
|
implementation 'org.springframework.boot:spring-boot-starter-web:3.1.2'
|
||||||
testImplementation 'org.springframework.boot:spring-boot-starter-test:3.1.0'
|
implementation 'org.springframework.boot:spring-boot-starter-thymeleaf:3.1.2'
|
||||||
|
|
||||||
|
if (System.getenv('DOCKER_ENABLE_SECURITY') != 'false') {
|
||||||
|
implementation 'org.springframework.boot:spring-boot-starter-security:3.1.2'
|
||||||
|
implementation 'org.thymeleaf.extras:thymeleaf-extras-springsecurity5:3.1.2.RELEASE'
|
||||||
|
implementation "org.springframework.boot:spring-boot-starter-data-jpa"
|
||||||
|
implementation "com.h2database:h2"
|
||||||
|
}
|
||||||
|
|
||||||
|
testImplementation 'org.springframework.boot:spring-boot-starter-test:3.1.4'
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
// https://mvnrepository.com/artifact/org.apache.pdfbox/jbig2-imageio
|
// https://mvnrepository.com/artifact/org.apache.pdfbox/jbig2-imageio
|
||||||
implementation group: 'org.apache.pdfbox', name: 'jbig2-imageio', version: '3.0.4'
|
implementation group: 'org.apache.pdfbox', name: 'jbig2-imageio', version: '3.0.4'
|
||||||
implementation 'commons-io:commons-io:2.11.0'
|
implementation 'commons-io:commons-io:2.13.0'
|
||||||
|
|
||||||
implementation 'org.springdoc:springdoc-openapi-starter-webmvc-ui:2.1.0'
|
implementation 'org.springdoc:springdoc-openapi-starter-webmvc-ui:2.2.0'
|
||||||
|
|
||||||
//general PDF
|
//general PDF
|
||||||
implementation 'org.apache.pdfbox:pdfbox:2.0.28'
|
implementation 'org.apache.pdfbox:pdfbox:2.0.29'
|
||||||
|
implementation 'org.apache.pdfbox:xmpbox:2.0.29'
|
||||||
implementation 'org.bouncycastle:bcprov-jdk15on:1.70'
|
implementation 'org.bouncycastle:bcprov-jdk15on:1.70'
|
||||||
implementation 'org.bouncycastle:bcpkix-jdk15on:1.70'
|
implementation 'org.bouncycastle:bcpkix-jdk15on:1.70'
|
||||||
implementation 'com.itextpdf:itext7-core:7.2.5'
|
|
||||||
implementation 'org.springframework.boot:spring-boot-starter-actuator'
|
implementation 'org.springframework.boot:spring-boot-starter-actuator'
|
||||||
implementation 'io.micrometer:micrometer-core'
|
implementation 'io.micrometer:micrometer-core'
|
||||||
|
implementation group: 'com.google.zxing', name: 'core', version: '3.5.2'
|
||||||
|
// https://mvnrepository.com/artifact/org.commonmark/commonmark
|
||||||
|
implementation 'org.commonmark:commonmark:0.21.0'
|
||||||
|
// https://mvnrepository.com/artifact/com.github.vladimir-bukhtoyarov/bucket4j-core
|
||||||
|
implementation 'com.github.vladimir-bukhtoyarov:bucket4j-core:7.6.0'
|
||||||
|
|
||||||
developmentOnly("org.springframework.boot:spring-boot-devtools")
|
developmentOnly("org.springframework.boot:spring-boot-devtools")
|
||||||
|
compileOnly 'org.projectlombok:lombok:1.18.28'
|
||||||
|
annotationProcessor 'org.projectlombok:lombok:1.18.28'
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
15
chart/stirling-pdf/Chart.yaml
Normal file
15
chart/stirling-pdf/Chart.yaml
Normal file
@@ -0,0 +1,15 @@
|
|||||||
|
apiVersion: v2
|
||||||
|
appVersion: 0.14.2
|
||||||
|
description: locally hosted web application that allows you to perform various operations on PDF files
|
||||||
|
home: https://github.com/Frooodle/Stirling-PDF
|
||||||
|
keywords:
|
||||||
|
- stirling-pdf
|
||||||
|
- helm
|
||||||
|
- charts repo
|
||||||
|
maintainers:
|
||||||
|
- name: Frooodle
|
||||||
|
url: https://github.com/Frooodle/Stirling-PDF
|
||||||
|
name: stirling-pdf
|
||||||
|
sources:
|
||||||
|
- https://github.com/Frooodle/Stirling-PDF
|
||||||
|
version: 1.0.0
|
||||||
30
chart/stirling-pdf/templates/NOTES.txt
Normal file
30
chart/stirling-pdf/templates/NOTES.txt
Normal file
@@ -0,0 +1,30 @@
|
|||||||
|
** Please be patient while the chart is being deployed **
|
||||||
|
|
||||||
|
Get the stirlingpdf URL by running:
|
||||||
|
|
||||||
|
{{- if contains "NodePort" .Values.service.type }}
|
||||||
|
|
||||||
|
export NODE_PORT=$(kubectl get --namespace {{ .Release.Namespace }} -o jsonpath="{.spec.ports[0].nodePort}" services {{ template "stirlingpdf.fullname" . }})
|
||||||
|
export NODE_IP=$(kubectl get nodes --namespace {{ .Release.Namespace }} -o jsonpath="{.items[0].status.addresses[0].address}")
|
||||||
|
echo http://$NODE_IP:$NODE_PORT/
|
||||||
|
|
||||||
|
{{- else if contains "LoadBalancer" .Values.service.type }}
|
||||||
|
|
||||||
|
** Please ensure an external IP is associated to the {{ template "stirlingpdf.fullname" . }} service before proceeding **
|
||||||
|
** Watch the status using: kubectl get svc --namespace {{ .Release.Namespace }} -w {{ template "stirlingpdf.fullname" . }} **
|
||||||
|
|
||||||
|
export SERVICE_IP=$(kubectl get svc --namespace {{ .Release.Namespace }} {{ template "stirlingpdf.fullname" . }} -o jsonpath='{.status.loadBalancer.ingress[0].ip}')
|
||||||
|
echo http://$SERVICE_IP:{{ .Values.service.externalPort }}/
|
||||||
|
|
||||||
|
OR
|
||||||
|
|
||||||
|
export SERVICE_HOST=$(kubectl get svc --namespace {{ .Release.Namespace }} {{ template "stirlingpdf.fullname" . }} -o jsonpath='{.status.loadBalancer.ingress[0].hostname}')
|
||||||
|
echo http://$SERVICE_HOST:{{ .Values.service.externalPort }}/
|
||||||
|
|
||||||
|
{{- else if contains "ClusterIP" .Values.service.type }}
|
||||||
|
|
||||||
|
export POD_NAME=$(kubectl get pods --namespace {{ .Release.Namespace }} -l "app={{ template "stirlingpdf.name" . }}" -l "release={{ .Release.Name }}" -o jsonpath="{.items[0].metadata.name}")
|
||||||
|
echo http://127.0.0.1:8080/
|
||||||
|
kubectl port-forward $POD_NAME 8080:8080 --namespace {{ .Release.Namespace }}
|
||||||
|
|
||||||
|
{{- end }}
|
||||||
129
chart/stirling-pdf/templates/_helpers.tpl
Normal file
129
chart/stirling-pdf/templates/_helpers.tpl
Normal file
@@ -0,0 +1,129 @@
|
|||||||
|
{{/*
|
||||||
|
Expand the name of the chart.
|
||||||
|
*/}}
|
||||||
|
{{- define "stirlingpdf.name" -}}
|
||||||
|
{{- default .Chart.Name .Values.nameOverride | trunc 63 | trimSuffix "-" }}
|
||||||
|
{{- end }}
|
||||||
|
|
||||||
|
{{/*
|
||||||
|
Create a default fully qualified app name.
|
||||||
|
We truncate at 63 chars because some Kubernetes name fields are limited to this (by the DNS naming spec).
|
||||||
|
If release name contains chart name it will be used as a full name.
|
||||||
|
*/}}
|
||||||
|
{{- define "stirlingpdf.fullname" -}}
|
||||||
|
{{- if .Values.fullnameOverride }}
|
||||||
|
{{- .Values.fullnameOverride | trunc 63 | trimSuffix "-" }}
|
||||||
|
{{- else }}
|
||||||
|
{{- $name := default .Chart.Name .Values.nameOverride }}
|
||||||
|
{{- if contains $name .Release.Name }}
|
||||||
|
{{- .Release.Name | trunc 63 | trimSuffix "-" }}
|
||||||
|
{{- else }}
|
||||||
|
{{- printf "%s-%s" .Release.Name $name | trunc 63 | trimSuffix "-" }}
|
||||||
|
{{- end }}
|
||||||
|
{{- end }}
|
||||||
|
{{- end }}
|
||||||
|
|
||||||
|
{{- /*
|
||||||
|
Create chart name and version as used by the chart label.
|
||||||
|
|
||||||
|
It does minimal escaping for use in Kubernetes labels.
|
||||||
|
|
||||||
|
Example output:
|
||||||
|
|
||||||
|
stirlingpdf-0.4.5
|
||||||
|
*/ -}}
|
||||||
|
{{- define "stirlingpdf.chart" -}}
|
||||||
|
{{- printf "%s-%s" .Chart.Name .Chart.Version | replace "+" "_" | trunc 63 | trimSuffix "-" }}
|
||||||
|
{{- end -}}
|
||||||
|
|
||||||
|
{{/*
|
||||||
|
Common labels
|
||||||
|
*/}}
|
||||||
|
{{- define "stirlingpdf.labels" -}}
|
||||||
|
helm.sh/chart: {{ include "stirlingpdf.chart" . }}
|
||||||
|
{{ include "stirlingpdf.selectorLabels" . }}
|
||||||
|
{{- if .Chart.AppVersion }}
|
||||||
|
app.kubernetes.io/version: {{ .Chart.AppVersion | quote }}
|
||||||
|
{{- end }}
|
||||||
|
{{- if .Values.commonLabels}}
|
||||||
|
{{ toYaml .Values.commonLabels }}
|
||||||
|
{{- end }}
|
||||||
|
app.kubernetes.io/managed-by: {{ .Release.Service }}
|
||||||
|
{{- end }}
|
||||||
|
|
||||||
|
{{/*
|
||||||
|
Selector labels
|
||||||
|
*/}}
|
||||||
|
{{- define "stirlingpdf.selectorLabels" -}}
|
||||||
|
app.kubernetes.io/name: {{ include "stirlingpdf.name" . }}
|
||||||
|
app.kubernetes.io/instance: {{ .Release.Name }}
|
||||||
|
{{- end }}
|
||||||
|
|
||||||
|
{{/*
|
||||||
|
Create the name of the service account to use
|
||||||
|
*/}}
|
||||||
|
{{- define "stirlingpdf.serviceAccountName" -}}
|
||||||
|
{{- if .Values.serviceAccount.create }}
|
||||||
|
{{- default (include "stirlingpdf.fullname" .) .Values.serviceAccount.name }}
|
||||||
|
{{- else }}
|
||||||
|
{{- default "default" .Values.serviceAccount.name }}
|
||||||
|
{{- end }}
|
||||||
|
{{- end }}
|
||||||
|
|
||||||
|
{{/*
|
||||||
|
Return the proper image name to change the volume permissions
|
||||||
|
*/}}
|
||||||
|
{{- define "stirlingpdf.volumePermissions.image" -}}
|
||||||
|
{{- $registryName := .Values.volumePermissions.image.registry -}}
|
||||||
|
{{- $repositoryName := .Values.volumePermissions.image.repository -}}
|
||||||
|
{{- $tag := .Values.volumePermissions.image.tag | toString -}}
|
||||||
|
{{/*
|
||||||
|
Helm 2.11 supports the assignment of a value to a variable defined in a different scope,
|
||||||
|
but Helm 2.9 and 2.10 doesn't support it, so we need to implement this if-else logic.
|
||||||
|
Also, we can't use a single if because lazy evaluation is not an option
|
||||||
|
*/}}
|
||||||
|
{{- if .Values.global }}
|
||||||
|
{{- if .Values.global.imageRegistry }}
|
||||||
|
{{- printf "%s/%s:%s" .Values.global.imageRegistry $repositoryName $tag -}}
|
||||||
|
{{- else -}}
|
||||||
|
{{- printf "%s/%s:%s" $registryName $repositoryName $tag -}}
|
||||||
|
{{- end -}}
|
||||||
|
{{- else -}}
|
||||||
|
{{- printf "%s/%s:%s" $registryName $repositoryName $tag -}}
|
||||||
|
{{- end -}}
|
||||||
|
{{- end -}}
|
||||||
|
|
||||||
|
{{/*
|
||||||
|
Return the proper Docker Image Registry Secret Names
|
||||||
|
*/}}
|
||||||
|
{{- define "stirlingpdf.imagePullSecrets" -}}
|
||||||
|
{{/*
|
||||||
|
Helm 2.11 supports the assignment of a value to a variable defined in a different scope,
|
||||||
|
but Helm 2.9 and 2.10 does not support it, so we need to implement this if-else logic.
|
||||||
|
Also, we can not use a single if because lazy evaluation is not an option
|
||||||
|
*/}}
|
||||||
|
{{- if .Values.global }}
|
||||||
|
{{- if .Values.global.imagePullSecrets }}
|
||||||
|
imagePullSecrets:
|
||||||
|
{{- range .Values.global.imagePullSecrets }}
|
||||||
|
- name: {{ . }}
|
||||||
|
{{- end }}
|
||||||
|
{{- else if or .Values.image.pullSecrets .Values.volumePermissions.image.pullSecrets }}
|
||||||
|
imagePullSecrets:
|
||||||
|
{{- range .Values.image.pullSecrets }}
|
||||||
|
- name: {{ . }}
|
||||||
|
{{- end }}
|
||||||
|
{{- range .Values.volumePermissions.image.pullSecrets }}
|
||||||
|
- name: {{ . }}
|
||||||
|
{{- end }}
|
||||||
|
{{- end -}}
|
||||||
|
{{- else if or .Values.image.pullSecrets .Values.volumePermissions.image.pullSecrets }}
|
||||||
|
imagePullSecrets:
|
||||||
|
{{- range .Values.image.pullSecrets }}
|
||||||
|
- name: {{ . }}
|
||||||
|
{{- end }}
|
||||||
|
{{- range .Values.volumePermissions.image.pullSecrets }}
|
||||||
|
- name: {{ . }}
|
||||||
|
{{- end }}
|
||||||
|
{{- end -}}
|
||||||
|
{{- end -}}
|
||||||
129
chart/stirling-pdf/templates/deployment.yaml
Normal file
129
chart/stirling-pdf/templates/deployment.yaml
Normal file
@@ -0,0 +1,129 @@
|
|||||||
|
apiVersion: apps/v1
|
||||||
|
kind: Deployment
|
||||||
|
metadata:
|
||||||
|
name: {{ include "stirlingpdf.fullname" . }}
|
||||||
|
{{- with .Values.deployment.annotations }}
|
||||||
|
annotations:
|
||||||
|
{{- toYaml . | nindent 4 }}
|
||||||
|
{{- end }}
|
||||||
|
labels:
|
||||||
|
{{- include "stirlingpdf.labels" . | nindent 4 }}
|
||||||
|
{{- if .Values.deployment.labels }}
|
||||||
|
{{- toYaml .Values.deployment.labels | nindent 4 }}
|
||||||
|
{{- end }}
|
||||||
|
spec:
|
||||||
|
selector:
|
||||||
|
matchLabels:
|
||||||
|
{{- include "stirlingpdf.selectorLabels" . | nindent 6 }}
|
||||||
|
replicas: {{ .Values.replicaCount }}
|
||||||
|
strategy:
|
||||||
|
{{ toYaml .Values.strategy | indent 4 }}
|
||||||
|
revisionHistoryLimit: 10
|
||||||
|
template:
|
||||||
|
metadata:
|
||||||
|
{{- with .Values.podAnnotations }}
|
||||||
|
annotations:
|
||||||
|
{{- toYaml . | nindent 8 }}
|
||||||
|
{{- end }}
|
||||||
|
labels:
|
||||||
|
{{- include "stirlingpdf.selectorLabels" . | nindent 8 }}
|
||||||
|
{{- if .Values.podLabels }}
|
||||||
|
{{- toYaml .Values.podLabels | nindent 8 }}
|
||||||
|
{{- end }}
|
||||||
|
spec:
|
||||||
|
{{- if .Values.priorityClassName }}
|
||||||
|
priorityClassName: "{{ .Values.priorityClassName }}"
|
||||||
|
{{- end }}
|
||||||
|
{{- if .Values.securityContext.enabled }}
|
||||||
|
securityContext:
|
||||||
|
fsGroup: {{ .Values.securityContext.fsGroup }}
|
||||||
|
{{- if .Values.securityContext.runAsNonRoot }}
|
||||||
|
runAsNonRoot: {{ .Values.securityContext.runAsNonRoot }}
|
||||||
|
{{- end }}
|
||||||
|
{{- if .Values.securityContext.supplementalGroups }}
|
||||||
|
supplementalGroups: {{ .Values.securityContext.supplementalGroups }}
|
||||||
|
{{- end }}
|
||||||
|
{{- else if .Values.persistence.enabled }}
|
||||||
|
initContainers:
|
||||||
|
- name: volume-permissions
|
||||||
|
image: {{ template "stirlingpdf.volumePermissions.image" . }}
|
||||||
|
imagePullPolicy: "{{ .Values.volumePermissions.image.pullPolicy }}"
|
||||||
|
securityContext:
|
||||||
|
{{- toYaml .Values.containerSecurityContext | nindent 10 }}
|
||||||
|
command: ['sh', '-c', 'chown -R {{ .Values.securityContext.fsGroup }}:{{ .Values.securityContext.fsGroup }} {{ .Values.persistence.path }}']
|
||||||
|
volumeMounts:
|
||||||
|
- mountPath: {{ .Values.persistence.path }}
|
||||||
|
name: storage-volume
|
||||||
|
{{- end }}
|
||||||
|
{{- include "stirlingpdf.imagePullSecrets" . | indent 6 }}
|
||||||
|
containers:
|
||||||
|
- name: {{ .Chart.Name }}
|
||||||
|
image: {{ .Values.image.repository }}:{{ .Values.image.tag | default .Chart.AppVersion }}
|
||||||
|
imagePullPolicy: {{ .Values.image.pullPolicy }}
|
||||||
|
securityContext:
|
||||||
|
{{- toYaml .Values.containerSecurityContext | nindent 10 }}
|
||||||
|
{{- if .Values.envs }}
|
||||||
|
env:
|
||||||
|
{{ toYaml .Values.envs | indent 8 }}
|
||||||
|
{{- end }}
|
||||||
|
{{- if .Values.extraArgs }}
|
||||||
|
args:
|
||||||
|
{{ toYaml .Values.extraArgs | indent 8 }}
|
||||||
|
{{- end }}
|
||||||
|
ports:
|
||||||
|
- name: http
|
||||||
|
containerPort: 8080
|
||||||
|
livenessProbe:
|
||||||
|
httpGet:
|
||||||
|
path: /
|
||||||
|
port: http
|
||||||
|
{{ toYaml .Values.probes.livenessHttpGetConfig | indent 12 }}
|
||||||
|
{{ toYaml .Values.probes.liveness | indent 10 }}
|
||||||
|
readinessProbe:
|
||||||
|
httpGet:
|
||||||
|
path: /
|
||||||
|
port: http
|
||||||
|
{{ toYaml .Values.probes.readinessHttpGetConfig | indent 12 }}
|
||||||
|
{{ toYaml .Values.probes.readiness | indent 10 }}
|
||||||
|
volumeMounts:
|
||||||
|
{{- if .Values.deployment.extraVolumeMounts }}
|
||||||
|
{{- toYaml .Values.deployment.extraVolumeMounts | nindent 8 }}
|
||||||
|
{{- end }}
|
||||||
|
{{- if .Values.deployment.sidecarContainers }}
|
||||||
|
{{- range $name, $spec := .Values.deployment.sidecarContainers }}
|
||||||
|
- name: {{ $name }}
|
||||||
|
{{- toYaml $spec | nindent 8 }}
|
||||||
|
{{- end }}
|
||||||
|
{{- end }}
|
||||||
|
{{- with .Values.resources }}
|
||||||
|
resources:
|
||||||
|
{{ toYaml . | indent 10 }}
|
||||||
|
{{- end }}
|
||||||
|
{{- with .Values.nodeSelector }}
|
||||||
|
nodeSelector:
|
||||||
|
{{ toYaml . | indent 8 }}
|
||||||
|
{{- end }}
|
||||||
|
{{- with .Values.affinity }}
|
||||||
|
affinity:
|
||||||
|
{{ toYaml . | indent 8 }}
|
||||||
|
{{- end }}
|
||||||
|
{{- with .Values.tolerations }}
|
||||||
|
tolerations:
|
||||||
|
{{ toYaml . | indent 8 }}
|
||||||
|
{{- end }}
|
||||||
|
{{- if .Values.schedulerName }}
|
||||||
|
schedulerName: {{ .Values.schedulerName }}
|
||||||
|
{{- end }}
|
||||||
|
serviceAccountName: {{ include "stirlingpdf.serviceAccountName" . }}
|
||||||
|
automountServiceAccountToken: {{ .Values.serviceAccount.automountServiceAccountToken }}
|
||||||
|
volumes:
|
||||||
|
{{- if .Values.deployment.extraVolumes }}
|
||||||
|
{{- toYaml .Values.deployment.extraVolumes | nindent 6 }}
|
||||||
|
{{- end }}
|
||||||
|
- name: storage-volume
|
||||||
|
{{- if .Values.persistence.enabled }}
|
||||||
|
persistentVolumeClaim:
|
||||||
|
claimName: {{ .Values.persistence.existingClaim | default (include "stirlingpdf.fullname" .) }}
|
||||||
|
{{- else }}
|
||||||
|
emptyDir: {}
|
||||||
|
{{- end }}
|
||||||
85
chart/stirling-pdf/templates/ingress.yaml
Normal file
85
chart/stirling-pdf/templates/ingress.yaml
Normal file
@@ -0,0 +1,85 @@
|
|||||||
|
{{- if .Values.ingress.enabled }}
|
||||||
|
{{- $servicePort := .Values.service.externalPort -}}
|
||||||
|
{{- $serviceName := include "stirlingpdf.fullname" . -}}
|
||||||
|
{{- $ingressExtraPaths := .Values.ingress.extraPaths -}}
|
||||||
|
---
|
||||||
|
{{- if semverCompare "<1.14-0" .Capabilities.KubeVersion.GitVersion }}
|
||||||
|
apiVersion: extensions/v1beta1
|
||||||
|
{{- else if semverCompare "<1.19-0" .Capabilities.KubeVersion.GitVersion }}
|
||||||
|
apiVersion: networking.k8s.io/v1beta1
|
||||||
|
{{- else }}
|
||||||
|
apiVersion: networking.k8s.io/v1
|
||||||
|
{{- end }}
|
||||||
|
kind: Ingress
|
||||||
|
metadata:
|
||||||
|
name: {{ include "stirlingpdf.fullname" . }}
|
||||||
|
{{- with .Values.ingress.annotations }}
|
||||||
|
annotations:
|
||||||
|
{{- toYaml . | nindent 4 }}
|
||||||
|
{{- end }}
|
||||||
|
labels:
|
||||||
|
{{- include "stirlingpdf.labels" . | nindent 4 }}
|
||||||
|
{{- with .Values.ingress.labels }}
|
||||||
|
{{- toYaml . | nindent 4 }}
|
||||||
|
{{- end }}
|
||||||
|
spec:
|
||||||
|
{{- with .Values.ingress.ingressClassName }}
|
||||||
|
ingressClassName: {{ . }}
|
||||||
|
{{- end }}
|
||||||
|
rules:
|
||||||
|
{{- range .Values.ingress.hosts }}
|
||||||
|
- host: {{ .name }}
|
||||||
|
http:
|
||||||
|
paths:
|
||||||
|
{{- range $ingressExtraPaths }}
|
||||||
|
- path: {{ default "/" .path | quote }}
|
||||||
|
backend:
|
||||||
|
{{- if semverCompare "<1.19-0" $.Capabilities.KubeVersion.GitVersion }}
|
||||||
|
{{- if $.Values.service.servicename }}
|
||||||
|
serviceName: {{ $.Values.service.servicename }}
|
||||||
|
{{- else }}
|
||||||
|
serviceName: {{ default $serviceName .service }}
|
||||||
|
{{- end }}
|
||||||
|
servicePort: {{ default $servicePort .port }}
|
||||||
|
{{- else }}
|
||||||
|
service:
|
||||||
|
{{- if $.Values.service.servicename }}
|
||||||
|
name: {{ $.Values.service.servicename }}
|
||||||
|
{{- else }}
|
||||||
|
name: {{ default $serviceName .service }}
|
||||||
|
{{- end }}
|
||||||
|
port:
|
||||||
|
number: {{ default $servicePort .port }}
|
||||||
|
pathType: {{ default $.Values.ingress.pathType .pathType }}
|
||||||
|
{{- end }}
|
||||||
|
{{- end }}
|
||||||
|
- path: {{ default "/" .path | quote }}
|
||||||
|
backend:
|
||||||
|
{{- if semverCompare "<1.19-0" $.Capabilities.KubeVersion.GitVersion }}
|
||||||
|
{{- if $.Values.service.servicename }}
|
||||||
|
serviceName: {{ $.Values.service.servicename }}
|
||||||
|
{{- else }}
|
||||||
|
serviceName: {{ default $serviceName .service }}
|
||||||
|
{{- end }}
|
||||||
|
servicePort: {{ default $servicePort .servicePort }}
|
||||||
|
{{- else }}
|
||||||
|
service:
|
||||||
|
{{- if $.Values.service.servicename }}
|
||||||
|
name: {{ $.Values.service.servicename }}
|
||||||
|
{{- else }}
|
||||||
|
name: {{ default $serviceName .service }}
|
||||||
|
{{- end }}
|
||||||
|
port:
|
||||||
|
number: {{ default $servicePort .port }}
|
||||||
|
pathType: {{ $.Values.ingress.pathType }}
|
||||||
|
{{- end }}
|
||||||
|
{{- end }}
|
||||||
|
tls:
|
||||||
|
{{- range .Values.ingress.hosts }}
|
||||||
|
{{- if .tls }}
|
||||||
|
- hosts:
|
||||||
|
- {{ .name }}
|
||||||
|
secretName: {{ .tlsSecret }}
|
||||||
|
{{- end }}
|
||||||
|
{{- end }}
|
||||||
|
{{- end -}}
|
||||||
16
chart/stirling-pdf/templates/pv.yaml
Normal file
16
chart/stirling-pdf/templates/pv.yaml
Normal file
@@ -0,0 +1,16 @@
|
|||||||
|
{{- if .Values.persistence.pv.enabled -}}
|
||||||
|
apiVersion: v1
|
||||||
|
kind: PersistentVolume
|
||||||
|
metadata:
|
||||||
|
name: {{ .Values.persistence.pv.pvname | default (include "stirlingpdf.fullname" .) }}
|
||||||
|
labels:
|
||||||
|
{{- include "stirlingpdf.labels" . | nindent 4 }}
|
||||||
|
spec:
|
||||||
|
capacity:
|
||||||
|
storage: {{ .Values.persistence.pv.capacity.storage }}
|
||||||
|
accessModes:
|
||||||
|
- {{ .Values.persistence.pv.accessMode | quote }}
|
||||||
|
nfs:
|
||||||
|
server: {{ .Values.persistence.pv.nfs.server }}
|
||||||
|
path: {{ .Values.persistence.pv.nfs.path | quote }}
|
||||||
|
{{- end }}
|
||||||
27
chart/stirling-pdf/templates/pvc.yaml
Normal file
27
chart/stirling-pdf/templates/pvc.yaml
Normal file
@@ -0,0 +1,27 @@
|
|||||||
|
{{- if and .Values.persistence.enabled (not .Values.persistence.existingClaim) -}}
|
||||||
|
kind: PersistentVolumeClaim
|
||||||
|
apiVersion: v1
|
||||||
|
metadata:
|
||||||
|
name: {{ include "stirlingpdf.fullname" . }}
|
||||||
|
labels:
|
||||||
|
{{- include "stirlingpdf.labels" . | nindent 4 }}
|
||||||
|
{{- with .Values.persistence.labels }}
|
||||||
|
{{- toYaml . | nindent 4 }}
|
||||||
|
{{- end }}
|
||||||
|
spec:
|
||||||
|
accessModes:
|
||||||
|
- {{ .Values.persistence.accessMode | quote }}
|
||||||
|
resources:
|
||||||
|
requests:
|
||||||
|
storage: {{ .Values.persistence.size | quote }}
|
||||||
|
{{- if .Values.persistence.storageClass }}
|
||||||
|
{{- if (eq "-" .Values.persistence.storageClass) }}
|
||||||
|
storageClassName: ""
|
||||||
|
{{- else }}
|
||||||
|
storageClassName: "{{ .Values.persistence.storageClass }}"
|
||||||
|
{{- end }}
|
||||||
|
{{- if .Values.persistence.volumeName }}
|
||||||
|
volumeName: "{{ .Values.persistence.volumeName }}"
|
||||||
|
{{- end }}
|
||||||
|
{{- end }}
|
||||||
|
{{- end }}
|
||||||
48
chart/stirling-pdf/templates/service.yaml
Normal file
48
chart/stirling-pdf/templates/service.yaml
Normal file
@@ -0,0 +1,48 @@
|
|||||||
|
apiVersion: v1
|
||||||
|
kind: Service
|
||||||
|
metadata:
|
||||||
|
name: {{ .Values.service.servicename | default (include "stirlingpdf.fullname" .) }}
|
||||||
|
{{- with .Values.service.annotations }}
|
||||||
|
annotations:
|
||||||
|
{{- toYaml . | nindent 4 }}
|
||||||
|
{{- end }}
|
||||||
|
labels:
|
||||||
|
{{- include "stirlingpdf.labels" . | nindent 4 }}
|
||||||
|
{{- with .Values.service.labels }}
|
||||||
|
{{- toYaml . | nindent 4 }}
|
||||||
|
{{- end }}
|
||||||
|
spec:
|
||||||
|
type: {{ .Values.service.type }}
|
||||||
|
{{- if (or (eq .Values.service.type "LoadBalancer") (and (eq .Values.service.type "NodePort") (not (empty .Values.service.nodePort)))) }}
|
||||||
|
externalTrafficPolicy: {{ .Values.service.externalTrafficPolicy }}
|
||||||
|
{{- end }}
|
||||||
|
{{- if (and (eq .Values.service.type "LoadBalancer") .Values.service.loadBalancerIP) }}
|
||||||
|
loadBalancerIP: {{ .Values.service.loadBalancerIP }}
|
||||||
|
{{- end }}
|
||||||
|
{{- if (and (eq .Values.service.type "LoadBalancer") .Values.service.loadBalancerSourceRanges) }}
|
||||||
|
loadBalancerSourceRanges:
|
||||||
|
{{- with .Values.service.loadBalancerSourceRanges }}
|
||||||
|
{{ toYaml . | indent 2 }}
|
||||||
|
{{- end }}
|
||||||
|
{{- end }}
|
||||||
|
{{- if eq .Values.service.type "ClusterIP" }}
|
||||||
|
{{- if .Values.service.clusterIP }}
|
||||||
|
clusterIP: {{ .Values.service.clusterIP }}
|
||||||
|
{{- end }}
|
||||||
|
{{- end }}
|
||||||
|
ports:
|
||||||
|
- port: {{ .Values.service.externalPort }}
|
||||||
|
{{- if (and (eq .Values.service.type "NodePort") (not (empty .Values.service.nodePort))) }}
|
||||||
|
nodePort: {{.Values.service.nodePort}}
|
||||||
|
{{- end }}
|
||||||
|
{{- if .Values.service.targetPort }}
|
||||||
|
targetPort: {{ .Values.service.targetPort }}
|
||||||
|
name: {{ .Values.service.targetPort }}
|
||||||
|
{{- else }}
|
||||||
|
targetPort: http
|
||||||
|
name: http
|
||||||
|
{{- end }}
|
||||||
|
protocol: TCP
|
||||||
|
|
||||||
|
selector:
|
||||||
|
{{- include "stirlingpdf.selectorLabels" . | nindent 4 }}
|
||||||
13
chart/stirling-pdf/templates/serviceaccount.yaml
Normal file
13
chart/stirling-pdf/templates/serviceaccount.yaml
Normal file
@@ -0,0 +1,13 @@
|
|||||||
|
{{- if .Values.serviceAccount.create -}}
|
||||||
|
---
|
||||||
|
apiVersion: v1
|
||||||
|
kind: ServiceAccount
|
||||||
|
metadata:
|
||||||
|
name: {{ include "stirlingpdf.serviceAccountName" . }}
|
||||||
|
{{- with .Values.serviceAccount.annotations }}
|
||||||
|
annotations:
|
||||||
|
{{ toYaml . | nindent 4 }}
|
||||||
|
{{- end }}
|
||||||
|
labels:
|
||||||
|
{{- include "stirlingpdf.labels" . | nindent 4 }}
|
||||||
|
{{- end }}
|
||||||
31
chart/stirling-pdf/templates/servicemonitor.yaml
Normal file
31
chart/stirling-pdf/templates/servicemonitor.yaml
Normal file
@@ -0,0 +1,31 @@
|
|||||||
|
{{- if and ( .Capabilities.APIVersions.Has "monitoring.coreos.com/v1" ) ( .Values.serviceMonitor.enabled ) }}
|
||||||
|
apiVersion: monitoring.coreos.com/v1
|
||||||
|
kind: ServiceMonitor
|
||||||
|
metadata:
|
||||||
|
name: {{ include "stirlingpdf.fullname" . }}
|
||||||
|
namespace: {{ .Values.serviceMonitor.namespace | default .Release.Namespace }}
|
||||||
|
labels:
|
||||||
|
{{- include "stirlingpdf.labels" . | nindent 4 }}
|
||||||
|
{{- with .Values.serviceMonitor.labels }}
|
||||||
|
{{- toYaml . | nindent 4 }}
|
||||||
|
{{- end }}
|
||||||
|
spec:
|
||||||
|
endpoints:
|
||||||
|
- targetPort: 8080
|
||||||
|
{{- if .Values.serviceMonitor.interval }}
|
||||||
|
interval: {{ .Values.serviceMonitor.interval }}
|
||||||
|
{{- end }}
|
||||||
|
{{- if .Values.serviceMonitor.metricsPath }}
|
||||||
|
path: {{ .Values.serviceMonitor.metricsPath }}
|
||||||
|
{{- end }}
|
||||||
|
{{- if .Values.serviceMonitor.timeout }}
|
||||||
|
scrapeTimeout: {{ .Values.serviceMonitor.timeout }}
|
||||||
|
{{- end }}
|
||||||
|
jobLabel: {{ include "stirlingpdf.fullname" . }}
|
||||||
|
namespaceSelector:
|
||||||
|
matchNames:
|
||||||
|
- {{ .Release.Namespace }}
|
||||||
|
selector:
|
||||||
|
matchLabels:
|
||||||
|
{{- include "stirlingpdf.selectorLabels" . | nindent 6 }}
|
||||||
|
{{- end }}
|
||||||
239
chart/stirling-pdf/values.yaml
Normal file
239
chart/stirling-pdf/values.yaml
Normal file
@@ -0,0 +1,239 @@
|
|||||||
|
extraArgs: []
|
||||||
|
# - --storage-timestamp-tolerance 1s
|
||||||
|
replicaCount: 1
|
||||||
|
strategy:
|
||||||
|
type: RollingUpdate
|
||||||
|
image:
|
||||||
|
repository: frooodle/s-pdf
|
||||||
|
# took Chart appVersion by default
|
||||||
|
tag: ~
|
||||||
|
pullPolicy: IfNotPresent
|
||||||
|
secret:
|
||||||
|
labels: {}
|
||||||
|
## Labels to apply to all resources
|
||||||
|
##
|
||||||
|
commonLabels: {}
|
||||||
|
# team_name: dev
|
||||||
|
|
||||||
|
envs: []
|
||||||
|
# - name: PP_HOME_NAME
|
||||||
|
# value: "Stirling PDF"
|
||||||
|
# - name: APP_HOME_DESCRIPTION
|
||||||
|
# value: "Your locally hosted one-stop-shop for all your PDF needs."
|
||||||
|
# - name: APP_NAVBAR_NAME
|
||||||
|
# value: "Stirling PDF"
|
||||||
|
# - name: ALLOW_GOOGLE_VISIBILITY
|
||||||
|
# value: "true"
|
||||||
|
# - name: APP_ROOT_PATH
|
||||||
|
# value: "/"
|
||||||
|
# - name: APP_LOCALE
|
||||||
|
# value: "en_GB"
|
||||||
|
|
||||||
|
deployment:
|
||||||
|
## stirling-pdf Deployment annotations
|
||||||
|
annotations: {}
|
||||||
|
# name: value
|
||||||
|
labels: {}
|
||||||
|
# name: value
|
||||||
|
# additional volumes
|
||||||
|
extraVolumes: []
|
||||||
|
# - name: nginx-config
|
||||||
|
# secret:
|
||||||
|
# secretName: nginx-config
|
||||||
|
# additional volumes to mount
|
||||||
|
extraVolumeMounts: []
|
||||||
|
## sidecarContainers for the stirling-pdf
|
||||||
|
# Can be used to add a proxy to the pod that does
|
||||||
|
# scanning for secrets, signing, authentication, validation
|
||||||
|
# of the chart's content, send notifications...
|
||||||
|
sidecarContainers: {}
|
||||||
|
## Example sidecarContainer which uses an extraVolume from above and
|
||||||
|
## a named port that can be referenced in the service as targetPort.
|
||||||
|
# proxy:
|
||||||
|
# image: nginx:latest
|
||||||
|
# ports:
|
||||||
|
# - name: proxy
|
||||||
|
# containerPort: 8081
|
||||||
|
# volumeMounts:
|
||||||
|
# - name: nginx-config
|
||||||
|
# readOnly: true
|
||||||
|
# mountPath: /etc/nginx
|
||||||
|
|
||||||
|
## Pod annotations
|
||||||
|
## ref: https://kubernetes.io/docs/concepts/overview/working-with-objects/annotations/
|
||||||
|
## Read more about kube2iam to provide access to s3 https://github.com/jtblin/kube2iam
|
||||||
|
##
|
||||||
|
podAnnotations: {}
|
||||||
|
# iam.amazonaws.com/role: role-arn
|
||||||
|
|
||||||
|
## Pod labels
|
||||||
|
## ref: https://kubernetes.io/docs/concepts/overview/working-with-objects/labels/
|
||||||
|
podLabels: {}
|
||||||
|
# name: value
|
||||||
|
|
||||||
|
service:
|
||||||
|
servicename:
|
||||||
|
type: ClusterIP
|
||||||
|
externalTrafficPolicy: Local
|
||||||
|
## Uses pre-assigned IP address from cloud provider
|
||||||
|
## Only valid if service.type: LoadBalancer
|
||||||
|
loadBalancerIP:
|
||||||
|
## Limits which cidr blocks can connect to service's load balancer
|
||||||
|
## Only valid if service.type: LoadBalancer
|
||||||
|
loadBalancerSourceRanges: []
|
||||||
|
# clusterIP: None
|
||||||
|
externalPort: 8080
|
||||||
|
## targetPort of the container to use. If a sidecar should handle the
|
||||||
|
## requests first, use the named port from the sidecar. See sidecar example
|
||||||
|
## from deployment above. Leave empty to use stirling-pdf directly.
|
||||||
|
targetPort:
|
||||||
|
nodePort:
|
||||||
|
annotations: {}
|
||||||
|
labels: {}
|
||||||
|
|
||||||
|
serviceMonitor:
|
||||||
|
enabled: false
|
||||||
|
# namespace: prometheus
|
||||||
|
labels: {}
|
||||||
|
metricsPath: "/metrics"
|
||||||
|
# timeout: 60
|
||||||
|
# interval: 60
|
||||||
|
|
||||||
|
resources: {}
|
||||||
|
# limits:
|
||||||
|
# cpu: 100m
|
||||||
|
# memory: 128Mi
|
||||||
|
# requests:
|
||||||
|
# cpu: 80m
|
||||||
|
# memory: 64Mi
|
||||||
|
|
||||||
|
probes:
|
||||||
|
liveness:
|
||||||
|
initialDelaySeconds: 5
|
||||||
|
periodSeconds: 10
|
||||||
|
timeoutSeconds: 1
|
||||||
|
successThreshold: 1
|
||||||
|
failureThreshold: 3
|
||||||
|
livenessHttpGetConfig:
|
||||||
|
scheme: HTTP
|
||||||
|
readiness:
|
||||||
|
initialDelaySeconds: 5
|
||||||
|
periodSeconds: 10
|
||||||
|
timeoutSeconds: 1
|
||||||
|
successThreshold: 1
|
||||||
|
failureThreshold: 3
|
||||||
|
readinessHttpGetConfig:
|
||||||
|
scheme: HTTP
|
||||||
|
|
||||||
|
serviceAccount:
|
||||||
|
create: true
|
||||||
|
name: ""
|
||||||
|
automountServiceAccountToken: false
|
||||||
|
## Annotations for the Service Account
|
||||||
|
annotations: {}
|
||||||
|
|
||||||
|
# UID/GID 1000 is the default user "stirling-pdf" used in
|
||||||
|
# the container image starting in v0.8.0 and above. This
|
||||||
|
# is required for local persistent storage. If your cluster
|
||||||
|
# does not allow this, try setting securityContext: {}
|
||||||
|
securityContext:
|
||||||
|
enabled: true
|
||||||
|
fsGroup: 1000
|
||||||
|
## Optionally, specify supplementalGroups and/or
|
||||||
|
## runAsNonRoot for security purposes
|
||||||
|
# runAsNonRoot: true
|
||||||
|
# supplementalGroups: [1000]
|
||||||
|
|
||||||
|
containerSecurityContext: {}
|
||||||
|
|
||||||
|
priorityClassName: ""
|
||||||
|
|
||||||
|
nodeSelector: {}
|
||||||
|
|
||||||
|
tolerations: []
|
||||||
|
|
||||||
|
affinity: {}
|
||||||
|
|
||||||
|
persistence:
|
||||||
|
enabled: false
|
||||||
|
accessMode: ReadWriteOnce
|
||||||
|
size: 8Gi
|
||||||
|
labels: {}
|
||||||
|
# name: value
|
||||||
|
path: /tmp
|
||||||
|
## A manually managed Persistent Volume and Claim
|
||||||
|
## Requires persistence.enabled: true
|
||||||
|
## If defined, PVC must be created manually before volume will be bound
|
||||||
|
# existingClaim:
|
||||||
|
|
||||||
|
## stirling-pdf data Persistent Volume Storage Class
|
||||||
|
## If defined, storageClassName: <storageClass>
|
||||||
|
## If set to "-", storageClassName: "", which disables dynamic provisioning
|
||||||
|
## If undefined (the default) or set to null, no storageClassName spec is
|
||||||
|
## set, choosing the default provisioner. (gp2 on AWS, standard on
|
||||||
|
## GKE, AWS & OpenStack)
|
||||||
|
##
|
||||||
|
# storageClass: "-"
|
||||||
|
# volumeName:
|
||||||
|
pv:
|
||||||
|
enabled: false
|
||||||
|
pvname:
|
||||||
|
capacity:
|
||||||
|
storage: 8Gi
|
||||||
|
accessMode: ReadWriteOnce
|
||||||
|
nfs:
|
||||||
|
server:
|
||||||
|
path:
|
||||||
|
|
||||||
|
## Init containers parameters:
|
||||||
|
## volumePermissions: Change the owner of the persistent volume mountpoint to RunAsUser:fsGroup
|
||||||
|
##
|
||||||
|
volumePermissions:
|
||||||
|
image:
|
||||||
|
registry: docker.io
|
||||||
|
repository: bitnami/minideb
|
||||||
|
tag: buster
|
||||||
|
pullPolicy: Always
|
||||||
|
## Optionally specify an array of imagePullSecrets.
|
||||||
|
## Secrets must be manually created in the namespace.
|
||||||
|
## ref: https://kubernetes.io/docs/tasks/configure-pod-container/pull-image-private-registry/
|
||||||
|
##
|
||||||
|
# pullSecrets:
|
||||||
|
# - myRegistryKeySecretName
|
||||||
|
|
||||||
|
## Ingress for load balancer
|
||||||
|
ingress:
|
||||||
|
enabled: false
|
||||||
|
pathType: "ImplementationSpecific"
|
||||||
|
## stirling-pdf Ingress labels
|
||||||
|
##
|
||||||
|
labels: {}
|
||||||
|
# dns: "route53"
|
||||||
|
|
||||||
|
## stirling-pdf Ingress annotations
|
||||||
|
##
|
||||||
|
annotations: {}
|
||||||
|
# kubernetes.io/ingress.class: nginx
|
||||||
|
# kubernetes.io/tls-acme: "true"
|
||||||
|
|
||||||
|
## stirling-pdf Ingress hostnames
|
||||||
|
## Must be provided if Ingress is enabled
|
||||||
|
##
|
||||||
|
hosts: []
|
||||||
|
# - name: stirling-pdf.domain1.com
|
||||||
|
# path: /
|
||||||
|
# tls: false
|
||||||
|
# - name: stirling-pdf.domain2.com
|
||||||
|
# path: /
|
||||||
|
#
|
||||||
|
# ## Set this to true in order to enable TLS on the ingress record
|
||||||
|
# tls: true
|
||||||
|
#
|
||||||
|
# ## If TLS is set to true, you must declare what secret will store the key/certificate for TLS
|
||||||
|
# ## Secrets must be added manually to the namespace
|
||||||
|
# tlsSecret: stirling-pdf.domain2-tls
|
||||||
|
|
||||||
|
# For Kubernetes >= 1.18 you should specify the ingress-controller via the field ingressClassName
|
||||||
|
# See https://kubernetes.io/blog/2020/04/02/improvements-to-the-ingress-api-in-kubernetes-1.18/#specifying-the-class-of-an-ingress
|
||||||
|
ingressClassName:
|
||||||
|
|
||||||
38
groups.md
38
groups.md
@@ -1,38 +0,0 @@
|
|||||||
Operation | PageOps | Convert | Security | Other | CLI | Python | OpenCV | LibreOffice | OCRmyPDF | Java | Javascript
|
|
||||||
--------------------|---------|---------|----------|-------|------|--------|--------|-------------|--------- |-------- |-----------
|
|
||||||
remove-pages | X | | | | | | | | | X |
|
|
||||||
merge-pdfs | X | | | | | | | | | X |
|
|
||||||
split-pdfs | X | | | | | | | | | X |
|
|
||||||
pdf-organizer | X | | | | | | | | | X | X
|
|
||||||
rotate-pdf | X | | | | | | | | | X |
|
|
||||||
multi-page-layout | X | | | | | | | | | X |
|
|
||||||
scale-pages | X | | | | | | | | | X |
|
|
||||||
pdf-to-img | | X | | | | | | | | X |
|
|
||||||
img-to-pdf | | X | | | | | | | | X |
|
|
||||||
pdf-to-pdfa | | X | | | X | | | | X | |
|
|
||||||
file-to-pdf | | X | | | X | | | X | | |
|
|
||||||
xlsx-to-pdf | | X | | | X | | | X | | |
|
|
||||||
pdf-to-word | | X | | | X | | | X | | |
|
|
||||||
pdf-to-presentation | | X | | | X | | | X | | |
|
|
||||||
pdf-to-text | | X | | | X | | | X | | |
|
|
||||||
pdf-to-html | | X | | | X | | | X | | |
|
|
||||||
pdf-to-xml | | X | | | X | | | X | | |
|
|
||||||
add-password | | | X | | | | | | | X |
|
|
||||||
remove-password | | | X | | | | | | | X |
|
|
||||||
change-permissions | | | X | | | | | | | X |
|
|
||||||
add-watermark | | | X | | | | | | | X |
|
|
||||||
cert-sign | | | X | | | | | | | X |
|
|
||||||
ocr-pdf | | | | X | X | | | | X | |
|
|
||||||
add-image | | | | X | | | | | | X |
|
|
||||||
compress-pdf | | | | X | X | | | | X
|
|
||||||
extract-images | | | | X | | | | | | X |
|
|
||||||
change-metadata | | | | X | | | | | | X |
|
|
||||||
extract-image-scans | | | | X | X | X | X | | | |
|
|
||||||
sign | | | | X | | | | | | | X
|
|
||||||
flatten | | | | X | | | | | | |
|
|
||||||
repair | | | | X | X | | | X | | |
|
|
||||||
remove-blanks | | | | X | X | X | X | | | |
|
|
||||||
compare | | | | X | | | | | | | X
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
BIN
images/login-dark.png
Normal file
BIN
images/login-dark.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 29 KiB |
BIN
images/login-light.png
Normal file
BIN
images/login-light.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 30 KiB |
Binary file not shown.
|
Before Width: | Height: | Size: 128 KiB After Width: | Height: | Size: 131 KiB |
36
lauch4jConfig.xml
Normal file
36
lauch4jConfig.xml
Normal file
@@ -0,0 +1,36 @@
|
|||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<launch4jConfig>
|
||||||
|
<dontWrapJar>false</dontWrapJar>
|
||||||
|
<headerType>console</headerType>
|
||||||
|
<jar>.\build\libs\S-PDF-0.10.1.jar</jar>
|
||||||
|
<outfile>.\Stirling-PDF.exe</outfile>
|
||||||
|
<errTitle>Please download Java17</errTitle>
|
||||||
|
<cmdLine></cmdLine>
|
||||||
|
<chdir>.</chdir>
|
||||||
|
<priority>normal</priority>
|
||||||
|
<downloadUrl>https://download.oracle.com/java/17/latest/jdk-17_windows-x64_bin.exe</downloadUrl>
|
||||||
|
<supportUrl></supportUrl>
|
||||||
|
<stayAlive>false</stayAlive>
|
||||||
|
<restartOnCrash>false</restartOnCrash>
|
||||||
|
<manifest></manifest>
|
||||||
|
<icon>./src/main/resources/static/favicon.ico</icon>
|
||||||
|
<var>BROWSER_OPEN=true</var>
|
||||||
|
<singleInstance>
|
||||||
|
<mutexName>Stirling-PDF</mutexName>
|
||||||
|
<windowTitle>Stirling-PDF</windowTitle>
|
||||||
|
</singleInstance>
|
||||||
|
<jre>
|
||||||
|
<path>%JAVA_HOME%;%PATH%</path>
|
||||||
|
<requiresJdk>false</requiresJdk>
|
||||||
|
<requires64Bit>false</requires64Bit>
|
||||||
|
<minVersion>17</minVersion>
|
||||||
|
<maxVersion></maxVersion>
|
||||||
|
</jre>
|
||||||
|
<messages>
|
||||||
|
<startupErr>An error occurred while starting Stirling-PDF</startupErr>
|
||||||
|
<jreNotFoundErr>This application requires a Java Runtime Environment, Please download Java 17.</jreNotFoundErr>
|
||||||
|
<jreVersionErr>You are running the wrong version of Java, Please download Java 17.</jreVersionErr>
|
||||||
|
<launcherErr>Java is corrupted. Please uninstall and then install Java 17.</launcherErr>
|
||||||
|
<instanceAlreadyExistsMsg>Stirling-PDF is already running.</instanceAlreadyExistsMsg>
|
||||||
|
</messages>
|
||||||
|
</launch4jConfig>
|
||||||
80
scripts/PropSync.java
Normal file
80
scripts/PropSync.java
Normal file
@@ -0,0 +1,80 @@
|
|||||||
|
package stirling.software.Stirling.Stats;
|
||||||
|
|
||||||
|
import java.nio.file.*;
|
||||||
|
import java.nio.charset.MalformedInputException;
|
||||||
|
import java.nio.charset.StandardCharsets;
|
||||||
|
import java.io.*;
|
||||||
|
import java.util.*;
|
||||||
|
|
||||||
|
public class PropSync {
|
||||||
|
|
||||||
|
public static void main(String[] args) throws IOException {
|
||||||
|
File folder = new File("C:\\Users\\systo\\git\\Stirling-PDF\\src\\main\\resources");
|
||||||
|
File[] files = folder.listFiles((dir, name) -> name.matches("messages_.*\\.properties"));
|
||||||
|
|
||||||
|
List<String> enLines = Files.readAllLines(Paths.get(folder + "\\messages_en_GB.properties"), StandardCharsets.UTF_8);
|
||||||
|
Map<String, String> enProps = linesToProps(enLines);
|
||||||
|
|
||||||
|
for (File file : files) {
|
||||||
|
if (!file.getName().equals("messages_en_GB.properties")) {
|
||||||
|
System.out.println("Processing file: " + file.getName());
|
||||||
|
List<String> lines;
|
||||||
|
try {
|
||||||
|
lines = Files.readAllLines(file.toPath(), StandardCharsets.UTF_8);
|
||||||
|
} catch (MalformedInputException e) {
|
||||||
|
System.out.println("Skipping due to not UTF8 format for file: " + file.getName());
|
||||||
|
continue;
|
||||||
|
} catch (IOException e) {
|
||||||
|
throw new UncheckedIOException(e);
|
||||||
|
}
|
||||||
|
|
||||||
|
Map<String, String> currentProps = linesToProps(lines);
|
||||||
|
List<String> newLines = syncPropsWithLines(enProps, currentProps, enLines);
|
||||||
|
|
||||||
|
Files.write(file.toPath(), newLines, StandardCharsets.UTF_8);
|
||||||
|
System.out.println("Finished processing file: " + file.getName());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private static Map<String, String> linesToProps(List<String> lines) {
|
||||||
|
Map<String, String> props = new LinkedHashMap<>();
|
||||||
|
for (String line : lines) {
|
||||||
|
if (!line.trim().isEmpty() && line.contains("=")) {
|
||||||
|
String[] parts = line.split("=", 2);
|
||||||
|
props.put(parts[0].trim(), parts[1].trim());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return props;
|
||||||
|
}
|
||||||
|
|
||||||
|
private static List<String> syncPropsWithLines(Map<String, String> enProps, Map<String, String> currentProps, List<String> enLines) {
|
||||||
|
List<String> newLines = new ArrayList<>();
|
||||||
|
boolean needsTranslateComment = false; // flag to check if we need to add "TODO: Translate"
|
||||||
|
|
||||||
|
for (String line : enLines) {
|
||||||
|
if (line.contains("=")) {
|
||||||
|
String key = line.split("=", 2)[0].trim();
|
||||||
|
|
||||||
|
if (currentProps.containsKey(key)) {
|
||||||
|
newLines.add(key + "=" + currentProps.get(key));
|
||||||
|
needsTranslateComment = false;
|
||||||
|
} else {
|
||||||
|
if (!needsTranslateComment) {
|
||||||
|
newLines.add("##########################");
|
||||||
|
newLines.add("### TODO: Translate ###");
|
||||||
|
newLines.add("##########################");
|
||||||
|
needsTranslateComment = true;
|
||||||
|
}
|
||||||
|
newLines.add(line);
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
// handle comments and other non-property lines
|
||||||
|
newLines.add(line);
|
||||||
|
needsTranslateComment = false; // reset the flag when we encounter comments or empty lines
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return newLines;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,5 +1,4 @@
|
|||||||
import cv2
|
import cv2
|
||||||
import numpy as np
|
|
||||||
import sys
|
import sys
|
||||||
import argparse
|
import argparse
|
||||||
|
|
||||||
|
|||||||
@@ -5,5 +5,36 @@ echo "Copying original files without overwriting existing files"
|
|||||||
mkdir -p /usr/share/tesseract-ocr
|
mkdir -p /usr/share/tesseract-ocr
|
||||||
cp -rn /usr/share/tesseract-ocr-original/* /usr/share/tesseract-ocr
|
cp -rn /usr/share/tesseract-ocr-original/* /usr/share/tesseract-ocr
|
||||||
|
|
||||||
|
# Check if TESSERACT_LANGS environment variable is set and is not empty
|
||||||
|
if [[ -n "$TESSERACT_LANGS" ]]; then
|
||||||
|
# Convert comma-separated values to a space-separated list
|
||||||
|
LANGS=$(echo $TESSERACT_LANGS | tr ',' ' ')
|
||||||
|
|
||||||
|
# Install each language pack
|
||||||
|
for LANG in $LANGS; do
|
||||||
|
apt-get install -y "tesseract-ocr-$LANG"
|
||||||
|
done
|
||||||
|
fi
|
||||||
|
|
||||||
|
# Check for DOCKER_ENABLE_SECURITY and download the appropriate JAR if required
|
||||||
|
if [ "$DOCKER_ENABLE_SECURITY" = "true" ] && [ "$VERSION_TAG" != "alpha" ]; then
|
||||||
|
if [ ! -f app-security.jar ]; then
|
||||||
|
echo "Trying to download from: https://github.com/Frooodle/Stirling-PDF/releases/download/v$VERSION_TAG/Stirling-PDF-with-login.jar"
|
||||||
|
curl -L -o app-security.jar https://github.com/Frooodle/Stirling-PDF/releases/download/v$VERSION_TAG/Stirling-PDF-with-login.jar
|
||||||
|
|
||||||
|
# If the first download attempt failed, try with the 'v' prefix
|
||||||
|
if [ $? -ne 0 ]; then
|
||||||
|
echo "Trying to download from: https://github.com/Frooodle/Stirling-PDF/releases/download/$VERSION_TAG/Stirling-PDF-with-login.jar"
|
||||||
|
curl -L -o app-security.jar https://github.com/Frooodle/Stirling-PDF/releases/download/$VERSION_TAG/Stirling-PDF-with-login.jar
|
||||||
|
fi
|
||||||
|
|
||||||
|
if [ $? -eq 0 ]; then # checks if curl was successful
|
||||||
|
rm -f app.jar
|
||||||
|
ln -s app-security.jar app.jar
|
||||||
|
fi
|
||||||
|
fi
|
||||||
|
fi
|
||||||
|
|
||||||
|
|
||||||
# Run the main command
|
# Run the main command
|
||||||
exec "$@"
|
exec "$@"
|
||||||
@@ -1 +1 @@
|
|||||||
rootProject.name = 'S-PDF'
|
rootProject.name = 'Stirling-PDF'
|
||||||
|
|||||||
@@ -1,11 +1,82 @@
|
|||||||
package stirling.software.SPDF;
|
package stirling.software.SPDF;
|
||||||
|
|
||||||
import org.springframework.boot.SpringApplication;
|
import java.nio.file.Files;
|
||||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
import java.nio.file.Paths;
|
||||||
|
import java.util.Collections;
|
||||||
@SpringBootApplication
|
|
||||||
public class SPdfApplication {
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
public static void main(String[] args) {
|
import org.springframework.boot.SpringApplication;
|
||||||
SpringApplication.run(SPdfApplication.class, args);
|
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||||
}
|
import org.springframework.core.env.Environment;
|
||||||
|
|
||||||
|
import jakarta.annotation.PostConstruct;
|
||||||
|
import stirling.software.SPDF.config.ConfigInitializer;
|
||||||
|
import stirling.software.SPDF.utils.GeneralUtils;
|
||||||
|
@SpringBootApplication
|
||||||
|
|
||||||
|
//@EnableScheduling
|
||||||
|
public class SPdfApplication {
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
private Environment env;
|
||||||
|
|
||||||
|
@PostConstruct
|
||||||
|
public void init() {
|
||||||
|
// Check if the BROWSER_OPEN environment variable is set to true
|
||||||
|
String browserOpenEnv = env.getProperty("BROWSER_OPEN");
|
||||||
|
boolean browserOpen = browserOpenEnv != null && browserOpenEnv.equalsIgnoreCase("true");
|
||||||
|
|
||||||
|
if (browserOpen) {
|
||||||
|
try {
|
||||||
|
String port = env.getProperty("local.server.port");
|
||||||
|
if(port == null || port.length() == 0) {
|
||||||
|
port="8080";
|
||||||
|
}
|
||||||
|
String url = "http://localhost:" + port;
|
||||||
|
|
||||||
|
String os = System.getProperty("os.name").toLowerCase();
|
||||||
|
Runtime rt = Runtime.getRuntime();
|
||||||
|
if (os.contains("win")) {
|
||||||
|
// For Windows
|
||||||
|
rt.exec("rundll32 url.dll,FileProtocolHandler " + url);
|
||||||
|
}
|
||||||
|
} catch (Exception e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void main(String[] args) {
|
||||||
|
SpringApplication app = new SpringApplication(SPdfApplication.class);
|
||||||
|
app.addInitializers(new ConfigInitializer());
|
||||||
|
if (Files.exists(Paths.get("configs/settings.yml"))) {
|
||||||
|
app.setDefaultProperties(Collections.singletonMap("spring.config.additional-location", "file:configs/settings.yml"));
|
||||||
|
} else {
|
||||||
|
System.out.println("External configuration file 'configs/settings.yml' does not exist. Using default configuration and environment configuration instead.");
|
||||||
|
}
|
||||||
|
app.run(args);
|
||||||
|
|
||||||
|
try {
|
||||||
|
Thread.sleep(1000);
|
||||||
|
} catch (InterruptedException e) {
|
||||||
|
// TODO Auto-generated catch block
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
|
||||||
|
GeneralUtils.createDir("customFiles/static/");
|
||||||
|
GeneralUtils.createDir("customFiles/templates/");
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
System.out.println("Stirling-PDF Started.");
|
||||||
|
|
||||||
|
String port = System.getProperty("local.server.port");
|
||||||
|
if(port == null || port.length() == 0) {
|
||||||
|
port="8080";
|
||||||
|
}
|
||||||
|
String url = "http://localhost:" + port;
|
||||||
|
System.out.println("Navigate to " + url);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
@@ -1,42 +1,55 @@
|
|||||||
package stirling.software.SPDF.config;
|
package stirling.software.SPDF.config;
|
||||||
|
|
||||||
import org.springframework.context.annotation.Bean;
|
import org.springframework.beans.PropertyEditorRegistrar;
|
||||||
import org.springframework.context.annotation.Configuration;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
import org.springframework.context.annotation.Bean;
|
||||||
@Configuration
|
import org.springframework.context.annotation.Configuration;
|
||||||
public class AppConfig {
|
|
||||||
@Bean(name = "appName")
|
import stirling.software.SPDF.model.ApplicationProperties;
|
||||||
public String appName() {
|
@Configuration
|
||||||
String appName = System.getProperty("APP_HOME_NAME");
|
public class AppConfig {
|
||||||
if (appName == null)
|
|
||||||
appName = System.getenv("APP_HOME_NAME");
|
|
||||||
return (appName != null) ? appName : "Stirling PDF";
|
@Autowired
|
||||||
}
|
ApplicationProperties applicationProperties;
|
||||||
|
|
||||||
@Bean(name = "appVersion")
|
@Bean(name = "loginEnabled")
|
||||||
public String appVersion() {
|
public boolean loginEnabled() {
|
||||||
String version = getClass().getPackage().getImplementationVersion();
|
return applicationProperties.getSecurity().getEnableLogin();
|
||||||
return (version != null) ? version : "0.0.0";
|
}
|
||||||
}
|
|
||||||
|
@Bean(name = "appName")
|
||||||
@Bean(name = "homeText")
|
public String appName() {
|
||||||
public String homeText() {
|
String homeTitle = applicationProperties.getUi().getAppName();
|
||||||
String homeText = System.getProperty("APP_HOME_DESCRIPTION");
|
return (homeTitle != null) ? homeTitle : "Stirling PDF";
|
||||||
if (homeText == null)
|
}
|
||||||
homeText = System.getenv("APP_HOME_DESCRIPTION");
|
|
||||||
return (homeText != null) ? homeText : "null";
|
@Bean(name = "appVersion")
|
||||||
}
|
public String appVersion() {
|
||||||
|
String version = getClass().getPackage().getImplementationVersion();
|
||||||
@Bean(name = "navBarText")
|
return (version != null) ? version : "0.0.0";
|
||||||
public String navBarText() {
|
}
|
||||||
String navBarText = System.getProperty("APP_NAVBAR_NAME");
|
|
||||||
if (navBarText == null)
|
@Bean(name = "homeText")
|
||||||
navBarText = System.getenv("APP_NAVBAR_NAME");
|
public String homeText() {
|
||||||
if (navBarText == null)
|
return (applicationProperties.getUi().getHomeDescription() != null) ? applicationProperties.getUi().getHomeDescription() : "null";
|
||||||
navBarText = System.getProperty("APP_HOME_NAME");
|
}
|
||||||
if (navBarText == null)
|
|
||||||
navBarText = System.getenv("APP_HOME_NAME");
|
|
||||||
|
@Bean(name = "navBarText")
|
||||||
return (navBarText != null) ? navBarText : "Stirling PDF";
|
public String navBarText() {
|
||||||
}
|
String defaultNavBar = applicationProperties.getUi().getAppNameNavbar() != null ? applicationProperties.getUi().getAppNameNavbar() : applicationProperties.getUi().getAppName();
|
||||||
|
return (defaultNavBar != null) ? defaultNavBar : "Stirling PDF";
|
||||||
|
}
|
||||||
|
|
||||||
|
@Bean(name = "rateLimit")
|
||||||
|
public boolean rateLimit() {
|
||||||
|
String appName = System.getProperty("rateLimit");
|
||||||
|
if (appName == null)
|
||||||
|
appName = System.getenv("rateLimit");
|
||||||
|
System.out.println("rateLimit=" + appName);
|
||||||
|
return (appName != null) ? Boolean.valueOf(appName) : false;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
@@ -1,53 +1,65 @@
|
|||||||
package stirling.software.SPDF.config;
|
package stirling.software.SPDF.config;
|
||||||
|
|
||||||
import java.util.Locale;
|
import java.util.Locale;
|
||||||
|
|
||||||
import org.springframework.context.annotation.Bean;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
import org.springframework.context.annotation.Configuration;
|
import org.springframework.context.annotation.Bean;
|
||||||
import org.springframework.web.servlet.LocaleResolver;
|
import org.springframework.context.annotation.Configuration;
|
||||||
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
|
import org.springframework.web.servlet.LocaleResolver;
|
||||||
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
|
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
|
||||||
import org.springframework.web.servlet.i18n.LocaleChangeInterceptor;
|
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
|
||||||
import org.springframework.web.servlet.i18n.SessionLocaleResolver;
|
import org.springframework.web.servlet.i18n.LocaleChangeInterceptor;
|
||||||
|
import org.springframework.web.servlet.i18n.SessionLocaleResolver;
|
||||||
@Configuration
|
|
||||||
public class Beans implements WebMvcConfigurer {
|
import stirling.software.SPDF.model.ApplicationProperties;
|
||||||
|
|
||||||
@Override
|
@Configuration
|
||||||
public void addInterceptors(InterceptorRegistry registry) {
|
public class Beans implements WebMvcConfigurer {
|
||||||
registry.addInterceptor(localeChangeInterceptor());
|
|
||||||
registry.addInterceptor(new CleanUrlInterceptor());
|
@Autowired
|
||||||
}
|
ApplicationProperties applicationProperties;
|
||||||
|
|
||||||
@Bean
|
@Override
|
||||||
public LocaleChangeInterceptor localeChangeInterceptor() {
|
public void addInterceptors(InterceptorRegistry registry) {
|
||||||
LocaleChangeInterceptor lci = new LocaleChangeInterceptor();
|
registry.addInterceptor(localeChangeInterceptor());
|
||||||
lci.setParamName("lang");
|
registry.addInterceptor(new CleanUrlInterceptor());
|
||||||
return lci;
|
}
|
||||||
}
|
|
||||||
|
@Bean
|
||||||
@Bean
|
public LocaleChangeInterceptor localeChangeInterceptor() {
|
||||||
public LocaleResolver localeResolver() {
|
LocaleChangeInterceptor lci = new LocaleChangeInterceptor();
|
||||||
SessionLocaleResolver slr = new SessionLocaleResolver();
|
lci.setParamName("lang");
|
||||||
|
return lci;
|
||||||
String appLocaleEnv = System.getProperty("APP_LOCALE");
|
}
|
||||||
if (appLocaleEnv == null)
|
|
||||||
appLocaleEnv = System.getenv("APP_LOCALE");
|
@Bean
|
||||||
Locale defaultLocale = Locale.UK; // Fallback to UK locale if environment variable is not set
|
public LocaleResolver localeResolver() {
|
||||||
|
SessionLocaleResolver slr = new SessionLocaleResolver();
|
||||||
if (appLocaleEnv != null && !appLocaleEnv.isEmpty()) {
|
|
||||||
Locale tempLocale = Locale.forLanguageTag(appLocaleEnv);
|
|
||||||
String tempLanguageTag = tempLocale.toLanguageTag();
|
String appLocaleEnv = applicationProperties.getSystem().getDefaultLocale();
|
||||||
|
Locale defaultLocale = Locale.UK; // Fallback to UK locale if environment variable is not set
|
||||||
if (appLocaleEnv.equalsIgnoreCase(tempLanguageTag)) {
|
|
||||||
defaultLocale = tempLocale;
|
if (appLocaleEnv != null && !appLocaleEnv.isEmpty()) {
|
||||||
} else {
|
Locale tempLocale = Locale.forLanguageTag(appLocaleEnv);
|
||||||
System.err.println("Invalid APP_LOCALE environment variable value. Falling back to default Locale.UK.");
|
String tempLanguageTag = tempLocale.toLanguageTag();
|
||||||
}
|
|
||||||
}
|
if (appLocaleEnv.equalsIgnoreCase(tempLanguageTag)) {
|
||||||
|
defaultLocale = tempLocale;
|
||||||
slr.setDefaultLocale(defaultLocale);
|
} else {
|
||||||
return slr;
|
tempLocale = Locale.forLanguageTag(appLocaleEnv.replace("_","-"));
|
||||||
}
|
tempLanguageTag = tempLocale.toLanguageTag();
|
||||||
|
|
||||||
}
|
if (appLocaleEnv.equalsIgnoreCase(tempLanguageTag)) {
|
||||||
|
defaultLocale = tempLocale;
|
||||||
|
} else {
|
||||||
|
System.err.println("Invalid APP_LOCALE environment variable value. Falling back to default Locale.UK.");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
slr.setDefaultLocale(defaultLocale);
|
||||||
|
return slr;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|||||||
@@ -1,45 +1,68 @@
|
|||||||
package stirling.software.SPDF.config;
|
package stirling.software.SPDF.config;
|
||||||
import java.util.regex.Matcher;
|
|
||||||
import java.util.regex.Pattern;
|
import java.util.Arrays;
|
||||||
|
import java.util.HashMap;
|
||||||
import org.springframework.web.servlet.HandlerInterceptor;
|
import java.util.List;
|
||||||
import org.springframework.web.servlet.ModelAndView;
|
import java.util.Map;
|
||||||
|
|
||||||
import jakarta.servlet.http.HttpServletRequest;
|
import org.springframework.web.servlet.HandlerInterceptor;
|
||||||
import jakarta.servlet.http.HttpServletResponse;
|
import org.springframework.web.servlet.ModelAndView;
|
||||||
|
|
||||||
public class CleanUrlInterceptor implements HandlerInterceptor {
|
import jakarta.servlet.http.HttpServletRequest;
|
||||||
|
import jakarta.servlet.http.HttpServletResponse;
|
||||||
private static final Pattern LANG_PATTERN = Pattern.compile("&?lang=([^&]+)");
|
|
||||||
|
public class CleanUrlInterceptor implements HandlerInterceptor {
|
||||||
@Override
|
|
||||||
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
|
private static final List<String> ALLOWED_PARAMS = Arrays.asList("lang", "endpoint", "endpoints", "logout", "error", "file", "messageType");
|
||||||
String queryString = request.getQueryString();
|
|
||||||
if (queryString != null && !queryString.isEmpty()) {
|
|
||||||
String requestURI = request.getRequestURI();
|
@Override
|
||||||
|
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)
|
||||||
// Keep the lang parameter if it exists
|
throws Exception {
|
||||||
Matcher langMatcher = LANG_PATTERN.matcher(queryString);
|
String queryString = request.getQueryString();
|
||||||
String langQueryString = langMatcher.find() ? "lang=" + langMatcher.group(1) : "";
|
if (queryString != null && !queryString.isEmpty()) {
|
||||||
|
String requestURI = request.getRequestURI();
|
||||||
// Check if there are any other query parameters besides the lang parameter
|
Map<String, String> parameters = new HashMap<>();
|
||||||
String remainingQueryString = queryString.replaceAll(LANG_PATTERN.pattern(), "").replaceAll("&+", "&").replaceAll("^&|&$", "");
|
|
||||||
|
// Keep only the allowed parameters
|
||||||
if (!remainingQueryString.isEmpty()) {
|
String[] queryParameters = queryString.split("&");
|
||||||
// Redirect to the URL without other query parameters
|
for (String param : queryParameters) {
|
||||||
String redirectUrl = requestURI + (langQueryString.isEmpty() ? "" : "?" + langQueryString);
|
String[] keyValue = param.split("=");
|
||||||
response.sendRedirect(redirectUrl);
|
if (keyValue.length != 2) {
|
||||||
return false;
|
continue;
|
||||||
}
|
}
|
||||||
}
|
if (ALLOWED_PARAMS.contains(keyValue[0])) {
|
||||||
return true;
|
parameters.put(keyValue[0], keyValue[1]);
|
||||||
}
|
}
|
||||||
|
}
|
||||||
@Override
|
|
||||||
public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) {
|
// If there are any parameters that are not allowed
|
||||||
}
|
if (parameters.size() != queryParameters.length) {
|
||||||
|
// Construct new query string
|
||||||
@Override
|
StringBuilder newQueryString = new StringBuilder();
|
||||||
public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) {
|
for (Map.Entry<String, String> entry : parameters.entrySet()) {
|
||||||
}
|
if (newQueryString.length() > 0) {
|
||||||
}
|
newQueryString.append("&");
|
||||||
|
}
|
||||||
|
newQueryString.append(entry.getKey()).append("=").append(entry.getValue());
|
||||||
|
}
|
||||||
|
|
||||||
|
// Redirect to the URL with only allowed query parameters
|
||||||
|
String redirectUrl = requestURI + "?" + newQueryString;
|
||||||
|
response.sendRedirect(redirectUrl);
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler,
|
||||||
|
ModelAndView modelAndView) {
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler,
|
||||||
|
Exception ex) {
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
@@ -0,0 +1,129 @@
|
|||||||
|
package stirling.software.SPDF.config;
|
||||||
|
|
||||||
|
import java.io.BufferedReader;
|
||||||
|
import java.io.FileNotFoundException;
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.io.InputStream;
|
||||||
|
import java.io.InputStreamReader;
|
||||||
|
import java.nio.charset.StandardCharsets;
|
||||||
|
import java.nio.file.Files;
|
||||||
|
import java.nio.file.Path;
|
||||||
|
import java.nio.file.Paths;
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Optional;
|
||||||
|
import java.util.Set;
|
||||||
|
import java.util.function.Function;
|
||||||
|
import java.util.stream.Collectors;
|
||||||
|
|
||||||
|
import org.springframework.context.ApplicationContextInitializer;
|
||||||
|
import org.springframework.context.ConfigurableApplicationContext;
|
||||||
|
|
||||||
|
public class ConfigInitializer implements ApplicationContextInitializer<ConfigurableApplicationContext> {
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void initialize(ConfigurableApplicationContext applicationContext) {
|
||||||
|
try {
|
||||||
|
ensureConfigExists();
|
||||||
|
} catch (IOException e) {
|
||||||
|
throw new RuntimeException("Failed to initialize application configuration", e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public void ensureConfigExists() throws IOException {
|
||||||
|
// Define the path to the external config directory
|
||||||
|
Path destPath = Paths.get("configs", "settings.yml");
|
||||||
|
|
||||||
|
// Check if the file already exists
|
||||||
|
if (Files.notExists(destPath)) {
|
||||||
|
// Ensure the destination directory exists
|
||||||
|
Files.createDirectories(destPath.getParent());
|
||||||
|
|
||||||
|
// Copy the resource from classpath to the external directory
|
||||||
|
try (InputStream in = getClass().getClassLoader().getResourceAsStream("settings.yml.template")) {
|
||||||
|
if (in != null) {
|
||||||
|
Files.copy(in, destPath);
|
||||||
|
} else {
|
||||||
|
throw new FileNotFoundException("Resource file not found: settings.yml.template");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
// If user file exists, we need to merge it with the template from the classpath
|
||||||
|
List<String> templateLines;
|
||||||
|
try (InputStream in = getClass().getClassLoader().getResourceAsStream("settings.yml.template")) {
|
||||||
|
templateLines = new BufferedReader(new InputStreamReader(in, StandardCharsets.UTF_8)).lines()
|
||||||
|
.collect(Collectors.toList());
|
||||||
|
}
|
||||||
|
|
||||||
|
mergeYamlFiles(templateLines, destPath, destPath);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public void mergeYamlFiles(List<String> templateLines, Path userFilePath, Path outputPath) throws IOException {
|
||||||
|
List<String> userLines = Files.readAllLines(userFilePath);
|
||||||
|
List<String> mergedLines = new ArrayList<>();
|
||||||
|
boolean insideAutoGenerated = false;
|
||||||
|
boolean beforeFirstKey = true;
|
||||||
|
|
||||||
|
Function<String, Boolean> isCommented = line -> line.trim().startsWith("#");
|
||||||
|
Function<String, String> extractKey = line -> {
|
||||||
|
String[] parts = line.split(":");
|
||||||
|
return parts.length > 0 ? parts[0].trim().replace("#", "").trim() : "";
|
||||||
|
};
|
||||||
|
|
||||||
|
Set<String> userKeys = userLines.stream().map(extractKey).collect(Collectors.toSet());
|
||||||
|
|
||||||
|
for (String line : templateLines) {
|
||||||
|
String key = extractKey.apply(line);
|
||||||
|
|
||||||
|
if (line.trim().equalsIgnoreCase("AutomaticallyGenerated:")) {
|
||||||
|
insideAutoGenerated = true;
|
||||||
|
mergedLines.add(line);
|
||||||
|
continue;
|
||||||
|
} else if (insideAutoGenerated && line.trim().isEmpty()) {
|
||||||
|
insideAutoGenerated = false;
|
||||||
|
mergedLines.add(line);
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (beforeFirstKey && (isCommented.apply(line) || line.trim().isEmpty())) {
|
||||||
|
// Handle top comments and empty lines before the first key.
|
||||||
|
mergedLines.add(line);
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!key.isEmpty())
|
||||||
|
beforeFirstKey = false;
|
||||||
|
|
||||||
|
if (userKeys.contains(key)) {
|
||||||
|
// If user has any version (commented or uncommented) of this key, skip the
|
||||||
|
// template line
|
||||||
|
Optional<String> userValue = userLines.stream()
|
||||||
|
.filter(l -> extractKey.apply(l).equalsIgnoreCase(key) && !isCommented.apply(l)).findFirst();
|
||||||
|
if (userValue.isPresent())
|
||||||
|
mergedLines.add(userValue.get());
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (isCommented.apply(line) || line.trim().isEmpty() || !userKeys.contains(key)) {
|
||||||
|
mergedLines.add(line); // If line is commented, empty or key not present in user's file, retain the
|
||||||
|
// template line
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Add any additional uncommented user lines that are not present in the
|
||||||
|
// template
|
||||||
|
for (String userLine : userLines) {
|
||||||
|
String userKey = extractKey.apply(userLine);
|
||||||
|
boolean isPresentInTemplate = templateLines.stream().map(extractKey)
|
||||||
|
.anyMatch(templateKey -> templateKey.equalsIgnoreCase(userKey));
|
||||||
|
if (!isPresentInTemplate && !isCommented.apply(userLine)) {
|
||||||
|
mergedLines.add(userLine);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
Files.write(outputPath, mergedLines, StandardCharsets.UTF_8);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -1,200 +1,228 @@
|
|||||||
package stirling.software.SPDF.config;
|
package stirling.software.SPDF.config;
|
||||||
|
|
||||||
import java.util.HashSet;
|
import java.util.HashSet;
|
||||||
import java.util.Map;
|
import java.util.List;
|
||||||
import java.util.Set;
|
import java.util.Map;
|
||||||
import java.util.concurrent.ConcurrentHashMap;
|
import java.util.Set;
|
||||||
|
import java.util.concurrent.ConcurrentHashMap;
|
||||||
import org.slf4j.Logger;
|
|
||||||
import org.slf4j.LoggerFactory;
|
import org.slf4j.Logger;
|
||||||
import org.springframework.stereotype.Service;
|
import org.slf4j.LoggerFactory;
|
||||||
@Service
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
public class EndpointConfiguration {
|
import org.springframework.stereotype.Service;
|
||||||
private static final Logger logger = LoggerFactory.getLogger(EndpointConfiguration.class);
|
|
||||||
private Map<String, Boolean> endpointStatuses = new ConcurrentHashMap<>();
|
import stirling.software.SPDF.model.ApplicationProperties;
|
||||||
private Map<String, Set<String>> endpointGroups = new ConcurrentHashMap<>();
|
@Service
|
||||||
|
public class EndpointConfiguration {
|
||||||
public EndpointConfiguration() {
|
private static final Logger logger = LoggerFactory.getLogger(EndpointConfiguration.class);
|
||||||
init();
|
private Map<String, Boolean> endpointStatuses = new ConcurrentHashMap<>();
|
||||||
processEnvironmentConfigs();
|
private Map<String, Set<String>> endpointGroups = new ConcurrentHashMap<>();
|
||||||
}
|
|
||||||
|
private final ApplicationProperties applicationProperties;
|
||||||
public void enableEndpoint(String endpoint) {
|
|
||||||
endpointStatuses.put(endpoint, true);
|
@Autowired
|
||||||
}
|
public EndpointConfiguration(ApplicationProperties applicationProperties) {
|
||||||
|
this.applicationProperties = applicationProperties;
|
||||||
public void disableEndpoint(String endpoint) {
|
init();
|
||||||
if(!endpointStatuses.containsKey(endpoint) || endpointStatuses.get(endpoint) != false) {
|
processEnvironmentConfigs();
|
||||||
logger.info("Disabling {}", endpoint);
|
}
|
||||||
endpointStatuses.put(endpoint, false);
|
|
||||||
}
|
public void enableEndpoint(String endpoint) {
|
||||||
}
|
endpointStatuses.put(endpoint, true);
|
||||||
|
}
|
||||||
public boolean isEndpointEnabled(String endpoint) {
|
|
||||||
if (endpoint.startsWith("/")) {
|
public void disableEndpoint(String endpoint) {
|
||||||
endpoint = endpoint.substring(1);
|
if(!endpointStatuses.containsKey(endpoint) || endpointStatuses.get(endpoint) != false) {
|
||||||
}
|
logger.info("Disabling {}", endpoint);
|
||||||
return endpointStatuses.getOrDefault(endpoint, true);
|
endpointStatuses.put(endpoint, false);
|
||||||
}
|
}
|
||||||
|
}
|
||||||
public void addEndpointToGroup(String group, String endpoint) {
|
|
||||||
endpointGroups.computeIfAbsent(group, k -> new HashSet<>()).add(endpoint);
|
public boolean isEndpointEnabled(String endpoint) {
|
||||||
}
|
if (endpoint.startsWith("/")) {
|
||||||
|
endpoint = endpoint.substring(1);
|
||||||
public void enableGroup(String group) {
|
}
|
||||||
Set<String> endpoints = endpointGroups.get(group);
|
return endpointStatuses.getOrDefault(endpoint, true);
|
||||||
if (endpoints != null) {
|
}
|
||||||
for (String endpoint : endpoints) {
|
|
||||||
enableEndpoint(endpoint);
|
public void addEndpointToGroup(String group, String endpoint) {
|
||||||
}
|
endpointGroups.computeIfAbsent(group, k -> new HashSet<>()).add(endpoint);
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
public void enableGroup(String group) {
|
||||||
public void disableGroup(String group) {
|
Set<String> endpoints = endpointGroups.get(group);
|
||||||
Set<String> endpoints = endpointGroups.get(group);
|
if (endpoints != null) {
|
||||||
if (endpoints != null) {
|
for (String endpoint : endpoints) {
|
||||||
for (String endpoint : endpoints) {
|
enableEndpoint(endpoint);
|
||||||
disableEndpoint(endpoint);
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
public void disableGroup(String group) {
|
||||||
public void init() {
|
Set<String> endpoints = endpointGroups.get(group);
|
||||||
// Adding endpoints to "PageOps" group
|
if (endpoints != null) {
|
||||||
addEndpointToGroup("PageOps", "remove-pages");
|
for (String endpoint : endpoints) {
|
||||||
addEndpointToGroup("PageOps", "merge-pdfs");
|
disableEndpoint(endpoint);
|
||||||
addEndpointToGroup("PageOps", "split-pdfs");
|
}
|
||||||
addEndpointToGroup("PageOps", "pdf-organizer");
|
}
|
||||||
addEndpointToGroup("PageOps", "rotate-pdf");
|
}
|
||||||
addEndpointToGroup("PageOps", "multi-page-layout");
|
|
||||||
addEndpointToGroup("PageOps", "scale-pages");
|
public void init() {
|
||||||
|
// Adding endpoints to "PageOps" group
|
||||||
// Adding endpoints to "Convert" group
|
addEndpointToGroup("PageOps", "remove-pages");
|
||||||
addEndpointToGroup("Convert", "pdf-to-img");
|
addEndpointToGroup("PageOps", "merge-pdfs");
|
||||||
addEndpointToGroup("Convert", "img-to-pdf");
|
addEndpointToGroup("PageOps", "split-pdfs");
|
||||||
addEndpointToGroup("Convert", "pdf-to-pdfa");
|
addEndpointToGroup("PageOps", "pdf-organizer");
|
||||||
addEndpointToGroup("Convert", "file-to-pdf");
|
addEndpointToGroup("PageOps", "rotate-pdf");
|
||||||
addEndpointToGroup("Convert", "xlsx-to-pdf");
|
addEndpointToGroup("PageOps", "multi-page-layout");
|
||||||
addEndpointToGroup("Convert", "pdf-to-word");
|
addEndpointToGroup("PageOps", "scale-pages");
|
||||||
addEndpointToGroup("Convert", "pdf-to-presentation");
|
addEndpointToGroup("PageOps", "adjust-contrast");
|
||||||
addEndpointToGroup("Convert", "pdf-to-text");
|
addEndpointToGroup("PageOps", "crop");
|
||||||
addEndpointToGroup("Convert", "pdf-to-html");
|
addEndpointToGroup("PageOps", "auto-split-pdf");
|
||||||
addEndpointToGroup("Convert", "pdf-to-xml");
|
addEndpointToGroup("PageOps", "extract-page");
|
||||||
|
addEndpointToGroup("PageOps", "pdf-to-single-page");
|
||||||
// Adding endpoints to "Security" group
|
|
||||||
addEndpointToGroup("Security", "add-password");
|
// Adding endpoints to "Convert" group
|
||||||
addEndpointToGroup("Security", "remove-password");
|
addEndpointToGroup("Convert", "pdf-to-img");
|
||||||
addEndpointToGroup("Security", "change-permissions");
|
addEndpointToGroup("Convert", "img-to-pdf");
|
||||||
addEndpointToGroup("Security", "add-watermark");
|
addEndpointToGroup("Convert", "pdf-to-pdfa");
|
||||||
addEndpointToGroup("Security", "cert-sign");
|
addEndpointToGroup("Convert", "file-to-pdf");
|
||||||
|
addEndpointToGroup("Convert", "xlsx-to-pdf");
|
||||||
|
addEndpointToGroup("Convert", "pdf-to-word");
|
||||||
|
addEndpointToGroup("Convert", "pdf-to-presentation");
|
||||||
// Adding endpoints to "Other" group
|
addEndpointToGroup("Convert", "pdf-to-text");
|
||||||
addEndpointToGroup("Other", "ocr-pdf");
|
addEndpointToGroup("Convert", "pdf-to-html");
|
||||||
addEndpointToGroup("Other", "add-image");
|
addEndpointToGroup("Convert", "pdf-to-xml");
|
||||||
addEndpointToGroup("Other", "compress-pdf");
|
addEndpointToGroup("Convert", "html-to-pdf");
|
||||||
addEndpointToGroup("Other", "extract-images");
|
addEndpointToGroup("Convert", "url-to-pdf");
|
||||||
addEndpointToGroup("Other", "change-metadata");
|
addEndpointToGroup("Convert", "markdown-to-pdf");
|
||||||
addEndpointToGroup("Other", "extract-image-scans");
|
|
||||||
addEndpointToGroup("Other", "sign");
|
// Adding endpoints to "Security" group
|
||||||
addEndpointToGroup("Other", "flatten");
|
addEndpointToGroup("Security", "add-password");
|
||||||
addEndpointToGroup("Other", "repair");
|
addEndpointToGroup("Security", "remove-password");
|
||||||
addEndpointToGroup("Other", "remove-blanks");
|
addEndpointToGroup("Security", "change-permissions");
|
||||||
addEndpointToGroup("Other", "compare");
|
addEndpointToGroup("Security", "add-watermark");
|
||||||
|
addEndpointToGroup("Security", "cert-sign");
|
||||||
|
addEndpointToGroup("Security", "sanitize-pdf");
|
||||||
|
|
||||||
|
|
||||||
|
// Adding endpoints to "Other" group
|
||||||
|
addEndpointToGroup("Other", "ocr-pdf");
|
||||||
|
addEndpointToGroup("Other", "add-image");
|
||||||
//CLI
|
addEndpointToGroup("Other", "compress-pdf");
|
||||||
addEndpointToGroup("CLI", "compress-pdf");
|
addEndpointToGroup("Other", "extract-images");
|
||||||
addEndpointToGroup("CLI", "extract-image-scans");
|
addEndpointToGroup("Other", "change-metadata");
|
||||||
addEndpointToGroup("CLI", "remove-blanks");
|
addEndpointToGroup("Other", "extract-image-scans");
|
||||||
addEndpointToGroup("CLI", "repair");
|
addEndpointToGroup("Other", "sign");
|
||||||
addEndpointToGroup("CLI", "pdf-to-pdfa");
|
addEndpointToGroup("Other", "flatten");
|
||||||
addEndpointToGroup("CLI", "file-to-pdf");
|
addEndpointToGroup("Other", "repair");
|
||||||
addEndpointToGroup("CLI", "xlsx-to-pdf");
|
addEndpointToGroup("Other", "remove-blanks");
|
||||||
addEndpointToGroup("CLI", "pdf-to-word");
|
addEndpointToGroup("Other", "compare");
|
||||||
addEndpointToGroup("CLI", "pdf-to-presentation");
|
addEndpointToGroup("Other", "add-page-numbers");
|
||||||
addEndpointToGroup("CLI", "pdf-to-text");
|
addEndpointToGroup("Other", "auto-rename");
|
||||||
addEndpointToGroup("CLI", "pdf-to-html");
|
addEndpointToGroup("Other", "get-info-on-pdf");
|
||||||
addEndpointToGroup("CLI", "pdf-to-xml");
|
addEndpointToGroup("Other", "show-javascript");
|
||||||
addEndpointToGroup("CLI", "ocr-pdf");
|
|
||||||
|
|
||||||
//python
|
|
||||||
addEndpointToGroup("Python", "extract-image-scans");
|
//CLI
|
||||||
addEndpointToGroup("Python", "remove-blanks");
|
addEndpointToGroup("CLI", "compress-pdf");
|
||||||
|
addEndpointToGroup("CLI", "extract-image-scans");
|
||||||
|
addEndpointToGroup("CLI", "remove-blanks");
|
||||||
|
addEndpointToGroup("CLI", "repair");
|
||||||
//openCV
|
addEndpointToGroup("CLI", "pdf-to-pdfa");
|
||||||
addEndpointToGroup("OpenCV", "extract-image-scans");
|
addEndpointToGroup("CLI", "file-to-pdf");
|
||||||
addEndpointToGroup("OpenCV", "remove-blanks");
|
addEndpointToGroup("CLI", "xlsx-to-pdf");
|
||||||
|
addEndpointToGroup("CLI", "pdf-to-word");
|
||||||
//LibreOffice
|
addEndpointToGroup("CLI", "pdf-to-presentation");
|
||||||
addEndpointToGroup("LibreOffice", "repair");
|
addEndpointToGroup("CLI", "pdf-to-text");
|
||||||
addEndpointToGroup("LibreOffice", "file-to-pdf");
|
addEndpointToGroup("CLI", "pdf-to-html");
|
||||||
addEndpointToGroup("LibreOffice", "xlsx-to-pdf");
|
addEndpointToGroup("CLI", "pdf-to-xml");
|
||||||
addEndpointToGroup("LibreOffice", "pdf-to-word");
|
addEndpointToGroup("CLI", "ocr-pdf");
|
||||||
addEndpointToGroup("LibreOffice", "pdf-to-presentation");
|
addEndpointToGroup("CLI", "html-to-pdf");
|
||||||
addEndpointToGroup("LibreOffice", "pdf-to-text");
|
addEndpointToGroup("CLI", "url-to-pdf");
|
||||||
addEndpointToGroup("LibreOffice", "pdf-to-html");
|
|
||||||
addEndpointToGroup("LibreOffice", "pdf-to-xml");
|
|
||||||
|
//python
|
||||||
|
addEndpointToGroup("Python", "extract-image-scans");
|
||||||
//OCRmyPDF
|
addEndpointToGroup("Python", "remove-blanks");
|
||||||
addEndpointToGroup("OCRmyPDF", "compress-pdf");
|
addEndpointToGroup("Python", "html-to-pdf");
|
||||||
addEndpointToGroup("OCRmyPDF", "pdf-to-pdfa");
|
addEndpointToGroup("Python", "url-to-pdf");
|
||||||
addEndpointToGroup("OCRmyPDF", "ocr-pdf");
|
|
||||||
|
//openCV
|
||||||
//Java
|
addEndpointToGroup("OpenCV", "extract-image-scans");
|
||||||
addEndpointToGroup("Java", "merge-pdfs");
|
addEndpointToGroup("OpenCV", "remove-blanks");
|
||||||
addEndpointToGroup("Java", "remove-pages");
|
|
||||||
addEndpointToGroup("Java", "split-pdfs");
|
//LibreOffice
|
||||||
addEndpointToGroup("Java", "pdf-organizer");
|
addEndpointToGroup("LibreOffice", "repair");
|
||||||
addEndpointToGroup("Java", "rotate-pdf");
|
addEndpointToGroup("LibreOffice", "file-to-pdf");
|
||||||
addEndpointToGroup("Java", "pdf-to-img");
|
addEndpointToGroup("LibreOffice", "xlsx-to-pdf");
|
||||||
addEndpointToGroup("Java", "img-to-pdf");
|
addEndpointToGroup("LibreOffice", "pdf-to-word");
|
||||||
addEndpointToGroup("Java", "add-password");
|
addEndpointToGroup("LibreOffice", "pdf-to-presentation");
|
||||||
addEndpointToGroup("Java", "remove-password");
|
addEndpointToGroup("LibreOffice", "pdf-to-text");
|
||||||
addEndpointToGroup("Java", "change-permissions");
|
addEndpointToGroup("LibreOffice", "pdf-to-html");
|
||||||
addEndpointToGroup("Java", "add-watermark");
|
addEndpointToGroup("LibreOffice", "pdf-to-xml");
|
||||||
addEndpointToGroup("Java", "add-image");
|
|
||||||
addEndpointToGroup("Java", "extract-images");
|
|
||||||
addEndpointToGroup("Java", "change-metadata");
|
//OCRmyPDF
|
||||||
addEndpointToGroup("Java", "cert-sign");
|
addEndpointToGroup("OCRmyPDF", "compress-pdf");
|
||||||
addEndpointToGroup("Java", "multi-page-layout");
|
addEndpointToGroup("OCRmyPDF", "pdf-to-pdfa");
|
||||||
addEndpointToGroup("Java", "scale-pages");
|
addEndpointToGroup("OCRmyPDF", "ocr-pdf");
|
||||||
|
|
||||||
|
//Java
|
||||||
//Javascript
|
addEndpointToGroup("Java", "merge-pdfs");
|
||||||
addEndpointToGroup("Javascript", "pdf-organizer");
|
addEndpointToGroup("Java", "remove-pages");
|
||||||
addEndpointToGroup("Javascript", "sign");
|
addEndpointToGroup("Java", "split-pdfs");
|
||||||
addEndpointToGroup("Javascript", "compare");
|
addEndpointToGroup("Java", "pdf-organizer");
|
||||||
|
addEndpointToGroup("Java", "rotate-pdf");
|
||||||
}
|
addEndpointToGroup("Java", "pdf-to-img");
|
||||||
|
addEndpointToGroup("Java", "img-to-pdf");
|
||||||
private void processEnvironmentConfigs() {
|
addEndpointToGroup("Java", "add-password");
|
||||||
String endpointsToRemove = System.getenv("ENDPOINTS_TO_REMOVE");
|
addEndpointToGroup("Java", "remove-password");
|
||||||
String groupsToRemove = System.getenv("GROUPS_TO_REMOVE");
|
addEndpointToGroup("Java", "change-permissions");
|
||||||
|
addEndpointToGroup("Java", "add-watermark");
|
||||||
if (endpointsToRemove != null) {
|
addEndpointToGroup("Java", "add-image");
|
||||||
String[] endpoints = endpointsToRemove.split(",");
|
addEndpointToGroup("Java", "extract-images");
|
||||||
for (String endpoint : endpoints) {
|
addEndpointToGroup("Java", "change-metadata");
|
||||||
disableEndpoint(endpoint.trim());
|
addEndpointToGroup("Java", "cert-sign");
|
||||||
}
|
addEndpointToGroup("Java", "multi-page-layout");
|
||||||
}
|
addEndpointToGroup("Java", "scale-pages");
|
||||||
|
addEndpointToGroup("Java", "add-page-numbers");
|
||||||
if (groupsToRemove != null) {
|
addEndpointToGroup("Java", "auto-rename");
|
||||||
String[] groups = groupsToRemove.split(",");
|
addEndpointToGroup("Java", "auto-split-pdf");
|
||||||
for (String group : groups) {
|
addEndpointToGroup("Java", "sanitize-pdf");
|
||||||
disableGroup(group.trim());
|
addEndpointToGroup("Java", "crop");
|
||||||
}
|
addEndpointToGroup("Java", "get-info-on-pdf");
|
||||||
}
|
addEndpointToGroup("Java", "extract-page");
|
||||||
}
|
addEndpointToGroup("Java", "pdf-to-single-page");
|
||||||
|
addEndpointToGroup("Java", "markdown-to-pdf");
|
||||||
}
|
addEndpointToGroup("Java", "show-javascript");
|
||||||
|
|
||||||
|
//Javascript
|
||||||
|
addEndpointToGroup("Javascript", "pdf-organizer");
|
||||||
|
addEndpointToGroup("Javascript", "sign");
|
||||||
|
addEndpointToGroup("Javascript", "compare");
|
||||||
|
addEndpointToGroup("Javascript", "adjust-contrast");
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
private void processEnvironmentConfigs() {
|
||||||
|
List<String> endpointsToRemove = applicationProperties.getEndpoints().getToRemove();
|
||||||
|
List<String> groupsToRemove = applicationProperties.getEndpoints().getGroupsToRemove();
|
||||||
|
|
||||||
|
if (endpointsToRemove != null) {
|
||||||
|
for (String endpoint : endpointsToRemove) {
|
||||||
|
disableEndpoint(endpoint.trim());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (groupsToRemove != null) {
|
||||||
|
for (String group : groupsToRemove) {
|
||||||
|
disableGroup(group.trim());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -1,26 +1,26 @@
|
|||||||
package stirling.software.SPDF.config;
|
package stirling.software.SPDF.config;
|
||||||
|
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
import org.springframework.stereotype.Component;
|
import org.springframework.stereotype.Component;
|
||||||
import org.springframework.web.servlet.HandlerInterceptor;
|
import org.springframework.web.servlet.HandlerInterceptor;
|
||||||
|
|
||||||
import jakarta.servlet.http.HttpServletRequest;
|
import jakarta.servlet.http.HttpServletRequest;
|
||||||
import jakarta.servlet.http.HttpServletResponse;
|
import jakarta.servlet.http.HttpServletResponse;
|
||||||
|
|
||||||
@Component
|
@Component
|
||||||
public class EndpointInterceptor implements HandlerInterceptor {
|
public class EndpointInterceptor implements HandlerInterceptor {
|
||||||
|
|
||||||
@Autowired
|
@Autowired
|
||||||
private EndpointConfiguration endpointConfiguration;
|
private EndpointConfiguration endpointConfiguration;
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)
|
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)
|
||||||
throws Exception {
|
throws Exception {
|
||||||
String requestURI = request.getRequestURI();
|
String requestURI = request.getRequestURI();
|
||||||
if (!endpointConfiguration.isEndpointEnabled(requestURI)) {
|
if (!endpointConfiguration.isEndpointEnabled(requestURI)) {
|
||||||
response.sendError(HttpServletResponse.SC_FORBIDDEN, "This endpoint is disabled");
|
response.sendError(HttpServletResponse.SC_FORBIDDEN, "This endpoint is disabled");
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -1,24 +1,24 @@
|
|||||||
package stirling.software.SPDF.config;
|
package stirling.software.SPDF.config;
|
||||||
import org.springframework.context.annotation.Bean;
|
import org.springframework.context.annotation.Bean;
|
||||||
import org.springframework.context.annotation.Configuration;
|
import org.springframework.context.annotation.Configuration;
|
||||||
|
|
||||||
import io.micrometer.core.instrument.Meter;
|
import io.micrometer.core.instrument.Meter;
|
||||||
import io.micrometer.core.instrument.config.MeterFilter;
|
import io.micrometer.core.instrument.config.MeterFilter;
|
||||||
import io.micrometer.core.instrument.config.MeterFilterReply;
|
import io.micrometer.core.instrument.config.MeterFilterReply;
|
||||||
|
|
||||||
@Configuration
|
@Configuration
|
||||||
public class MetricsConfig {
|
public class MetricsConfig {
|
||||||
|
|
||||||
@Bean
|
@Bean
|
||||||
public MeterFilter meterFilter() {
|
public MeterFilter meterFilter() {
|
||||||
return new MeterFilter() {
|
return new MeterFilter() {
|
||||||
@Override
|
@Override
|
||||||
public MeterFilterReply accept(Meter.Id id) {
|
public MeterFilterReply accept(Meter.Id id) {
|
||||||
if (id.getName().equals("http.requests")) {
|
if (id.getName().equals("http.requests")) {
|
||||||
return MeterFilterReply.NEUTRAL;
|
return MeterFilterReply.NEUTRAL;
|
||||||
}
|
}
|
||||||
return MeterFilterReply.DENY;
|
return MeterFilterReply.DENY;
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -1,46 +1,48 @@
|
|||||||
package stirling.software.SPDF.config;
|
package stirling.software.SPDF.config;
|
||||||
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
|
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
import org.springframework.stereotype.Component;
|
import org.springframework.stereotype.Component;
|
||||||
import org.springframework.web.filter.OncePerRequestFilter;
|
import org.springframework.web.filter.OncePerRequestFilter;
|
||||||
|
|
||||||
import io.micrometer.core.instrument.Counter;
|
import io.micrometer.core.instrument.Counter;
|
||||||
import io.micrometer.core.instrument.MeterRegistry;
|
import io.micrometer.core.instrument.MeterRegistry;
|
||||||
import jakarta.servlet.FilterChain;
|
import jakarta.servlet.FilterChain;
|
||||||
import jakarta.servlet.ServletException;
|
import jakarta.servlet.ServletException;
|
||||||
import jakarta.servlet.http.HttpServletRequest;
|
import jakarta.servlet.http.HttpServletRequest;
|
||||||
import jakarta.servlet.http.HttpServletResponse;
|
import jakarta.servlet.http.HttpServletResponse;
|
||||||
|
|
||||||
@Component
|
@Component
|
||||||
public class MetricsFilter extends OncePerRequestFilter {
|
public class MetricsFilter extends OncePerRequestFilter {
|
||||||
|
|
||||||
private final MeterRegistry meterRegistry;
|
private final MeterRegistry meterRegistry;
|
||||||
|
|
||||||
@Autowired
|
@Autowired
|
||||||
public MetricsFilter(MeterRegistry meterRegistry) {
|
public MetricsFilter(MeterRegistry meterRegistry) {
|
||||||
this.meterRegistry = meterRegistry;
|
this.meterRegistry = meterRegistry;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain)
|
protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain)
|
||||||
throws ServletException, IOException {
|
throws ServletException, IOException {
|
||||||
String uri = request.getRequestURI();
|
String uri = request.getRequestURI();
|
||||||
|
|
||||||
// Ignore static resources
|
//System.out.println("uri="+uri + ", method=" + request.getMethod() );
|
||||||
if (!(uri.startsWith("/js") || uri.startsWith("/images") || uri.endsWith(".ico") || uri.endsWith(".css") || uri.endsWith(".svg")|| uri.endsWith(".js") || uri.contains("swagger") || uri.startsWith("/api"))) {
|
// Ignore static resources
|
||||||
Counter counter = Counter.builder("http.requests")
|
if (!(uri.startsWith("/js") || uri.startsWith("api-docs") || uri.endsWith("robots.txt") || uri.startsWith("/images") || uri.endsWith(".png") || uri.endsWith(".ico") || uri.endsWith(".css") || uri.endsWith(".svg")|| uri.endsWith(".js") || uri.contains("swagger") || uri.startsWith("/api"))) {
|
||||||
.tag("uri", uri)
|
Counter counter = Counter.builder("http.requests")
|
||||||
.tag("method", request.getMethod())
|
.tag("uri", uri)
|
||||||
.register(meterRegistry);
|
.tag("method", request.getMethod())
|
||||||
|
.register(meterRegistry);
|
||||||
counter.increment();
|
|
||||||
}
|
counter.increment();
|
||||||
|
//System.out.println("Counted");
|
||||||
filterChain.doFilter(request, response);
|
}
|
||||||
}
|
|
||||||
|
filterChain.doFilter(request, response);
|
||||||
|
}
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
|
}
|
||||||
|
|||||||
@@ -1,36 +1,27 @@
|
|||||||
package stirling.software.SPDF.config;
|
package stirling.software.SPDF.config;
|
||||||
|
|
||||||
import java.io.IOException;
|
import org.springframework.context.annotation.Bean;
|
||||||
import java.io.InputStream;
|
import org.springframework.context.annotation.Configuration;
|
||||||
import java.util.Properties;
|
|
||||||
|
import io.swagger.v3.oas.models.Components;
|
||||||
import org.springframework.context.annotation.Bean;
|
import io.swagger.v3.oas.models.OpenAPI;
|
||||||
import org.springframework.context.annotation.Configuration;
|
import io.swagger.v3.oas.models.info.Info;
|
||||||
|
|
||||||
import io.swagger.v3.oas.models.Components;
|
@Configuration
|
||||||
import io.swagger.v3.oas.models.OpenAPI;
|
public class OpenApiConfig {
|
||||||
import io.swagger.v3.oas.models.info.Info;
|
|
||||||
|
@Bean
|
||||||
@Configuration
|
public OpenAPI customOpenAPI() {
|
||||||
public class OpenApiConfig {
|
String version = getClass().getPackage().getImplementationVersion();
|
||||||
|
if (version == null) {
|
||||||
@Bean
|
|
||||||
public OpenAPI customOpenAPI() {
|
version = "1.0.0"; // default version if all else fails
|
||||||
String version = getClass().getPackage().getImplementationVersion();
|
|
||||||
if (version == null) {
|
}
|
||||||
Properties props = new Properties();
|
|
||||||
try (InputStream input = getClass().getClassLoader().getResourceAsStream("version.properties")) {
|
return new OpenAPI().components(new Components()).info(
|
||||||
props.load(input);
|
new Info().title("Stirling PDF API").version(version).description("API documentation for all Server-Side processing.\nPlease note some functionality might be UI only and missing from here."));
|
||||||
version = props.getProperty("version");
|
}
|
||||||
} catch (IOException ex) {
|
|
||||||
ex.printStackTrace();
|
|
||||||
version = "1.0.0"; // default version if all else fails
|
}
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return new OpenAPI().components(new Components()).info(
|
|
||||||
new Info().title("Stirling PDF API").version(version).description("API documentation for all Server-Side processing.\nPlease note some functionality might be UI only and missing from here."));
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|||||||
@@ -0,0 +1,20 @@
|
|||||||
|
package stirling.software.SPDF.config;
|
||||||
|
|
||||||
|
|
||||||
|
import java.time.LocalDateTime;
|
||||||
|
|
||||||
|
import org.springframework.context.ApplicationListener;
|
||||||
|
import org.springframework.context.event.ContextRefreshedEvent;
|
||||||
|
import org.springframework.stereotype.Component;
|
||||||
|
|
||||||
|
@Component
|
||||||
|
public class StartupApplicationListener implements ApplicationListener<ContextRefreshedEvent> {
|
||||||
|
|
||||||
|
public static LocalDateTime startTime;
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onApplicationEvent(ContextRefreshedEvent event) {
|
||||||
|
startTime = LocalDateTime.now();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
@@ -1,18 +1,27 @@
|
|||||||
package stirling.software.SPDF.config;
|
package stirling.software.SPDF.config;
|
||||||
|
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
import org.springframework.context.annotation.Configuration;
|
import org.springframework.context.annotation.Configuration;
|
||||||
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
|
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
|
||||||
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
|
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
|
||||||
|
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
|
||||||
@Configuration
|
|
||||||
public class WebMvcConfig implements WebMvcConfigurer {
|
@Configuration
|
||||||
|
public class WebMvcConfig implements WebMvcConfigurer {
|
||||||
@Autowired
|
|
||||||
private EndpointInterceptor endpointInterceptor;
|
@Autowired
|
||||||
|
private EndpointInterceptor endpointInterceptor;
|
||||||
@Override
|
|
||||||
public void addInterceptors(InterceptorRegistry registry) {
|
@Override
|
||||||
registry.addInterceptor(endpointInterceptor);
|
public void addInterceptors(InterceptorRegistry registry) {
|
||||||
}
|
registry.addInterceptor(endpointInterceptor);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void addResourceHandlers(ResourceHandlerRegistry registry) {
|
||||||
|
// Handler for external static resources
|
||||||
|
registry.addResourceHandler("/**")
|
||||||
|
.addResourceLocations("file:customFiles/static/", "classpath:/static/");
|
||||||
|
//.setCachePeriod(0); // Optional: disable caching
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
@@ -0,0 +1,23 @@
|
|||||||
|
package stirling.software.SPDF.config;
|
||||||
|
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.util.Properties;
|
||||||
|
|
||||||
|
import org.springframework.beans.factory.config.YamlPropertiesFactoryBean;
|
||||||
|
import org.springframework.core.env.PropertiesPropertySource;
|
||||||
|
import org.springframework.core.env.PropertySource;
|
||||||
|
import org.springframework.core.io.support.EncodedResource;
|
||||||
|
import org.springframework.core.io.support.PropertySourceFactory;
|
||||||
|
public class YamlPropertySourceFactory implements PropertySourceFactory {
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public PropertySource<?> createPropertySource(String name, EncodedResource encodedResource)
|
||||||
|
throws IOException {
|
||||||
|
YamlPropertiesFactoryBean factory = new YamlPropertiesFactoryBean();
|
||||||
|
factory.setResources(encodedResource.getResource());
|
||||||
|
|
||||||
|
Properties properties = factory.getObject();
|
||||||
|
|
||||||
|
return new PropertiesPropertySource(encodedResource.getResource().getFilename(), properties);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,26 @@
|
|||||||
|
package stirling.software.SPDF.config.security;
|
||||||
|
|
||||||
|
import java.io.IOException;
|
||||||
|
|
||||||
|
import org.springframework.security.authentication.BadCredentialsException;
|
||||||
|
import org.springframework.security.authentication.LockedException;
|
||||||
|
import org.springframework.security.core.AuthenticationException;
|
||||||
|
import org.springframework.security.web.authentication.SimpleUrlAuthenticationFailureHandler;
|
||||||
|
|
||||||
|
import jakarta.servlet.ServletException;
|
||||||
|
import jakarta.servlet.http.HttpServletRequest;
|
||||||
|
import jakarta.servlet.http.HttpServletResponse;
|
||||||
|
|
||||||
|
public class CustomAuthenticationFailureHandler extends SimpleUrlAuthenticationFailureHandler {
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onAuthenticationFailure(HttpServletRequest request, HttpServletResponse response, AuthenticationException exception)
|
||||||
|
throws IOException, ServletException {
|
||||||
|
if (exception.getClass().isAssignableFrom(BadCredentialsException.class)) {
|
||||||
|
setDefaultFailureUrl("/login?error=badcredentials");
|
||||||
|
} else if (exception.getClass().isAssignableFrom(LockedException.class)) {
|
||||||
|
setDefaultFailureUrl("/login?error=locked");
|
||||||
|
}
|
||||||
|
super.onAuthenticationFailure(request, response, exception);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,45 @@
|
|||||||
|
package stirling.software.SPDF.config.security;
|
||||||
|
|
||||||
|
import java.util.Collection;
|
||||||
|
import java.util.Set;
|
||||||
|
import java.util.stream.Collectors;
|
||||||
|
|
||||||
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
import org.springframework.security.core.GrantedAuthority;
|
||||||
|
import org.springframework.security.core.authority.SimpleGrantedAuthority;
|
||||||
|
import org.springframework.security.core.userdetails.UserDetails;
|
||||||
|
import org.springframework.security.core.userdetails.UserDetailsService;
|
||||||
|
import org.springframework.security.core.userdetails.UsernameNotFoundException;
|
||||||
|
import org.springframework.stereotype.Service;
|
||||||
|
|
||||||
|
import stirling.software.SPDF.model.Authority;
|
||||||
|
import stirling.software.SPDF.model.User;
|
||||||
|
import stirling.software.SPDF.repository.UserRepository;
|
||||||
|
|
||||||
|
@Service
|
||||||
|
public class CustomUserDetailsService implements UserDetailsService {
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
private UserRepository userRepository;
|
||||||
|
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
|
||||||
|
User user = userRepository.findByUsername(username)
|
||||||
|
.orElseThrow(() -> new UsernameNotFoundException("No user found with username: " + username));
|
||||||
|
|
||||||
|
return new org.springframework.security.core.userdetails.User(
|
||||||
|
user.getUsername(),
|
||||||
|
user.getPassword(),
|
||||||
|
user.isEnabled(),
|
||||||
|
true, true, true,
|
||||||
|
getAuthorities(user.getAuthorities())
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
private Collection<? extends GrantedAuthority> getAuthorities(Set<Authority> authorities) {
|
||||||
|
return authorities.stream()
|
||||||
|
.map(authority -> new SimpleGrantedAuthority(authority.getAuthority()))
|
||||||
|
.collect(Collectors.toList());
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,53 @@
|
|||||||
|
package stirling.software.SPDF.config.security;
|
||||||
|
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.util.Optional;
|
||||||
|
|
||||||
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
import org.springframework.context.annotation.Lazy;
|
||||||
|
import org.springframework.security.core.Authentication;
|
||||||
|
import org.springframework.security.core.context.SecurityContextHolder;
|
||||||
|
import org.springframework.stereotype.Component;
|
||||||
|
import org.springframework.web.filter.OncePerRequestFilter;
|
||||||
|
|
||||||
|
import jakarta.servlet.FilterChain;
|
||||||
|
import jakarta.servlet.ServletException;
|
||||||
|
import jakarta.servlet.http.HttpServletRequest;
|
||||||
|
import jakarta.servlet.http.HttpServletResponse;
|
||||||
|
import stirling.software.SPDF.model.User;
|
||||||
|
|
||||||
|
@Component
|
||||||
|
public class FirstLoginFilter extends OncePerRequestFilter {
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
@Lazy
|
||||||
|
private UserService userService;
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain) throws ServletException, IOException {
|
||||||
|
String method = request.getMethod();
|
||||||
|
String requestURI = request.getRequestURI();
|
||||||
|
// Check if the request is for static resources
|
||||||
|
boolean isStaticResource = requestURI.startsWith("/css/")
|
||||||
|
|| requestURI.startsWith("/js/")
|
||||||
|
|| requestURI.startsWith("/images/")
|
||||||
|
|| requestURI.startsWith("/public/")
|
||||||
|
|| requestURI.endsWith(".svg");
|
||||||
|
|
||||||
|
// If it's a static resource, just continue the filter chain and skip the logic below
|
||||||
|
if (isStaticResource) {
|
||||||
|
filterChain.doFilter(request, response);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
|
||||||
|
if (authentication != null && authentication.isAuthenticated()) {
|
||||||
|
Optional<User> user = userService.findByUsername(authentication.getName());
|
||||||
|
if ("GET".equalsIgnoreCase(method) && user.isPresent() && user.get().isFirstLogin() && !"/change-creds".equals(requestURI)) {
|
||||||
|
response.sendRedirect("/change-creds");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
filterChain.doFilter(request, response);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,85 @@
|
|||||||
|
package stirling.software.SPDF.config.security;
|
||||||
|
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.nio.file.Files;
|
||||||
|
import java.nio.file.Path;
|
||||||
|
import java.nio.file.Paths;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.UUID;
|
||||||
|
|
||||||
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
import org.springframework.stereotype.Component;
|
||||||
|
|
||||||
|
import jakarta.annotation.PostConstruct;
|
||||||
|
import stirling.software.SPDF.model.ApplicationProperties;
|
||||||
|
import stirling.software.SPDF.model.Role;
|
||||||
|
@Component
|
||||||
|
public class InitialSecuritySetup {
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
private UserService userService;
|
||||||
|
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
ApplicationProperties applicationProperties;
|
||||||
|
|
||||||
|
@PostConstruct
|
||||||
|
public void init() {
|
||||||
|
if (!userService.hasUsers()) {
|
||||||
|
|
||||||
|
|
||||||
|
String initialUsername = applicationProperties.getSecurity().getInitialLogin().getUsername();
|
||||||
|
String initialPassword = applicationProperties.getSecurity().getInitialLogin().getPassword();
|
||||||
|
if (initialUsername != null && initialPassword != null) {
|
||||||
|
userService.saveUser(initialUsername, initialPassword, Role.ADMIN.getRoleId());
|
||||||
|
} else {
|
||||||
|
initialUsername = "admin";
|
||||||
|
initialPassword = "stirling";
|
||||||
|
userService.saveUser(initialUsername, initialPassword, Role.ADMIN.getRoleId(), true);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
@PostConstruct
|
||||||
|
public void initSecretKey() throws IOException {
|
||||||
|
String secretKey = applicationProperties.getAutomaticallyGenerated().getKey();
|
||||||
|
if (secretKey == null || secretKey.isEmpty()) {
|
||||||
|
secretKey = UUID.randomUUID().toString(); // Generating a random UUID as the secret key
|
||||||
|
saveKeyToConfig(secretKey);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private void saveKeyToConfig(String key) throws IOException {
|
||||||
|
Path path = Paths.get("configs", "settings.yml"); // Target the configs/settings.yml
|
||||||
|
List<String> lines = Files.readAllLines(path);
|
||||||
|
boolean keyFound = false;
|
||||||
|
|
||||||
|
// Search for the existing key to replace it or place to add it
|
||||||
|
for (int i = 0; i < lines.size(); i++) {
|
||||||
|
if (lines.get(i).startsWith("AutomaticallyGenerated:")) {
|
||||||
|
keyFound = true;
|
||||||
|
if (i + 1 < lines.size() && lines.get(i + 1).trim().startsWith("key:")) {
|
||||||
|
lines.set(i + 1, " key: " + key);
|
||||||
|
break;
|
||||||
|
} else {
|
||||||
|
lines.add(i + 1, " key: " + key);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// If the section doesn't exist, append it
|
||||||
|
if (!keyFound) {
|
||||||
|
lines.add("# Automatically Generated Settings (Do Not Edit Directly)");
|
||||||
|
lines.add("AutomaticallyGenerated:");
|
||||||
|
lines.add(" key: " + key);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Write back to the file
|
||||||
|
Files.write(path, lines);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,106 @@
|
|||||||
|
package stirling.software.SPDF.config.security;
|
||||||
|
|
||||||
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
import org.springframework.beans.factory.annotation.Qualifier;
|
||||||
|
import org.springframework.context.annotation.Bean;
|
||||||
|
import org.springframework.context.annotation.Configuration;
|
||||||
|
import org.springframework.context.annotation.Lazy;
|
||||||
|
import org.springframework.security.authentication.dao.DaoAuthenticationProvider;
|
||||||
|
import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity;
|
||||||
|
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
|
||||||
|
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
|
||||||
|
import org.springframework.security.core.userdetails.UserDetailsService;
|
||||||
|
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
|
||||||
|
import org.springframework.security.crypto.password.PasswordEncoder;
|
||||||
|
import org.springframework.security.web.SecurityFilterChain;
|
||||||
|
import org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter;
|
||||||
|
import org.springframework.security.web.authentication.rememberme.PersistentTokenRepository;
|
||||||
|
import org.springframework.security.web.util.matcher.AntPathRequestMatcher;
|
||||||
|
|
||||||
|
import stirling.software.SPDF.repository.JPATokenRepositoryImpl;
|
||||||
|
@Configuration
|
||||||
|
@EnableWebSecurity()
|
||||||
|
@EnableGlobalMethodSecurity(prePostEnabled = true)
|
||||||
|
public class SecurityConfiguration {
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
private UserDetailsService userDetailsService;
|
||||||
|
|
||||||
|
@Bean
|
||||||
|
public PasswordEncoder passwordEncoder() {
|
||||||
|
return new BCryptPasswordEncoder();
|
||||||
|
}
|
||||||
|
@Autowired
|
||||||
|
@Lazy
|
||||||
|
private UserService userService;
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
@Qualifier("loginEnabled")
|
||||||
|
public boolean loginEnabledValue;
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
private UserAuthenticationFilter userAuthenticationFilter;
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
private FirstLoginFilter firstLoginFilter;
|
||||||
|
|
||||||
|
@Bean
|
||||||
|
public SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
|
||||||
|
http.addFilterBefore(userAuthenticationFilter, UsernamePasswordAuthenticationFilter.class);
|
||||||
|
|
||||||
|
if(loginEnabledValue) {
|
||||||
|
|
||||||
|
http.csrf(csrf -> csrf.disable());
|
||||||
|
http.addFilterAfter(firstLoginFilter, UsernamePasswordAuthenticationFilter.class);
|
||||||
|
http
|
||||||
|
.formLogin(formLogin -> formLogin
|
||||||
|
.loginPage("/login")
|
||||||
|
.defaultSuccessUrl("/")
|
||||||
|
.failureHandler(new CustomAuthenticationFailureHandler())
|
||||||
|
.permitAll()
|
||||||
|
)
|
||||||
|
.logout(logout -> logout
|
||||||
|
.logoutRequestMatcher(new AntPathRequestMatcher("/logout"))
|
||||||
|
.logoutSuccessUrl("/login?logout=true")
|
||||||
|
.invalidateHttpSession(true) // Invalidate session
|
||||||
|
.deleteCookies("JSESSIONID", "remember-me")
|
||||||
|
).rememberMe(rememberMeConfigurer -> rememberMeConfigurer // Use the configurator directly
|
||||||
|
.key("uniqueAndSecret")
|
||||||
|
.tokenRepository(persistentTokenRepository())
|
||||||
|
.tokenValiditySeconds(1209600) // 2 weeks
|
||||||
|
)
|
||||||
|
.authorizeHttpRequests(authz -> authz
|
||||||
|
.requestMatchers(req -> req.getRequestURI().startsWith("/login") || req.getRequestURI().endsWith(".svg") || req.getRequestURI().startsWith("/register") || req.getRequestURI().startsWith("/error") || req.getRequestURI().startsWith("/images/") || req.getRequestURI().startsWith("/public/") || req.getRequestURI().startsWith("/css/") || req.getRequestURI().startsWith("/js/"))
|
||||||
|
.permitAll()
|
||||||
|
.anyRequest().authenticated()
|
||||||
|
)
|
||||||
|
.userDetailsService(userDetailsService)
|
||||||
|
.authenticationProvider(authenticationProvider());
|
||||||
|
} else {
|
||||||
|
http.csrf(csrf -> csrf.disable())
|
||||||
|
.authorizeHttpRequests(authz -> authz
|
||||||
|
.anyRequest().permitAll()
|
||||||
|
);
|
||||||
|
}
|
||||||
|
return http.build();
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
@Bean
|
||||||
|
public DaoAuthenticationProvider authenticationProvider() {
|
||||||
|
DaoAuthenticationProvider authProvider = new DaoAuthenticationProvider();
|
||||||
|
authProvider.setUserDetailsService(userDetailsService);
|
||||||
|
authProvider.setPasswordEncoder(passwordEncoder());
|
||||||
|
return authProvider;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Bean
|
||||||
|
public PersistentTokenRepository persistentTokenRepository() {
|
||||||
|
return new JPATokenRepositoryImpl();
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
@@ -0,0 +1,113 @@
|
|||||||
|
package stirling.software.SPDF.config.security;
|
||||||
|
|
||||||
|
import java.io.IOException;
|
||||||
|
|
||||||
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
import org.springframework.beans.factory.annotation.Qualifier;
|
||||||
|
import org.springframework.context.annotation.Lazy;
|
||||||
|
import org.springframework.http.HttpStatus;
|
||||||
|
import org.springframework.security.core.Authentication;
|
||||||
|
import org.springframework.security.core.AuthenticationException;
|
||||||
|
import org.springframework.security.core.context.SecurityContextHolder;
|
||||||
|
import org.springframework.security.core.userdetails.UserDetails;
|
||||||
|
import org.springframework.security.core.userdetails.UserDetailsService;
|
||||||
|
import org.springframework.stereotype.Component;
|
||||||
|
import org.springframework.web.filter.OncePerRequestFilter;
|
||||||
|
|
||||||
|
import jakarta.servlet.FilterChain;
|
||||||
|
import jakarta.servlet.ServletException;
|
||||||
|
import jakarta.servlet.http.HttpServletRequest;
|
||||||
|
import jakarta.servlet.http.HttpServletResponse;
|
||||||
|
import stirling.software.SPDF.model.ApiKeyAuthenticationToken;
|
||||||
|
@Component
|
||||||
|
public class UserAuthenticationFilter extends OncePerRequestFilter {
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
private UserDetailsService userDetailsService;
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
@Lazy
|
||||||
|
private UserService userService;
|
||||||
|
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
@Qualifier("loginEnabled")
|
||||||
|
public boolean loginEnabledValue;
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected void doFilterInternal(HttpServletRequest request,
|
||||||
|
HttpServletResponse response,
|
||||||
|
FilterChain filterChain) throws ServletException, IOException {
|
||||||
|
|
||||||
|
if (!loginEnabledValue) {
|
||||||
|
// If login is not enabled, just pass all requests without authentication
|
||||||
|
filterChain.doFilter(request, response);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
String requestURI = request.getRequestURI();
|
||||||
|
Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
|
||||||
|
|
||||||
|
// Check for API key in the request headers if no authentication exists
|
||||||
|
if (authentication == null || !authentication.isAuthenticated()) {
|
||||||
|
String apiKey = request.getHeader("X-API-Key");
|
||||||
|
if (apiKey != null && !apiKey.trim().isEmpty()) {
|
||||||
|
try {
|
||||||
|
// Use API key to authenticate. This requires you to have an authentication provider for API keys.
|
||||||
|
UserDetails userDetails = userService.loadUserByApiKey(apiKey);
|
||||||
|
if(userDetails == null)
|
||||||
|
{
|
||||||
|
response.setStatus(HttpStatus.UNAUTHORIZED.value());
|
||||||
|
response.getWriter().write("Invalid API Key.");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
authentication = new ApiKeyAuthenticationToken(userDetails, apiKey, userDetails.getAuthorities());
|
||||||
|
SecurityContextHolder.getContext().setAuthentication(authentication);
|
||||||
|
} catch (AuthenticationException e) {
|
||||||
|
// If API key authentication fails, deny the request
|
||||||
|
response.setStatus(HttpStatus.UNAUTHORIZED.value());
|
||||||
|
response.getWriter().write("Invalid API Key.");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// If we still don't have any authentication, deny the request
|
||||||
|
if (authentication == null || !authentication.isAuthenticated()) {
|
||||||
|
String method = request.getMethod();
|
||||||
|
if ("GET".equalsIgnoreCase(method) && !"/login".equals(requestURI)) {
|
||||||
|
response.sendRedirect("/login"); // redirect to the login page
|
||||||
|
return;
|
||||||
|
} else {
|
||||||
|
response.setStatus(HttpStatus.UNAUTHORIZED.value());
|
||||||
|
response.getWriter().write("Authentication required. Please provide a X-API-KEY in request header.\nThis is found in Settings -> Account Settings -> API Key\nAlternativly you can disable authentication if this is unexpected");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
filterChain.doFilter(request, response);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected boolean shouldNotFilter(HttpServletRequest request) throws ServletException {
|
||||||
|
String uri = request.getRequestURI();
|
||||||
|
|
||||||
|
String[] permitAllPatterns = {
|
||||||
|
"/login",
|
||||||
|
"/register",
|
||||||
|
"/error",
|
||||||
|
"/images/",
|
||||||
|
"/public/",
|
||||||
|
"/css/",
|
||||||
|
"/js/"
|
||||||
|
};
|
||||||
|
|
||||||
|
for (String pattern : permitAllPatterns) {
|
||||||
|
if (uri.startsWith(pattern) || uri.endsWith(".svg")) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -0,0 +1,125 @@
|
|||||||
|
package stirling.software.SPDF.config.security;
|
||||||
|
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.time.Duration;
|
||||||
|
import java.util.Map;
|
||||||
|
import java.util.concurrent.ConcurrentHashMap;
|
||||||
|
|
||||||
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
import org.springframework.beans.factory.annotation.Qualifier;
|
||||||
|
import org.springframework.http.HttpStatus;
|
||||||
|
import org.springframework.security.core.Authentication;
|
||||||
|
import org.springframework.security.core.GrantedAuthority;
|
||||||
|
import org.springframework.security.core.context.SecurityContextHolder;
|
||||||
|
import org.springframework.security.core.userdetails.UserDetails;
|
||||||
|
import org.springframework.security.core.userdetails.UserDetailsService;
|
||||||
|
import org.springframework.stereotype.Component;
|
||||||
|
import org.springframework.web.filter.OncePerRequestFilter;
|
||||||
|
|
||||||
|
import io.github.bucket4j.Bandwidth;
|
||||||
|
import io.github.bucket4j.Bucket;
|
||||||
|
import io.github.bucket4j.ConsumptionProbe;
|
||||||
|
import io.github.bucket4j.Refill;
|
||||||
|
import jakarta.servlet.FilterChain;
|
||||||
|
import jakarta.servlet.ServletException;
|
||||||
|
import jakarta.servlet.http.HttpServletRequest;
|
||||||
|
import jakarta.servlet.http.HttpServletResponse;
|
||||||
|
import stirling.software.SPDF.model.Role;
|
||||||
|
@Component
|
||||||
|
public class UserBasedRateLimitingFilter extends OncePerRequestFilter {
|
||||||
|
|
||||||
|
private final Map<String, Bucket> apiBuckets = new ConcurrentHashMap<>();
|
||||||
|
private final Map<String, Bucket> webBuckets = new ConcurrentHashMap<>();
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
private UserDetailsService userDetailsService;
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
@Qualifier("rateLimit")
|
||||||
|
public boolean rateLimit;
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected void doFilterInternal(HttpServletRequest request,
|
||||||
|
HttpServletResponse response,
|
||||||
|
FilterChain filterChain) throws ServletException, IOException {
|
||||||
|
if (!rateLimit) {
|
||||||
|
// If rateLimit is not enabled, just pass all requests without rate limiting
|
||||||
|
filterChain.doFilter(request, response);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
String method = request.getMethod();
|
||||||
|
if (!"POST".equalsIgnoreCase(method)) {
|
||||||
|
// If the request is not a POST, just pass it through without rate limiting
|
||||||
|
filterChain.doFilter(request, response);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
String identifier = null;
|
||||||
|
|
||||||
|
// Check for API key in the request headers
|
||||||
|
String apiKey = request.getHeader("X-API-Key");
|
||||||
|
if (apiKey != null && !apiKey.trim().isEmpty()) {
|
||||||
|
identifier = "API_KEY_" + apiKey; // Prefix to distinguish between API keys and usernames
|
||||||
|
} else {
|
||||||
|
Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
|
||||||
|
if (authentication != null && authentication.isAuthenticated()) {
|
||||||
|
UserDetails userDetails = (UserDetails) authentication.getPrincipal();
|
||||||
|
identifier = userDetails.getUsername();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// If neither API key nor an authenticated user is present, use IP address
|
||||||
|
if (identifier == null) {
|
||||||
|
identifier = request.getRemoteAddr();
|
||||||
|
}
|
||||||
|
|
||||||
|
Role userRole = getRoleFromAuthentication(SecurityContextHolder.getContext().getAuthentication());
|
||||||
|
|
||||||
|
if (request.getHeader("X-API-Key") != null) {
|
||||||
|
// It's an API call
|
||||||
|
processRequest(userRole.getApiCallsPerDay(), identifier, apiBuckets, request, response, filterChain);
|
||||||
|
} else {
|
||||||
|
// It's a Web UI call
|
||||||
|
processRequest(userRole.getWebCallsPerDay(), identifier, webBuckets, request, response, filterChain);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private Role getRoleFromAuthentication(Authentication authentication) {
|
||||||
|
if (authentication != null && authentication.isAuthenticated()) {
|
||||||
|
for (GrantedAuthority authority : authentication.getAuthorities()) {
|
||||||
|
try {
|
||||||
|
return Role.fromString(authority.getAuthority());
|
||||||
|
} catch (IllegalArgumentException ex) {
|
||||||
|
// Ignore and continue to next authority.
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
throw new IllegalStateException("User does not have a valid role.");
|
||||||
|
}
|
||||||
|
|
||||||
|
private void processRequest(int limitPerDay, String identifier, Map<String, Bucket> buckets,
|
||||||
|
HttpServletRequest request, HttpServletResponse response, FilterChain filterChain)
|
||||||
|
throws IOException, ServletException {
|
||||||
|
Bucket userBucket = buckets.computeIfAbsent(identifier, k -> createUserBucket(limitPerDay));
|
||||||
|
ConsumptionProbe probe = userBucket.tryConsumeAndReturnRemaining(1);
|
||||||
|
|
||||||
|
if (probe.isConsumed()) {
|
||||||
|
response.setHeader("X-Rate-Limit-Remaining", Long.toString(probe.getRemainingTokens()));
|
||||||
|
filterChain.doFilter(request, response);
|
||||||
|
} else {
|
||||||
|
long waitForRefill = probe.getNanosToWaitForRefill() / 1_000_000_000;
|
||||||
|
response.setStatus(HttpStatus.TOO_MANY_REQUESTS.value());
|
||||||
|
response.setHeader("X-Rate-Limit-Retry-After-Seconds", String.valueOf(waitForRefill));
|
||||||
|
response.getWriter().write("Rate limit exceeded for POST requests.");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private Bucket createUserBucket(int limitPerDay) {
|
||||||
|
Bandwidth limit = Bandwidth.classic(limitPerDay, Refill.intervally(limitPerDay, Duration.ofDays(1)));
|
||||||
|
return Bucket.builder().addLimit(limit).build();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
@@ -0,0 +1,191 @@
|
|||||||
|
package stirling.software.SPDF.config.security;
|
||||||
|
import java.util.Collection;
|
||||||
|
import java.util.HashMap;
|
||||||
|
import java.util.Map;
|
||||||
|
import java.util.Optional;
|
||||||
|
import java.util.UUID;
|
||||||
|
import java.util.stream.Collectors;
|
||||||
|
|
||||||
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
|
||||||
|
import org.springframework.security.core.Authentication;
|
||||||
|
import org.springframework.security.core.GrantedAuthority;
|
||||||
|
import org.springframework.security.core.authority.SimpleGrantedAuthority;
|
||||||
|
import org.springframework.security.core.userdetails.UserDetails;
|
||||||
|
import org.springframework.security.core.userdetails.UsernameNotFoundException;
|
||||||
|
import org.springframework.security.crypto.password.PasswordEncoder;
|
||||||
|
import org.springframework.stereotype.Service;
|
||||||
|
|
||||||
|
import stirling.software.SPDF.model.Authority;
|
||||||
|
import stirling.software.SPDF.model.User;
|
||||||
|
import stirling.software.SPDF.repository.UserRepository;
|
||||||
|
@Service
|
||||||
|
public class UserService {
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
private UserRepository userRepository;
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
private PasswordEncoder passwordEncoder;
|
||||||
|
|
||||||
|
public Authentication getAuthentication(String apiKey) {
|
||||||
|
User user = getUserByApiKey(apiKey);
|
||||||
|
if (user == null) {
|
||||||
|
throw new UsernameNotFoundException("API key is not valid");
|
||||||
|
}
|
||||||
|
|
||||||
|
// Convert the user into an Authentication object
|
||||||
|
return new UsernamePasswordAuthenticationToken(
|
||||||
|
user, // principal (typically the user)
|
||||||
|
null, // credentials (we don't expose the password or API key here)
|
||||||
|
getAuthorities(user) // user's authorities (roles/permissions)
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
private Collection<? extends GrantedAuthority> getAuthorities(User user) {
|
||||||
|
// Convert each Authority object into a SimpleGrantedAuthority object.
|
||||||
|
return user.getAuthorities().stream()
|
||||||
|
.map((Authority authority) -> new SimpleGrantedAuthority(authority.getAuthority()))
|
||||||
|
.collect(Collectors.toList());
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
private String generateApiKey() {
|
||||||
|
String apiKey;
|
||||||
|
do {
|
||||||
|
apiKey = UUID.randomUUID().toString();
|
||||||
|
} while (userRepository.findByApiKey(apiKey) != null); // Ensure uniqueness
|
||||||
|
return apiKey;
|
||||||
|
}
|
||||||
|
|
||||||
|
public User addApiKeyToUser(String username) {
|
||||||
|
User user = userRepository.findByUsername(username)
|
||||||
|
.orElseThrow(() -> new UsernameNotFoundException("User not found"));
|
||||||
|
|
||||||
|
user.setApiKey(generateApiKey());
|
||||||
|
return userRepository.save(user);
|
||||||
|
}
|
||||||
|
|
||||||
|
public User refreshApiKeyForUser(String username) {
|
||||||
|
return addApiKeyToUser(username); // reuse the add API key method for refreshing
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getApiKeyForUser(String username) {
|
||||||
|
User user = userRepository.findByUsername(username)
|
||||||
|
.orElseThrow(() -> new UsernameNotFoundException("User not found"));
|
||||||
|
return user.getApiKey();
|
||||||
|
}
|
||||||
|
|
||||||
|
public boolean isValidApiKey(String apiKey) {
|
||||||
|
return userRepository.findByApiKey(apiKey) != null;
|
||||||
|
}
|
||||||
|
|
||||||
|
public User getUserByApiKey(String apiKey) {
|
||||||
|
return userRepository.findByApiKey(apiKey);
|
||||||
|
}
|
||||||
|
|
||||||
|
public UserDetails loadUserByApiKey(String apiKey) {
|
||||||
|
User userOptional = userRepository.findByApiKey(apiKey);
|
||||||
|
if (userOptional != null) {
|
||||||
|
User user = userOptional;
|
||||||
|
// Convert your User entity to a UserDetails object with authorities
|
||||||
|
return new org.springframework.security.core.userdetails.User(
|
||||||
|
user.getUsername(),
|
||||||
|
user.getPassword(), // you might not need this for API key auth
|
||||||
|
getAuthorities(user)
|
||||||
|
);
|
||||||
|
}
|
||||||
|
return null; // or throw an exception
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
public boolean validateApiKeyForUser(String username, String apiKey) {
|
||||||
|
Optional<User> userOpt = userRepository.findByUsername(username);
|
||||||
|
return userOpt.isPresent() && userOpt.get().getApiKey().equals(apiKey);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void saveUser(String username, String password) {
|
||||||
|
User user = new User();
|
||||||
|
user.setUsername(username);
|
||||||
|
user.setPassword(passwordEncoder.encode(password));
|
||||||
|
user.setEnabled(true);
|
||||||
|
userRepository.save(user);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void saveUser(String username, String password, String role, boolean firstLogin) {
|
||||||
|
User user = new User();
|
||||||
|
user.setUsername(username);
|
||||||
|
user.setPassword(passwordEncoder.encode(password));
|
||||||
|
user.addAuthority(new Authority(role, user));
|
||||||
|
user.setEnabled(true);
|
||||||
|
user.setFirstLogin(firstLogin);
|
||||||
|
userRepository.save(user);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void saveUser(String username, String password, String role) {
|
||||||
|
User user = new User();
|
||||||
|
user.setUsername(username);
|
||||||
|
user.setPassword(passwordEncoder.encode(password));
|
||||||
|
user.addAuthority(new Authority(role, user));
|
||||||
|
user.setEnabled(true);
|
||||||
|
user.setFirstLogin(false);
|
||||||
|
userRepository.save(user);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void deleteUser(String username) {
|
||||||
|
Optional<User> userOpt = userRepository.findByUsername(username);
|
||||||
|
if (userOpt.isPresent()) {
|
||||||
|
userRepository.delete(userOpt.get());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public boolean usernameExists(String username) {
|
||||||
|
return userRepository.findByUsername(username).isPresent();
|
||||||
|
}
|
||||||
|
|
||||||
|
public boolean hasUsers() {
|
||||||
|
return userRepository.count() > 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void updateUserSettings(String username, Map<String, String> updates) {
|
||||||
|
Optional<User> userOpt = userRepository.findByUsername(username);
|
||||||
|
if (userOpt.isPresent()) {
|
||||||
|
User user = userOpt.get();
|
||||||
|
Map<String, String> settingsMap = user.getSettings();
|
||||||
|
|
||||||
|
if(settingsMap == null) {
|
||||||
|
settingsMap = new HashMap<String,String>();
|
||||||
|
}
|
||||||
|
settingsMap.clear();
|
||||||
|
settingsMap.putAll(updates);
|
||||||
|
user.setSettings(settingsMap);
|
||||||
|
|
||||||
|
userRepository.save(user);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public Optional<User> findByUsername(String username) {
|
||||||
|
return userRepository.findByUsername(username);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void changeUsername(User user, String newUsername) {
|
||||||
|
user.setUsername(newUsername);
|
||||||
|
userRepository.save(user);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void changePassword(User user, String newPassword) {
|
||||||
|
user.setPassword(passwordEncoder.encode(newPassword));
|
||||||
|
userRepository.save(user);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void changeFirstUse(User user, boolean firstUse) {
|
||||||
|
user.setFirstLogin(firstUse);
|
||||||
|
userRepository.save(user);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
public boolean isPasswordCorrect(User user, String currentPassword) {
|
||||||
|
return passwordEncoder.matches(currentPassword, user.getPassword());
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,86 @@
|
|||||||
|
package stirling.software.SPDF.controller.api;
|
||||||
|
|
||||||
|
import java.io.ByteArrayInputStream;
|
||||||
|
import java.io.ByteArrayOutputStream;
|
||||||
|
import java.io.IOException;
|
||||||
|
|
||||||
|
import org.apache.pdfbox.multipdf.LayerUtility;
|
||||||
|
import org.apache.pdfbox.pdmodel.PDDocument;
|
||||||
|
import org.apache.pdfbox.pdmodel.PDPage;
|
||||||
|
import org.apache.pdfbox.pdmodel.PDPageContentStream;
|
||||||
|
import org.apache.pdfbox.pdmodel.common.PDRectangle;
|
||||||
|
import org.apache.pdfbox.pdmodel.graphics.form.PDFormXObject;
|
||||||
|
import org.slf4j.Logger;
|
||||||
|
import org.slf4j.LoggerFactory;
|
||||||
|
import org.springframework.http.ResponseEntity;
|
||||||
|
import org.springframework.web.bind.annotation.ModelAttribute;
|
||||||
|
import org.springframework.web.bind.annotation.PostMapping;
|
||||||
|
import org.springframework.web.bind.annotation.RequestMapping;
|
||||||
|
import org.springframework.web.bind.annotation.RestController;
|
||||||
|
|
||||||
|
import io.swagger.v3.oas.annotations.Operation;
|
||||||
|
import io.swagger.v3.oas.annotations.tags.Tag;
|
||||||
|
import stirling.software.SPDF.model.api.general.CropPdfForm;
|
||||||
|
import stirling.software.SPDF.utils.WebResponseUtils;
|
||||||
|
|
||||||
|
@RestController
|
||||||
|
@RequestMapping("/api/v1/general")
|
||||||
|
@Tag(name = "General", description = "General APIs")
|
||||||
|
public class CropController {
|
||||||
|
|
||||||
|
private static final Logger logger = LoggerFactory.getLogger(CropController.class);
|
||||||
|
|
||||||
|
@PostMapping(value = "/crop", consumes = "multipart/form-data")
|
||||||
|
@Operation(summary = "Crops a PDF document", description = "This operation takes an input PDF file and crops it according to the given coordinates. Input:PDF Output:PDF Type:SISO")
|
||||||
|
public ResponseEntity<byte[]> cropPdf(@ModelAttribute CropPdfForm form)
|
||||||
|
throws IOException {
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
PDDocument sourceDocument = PDDocument.load(new ByteArrayInputStream(form.getFileInput().getBytes()));
|
||||||
|
|
||||||
|
PDDocument newDocument = new PDDocument();
|
||||||
|
|
||||||
|
int totalPages = sourceDocument.getNumberOfPages();
|
||||||
|
|
||||||
|
LayerUtility layerUtility = new LayerUtility(newDocument);
|
||||||
|
|
||||||
|
for (int i = 0; i < totalPages; i++) {
|
||||||
|
PDPage sourcePage = sourceDocument.getPage(i);
|
||||||
|
|
||||||
|
// Create a new page with the size of the source page
|
||||||
|
PDPage newPage = new PDPage(sourcePage.getMediaBox());
|
||||||
|
newDocument.addPage(newPage);
|
||||||
|
PDPageContentStream contentStream = new PDPageContentStream(newDocument, newPage);
|
||||||
|
|
||||||
|
// Import the source page as a form XObject
|
||||||
|
PDFormXObject formXObject = layerUtility.importPageAsForm(sourceDocument, i);
|
||||||
|
|
||||||
|
contentStream.saveGraphicsState();
|
||||||
|
|
||||||
|
// Define the crop area
|
||||||
|
contentStream.addRect(form.getX(), form.getY(), form.getWidth(), form.getHeight());
|
||||||
|
contentStream.clip();
|
||||||
|
|
||||||
|
// Draw the entire formXObject
|
||||||
|
contentStream.drawForm(formXObject);
|
||||||
|
|
||||||
|
contentStream.restoreGraphicsState();
|
||||||
|
|
||||||
|
contentStream.close();
|
||||||
|
|
||||||
|
// Now, set the new page's media box to the cropped size
|
||||||
|
newPage.setMediaBox(new PDRectangle(form.getX(), form.getY(), form.getWidth(), form.getHeight()));
|
||||||
|
}
|
||||||
|
|
||||||
|
ByteArrayOutputStream baos = new ByteArrayOutputStream();
|
||||||
|
newDocument.save(baos);
|
||||||
|
newDocument.close();
|
||||||
|
sourceDocument.close();
|
||||||
|
|
||||||
|
byte[] pdfContent = baos.toByteArray();
|
||||||
|
return WebResponseUtils.bytesToWebResponse(pdfContent, form.getFileInput().getOriginalFilename().replaceFirst("[.][^.]+$", "") + "_cropped.pdf");
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -1,78 +1,115 @@
|
|||||||
package stirling.software.SPDF.controller.api;
|
package stirling.software.SPDF.controller.api;
|
||||||
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
|
import java.io.InputStream;
|
||||||
|
import java.nio.file.Files;
|
||||||
|
import java.nio.file.Paths;
|
||||||
|
import java.nio.file.attribute.BasicFileAttributes;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
|
import java.util.Arrays;
|
||||||
|
import java.util.Comparator;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
import org.apache.pdfbox.pdmodel.PDDocument;
|
import org.apache.pdfbox.pdmodel.PDDocument;
|
||||||
import org.apache.pdfbox.pdmodel.PDPage;
|
import org.apache.pdfbox.pdmodel.PDPage;
|
||||||
import org.apache.pdfbox.pdmodel.PDPageTree;
|
|
||||||
import org.slf4j.Logger;
|
import org.slf4j.Logger;
|
||||||
import org.slf4j.LoggerFactory;
|
import org.slf4j.LoggerFactory;
|
||||||
import org.springframework.http.ResponseEntity;
|
import org.springframework.http.ResponseEntity;
|
||||||
|
import org.springframework.web.bind.annotation.ModelAttribute;
|
||||||
import org.springframework.web.bind.annotation.PostMapping;
|
import org.springframework.web.bind.annotation.PostMapping;
|
||||||
import org.springframework.web.bind.annotation.RequestPart;
|
import org.springframework.web.bind.annotation.RequestMapping;
|
||||||
import org.springframework.web.bind.annotation.RestController;
|
import org.springframework.web.bind.annotation.RestController;
|
||||||
import org.springframework.web.multipart.MultipartFile;
|
import org.springframework.web.multipart.MultipartFile;
|
||||||
|
|
||||||
import io.swagger.v3.oas.annotations.Operation;
|
import io.swagger.v3.oas.annotations.Operation;
|
||||||
import io.swagger.v3.oas.annotations.Parameter;
|
import io.swagger.v3.oas.annotations.tags.Tag;
|
||||||
|
import stirling.software.SPDF.model.api.general.MergePdfsRequest;
|
||||||
import stirling.software.SPDF.utils.WebResponseUtils;
|
import stirling.software.SPDF.utils.WebResponseUtils;
|
||||||
|
|
||||||
@RestController
|
@RestController
|
||||||
|
@RequestMapping("/api/v1/general")
|
||||||
|
@Tag(name = "General", description = "General APIs")
|
||||||
public class MergeController {
|
public class MergeController {
|
||||||
|
|
||||||
private static final Logger logger = LoggerFactory.getLogger(MergeController.class);
|
private static final Logger logger = LoggerFactory.getLogger(MergeController.class);
|
||||||
|
|
||||||
private PDDocument mergeDocuments(List<PDDocument> documents) throws IOException {
|
|
||||||
// Create a new empty document
|
|
||||||
PDDocument mergedDoc = new PDDocument();
|
|
||||||
|
|
||||||
// Iterate over the list of documents and add their pages to the merged document
|
private PDDocument mergeDocuments(List<PDDocument> documents) throws IOException {
|
||||||
for (PDDocument doc : documents) {
|
PDDocument mergedDoc = new PDDocument();
|
||||||
// Get all pages from the current document
|
for (PDDocument doc : documents) {
|
||||||
PDPageTree pages = doc.getPages();
|
for (PDPage page : doc.getPages()) {
|
||||||
// Iterate over the pages and add them to the merged document
|
mergedDoc.addPage(page);
|
||||||
for (PDPage page : pages) {
|
|
||||||
mergedDoc.addPage(page);
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
return mergedDoc;
|
||||||
|
}
|
||||||
|
|
||||||
// Return the merged document
|
private Comparator<MultipartFile> getSortComparator(String sortType) {
|
||||||
return mergedDoc;
|
switch (sortType) {
|
||||||
|
case "byFileName":
|
||||||
|
return Comparator.comparing(MultipartFile::getOriginalFilename);
|
||||||
|
case "byDateModified":
|
||||||
|
return (file1, file2) -> {
|
||||||
|
try {
|
||||||
|
BasicFileAttributes attr1 = Files.readAttributes(Paths.get(file1.getOriginalFilename()), BasicFileAttributes.class);
|
||||||
|
BasicFileAttributes attr2 = Files.readAttributes(Paths.get(file2.getOriginalFilename()), BasicFileAttributes.class);
|
||||||
|
return attr1.lastModifiedTime().compareTo(attr2.lastModifiedTime());
|
||||||
|
} catch (IOException e) {
|
||||||
|
return 0; // If there's an error, treat them as equal
|
||||||
|
}
|
||||||
|
};
|
||||||
|
case "byDateCreated":
|
||||||
|
return (file1, file2) -> {
|
||||||
|
try {
|
||||||
|
BasicFileAttributes attr1 = Files.readAttributes(Paths.get(file1.getOriginalFilename()), BasicFileAttributes.class);
|
||||||
|
BasicFileAttributes attr2 = Files.readAttributes(Paths.get(file2.getOriginalFilename()), BasicFileAttributes.class);
|
||||||
|
return attr1.creationTime().compareTo(attr2.creationTime());
|
||||||
|
} catch (IOException e) {
|
||||||
|
return 0; // If there's an error, treat them as equal
|
||||||
|
}
|
||||||
|
};
|
||||||
|
case "byPDFTitle":
|
||||||
|
return (file1, file2) -> {
|
||||||
|
try (PDDocument doc1 = PDDocument.load(file1.getInputStream());
|
||||||
|
PDDocument doc2 = PDDocument.load(file2.getInputStream())) {
|
||||||
|
String title1 = doc1.getDocumentInformation().getTitle();
|
||||||
|
String title2 = doc2.getDocumentInformation().getTitle();
|
||||||
|
return title1.compareTo(title2);
|
||||||
|
} catch (IOException e) {
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
case "orderProvided":
|
||||||
|
default:
|
||||||
|
return (file1, file2) -> 0; // Default is the order provided
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@PostMapping(consumes = "multipart/form-data", value = "/merge-pdfs")
|
||||||
|
@Operation(summary = "Merge multiple PDF files into one",
|
||||||
|
description = "This endpoint merges multiple PDF files into a single PDF file. The merged file will contain all pages from the input files in the order they were provided. Input:PDF Output:PDF Type:MISO")
|
||||||
|
public ResponseEntity<byte[]> mergePdfs(@ModelAttribute MergePdfsRequest form) throws IOException {
|
||||||
|
|
||||||
|
MultipartFile[] files = form.getFileInput();
|
||||||
|
Arrays.sort(files, getSortComparator(form.getSortType()));
|
||||||
|
|
||||||
|
List<PDDocument> documents = new ArrayList<>();
|
||||||
|
for (MultipartFile file : files) {
|
||||||
|
try (InputStream is = file.getInputStream()) {
|
||||||
|
documents.add(PDDocument.load(is));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@PostMapping(consumes = "multipart/form-data", value = "/merge-pdfs")
|
try (PDDocument mergedDoc = mergeDocuments(documents)) {
|
||||||
@Operation(
|
|
||||||
summary = "Merge multiple PDF files into one",
|
|
||||||
description = "This endpoint merges multiple PDF files into a single PDF file. The merged file will contain all pages from the input files in the order they were provided."
|
|
||||||
)
|
|
||||||
public ResponseEntity<byte[]> mergePdfs(
|
|
||||||
@RequestPart(required = true, value = "fileInput")
|
|
||||||
@Parameter(description = "The input PDF files to be merged into a single file", required = true)
|
|
||||||
MultipartFile[] files) throws IOException {
|
|
||||||
// Read the input PDF files into PDDocument objects
|
|
||||||
List<PDDocument> documents = new ArrayList<>();
|
|
||||||
|
|
||||||
// Loop through the files array and read each file into a PDDocument
|
|
||||||
for (MultipartFile file : files) {
|
|
||||||
documents.add(PDDocument.load(file.getInputStream()));
|
|
||||||
}
|
|
||||||
|
|
||||||
PDDocument mergedDoc = mergeDocuments(documents);
|
|
||||||
|
|
||||||
|
|
||||||
// Return the merged PDF as a response
|
|
||||||
ResponseEntity<byte[]> response = WebResponseUtils.pdfDocToWebResponse(mergedDoc, files[0].getOriginalFilename().replaceFirst("[.][^.]+$", "") + "_merged.pdf");
|
ResponseEntity<byte[]> response = WebResponseUtils.pdfDocToWebResponse(mergedDoc, files[0].getOriginalFilename().replaceFirst("[.][^.]+$", "") + "_merged.pdf");
|
||||||
|
|
||||||
for (PDDocument doc : documents) {
|
|
||||||
// Close the document after processing
|
|
||||||
doc.close();
|
|
||||||
}
|
|
||||||
|
|
||||||
return response;
|
return response;
|
||||||
|
} finally {
|
||||||
|
for (PDDocument doc : documents) {
|
||||||
|
if (doc != null) {
|
||||||
|
doc.close();
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
@@ -1,99 +1,124 @@
|
|||||||
package stirling.software.SPDF.controller.api;
|
package stirling.software.SPDF.controller.api;
|
||||||
|
|
||||||
import java.io.ByteArrayInputStream;
|
|
||||||
|
import java.awt.Color;
|
||||||
import java.io.ByteArrayOutputStream;
|
import java.io.ByteArrayOutputStream;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
|
|
||||||
|
import org.apache.pdfbox.multipdf.LayerUtility;
|
||||||
|
import org.apache.pdfbox.pdmodel.PDDocument;
|
||||||
|
import org.apache.pdfbox.pdmodel.PDPage;
|
||||||
|
import org.apache.pdfbox.pdmodel.PDPageContentStream;
|
||||||
|
import org.apache.pdfbox.pdmodel.common.PDRectangle;
|
||||||
|
import org.apache.pdfbox.pdmodel.graphics.form.PDFormXObject;
|
||||||
|
import org.apache.pdfbox.util.Matrix;
|
||||||
import org.slf4j.Logger;
|
import org.slf4j.Logger;
|
||||||
import org.slf4j.LoggerFactory;
|
import org.slf4j.LoggerFactory;
|
||||||
import org.springframework.http.ResponseEntity;
|
import org.springframework.http.ResponseEntity;
|
||||||
|
import org.springframework.web.bind.annotation.ModelAttribute;
|
||||||
import org.springframework.web.bind.annotation.PostMapping;
|
import org.springframework.web.bind.annotation.PostMapping;
|
||||||
import org.springframework.web.bind.annotation.RequestParam;
|
import org.springframework.web.bind.annotation.RequestMapping;
|
||||||
import org.springframework.web.bind.annotation.RestController;
|
import org.springframework.web.bind.annotation.RestController;
|
||||||
import org.springframework.web.multipart.MultipartFile;
|
import org.springframework.web.multipart.MultipartFile;
|
||||||
|
|
||||||
import com.itextpdf.kernel.geom.PageSize;
|
|
||||||
import com.itextpdf.kernel.geom.Rectangle;
|
|
||||||
import com.itextpdf.kernel.pdf.PdfDocument;
|
|
||||||
import com.itextpdf.kernel.pdf.PdfPage;
|
|
||||||
import com.itextpdf.kernel.pdf.PdfReader;
|
|
||||||
import com.itextpdf.kernel.pdf.PdfWriter;
|
|
||||||
import com.itextpdf.kernel.pdf.canvas.PdfCanvas;
|
|
||||||
import com.itextpdf.kernel.pdf.xobject.PdfFormXObject;
|
|
||||||
|
|
||||||
import io.swagger.v3.oas.annotations.Operation;
|
import io.swagger.v3.oas.annotations.Operation;
|
||||||
import io.swagger.v3.oas.annotations.Parameter;
|
import io.swagger.v3.oas.annotations.tags.Tag;
|
||||||
import io.swagger.v3.oas.annotations.media.Schema;
|
import stirling.software.SPDF.model.api.general.MergeMultiplePagesRequest;
|
||||||
import stirling.software.SPDF.utils.WebResponseUtils;
|
import stirling.software.SPDF.utils.WebResponseUtils;
|
||||||
|
|
||||||
@RestController
|
@RestController
|
||||||
|
@RequestMapping("/api/v1/general")
|
||||||
|
@Tag(name = "General", description = "General APIs")
|
||||||
public class MultiPageLayoutController {
|
public class MultiPageLayoutController {
|
||||||
|
|
||||||
private static final Logger logger = LoggerFactory.getLogger(MultiPageLayoutController.class);
|
private static final Logger logger = LoggerFactory.getLogger(MultiPageLayoutController.class);
|
||||||
|
|
||||||
@PostMapping(value = "/multi-page-layout", consumes = "multipart/form-data")
|
@PostMapping(value = "/multi-page-layout", consumes = "multipart/form-data")
|
||||||
@Operation(summary = "Merge multiple pages of a PDF document into a single page", description = "This operation takes an input PDF file and the number of pages to merge into a single sheet in the output PDF file.")
|
@Operation(
|
||||||
public ResponseEntity<byte[]> mergeMultiplePagesIntoOne(
|
summary = "Merge multiple pages of a PDF document into a single page",
|
||||||
@Parameter(description = "The input PDF file", required = true) @RequestParam("fileInput") MultipartFile file,
|
description = "This operation takes an input PDF file and the number of pages to merge into a single sheet in the output PDF file. Input:PDF Output:PDF Type:SISO"
|
||||||
@Parameter(description = "The number of pages to fit onto a single sheet in the output PDF. Acceptable values are 2, 3, 4, 9, 16.", required = true, schema = @Schema(type = "integer", allowableValues = {
|
)
|
||||||
"2", "3", "4", "9", "16" })) @RequestParam("pagesPerSheet") int pagesPerSheet)
|
public ResponseEntity<byte[]> mergeMultiplePagesIntoOne(@ModelAttribute MergeMultiplePagesRequest request)
|
||||||
throws IOException {
|
throws IOException {
|
||||||
|
|
||||||
if (pagesPerSheet != 2 && pagesPerSheet != 3
|
int pagesPerSheet = request.getPagesPerSheet();
|
||||||
&& pagesPerSheet != (int) Math.sqrt(pagesPerSheet) * Math.sqrt(pagesPerSheet)) {
|
MultipartFile file = request.getFileInput();
|
||||||
throw new IllegalArgumentException("pagesPerSheet must be 2, 3 or a perfect square");
|
boolean addBorder = request.isAddBorder();
|
||||||
}
|
|
||||||
|
if (pagesPerSheet != 2 && pagesPerSheet != 3 && pagesPerSheet != (int) Math.sqrt(pagesPerSheet) * Math.sqrt(pagesPerSheet)) {
|
||||||
|
throw new IllegalArgumentException("pagesPerSheet must be 2, 3 or a perfect square");
|
||||||
|
}
|
||||||
|
|
||||||
int cols = pagesPerSheet == 2 || pagesPerSheet == 3 ? pagesPerSheet : (int) Math.sqrt(pagesPerSheet);
|
int cols = pagesPerSheet == 2 || pagesPerSheet == 3 ? pagesPerSheet : (int) Math.sqrt(pagesPerSheet);
|
||||||
int rows = pagesPerSheet == 2 || pagesPerSheet == 3 ? 1 : (int) Math.sqrt(pagesPerSheet);
|
int rows = pagesPerSheet == 2 || pagesPerSheet == 3 ? 1 : (int) Math.sqrt(pagesPerSheet);
|
||||||
|
|
||||||
byte[] bytes = file.getBytes();
|
PDDocument sourceDocument = PDDocument.load(file.getInputStream());
|
||||||
PdfReader reader = new PdfReader(new ByteArrayInputStream(bytes));
|
PDDocument newDocument = new PDDocument();
|
||||||
PdfDocument pdfDoc = new PdfDocument(reader);
|
PDPage newPage = new PDPage(PDRectangle.A4);
|
||||||
|
newDocument.addPage(newPage);
|
||||||
|
|
||||||
ByteArrayOutputStream baos = new ByteArrayOutputStream();
|
int totalPages = sourceDocument.getNumberOfPages();
|
||||||
PdfWriter writer = new PdfWriter(baos);
|
float cellWidth = newPage.getMediaBox().getWidth() / cols;
|
||||||
PdfDocument outputPdf = new PdfDocument(writer);
|
float cellHeight = newPage.getMediaBox().getHeight() / rows;
|
||||||
PageSize pageSize = new PageSize(PageSize.A4.rotate());
|
|
||||||
|
|
||||||
int totalPages = pdfDoc.getNumberOfPages();
|
PDPageContentStream contentStream = new PDPageContentStream(newDocument, newPage, PDPageContentStream.AppendMode.APPEND, true, true);
|
||||||
float cellWidth = pageSize.getWidth() / cols;
|
LayerUtility layerUtility = new LayerUtility(newDocument);
|
||||||
float cellHeight = pageSize.getHeight() / rows;
|
|
||||||
|
|
||||||
for (int i = 1; i <= totalPages; i += pagesPerSheet) {
|
float borderThickness = 1.5f; // Specify border thickness as required
|
||||||
PdfPage page = outputPdf.addNewPage(pageSize);
|
contentStream.setLineWidth(borderThickness);
|
||||||
PdfCanvas pdfCanvas = new PdfCanvas(page);
|
contentStream.setStrokingColor(Color.BLACK);
|
||||||
|
|
||||||
|
for (int i = 0; i < totalPages; i++) {
|
||||||
|
if (i != 0 && i % pagesPerSheet == 0) {
|
||||||
|
// Close the current content stream and create a new page and content stream
|
||||||
|
contentStream.close();
|
||||||
|
newPage = new PDPage(PDRectangle.A4);
|
||||||
|
newDocument.addPage(newPage);
|
||||||
|
contentStream = new PDPageContentStream(newDocument, newPage, PDPageContentStream.AppendMode.APPEND, true, true);
|
||||||
|
}
|
||||||
|
|
||||||
for (int row = 0; row < rows; row++) {
|
PDPage sourcePage = sourceDocument.getPage(i);
|
||||||
for (int col = 0; col < cols; col++) {
|
PDRectangle rect = sourcePage.getMediaBox();
|
||||||
int index = i + row * cols + col;
|
float scaleWidth = cellWidth / rect.getWidth();
|
||||||
if (index <= totalPages) {
|
float scaleHeight = cellHeight / rect.getHeight();
|
||||||
// Get the page and calculate scaling factors
|
float scale = Math.min(scaleWidth, scaleHeight);
|
||||||
Rectangle rect = pdfDoc.getPage(index).getPageSize();
|
|
||||||
float scaleWidth = cellWidth / rect.getWidth();
|
|
||||||
float scaleHeight = cellHeight / rect.getHeight();
|
|
||||||
float scale = Math.min(scaleWidth, scaleHeight);
|
|
||||||
|
|
||||||
PdfFormXObject formXObject = pdfDoc.getPage(index).copyAsFormXObject(outputPdf);
|
int adjustedPageIndex = i % pagesPerSheet; // This will reset the index for every new page
|
||||||
float x = col * cellWidth + (cellWidth - rect.getWidth() * scale) / 2;
|
int rowIndex = adjustedPageIndex / cols;
|
||||||
float y = (rows - 1 - row) * cellHeight + (cellHeight - rect.getHeight() * scale) / 2;
|
int colIndex = adjustedPageIndex % cols;
|
||||||
|
|
||||||
// Save the graphics state, apply the transformations, add the object, and then
|
float x = colIndex * cellWidth + (cellWidth - rect.getWidth() * scale) / 2;
|
||||||
// restore the graphics state
|
float y = newPage.getMediaBox().getHeight() - ((rowIndex + 1) * cellHeight - (cellHeight - rect.getHeight() * scale) / 2);
|
||||||
pdfCanvas.saveState();
|
|
||||||
pdfCanvas.concatMatrix(scale, 0, 0, scale, x, y);
|
|
||||||
pdfCanvas.addXObject(formXObject, 0, 0);
|
|
||||||
pdfCanvas.restoreState();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
outputPdf.close();
|
contentStream.saveGraphicsState();
|
||||||
byte[] pdfContent = baos.toByteArray();
|
contentStream.transform(Matrix.getTranslateInstance(x, y));
|
||||||
pdfDoc.close();
|
contentStream.transform(Matrix.getScaleInstance(scale, scale));
|
||||||
|
|
||||||
return WebResponseUtils.bytesToWebResponse(pdfContent, file.getOriginalFilename().replaceFirst("[.][^.]+$", "") + "_layoutChanged.pdf");
|
PDFormXObject formXObject = layerUtility.importPageAsForm(sourceDocument, i);
|
||||||
|
contentStream.drawForm(formXObject);
|
||||||
|
|
||||||
|
contentStream.restoreGraphicsState();
|
||||||
|
|
||||||
|
if(addBorder) {
|
||||||
|
// Draw border around each page
|
||||||
|
float borderX = colIndex * cellWidth;
|
||||||
|
float borderY = newPage.getMediaBox().getHeight() - (rowIndex + 1) * cellHeight;
|
||||||
|
contentStream.addRect(borderX, borderY, cellWidth, cellHeight);
|
||||||
|
contentStream.stroke();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
contentStream.close(); // Close the final content stream
|
||||||
|
sourceDocument.close();
|
||||||
|
|
||||||
|
ByteArrayOutputStream baos = new ByteArrayOutputStream();
|
||||||
|
newDocument.save(baos);
|
||||||
|
newDocument.close();
|
||||||
|
|
||||||
|
byte[] result = baos.toByteArray();
|
||||||
|
return WebResponseUtils.bytesToWebResponse(result, file.getOriginalFilename().replaceFirst("[.][^.]+$", "") + "_layoutChanged.pdf");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -9,7 +9,9 @@ import org.apache.pdfbox.pdmodel.PDPage;
|
|||||||
import org.slf4j.Logger;
|
import org.slf4j.Logger;
|
||||||
import org.slf4j.LoggerFactory;
|
import org.slf4j.LoggerFactory;
|
||||||
import org.springframework.http.ResponseEntity;
|
import org.springframework.http.ResponseEntity;
|
||||||
|
import org.springframework.web.bind.annotation.ModelAttribute;
|
||||||
import org.springframework.web.bind.annotation.PostMapping;
|
import org.springframework.web.bind.annotation.PostMapping;
|
||||||
|
import org.springframework.web.bind.annotation.RequestMapping;
|
||||||
import org.springframework.web.bind.annotation.RequestParam;
|
import org.springframework.web.bind.annotation.RequestParam;
|
||||||
import org.springframework.web.bind.annotation.RequestPart;
|
import org.springframework.web.bind.annotation.RequestPart;
|
||||||
import org.springframework.web.bind.annotation.RestController;
|
import org.springframework.web.bind.annotation.RestController;
|
||||||
@@ -17,22 +19,27 @@ import org.springframework.web.multipart.MultipartFile;
|
|||||||
|
|
||||||
import io.swagger.v3.oas.annotations.Operation;
|
import io.swagger.v3.oas.annotations.Operation;
|
||||||
import io.swagger.v3.oas.annotations.Parameter;
|
import io.swagger.v3.oas.annotations.Parameter;
|
||||||
import io.swagger.v3.oas.annotations.media.Schema;
|
import io.swagger.v3.oas.annotations.tags.Tag;
|
||||||
|
import stirling.software.SPDF.model.SortTypes;
|
||||||
|
import stirling.software.SPDF.model.api.PDFWithPageNums;
|
||||||
|
import stirling.software.SPDF.model.api.general.RearrangePagesRequest;
|
||||||
import stirling.software.SPDF.utils.GeneralUtils;
|
import stirling.software.SPDF.utils.GeneralUtils;
|
||||||
import stirling.software.SPDF.utils.WebResponseUtils;
|
import stirling.software.SPDF.utils.WebResponseUtils;
|
||||||
|
|
||||||
@RestController
|
@RestController
|
||||||
|
@RequestMapping("/api/v1/general")
|
||||||
|
@Tag(name = "General", description = "General APIs")
|
||||||
public class RearrangePagesPDFController {
|
public class RearrangePagesPDFController {
|
||||||
|
|
||||||
private static final Logger logger = LoggerFactory.getLogger(RearrangePagesPDFController.class);
|
private static final Logger logger = LoggerFactory.getLogger(RearrangePagesPDFController.class);
|
||||||
|
|
||||||
@PostMapping(consumes = "multipart/form-data", value = "/remove-pages")
|
@PostMapping(consumes = "multipart/form-data", value = "/remove-pages")
|
||||||
@Operation(summary = "Remove pages from a PDF file", description = "This endpoint removes specified pages from a given PDF file. Users can provide a comma-separated list of page numbers or ranges to delete.")
|
@Operation(summary = "Remove pages from a PDF file", description = "This endpoint removes specified pages from a given PDF file. Users can provide a comma-separated list of page numbers or ranges to delete. Input:PDF Output:PDF Type:SISO")
|
||||||
public ResponseEntity<byte[]> deletePages(
|
public ResponseEntity<byte[]> deletePages(@ModelAttribute PDFWithPageNums request )
|
||||||
@RequestPart(required = true, value = "fileInput") @Parameter(description = "The input PDF file from which pages will be removed") MultipartFile pdfFile,
|
|
||||||
@RequestParam("pagesToDelete") @Parameter(description = "Comma-separated list of pages or page ranges to delete, e.g., '1,3,5-8'") String pagesToDelete)
|
|
||||||
throws IOException {
|
throws IOException {
|
||||||
|
|
||||||
|
MultipartFile pdfFile = request.getFileInput();
|
||||||
|
String pagesToDelete = request.getPageNumbers();
|
||||||
|
|
||||||
PDDocument document = PDDocument.load(pdfFile.getBytes());
|
PDDocument document = PDDocument.load(pdfFile.getBytes());
|
||||||
|
|
||||||
// Split the page order string into an array of page numbers or range of numbers
|
// Split the page order string into an array of page numbers or range of numbers
|
||||||
@@ -49,9 +56,7 @@ public class RearrangePagesPDFController {
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private enum CustomMode {
|
|
||||||
REVERSE_ORDER, DUPLEX_SORT, BOOKLET_SORT, ODD_EVEN_SPLIT, REMOVE_FIRST, REMOVE_LAST, REMOVE_FIRST_AND_LAST,
|
|
||||||
}
|
|
||||||
|
|
||||||
private List<Integer> removeFirst(int totalPages) {
|
private List<Integer> removeFirst(int totalPages) {
|
||||||
if (totalPages <= 1)
|
if (totalPages <= 1)
|
||||||
@@ -112,6 +117,18 @@ public class RearrangePagesPDFController {
|
|||||||
return newPageOrder;
|
return newPageOrder;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private List<Integer> sideStitchBooklet(int totalPages) {
|
||||||
|
List<Integer> newPageOrder = new ArrayList<>();
|
||||||
|
for (int i = 0; i < (totalPages + 3) / 4; i++) {
|
||||||
|
int begin = i * 4;
|
||||||
|
newPageOrder.add(Math.min(begin + 3, totalPages - 1));
|
||||||
|
newPageOrder.add(Math.min(begin, totalPages - 1));
|
||||||
|
newPageOrder.add(Math.min(begin + 1, totalPages - 1));
|
||||||
|
newPageOrder.add(Math.min(begin + 2, totalPages - 1));
|
||||||
|
}
|
||||||
|
return newPageOrder;
|
||||||
|
}
|
||||||
|
|
||||||
private List<Integer> oddEvenSplit(int totalPages) {
|
private List<Integer> oddEvenSplit(int totalPages) {
|
||||||
List<Integer> newPageOrder = new ArrayList<>();
|
List<Integer> newPageOrder = new ArrayList<>();
|
||||||
for (int i = 1; i <= totalPages; i += 2) {
|
for (int i = 1; i <= totalPages; i += 2) {
|
||||||
@@ -123,9 +140,9 @@ public class RearrangePagesPDFController {
|
|||||||
return newPageOrder;
|
return newPageOrder;
|
||||||
}
|
}
|
||||||
|
|
||||||
private List<Integer> processCustomMode(String customMode, int totalPages) {
|
private List<Integer> processSortTypes(String sortTypes, int totalPages) {
|
||||||
try {
|
try {
|
||||||
CustomMode mode = CustomMode.valueOf(customMode.toUpperCase());
|
SortTypes mode = SortTypes.valueOf(sortTypes.toUpperCase());
|
||||||
switch (mode) {
|
switch (mode) {
|
||||||
case REVERSE_ORDER:
|
case REVERSE_ORDER:
|
||||||
return reverseOrder(totalPages);
|
return reverseOrder(totalPages);
|
||||||
@@ -133,6 +150,8 @@ public class RearrangePagesPDFController {
|
|||||||
return duplexSort(totalPages);
|
return duplexSort(totalPages);
|
||||||
case BOOKLET_SORT:
|
case BOOKLET_SORT:
|
||||||
return bookletSort(totalPages);
|
return bookletSort(totalPages);
|
||||||
|
case SIDE_STITCH_BOOKLET_SORT:
|
||||||
|
return sideStitchBooklet(totalPages);
|
||||||
case ODD_EVEN_SPLIT:
|
case ODD_EVEN_SPLIT:
|
||||||
return oddEvenSplit(totalPages);
|
return oddEvenSplit(totalPages);
|
||||||
case REMOVE_FIRST:
|
case REMOVE_FIRST:
|
||||||
@@ -151,17 +170,11 @@ public class RearrangePagesPDFController {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@PostMapping(consumes = "multipart/form-data", value = "/rearrange-pages")
|
@PostMapping(consumes = "multipart/form-data", value = "/rearrange-pages")
|
||||||
@Operation(summary = "Rearrange pages in a PDF file", description = "This endpoint rearranges pages in a given PDF file based on the specified page order or custom mode. Users can provide a page order as a comma-separated list of page numbers or page ranges, or a custom mode.")
|
@Operation(summary = "Rearrange pages in a PDF file", description = "This endpoint rearranges pages in a given PDF file based on the specified page order or custom mode. Users can provide a page order as a comma-separated list of page numbers or page ranges, or a custom mode. Input:PDF Output:PDF")
|
||||||
public ResponseEntity<byte[]> rearrangePages(
|
public ResponseEntity<byte[]> rearrangePages(@ModelAttribute RearrangePagesRequest request) throws IOException {
|
||||||
@RequestPart(required = true, value = "fileInput") @Parameter(description = "The input PDF file to rearrange pages") MultipartFile pdfFile,
|
MultipartFile pdfFile = request.getFileInput();
|
||||||
@RequestParam(required = false, value = "pageOrder") @Parameter(description = "The new page order as a comma-separated list of page numbers, page ranges (e.g., '1,3,5-7'), or functions in the format 'an+b' where 'a' is the multiplier of the page number 'n', and 'b' is a constant (e.g., '2n+1', '3n', '6n-5')") String pageOrder,
|
String pageOrder = request.getPageNumbers();
|
||||||
@RequestParam(required = false, value = "customMode") @Parameter(schema = @Schema(implementation = CustomMode.class, description = "The custom mode for page rearrangement. "
|
String sortType = request.getCustomMode();
|
||||||
+ "Valid values are:\n" + "REVERSE_ORDER: Reverses the order of all pages.\n"
|
|
||||||
+ "DUPLEX_SORT: Sorts pages as if all fronts were scanned then all backs in reverse (1, n, 2, n-1, ...). "
|
|
||||||
+ "BOOKLET_SORT: Arranges pages for booklet printing (last, first, second, second last, ...).\n"
|
|
||||||
+ "ODD_EVEN_SPLIT: Splits and arranges pages into odd and even numbered pages.\n"
|
|
||||||
+ "REMOVE_FIRST: Removes the first page.\n" + "REMOVE_LAST: Removes the last page.\n"
|
|
||||||
+ "REMOVE_FIRST_AND_LAST: Removes both the first and the last pages.\n")) String customMode) {
|
|
||||||
try {
|
try {
|
||||||
// Load the input PDF
|
// Load the input PDF
|
||||||
PDDocument document = PDDocument.load(pdfFile.getInputStream());
|
PDDocument document = PDDocument.load(pdfFile.getInputStream());
|
||||||
@@ -169,15 +182,14 @@ public class RearrangePagesPDFController {
|
|||||||
// Split the page order string into an array of page numbers or range of numbers
|
// Split the page order string into an array of page numbers or range of numbers
|
||||||
String[] pageOrderArr = pageOrder != null ? pageOrder.split(",") : new String[0];
|
String[] pageOrderArr = pageOrder != null ? pageOrder.split(",") : new String[0];
|
||||||
int totalPages = document.getNumberOfPages();
|
int totalPages = document.getNumberOfPages();
|
||||||
System.out.println("pageOrder=" + pageOrder);
|
|
||||||
System.out.println("customMode length =" + customMode.length());
|
|
||||||
List<Integer> newPageOrder;
|
List<Integer> newPageOrder;
|
||||||
if (customMode != null && customMode.length() > 0) {
|
if (sortType != null && sortType.length() > 0) {
|
||||||
newPageOrder = processCustomMode(customMode, totalPages);
|
newPageOrder = processSortTypes(sortType, totalPages);
|
||||||
} else {
|
} else {
|
||||||
newPageOrder = GeneralUtils.parsePageList(pageOrderArr, totalPages);
|
newPageOrder = GeneralUtils.parsePageList(pageOrderArr, totalPages);
|
||||||
}
|
}
|
||||||
|
logger.info("newPageOrder = " +newPageOrder);
|
||||||
|
logger.info("totalPages = " +totalPages);
|
||||||
// Create a new list to hold the pages in the new order
|
// Create a new list to hold the pages in the new order
|
||||||
List<PDPage> newPages = new ArrayList<>();
|
List<PDPage> newPages = new ArrayList<>();
|
||||||
for (int i = 0; i < newPageOrder.size(); i++) {
|
for (int i = 0; i < newPageOrder.size(); i++) {
|
||||||
|
|||||||
@@ -8,17 +8,20 @@ import org.apache.pdfbox.pdmodel.PDPageTree;
|
|||||||
import org.slf4j.Logger;
|
import org.slf4j.Logger;
|
||||||
import org.slf4j.LoggerFactory;
|
import org.slf4j.LoggerFactory;
|
||||||
import org.springframework.http.ResponseEntity;
|
import org.springframework.http.ResponseEntity;
|
||||||
|
import org.springframework.web.bind.annotation.ModelAttribute;
|
||||||
import org.springframework.web.bind.annotation.PostMapping;
|
import org.springframework.web.bind.annotation.PostMapping;
|
||||||
import org.springframework.web.bind.annotation.RequestParam;
|
import org.springframework.web.bind.annotation.RequestMapping;
|
||||||
import org.springframework.web.bind.annotation.RequestPart;
|
|
||||||
import org.springframework.web.bind.annotation.RestController;
|
import org.springframework.web.bind.annotation.RestController;
|
||||||
import org.springframework.web.multipart.MultipartFile;
|
import org.springframework.web.multipart.MultipartFile;
|
||||||
|
|
||||||
import io.swagger.v3.oas.annotations.Operation;
|
import io.swagger.v3.oas.annotations.Operation;
|
||||||
import io.swagger.v3.oas.annotations.Parameter;
|
import io.swagger.v3.oas.annotations.tags.Tag;
|
||||||
|
import stirling.software.SPDF.model.api.general.RotatePDFRequest;
|
||||||
import stirling.software.SPDF.utils.WebResponseUtils;
|
import stirling.software.SPDF.utils.WebResponseUtils;
|
||||||
|
|
||||||
@RestController
|
@RestController
|
||||||
|
@RequestMapping("/api/v1/general")
|
||||||
|
@Tag(name = "General", description = "General APIs")
|
||||||
public class RotationController {
|
public class RotationController {
|
||||||
|
|
||||||
private static final Logger logger = LoggerFactory.getLogger(RotationController.class);
|
private static final Logger logger = LoggerFactory.getLogger(RotationController.class);
|
||||||
@@ -26,16 +29,12 @@ public class RotationController {
|
|||||||
@PostMapping(consumes = "multipart/form-data", value = "/rotate-pdf")
|
@PostMapping(consumes = "multipart/form-data", value = "/rotate-pdf")
|
||||||
@Operation(
|
@Operation(
|
||||||
summary = "Rotate a PDF file",
|
summary = "Rotate a PDF file",
|
||||||
description = "This endpoint rotates a given PDF file by a specified angle. The angle must be a multiple of 90."
|
description = "This endpoint rotates a given PDF file by a specified angle. The angle must be a multiple of 90. Input:PDF Output:PDF Type:SISO"
|
||||||
)
|
)
|
||||||
public ResponseEntity<byte[]> rotatePDF(
|
public ResponseEntity<byte[]> rotatePDF(
|
||||||
@RequestPart(required = true, value = "fileInput")
|
@ModelAttribute RotatePDFRequest request) throws IOException {
|
||||||
@Parameter(description = "The PDF file to be rotated", required = true)
|
MultipartFile pdfFile = request.getFileInput();
|
||||||
MultipartFile pdfFile,
|
Integer angle = request.getAngle();
|
||||||
@RequestParam("angle")
|
|
||||||
@Parameter(description = "The angle by which to rotate the PDF file. This should be a multiple of 90.", example = "90", required = true)
|
|
||||||
Integer angle) throws IOException {
|
|
||||||
|
|
||||||
// Load the PDF document
|
// Load the PDF document
|
||||||
PDDocument document = PDDocument.load(pdfFile.getBytes());
|
PDDocument document = PDDocument.load(pdfFile.getBytes());
|
||||||
|
|
||||||
|
|||||||
@@ -1,241 +1,110 @@
|
|||||||
package stirling.software.SPDF.controller.api;
|
package stirling.software.SPDF.controller.api;
|
||||||
|
|
||||||
import java.io.ByteArrayInputStream;
|
|
||||||
import java.io.ByteArrayOutputStream;
|
import java.io.ByteArrayOutputStream;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.util.ArrayList;
|
|
||||||
import java.util.Collections;
|
|
||||||
import java.util.Comparator;
|
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
import java.util.List;
|
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
import java.util.Set;
|
|
||||||
|
|
||||||
|
import org.apache.pdfbox.multipdf.LayerUtility;
|
||||||
|
import org.apache.pdfbox.pdmodel.PDDocument;
|
||||||
|
import org.apache.pdfbox.pdmodel.PDPage;
|
||||||
|
import org.apache.pdfbox.pdmodel.PDPageContentStream;
|
||||||
|
import org.apache.pdfbox.pdmodel.common.PDRectangle;
|
||||||
|
import org.apache.pdfbox.pdmodel.graphics.form.PDFormXObject;
|
||||||
|
import org.apache.pdfbox.util.Matrix;
|
||||||
import org.slf4j.Logger;
|
import org.slf4j.Logger;
|
||||||
import org.slf4j.LoggerFactory;
|
import org.slf4j.LoggerFactory;
|
||||||
import org.springframework.http.HttpHeaders;
|
|
||||||
import org.springframework.http.MediaType;
|
|
||||||
import org.springframework.http.ResponseEntity;
|
import org.springframework.http.ResponseEntity;
|
||||||
|
import org.springframework.web.bind.annotation.ModelAttribute;
|
||||||
import org.springframework.web.bind.annotation.PostMapping;
|
import org.springframework.web.bind.annotation.PostMapping;
|
||||||
import org.springframework.web.bind.annotation.RequestParam;
|
import org.springframework.web.bind.annotation.RequestMapping;
|
||||||
import org.springframework.web.bind.annotation.RestController;
|
import org.springframework.web.bind.annotation.RestController;
|
||||||
import org.springframework.web.multipart.MultipartFile;
|
import org.springframework.web.multipart.MultipartFile;
|
||||||
|
|
||||||
import com.itextpdf.kernel.geom.PageSize;
|
|
||||||
import com.itextpdf.kernel.geom.Rectangle;
|
|
||||||
import com.itextpdf.kernel.pdf.PdfDocument;
|
|
||||||
import com.itextpdf.kernel.pdf.PdfPage;
|
|
||||||
import com.itextpdf.kernel.pdf.PdfReader;
|
|
||||||
import com.itextpdf.kernel.pdf.PdfWriter;
|
|
||||||
import com.itextpdf.kernel.pdf.canvas.PdfCanvas;
|
|
||||||
import com.itextpdf.kernel.pdf.canvas.parser.EventType;
|
|
||||||
import com.itextpdf.kernel.pdf.canvas.parser.PdfCanvasProcessor;
|
|
||||||
import com.itextpdf.kernel.pdf.canvas.parser.data.IEventData;
|
|
||||||
import com.itextpdf.kernel.pdf.canvas.parser.data.TextRenderInfo;
|
|
||||||
import com.itextpdf.kernel.pdf.canvas.parser.listener.IEventListener;
|
|
||||||
import com.itextpdf.kernel.pdf.xobject.PdfFormXObject;
|
|
||||||
|
|
||||||
import io.swagger.v3.oas.annotations.Hidden;
|
|
||||||
import io.swagger.v3.oas.annotations.Operation;
|
import io.swagger.v3.oas.annotations.Operation;
|
||||||
import io.swagger.v3.oas.annotations.Parameter;
|
import io.swagger.v3.oas.annotations.tags.Tag;
|
||||||
import io.swagger.v3.oas.annotations.media.Schema;
|
import stirling.software.SPDF.model.api.general.ScalePagesRequest;
|
||||||
import stirling.software.SPDF.utils.WebResponseUtils;
|
import stirling.software.SPDF.utils.WebResponseUtils;
|
||||||
|
|
||||||
@RestController
|
@RestController
|
||||||
|
@RequestMapping("/api/v1/general")
|
||||||
|
@Tag(name = "General", description = "General APIs")
|
||||||
public class ScalePagesController {
|
public class ScalePagesController {
|
||||||
|
|
||||||
private static final Logger logger = LoggerFactory.getLogger(ScalePagesController.class);
|
private static final Logger logger = LoggerFactory.getLogger(ScalePagesController.class);
|
||||||
|
|
||||||
@PostMapping(value = "/scale-pages", consumes = "multipart/form-data")
|
@PostMapping(value = "/scale-pages", consumes = "multipart/form-data")
|
||||||
@Operation(summary = "Change the size of a PDF page/document", description = "This operation takes an input PDF file and the size to scale the pages to in the output PDF file.")
|
@Operation(summary = "Change the size of a PDF page/document", description = "This operation takes an input PDF file and the size to scale the pages to in the output PDF file. Input:PDF Output:PDF Type:SISO")
|
||||||
public ResponseEntity<byte[]> scalePages(
|
public ResponseEntity<byte[]> scalePages(@ModelAttribute ScalePagesRequest request) throws IOException {
|
||||||
@Parameter(description = "The input PDF file", required = true) @RequestParam("fileInput") MultipartFile file,
|
MultipartFile file = request.getFileInput();
|
||||||
@Parameter(description = "The scale of pages in the output PDF. Acceptable values are A0-A10, B0-B9, LETTER, TABLOID, LEDGER, LEGAL, EXECUTIVE.", required = true, schema = @Schema(type = "String", allowableValues = {
|
String targetPDRectangle = request.getPageSize();
|
||||||
"A0", "A1", "A2", "A3", "A4", "A5", "A6", "A7", "A8", "A9", "A10", "B0", "B1", "B2", "B3", "B4",
|
float scaleFactor = request.getScaleFactor();
|
||||||
"B5", "B6", "B7", "B8", "B9", "LETTER", "TABLOID", "LEDGER", "LEGAL",
|
|
||||||
"EXECUTIVE" })) @RequestParam("pageSize") String targetPageSize,
|
|
||||||
@Parameter(description = "The scale of the content on the pages of the output PDF. Acceptable values are floats.", required = true, schema = @Schema(type = "float")) @RequestParam("scaleFactor") float scaleFactor)
|
|
||||||
throws IOException {
|
|
||||||
|
|
||||||
Map<String, PageSize> sizeMap = new HashMap<>();
|
Map<String, PDRectangle> sizeMap = new HashMap<>();
|
||||||
// Add A0 - A10
|
// Add A0 - A10
|
||||||
sizeMap.put("A0", PageSize.A0);
|
sizeMap.put("A0", PDRectangle.A0);
|
||||||
sizeMap.put("A1", PageSize.A1);
|
sizeMap.put("A1", PDRectangle.A1);
|
||||||
sizeMap.put("A2", PageSize.A2);
|
sizeMap.put("A2", PDRectangle.A2);
|
||||||
sizeMap.put("A3", PageSize.A3);
|
sizeMap.put("A3", PDRectangle.A3);
|
||||||
sizeMap.put("A4", PageSize.A4);
|
sizeMap.put("A4", PDRectangle.A4);
|
||||||
sizeMap.put("A5", PageSize.A5);
|
sizeMap.put("A5", PDRectangle.A5);
|
||||||
sizeMap.put("A6", PageSize.A6);
|
sizeMap.put("A6", PDRectangle.A6);
|
||||||
sizeMap.put("A7", PageSize.A7);
|
|
||||||
sizeMap.put("A8", PageSize.A8);
|
|
||||||
sizeMap.put("A9", PageSize.A9);
|
|
||||||
sizeMap.put("A10", PageSize.A10);
|
|
||||||
// Add B0 - B9
|
|
||||||
sizeMap.put("B0", PageSize.B0);
|
|
||||||
sizeMap.put("B1", PageSize.B1);
|
|
||||||
sizeMap.put("B2", PageSize.B2);
|
|
||||||
sizeMap.put("B3", PageSize.B3);
|
|
||||||
sizeMap.put("B4", PageSize.B4);
|
|
||||||
sizeMap.put("B5", PageSize.B5);
|
|
||||||
sizeMap.put("B6", PageSize.B6);
|
|
||||||
sizeMap.put("B7", PageSize.B7);
|
|
||||||
sizeMap.put("B8", PageSize.B8);
|
|
||||||
sizeMap.put("B9", PageSize.B9);
|
|
||||||
// Add other sizes
|
// Add other sizes
|
||||||
sizeMap.put("LETTER", PageSize.LETTER);
|
sizeMap.put("LETTER", PDRectangle.LETTER);
|
||||||
sizeMap.put("TABLOID", PageSize.TABLOID);
|
sizeMap.put("LEGAL", PDRectangle.LEGAL);
|
||||||
sizeMap.put("LEDGER", PageSize.LEDGER);
|
|
||||||
sizeMap.put("LEGAL", PageSize.LEGAL);
|
|
||||||
sizeMap.put("EXECUTIVE", PageSize.EXECUTIVE);
|
|
||||||
|
|
||||||
if (!sizeMap.containsKey(targetPageSize)) {
|
if (!sizeMap.containsKey(targetPDRectangle)) {
|
||||||
throw new IllegalArgumentException(
|
throw new IllegalArgumentException(
|
||||||
"Invalid pageSize. It must be one of the following: A0, A1, A2, A3, A4, A5, A6, A7, A8, A9, A10");
|
"Invalid PDRectangle. It must be one of the following: A0, A1, A2, A3, A4, A5, A6, A7, A8, A9, A10");
|
||||||
}
|
}
|
||||||
|
|
||||||
PageSize pageSize = sizeMap.get(targetPageSize);
|
PDRectangle targetSize = sizeMap.get(targetPDRectangle);
|
||||||
|
|
||||||
byte[] bytes = file.getBytes();
|
PDDocument sourceDocument = PDDocument.load(file.getBytes());
|
||||||
PdfReader reader = new PdfReader(new ByteArrayInputStream(bytes));
|
PDDocument outputDocument = new PDDocument();
|
||||||
PdfDocument pdfDoc = new PdfDocument(reader);
|
|
||||||
|
|
||||||
ByteArrayOutputStream baos = new ByteArrayOutputStream();
|
int totalPages = sourceDocument.getNumberOfPages();
|
||||||
PdfWriter writer = new PdfWriter(baos);
|
for (int i = 0; i < totalPages; i++) {
|
||||||
PdfDocument outputPdf = new PdfDocument(writer);
|
PDPage sourcePage = sourceDocument.getPage(i);
|
||||||
|
PDRectangle sourceSize = sourcePage.getMediaBox();
|
||||||
|
|
||||||
|
float scaleWidth = targetSize.getWidth() / sourceSize.getWidth();
|
||||||
|
float scaleHeight = targetSize.getHeight() / sourceSize.getHeight();
|
||||||
|
float scale = Math.min(scaleWidth, scaleHeight) * scaleFactor;
|
||||||
|
|
||||||
|
PDPage newPage = new PDPage(targetSize);
|
||||||
|
outputDocument.addPage(newPage);
|
||||||
|
|
||||||
|
PDPageContentStream contentStream = new PDPageContentStream(outputDocument, newPage, PDPageContentStream.AppendMode.APPEND, true);
|
||||||
|
|
||||||
|
float x = (targetSize.getWidth() - sourceSize.getWidth() * scale) / 2;
|
||||||
|
float y = (targetSize.getHeight() - sourceSize.getHeight() * scale) / 2;
|
||||||
|
|
||||||
|
contentStream.saveGraphicsState();
|
||||||
|
contentStream.transform(Matrix.getTranslateInstance(x, y));
|
||||||
|
contentStream.transform(Matrix.getScaleInstance(scale, scale));
|
||||||
|
|
||||||
|
LayerUtility layerUtility = new LayerUtility(outputDocument);
|
||||||
|
PDFormXObject form = layerUtility.importPageAsForm(sourceDocument, i);
|
||||||
|
contentStream.drawForm(form);
|
||||||
|
|
||||||
int totalPages = pdfDoc.getNumberOfPages();
|
contentStream.restoreGraphicsState();
|
||||||
|
contentStream.close();
|
||||||
|
}
|
||||||
|
|
||||||
for (int i = 1; i <= totalPages; i++) {
|
|
||||||
PdfPage page = outputPdf.addNewPage(pageSize);
|
|
||||||
PdfCanvas pdfCanvas = new PdfCanvas(page);
|
|
||||||
|
|
||||||
// Get the page and calculate scaling factors
|
|
||||||
Rectangle rect = pdfDoc.getPage(i).getPageSize();
|
|
||||||
float scaleWidth = pageSize.getWidth() / rect.getWidth();
|
|
||||||
float scaleHeight = pageSize.getHeight() / rect.getHeight();
|
|
||||||
float scale = Math.min(scaleWidth, scaleHeight) * scaleFactor;
|
|
||||||
System.out.println("Scale: " + scale);
|
|
||||||
|
|
||||||
PdfFormXObject formXObject = pdfDoc.getPage(i).copyAsFormXObject(outputPdf);
|
|
||||||
float x = (pageSize.getWidth() - rect.getWidth() * scale) / 2; // Center Page
|
|
||||||
float y = (pageSize.getHeight() - rect.getHeight() * scale) / 2;
|
|
||||||
|
|
||||||
// Save the graphics state, apply the transformations, add the object, and then
|
|
||||||
// restore the graphics state
|
|
||||||
pdfCanvas.saveState();
|
|
||||||
pdfCanvas.concatMatrix(scale, 0, 0, scale, x, y);
|
|
||||||
pdfCanvas.addXObject(formXObject, 0, 0);
|
|
||||||
pdfCanvas.restoreState();
|
|
||||||
}
|
|
||||||
|
|
||||||
outputPdf.close();
|
ByteArrayOutputStream baos = new ByteArrayOutputStream();
|
||||||
byte[] pdfContent = baos.toByteArray();
|
outputDocument.save(baos);
|
||||||
pdfDoc.close();
|
outputDocument.close();
|
||||||
return WebResponseUtils.bytesToWebResponse(pdfContent,
|
sourceDocument.close();
|
||||||
|
|
||||||
|
|
||||||
|
return WebResponseUtils.bytesToWebResponse(baos.toByteArray(),
|
||||||
file.getOriginalFilename().replaceFirst("[.][^.]+$", "") + "_scaled.pdf");
|
file.getOriginalFilename().replaceFirst("[.][^.]+$", "") + "_scaled.pdf");
|
||||||
}
|
}
|
||||||
|
|
||||||
//TODO
|
|
||||||
@Hidden
|
|
||||||
@PostMapping(value = "/auto-crop", consumes = "multipart/form-data")
|
|
||||||
public ResponseEntity<byte[]> cropPdf(@RequestParam("fileInput") MultipartFile file) throws IOException {
|
|
||||||
byte[] bytes = file.getBytes();
|
|
||||||
PdfReader reader = new PdfReader(new ByteArrayInputStream(bytes));
|
|
||||||
PdfDocument pdfDoc = new PdfDocument(reader);
|
|
||||||
|
|
||||||
ByteArrayOutputStream baos = new ByteArrayOutputStream();
|
|
||||||
PdfWriter writer = new PdfWriter(baos);
|
|
||||||
PdfDocument outputPdf = new PdfDocument(writer);
|
|
||||||
|
|
||||||
int totalPages = pdfDoc.getNumberOfPages();
|
|
||||||
for (int i = 1; i <= totalPages; i++) {
|
|
||||||
PdfPage page = pdfDoc.getPage(i);
|
|
||||||
Rectangle originalMediaBox = page.getMediaBox();
|
|
||||||
|
|
||||||
Rectangle contentBox = determineContentBox(page);
|
|
||||||
|
|
||||||
// Make sure we don't go outside the original media box.
|
|
||||||
Rectangle intersection = originalMediaBox.getIntersection(contentBox);
|
|
||||||
page.setCropBox(intersection);
|
|
||||||
|
|
||||||
// Copy page to the new document
|
|
||||||
outputPdf.addPage(page.copyTo(outputPdf));
|
|
||||||
}
|
|
||||||
|
|
||||||
outputPdf.close();
|
|
||||||
byte[] pdfContent = baos.toByteArray();
|
|
||||||
pdfDoc.close();
|
|
||||||
return ResponseEntity.ok()
|
|
||||||
.header(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=\""
|
|
||||||
+ file.getOriginalFilename().replaceFirst("[.][^.]+$", "") + "_cropped.pdf\"")
|
|
||||||
.contentType(MediaType.APPLICATION_PDF).body(pdfContent);
|
|
||||||
}
|
|
||||||
|
|
||||||
private Rectangle determineContentBox(PdfPage page) {
|
|
||||||
// Extract the text from the page and find the bounding box.
|
|
||||||
TextBoundingRectangleFinder finder = new TextBoundingRectangleFinder();
|
|
||||||
PdfCanvasProcessor processor = new PdfCanvasProcessor(finder);
|
|
||||||
processor.processPageContent(page);
|
|
||||||
return finder.getBoundingBox();
|
|
||||||
}
|
|
||||||
|
|
||||||
private static class TextBoundingRectangleFinder implements IEventListener {
|
|
||||||
private List<Rectangle> allTextBoxes = new ArrayList<>();
|
|
||||||
|
|
||||||
public Rectangle getBoundingBox() {
|
|
||||||
// Sort the text boxes based on their vertical position
|
|
||||||
allTextBoxes.sort(Comparator.comparingDouble(Rectangle::getTop));
|
|
||||||
|
|
||||||
// Consider a box an outlier if its top is more than 1.5 times the IQR above the
|
|
||||||
// third quartile.
|
|
||||||
int q1Index = allTextBoxes.size() / 4;
|
|
||||||
int q3Index = 3 * allTextBoxes.size() / 4;
|
|
||||||
double iqr = allTextBoxes.get(q3Index).getTop() - allTextBoxes.get(q1Index).getTop();
|
|
||||||
double threshold = allTextBoxes.get(q3Index).getTop() + 1.5 * iqr;
|
|
||||||
|
|
||||||
// Initialize boundingBox to the first non-outlier box
|
|
||||||
int i = 0;
|
|
||||||
while (i < allTextBoxes.size() && allTextBoxes.get(i).getTop() > threshold) {
|
|
||||||
i++;
|
|
||||||
}
|
|
||||||
if (i == allTextBoxes.size()) {
|
|
||||||
// If all boxes are outliers, just return the first one
|
|
||||||
return allTextBoxes.get(0);
|
|
||||||
}
|
|
||||||
Rectangle boundingBox = allTextBoxes.get(i);
|
|
||||||
|
|
||||||
// Extend the bounding box to include all non-outlier boxes
|
|
||||||
for (; i < allTextBoxes.size(); i++) {
|
|
||||||
Rectangle textBoundingBox = allTextBoxes.get(i);
|
|
||||||
if (textBoundingBox.getTop() > threshold) {
|
|
||||||
// This box is an outlier, skip it
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
float left = Math.min(boundingBox.getLeft(), textBoundingBox.getLeft());
|
|
||||||
float bottom = Math.min(boundingBox.getBottom(), textBoundingBox.getBottom());
|
|
||||||
float right = Math.max(boundingBox.getRight(), textBoundingBox.getRight());
|
|
||||||
float top = Math.max(boundingBox.getTop(), textBoundingBox.getTop());
|
|
||||||
|
|
||||||
// Add a small padding around the bounding box
|
|
||||||
float padding = 10;
|
|
||||||
boundingBox = new Rectangle(left - padding, bottom - padding, right - left + 2 * padding,
|
|
||||||
top - bottom + 2 * padding);
|
|
||||||
}
|
|
||||||
return boundingBox;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void eventOccurred(IEventData data, EventType type) {
|
|
||||||
if (type == EventType.RENDER_TEXT) {
|
|
||||||
TextRenderInfo renderInfo = (TextRenderInfo) data;
|
|
||||||
allTextBoxes.add(renderInfo.getBaseline().getBoundingRectangle());
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public Set<EventType> getSupportedEvents() {
|
|
||||||
return Collections.singleton(EventType.RENDER_TEXT);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -15,81 +15,59 @@ import org.apache.pdfbox.pdmodel.PDDocument;
|
|||||||
import org.apache.pdfbox.pdmodel.PDPage;
|
import org.apache.pdfbox.pdmodel.PDPage;
|
||||||
import org.slf4j.Logger;
|
import org.slf4j.Logger;
|
||||||
import org.slf4j.LoggerFactory;
|
import org.slf4j.LoggerFactory;
|
||||||
import org.springframework.core.io.ByteArrayResource;
|
|
||||||
import org.springframework.core.io.Resource;
|
|
||||||
import org.springframework.http.HttpHeaders;
|
|
||||||
import org.springframework.http.MediaType;
|
import org.springframework.http.MediaType;
|
||||||
import org.springframework.http.ResponseEntity;
|
import org.springframework.http.ResponseEntity;
|
||||||
|
import org.springframework.web.bind.annotation.ModelAttribute;
|
||||||
import org.springframework.web.bind.annotation.PostMapping;
|
import org.springframework.web.bind.annotation.PostMapping;
|
||||||
import org.springframework.web.bind.annotation.RequestParam;
|
import org.springframework.web.bind.annotation.RequestMapping;
|
||||||
import org.springframework.web.bind.annotation.RequestPart;
|
|
||||||
import org.springframework.web.bind.annotation.RestController;
|
import org.springframework.web.bind.annotation.RestController;
|
||||||
import org.springframework.web.multipart.MultipartFile;
|
import org.springframework.web.multipart.MultipartFile;
|
||||||
|
|
||||||
import io.swagger.v3.oas.annotations.Operation;
|
import io.swagger.v3.oas.annotations.Operation;
|
||||||
import io.swagger.v3.oas.annotations.Parameter;
|
import io.swagger.v3.oas.annotations.tags.Tag;
|
||||||
import stirling.software.SPDF.utils.GeneralUtils;
|
import stirling.software.SPDF.model.api.PDFWithPageNums;
|
||||||
import stirling.software.SPDF.utils.WebResponseUtils;
|
import stirling.software.SPDF.utils.WebResponseUtils;
|
||||||
|
import org.apache.pdfbox.multipdf.Splitter;
|
||||||
@RestController
|
@RestController
|
||||||
|
@RequestMapping("/api/v1/general")
|
||||||
|
@Tag(name = "General", description = "General APIs")
|
||||||
public class SplitPDFController {
|
public class SplitPDFController {
|
||||||
|
|
||||||
private static final Logger logger = LoggerFactory.getLogger(SplitPDFController.class);
|
private static final Logger logger = LoggerFactory.getLogger(SplitPDFController.class);
|
||||||
|
|
||||||
@PostMapping(consumes = "multipart/form-data", value = "/split-pages")
|
@PostMapping(consumes = "multipart/form-data", value = "/split-pages")
|
||||||
@Operation(summary = "Split a PDF file into separate documents",
|
@Operation(summary = "Split a PDF file into separate documents",
|
||||||
description = "This endpoint splits a given PDF file into separate documents based on the specified page numbers or ranges. Users can specify pages using individual numbers, ranges, or 'all' for every page.")
|
description = "This endpoint splits a given PDF file into separate documents based on the specified page numbers or ranges. Users can specify pages using individual numbers, ranges, or 'all' for every page. Input:PDF Output:PDF Type:SIMO")
|
||||||
public ResponseEntity<byte[]> splitPdf(
|
public ResponseEntity<byte[]> splitPdf(@ModelAttribute PDFWithPageNums request) throws IOException {
|
||||||
@RequestPart(required = true, value = "fileInput")
|
MultipartFile file = request.getFileInput();
|
||||||
@Parameter(description = "The input PDF file to be split")
|
String pages = request.getPageNumbers();
|
||||||
MultipartFile file,
|
|
||||||
@RequestParam("pages")
|
|
||||||
@Parameter(description = "The pages to be included in separate documents. Specify individual page numbers (e.g., '1,3,5'), ranges (e.g., '1-3,5-7'), or 'all' for every page.")
|
|
||||||
String pages) throws IOException {
|
|
||||||
// parse user input
|
|
||||||
|
|
||||||
// open the pdf document
|
// open the pdf document
|
||||||
InputStream inputStream = file.getInputStream();
|
InputStream inputStream = file.getInputStream();
|
||||||
PDDocument document = PDDocument.load(inputStream);
|
PDDocument document = PDDocument.load(inputStream);
|
||||||
|
|
||||||
List<Integer> pageNumbers = new ArrayList<>();
|
List<Integer> pageNumbers = request.getPageNumbersList(document);
|
||||||
pages = pages.replaceAll("\\s+", ""); // remove whitespaces
|
if(!pageNumbers.contains(document.getNumberOfPages() - 1))
|
||||||
if (pages.toLowerCase().equals("all")) {
|
pageNumbers.add(document.getNumberOfPages()- 1);
|
||||||
for (int i = 0; i < document.getNumberOfPages(); i++) {
|
|
||||||
pageNumbers.add(i);
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
String[] splitPoints = pages.split(",");
|
|
||||||
for (String splitPoint : splitPoints) {
|
|
||||||
List<Integer> orderedPages = GeneralUtils.parsePageList(new String[] {splitPoint}, document.getNumberOfPages());
|
|
||||||
pageNumbers.addAll(orderedPages);
|
|
||||||
}
|
|
||||||
// Add the last page as a split point
|
|
||||||
pageNumbers.add(document.getNumberOfPages() - 1);
|
|
||||||
}
|
|
||||||
|
|
||||||
logger.info("Splitting PDF into pages: {}", pageNumbers.stream().map(String::valueOf).collect(Collectors.joining(",")));
|
logger.info("Splitting PDF into pages: {}", pageNumbers.stream().map(String::valueOf).collect(Collectors.joining(",")));
|
||||||
|
|
||||||
// split the document
|
Splitter splitter = new Splitter();
|
||||||
List<ByteArrayOutputStream> splitDocumentsBoas = new ArrayList<>();
|
List<ByteArrayOutputStream> splitDocumentsBoas = new ArrayList<>();
|
||||||
int previousPageNumber = 0;
|
|
||||||
|
int previousPageNumber = 1; // PDFBox uses 1-based indexing for pages.
|
||||||
for (int splitPoint : pageNumbers) {
|
for (int splitPoint : pageNumbers) {
|
||||||
try (PDDocument splitDocument = new PDDocument()) {
|
splitPoint = splitPoint + 1;
|
||||||
for (int i = previousPageNumber; i <= splitPoint; i++) {
|
splitter.setStartPage(previousPageNumber);
|
||||||
PDPage page = document.getPage(i);
|
splitter.setEndPage(splitPoint);
|
||||||
splitDocument.addPage(page);
|
List<PDDocument> splitDocuments = splitter.split(document);
|
||||||
logger.debug("Adding page {} to split document", i);
|
|
||||||
}
|
|
||||||
previousPageNumber = splitPoint + 1;
|
|
||||||
|
|
||||||
|
for (PDDocument splitDoc : splitDocuments) {
|
||||||
ByteArrayOutputStream baos = new ByteArrayOutputStream();
|
ByteArrayOutputStream baos = new ByteArrayOutputStream();
|
||||||
splitDocument.save(baos);
|
splitDoc.save(baos);
|
||||||
|
|
||||||
splitDocumentsBoas.add(baos);
|
splitDocumentsBoas.add(baos);
|
||||||
} catch (Exception e) {
|
splitDoc.close();
|
||||||
logger.error("Failed splitting documents and saving them", e);
|
|
||||||
throw e;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
previousPageNumber = splitPoint + 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -0,0 +1,86 @@
|
|||||||
|
package stirling.software.SPDF.controller.api;
|
||||||
|
|
||||||
|
import java.awt.geom.AffineTransform;
|
||||||
|
import java.io.ByteArrayOutputStream;
|
||||||
|
import java.io.IOException;
|
||||||
|
|
||||||
|
import org.apache.pdfbox.multipdf.LayerUtility;
|
||||||
|
import org.apache.pdfbox.pdmodel.PDDocument;
|
||||||
|
import org.apache.pdfbox.pdmodel.PDPage;
|
||||||
|
import org.apache.pdfbox.pdmodel.PDPageContentStream;
|
||||||
|
import org.apache.pdfbox.pdmodel.common.PDRectangle;
|
||||||
|
import org.apache.pdfbox.pdmodel.graphics.form.PDFormXObject;
|
||||||
|
import org.slf4j.Logger;
|
||||||
|
import org.slf4j.LoggerFactory;
|
||||||
|
import org.springframework.http.ResponseEntity;
|
||||||
|
import org.springframework.web.bind.annotation.ModelAttribute;
|
||||||
|
import org.springframework.web.bind.annotation.PostMapping;
|
||||||
|
import org.springframework.web.bind.annotation.RequestMapping;
|
||||||
|
import org.springframework.web.bind.annotation.RestController;
|
||||||
|
|
||||||
|
import io.swagger.v3.oas.annotations.Operation;
|
||||||
|
import io.swagger.v3.oas.annotations.tags.Tag;
|
||||||
|
import stirling.software.SPDF.model.api.PDFFile;
|
||||||
|
import stirling.software.SPDF.utils.WebResponseUtils;
|
||||||
|
@RestController
|
||||||
|
@RequestMapping("/api/v1/general")
|
||||||
|
@Tag(name = "General", description = "General APIs")
|
||||||
|
public class ToSinglePageController {
|
||||||
|
|
||||||
|
private static final Logger logger = LoggerFactory.getLogger(ToSinglePageController.class);
|
||||||
|
|
||||||
|
|
||||||
|
@PostMapping(consumes = "multipart/form-data", value = "/pdf-to-single-page")
|
||||||
|
@Operation(
|
||||||
|
summary = "Convert a multi-page PDF into a single long page PDF",
|
||||||
|
description = "This endpoint converts a multi-page PDF document into a single paged PDF document. The width of the single page will be same as the input's width, but the height will be the sum of all the pages' heights. Input:PDF Output:PDF Type:SISO"
|
||||||
|
)
|
||||||
|
public ResponseEntity<byte[]> pdfToSinglePage(@ModelAttribute PDFFile request) throws IOException {
|
||||||
|
|
||||||
|
// Load the source document
|
||||||
|
PDDocument sourceDocument = PDDocument.load(request.getFileInput().getInputStream());
|
||||||
|
|
||||||
|
// Calculate total height and max width
|
||||||
|
float totalHeight = 0;
|
||||||
|
float maxWidth = 0;
|
||||||
|
for (PDPage page : sourceDocument.getPages()) {
|
||||||
|
PDRectangle pageSize = page.getMediaBox();
|
||||||
|
totalHeight += pageSize.getHeight();
|
||||||
|
maxWidth = Math.max(maxWidth, pageSize.getWidth());
|
||||||
|
}
|
||||||
|
|
||||||
|
// Create new document and page with calculated dimensions
|
||||||
|
PDDocument newDocument = new PDDocument();
|
||||||
|
PDPage newPage = new PDPage(new PDRectangle(maxWidth, totalHeight));
|
||||||
|
newDocument.addPage(newPage);
|
||||||
|
|
||||||
|
// Initialize the content stream of the new page
|
||||||
|
PDPageContentStream contentStream = new PDPageContentStream(newDocument, newPage);
|
||||||
|
contentStream.close();
|
||||||
|
|
||||||
|
LayerUtility layerUtility = new LayerUtility(newDocument);
|
||||||
|
float yOffset = totalHeight;
|
||||||
|
|
||||||
|
// For each page, copy its content to the new page at the correct offset
|
||||||
|
for (PDPage page : sourceDocument.getPages()) {
|
||||||
|
PDFormXObject form = layerUtility.importPageAsForm(sourceDocument, sourceDocument.getPages().indexOf(page));
|
||||||
|
AffineTransform af = AffineTransform.getTranslateInstance(0, yOffset - page.getMediaBox().getHeight());
|
||||||
|
layerUtility.wrapInSaveRestore(newPage);
|
||||||
|
String defaultLayerName = "Layer" + sourceDocument.getPages().indexOf(page);
|
||||||
|
layerUtility.appendFormAsLayer(newPage, form, af, defaultLayerName);
|
||||||
|
yOffset -= page.getMediaBox().getHeight();
|
||||||
|
}
|
||||||
|
|
||||||
|
ByteArrayOutputStream baos = new ByteArrayOutputStream();
|
||||||
|
newDocument.save(baos);
|
||||||
|
newDocument.close();
|
||||||
|
sourceDocument.close();
|
||||||
|
|
||||||
|
byte[] result = baos.toByteArray();
|
||||||
|
return WebResponseUtils.bytesToWebResponse(result, request.getFileInput().getOriginalFilename().replaceFirst("[.][^.]+$", "") + "_singlePage.pdf");
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,234 @@
|
|||||||
|
package stirling.software.SPDF.controller.api;
|
||||||
|
|
||||||
|
import java.security.Principal;
|
||||||
|
import java.util.HashMap;
|
||||||
|
import java.util.Map;
|
||||||
|
import java.util.Optional;
|
||||||
|
|
||||||
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
import org.springframework.http.HttpStatus;
|
||||||
|
import org.springframework.http.ResponseEntity;
|
||||||
|
import org.springframework.security.access.prepost.PreAuthorize;
|
||||||
|
import org.springframework.security.core.Authentication;
|
||||||
|
import org.springframework.security.web.authentication.logout.SecurityContextLogoutHandler;
|
||||||
|
import org.springframework.stereotype.Controller;
|
||||||
|
import org.springframework.ui.Model;
|
||||||
|
import org.springframework.web.bind.annotation.PathVariable;
|
||||||
|
import org.springframework.web.bind.annotation.PostMapping;
|
||||||
|
import org.springframework.web.bind.annotation.RequestMapping;
|
||||||
|
import org.springframework.web.bind.annotation.RequestParam;
|
||||||
|
import org.springframework.web.servlet.mvc.support.RedirectAttributes;
|
||||||
|
import org.springframework.web.servlet.view.RedirectView;
|
||||||
|
|
||||||
|
import jakarta.servlet.http.HttpServletRequest;
|
||||||
|
import jakarta.servlet.http.HttpServletResponse;
|
||||||
|
import stirling.software.SPDF.config.security.UserService;
|
||||||
|
import stirling.software.SPDF.model.User;
|
||||||
|
|
||||||
|
@Controller
|
||||||
|
@RequestMapping("/api/v1/user")
|
||||||
|
public class UserController {
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
private UserService userService;
|
||||||
|
|
||||||
|
@PostMapping("/register")
|
||||||
|
public String register(@RequestParam String username, @RequestParam String password, Model model) {
|
||||||
|
if(userService.usernameExists(username)) {
|
||||||
|
model.addAttribute("error", "Username already exists");
|
||||||
|
return "register";
|
||||||
|
}
|
||||||
|
|
||||||
|
userService.saveUser(username, password);
|
||||||
|
return "redirect:/login?registered=true";
|
||||||
|
}
|
||||||
|
|
||||||
|
@PostMapping("/change-username-and-password")
|
||||||
|
public RedirectView changeUsernameAndPassword(Principal principal,
|
||||||
|
@RequestParam String currentPassword,
|
||||||
|
@RequestParam String newUsername,
|
||||||
|
@RequestParam String newPassword,
|
||||||
|
HttpServletRequest request,
|
||||||
|
HttpServletResponse response,
|
||||||
|
RedirectAttributes redirectAttributes) {
|
||||||
|
if (principal == null) {
|
||||||
|
return new RedirectView("/change-creds?messageType=notAuthenticated");
|
||||||
|
}
|
||||||
|
|
||||||
|
Optional<User> userOpt = userService.findByUsername(principal.getName());
|
||||||
|
|
||||||
|
if (userOpt == null || userOpt.isEmpty()) {
|
||||||
|
return new RedirectView("/change-creds?messageType=userNotFound");
|
||||||
|
}
|
||||||
|
|
||||||
|
User user = userOpt.get();
|
||||||
|
|
||||||
|
if (!userService.isPasswordCorrect(user, currentPassword)) {
|
||||||
|
return new RedirectView("/change-creds?messageType=incorrectPassword");
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!user.getUsername().equals(newUsername) && userService.usernameExists(newUsername)) {
|
||||||
|
return new RedirectView("/change-creds?messageType=usernameExists");
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
userService.changePassword(user, newPassword);
|
||||||
|
if(newUsername != null && newUsername.length() > 0 && !user.getUsername().equals(newUsername)) {
|
||||||
|
userService.changeUsername(user, newUsername);
|
||||||
|
}
|
||||||
|
userService.changeFirstUse(user, false);
|
||||||
|
|
||||||
|
// Logout using Spring's utility
|
||||||
|
new SecurityContextLogoutHandler().logout(request, response, null);
|
||||||
|
|
||||||
|
return new RedirectView("/login?messageType=credsUpdated");
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
@PostMapping("/change-username")
|
||||||
|
public RedirectView changeUsername(Principal principal,
|
||||||
|
@RequestParam String currentPassword,
|
||||||
|
@RequestParam String newUsername,
|
||||||
|
HttpServletRequest request,
|
||||||
|
HttpServletResponse response,
|
||||||
|
RedirectAttributes redirectAttributes) {
|
||||||
|
if (principal == null) {
|
||||||
|
return new RedirectView("/account?messageType=notAuthenticated");
|
||||||
|
}
|
||||||
|
|
||||||
|
Optional<User> userOpt = userService.findByUsername(principal.getName());
|
||||||
|
|
||||||
|
if (userOpt == null || userOpt.isEmpty()) {
|
||||||
|
return new RedirectView("/account?messageType=userNotFound");
|
||||||
|
}
|
||||||
|
|
||||||
|
User user = userOpt.get();
|
||||||
|
|
||||||
|
if (!userService.isPasswordCorrect(user, currentPassword)) {
|
||||||
|
return new RedirectView("/account?messageType=incorrectPassword");
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!user.getUsername().equals(newUsername) && userService.usernameExists(newUsername)) {
|
||||||
|
return new RedirectView("/account?messageType=usernameExists");
|
||||||
|
}
|
||||||
|
|
||||||
|
if(newUsername != null && newUsername.length() > 0) {
|
||||||
|
userService.changeUsername(user, newUsername);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Logout using Spring's utility
|
||||||
|
new SecurityContextLogoutHandler().logout(request, response, null);
|
||||||
|
|
||||||
|
return new RedirectView("/login?messageType=credsUpdated");
|
||||||
|
}
|
||||||
|
|
||||||
|
@PostMapping("/change-password")
|
||||||
|
public RedirectView changePassword(Principal principal,
|
||||||
|
@RequestParam String currentPassword,
|
||||||
|
@RequestParam String newPassword,
|
||||||
|
HttpServletRequest request,
|
||||||
|
HttpServletResponse response,
|
||||||
|
RedirectAttributes redirectAttributes) {
|
||||||
|
if (principal == null) {
|
||||||
|
return new RedirectView("/account?messageType=notAuthenticated");
|
||||||
|
}
|
||||||
|
|
||||||
|
Optional<User> userOpt = userService.findByUsername(principal.getName());
|
||||||
|
|
||||||
|
if (userOpt == null || userOpt.isEmpty()) {
|
||||||
|
return new RedirectView("/account?messageType=userNotFound");
|
||||||
|
}
|
||||||
|
|
||||||
|
User user = userOpt.get();
|
||||||
|
|
||||||
|
if (!userService.isPasswordCorrect(user, currentPassword)) {
|
||||||
|
return new RedirectView("/account?messageType=incorrectPassword");
|
||||||
|
}
|
||||||
|
|
||||||
|
userService.changePassword(user, newPassword);
|
||||||
|
|
||||||
|
// Logout using Spring's utility
|
||||||
|
new SecurityContextLogoutHandler().logout(request, response, null);
|
||||||
|
|
||||||
|
return new RedirectView("/login?messageType=credsUpdated");
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@PostMapping("/updateUserSettings")
|
||||||
|
public String updateUserSettings(HttpServletRequest request, Principal principal) {
|
||||||
|
Map<String, String[]> paramMap = request.getParameterMap();
|
||||||
|
Map<String, String> updates = new HashMap<>();
|
||||||
|
|
||||||
|
System.out.println("Received parameter map: " + paramMap);
|
||||||
|
|
||||||
|
for (Map.Entry<String, String[]> entry : paramMap.entrySet()) {
|
||||||
|
updates.put(entry.getKey(), entry.getValue()[0]);
|
||||||
|
}
|
||||||
|
|
||||||
|
System.out.println("Processed updates: " + updates);
|
||||||
|
|
||||||
|
// Assuming you have a method in userService to update the settings for a user
|
||||||
|
userService.updateUserSettings(principal.getName(), updates);
|
||||||
|
|
||||||
|
return "redirect:/account"; // Redirect to a page of your choice after updating
|
||||||
|
}
|
||||||
|
|
||||||
|
@PreAuthorize("hasRole('ROLE_ADMIN')")
|
||||||
|
@PostMapping("/admin/saveUser")
|
||||||
|
public RedirectView saveUser(@RequestParam String username, @RequestParam String password, @RequestParam String role,
|
||||||
|
@RequestParam(name = "forceChange", required = false, defaultValue = "false") boolean forceChange) {
|
||||||
|
|
||||||
|
if(userService.usernameExists(username)) {
|
||||||
|
return new RedirectView("/addUsers?messageType=usernameExists");
|
||||||
|
}
|
||||||
|
userService.saveUser(username, password, role, forceChange);
|
||||||
|
return new RedirectView("/addUsers"); // Redirect to account page after adding the user
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@PreAuthorize("hasRole('ROLE_ADMIN')")
|
||||||
|
@PostMapping("/admin/deleteUser/{username}")
|
||||||
|
public String deleteUser(@PathVariable String username, Authentication authentication) {
|
||||||
|
|
||||||
|
// Get the currently authenticated username
|
||||||
|
String currentUsername = authentication.getName();
|
||||||
|
|
||||||
|
// Check if the provided username matches the current session's username
|
||||||
|
if (currentUsername.equals(username)) {
|
||||||
|
throw new IllegalArgumentException("Cannot delete currently logined in user.");
|
||||||
|
}
|
||||||
|
|
||||||
|
userService.deleteUser(username);
|
||||||
|
return "redirect:/addUsers";
|
||||||
|
}
|
||||||
|
|
||||||
|
@PostMapping("/get-api-key")
|
||||||
|
public ResponseEntity<String> getApiKey(Principal principal) {
|
||||||
|
if (principal == null) {
|
||||||
|
return ResponseEntity.status(HttpStatus.FORBIDDEN).body("User not authenticated.");
|
||||||
|
}
|
||||||
|
String username = principal.getName();
|
||||||
|
String apiKey = userService.getApiKeyForUser(username);
|
||||||
|
if (apiKey == null) {
|
||||||
|
return ResponseEntity.status(HttpStatus.NOT_FOUND).body("API key not found for user.");
|
||||||
|
}
|
||||||
|
return ResponseEntity.ok(apiKey);
|
||||||
|
}
|
||||||
|
|
||||||
|
@PostMapping("/update-api-key")
|
||||||
|
public ResponseEntity<String> updateApiKey(Principal principal) {
|
||||||
|
if (principal == null) {
|
||||||
|
return ResponseEntity.status(HttpStatus.FORBIDDEN).body("User not authenticated.");
|
||||||
|
}
|
||||||
|
String username = principal.getName();
|
||||||
|
User user = userService.refreshApiKeyForUser(username);
|
||||||
|
String apiKey = user.getApiKey();
|
||||||
|
if (apiKey == null) {
|
||||||
|
return ResponseEntity.status(HttpStatus.NOT_FOUND).body("API key not found for user.");
|
||||||
|
}
|
||||||
|
return ResponseEntity.ok(apiKey);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
@@ -0,0 +1,130 @@
|
|||||||
|
package stirling.software.SPDF.controller.api.converters;
|
||||||
|
|
||||||
|
import java.io.ByteArrayOutputStream;
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.io.StringReader;
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.HashMap;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Map;
|
||||||
|
import java.util.zip.ZipEntry;
|
||||||
|
import java.util.zip.ZipInputStream;
|
||||||
|
|
||||||
|
import javax.xml.parsers.DocumentBuilder;
|
||||||
|
import javax.xml.parsers.DocumentBuilderFactory;
|
||||||
|
|
||||||
|
import org.springframework.http.ResponseEntity;
|
||||||
|
import org.springframework.web.bind.annotation.ModelAttribute;
|
||||||
|
import org.springframework.web.bind.annotation.PostMapping;
|
||||||
|
import org.springframework.web.bind.annotation.RequestMapping;
|
||||||
|
import org.springframework.web.bind.annotation.RestController;
|
||||||
|
import org.springframework.web.multipart.MultipartFile;
|
||||||
|
import org.w3c.dom.Document;
|
||||||
|
import org.w3c.dom.Element;
|
||||||
|
import org.w3c.dom.NodeList;
|
||||||
|
import org.xml.sax.InputSource;
|
||||||
|
|
||||||
|
import io.swagger.v3.oas.annotations.Hidden;
|
||||||
|
import io.swagger.v3.oas.annotations.Operation;
|
||||||
|
import io.swagger.v3.oas.annotations.tags.Tag;
|
||||||
|
import stirling.software.SPDF.model.api.GeneralFile;
|
||||||
|
import stirling.software.SPDF.utils.FileToPdf;
|
||||||
|
import stirling.software.SPDF.utils.WebResponseUtils;
|
||||||
|
|
||||||
|
@RestController
|
||||||
|
@RequestMapping("/api/v1/convert")
|
||||||
|
@Tag(name = "Convert", description = "Convert APIs")
|
||||||
|
public class ConvertEpubToPdf {
|
||||||
|
//TODO
|
||||||
|
@PostMapping(consumes = "multipart/form-data", value = "/epub-to-single-pdf")
|
||||||
|
@Hidden
|
||||||
|
@Operation(
|
||||||
|
summary = "Convert an EPUB file to a single PDF",
|
||||||
|
description = "This endpoint takes an EPUB file input and converts it to a single PDF."
|
||||||
|
)
|
||||||
|
public ResponseEntity<byte[]> epubToSinglePdf(
|
||||||
|
@ModelAttribute GeneralFile request)
|
||||||
|
throws Exception {
|
||||||
|
MultipartFile fileInput = request.getFileInput();
|
||||||
|
if (fileInput == null) {
|
||||||
|
throw new IllegalArgumentException("Please provide an EPUB file for conversion.");
|
||||||
|
}
|
||||||
|
|
||||||
|
String originalFilename = fileInput.getOriginalFilename();
|
||||||
|
if (originalFilename == null || !originalFilename.endsWith(".epub")) {
|
||||||
|
throw new IllegalArgumentException("File must be in .epub format.");
|
||||||
|
}
|
||||||
|
|
||||||
|
Map<String, byte[]> epubContents = extractEpubContent(fileInput);
|
||||||
|
List<String> htmlFilesOrder = getHtmlFilesOrderFromOpf(epubContents);
|
||||||
|
|
||||||
|
List<byte[]> individualPdfs = new ArrayList<>();
|
||||||
|
|
||||||
|
for (String htmlFile : htmlFilesOrder) {
|
||||||
|
byte[] htmlContent = epubContents.get(htmlFile);
|
||||||
|
byte[] pdfBytes = FileToPdf.convertHtmlToPdf(htmlContent, htmlFile.replace(".html", ".pdf"));
|
||||||
|
individualPdfs.add(pdfBytes);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Pseudo-code to merge individual PDFs into one.
|
||||||
|
byte[] mergedPdfBytes = mergeMultiplePdfsIntoOne(individualPdfs);
|
||||||
|
|
||||||
|
return WebResponseUtils.bytesToWebResponse(mergedPdfBytes, originalFilename.replace(".epub", ".pdf"));
|
||||||
|
}
|
||||||
|
|
||||||
|
// Assuming a pseudo-code function that merges multiple PDFs into one.
|
||||||
|
private byte[] mergeMultiplePdfsIntoOne(List<byte[]> individualPdfs) {
|
||||||
|
// You can use a library such as PDFBox to perform the merging here.
|
||||||
|
// Return the byte[] of the merged PDF.
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
private Map<String, byte[]> extractEpubContent(MultipartFile fileInput) throws IOException {
|
||||||
|
Map<String, byte[]> contentMap = new HashMap<>();
|
||||||
|
|
||||||
|
try (ZipInputStream zis = new ZipInputStream(fileInput.getInputStream())) {
|
||||||
|
ZipEntry zipEntry = zis.getNextEntry();
|
||||||
|
while (zipEntry != null) {
|
||||||
|
ByteArrayOutputStream baos = new ByteArrayOutputStream();
|
||||||
|
byte[] buffer = new byte[1024];
|
||||||
|
int read = 0;
|
||||||
|
while ((read = zis.read(buffer)) != -1) {
|
||||||
|
baos.write(buffer, 0, read);
|
||||||
|
}
|
||||||
|
contentMap.put(zipEntry.getName(), baos.toByteArray());
|
||||||
|
zipEntry = zis.getNextEntry();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return contentMap;
|
||||||
|
}
|
||||||
|
|
||||||
|
private List<String> getHtmlFilesOrderFromOpf(Map<String, byte[]> epubContents) throws Exception {
|
||||||
|
String opfContent = new String(epubContents.get("OEBPS/content.opf")); // Adjusting for given path
|
||||||
|
DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
|
||||||
|
DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
|
||||||
|
InputSource is = new InputSource(new StringReader(opfContent));
|
||||||
|
Document doc = dBuilder.parse(is);
|
||||||
|
|
||||||
|
NodeList itemRefs = doc.getElementsByTagName("itemref");
|
||||||
|
List<String> htmlFilesOrder = new ArrayList<>();
|
||||||
|
|
||||||
|
for (int i = 0; i < itemRefs.getLength(); i++) {
|
||||||
|
Element itemRef = (Element) itemRefs.item(i);
|
||||||
|
String idref = itemRef.getAttribute("idref");
|
||||||
|
|
||||||
|
NodeList items = doc.getElementsByTagName("item");
|
||||||
|
for (int j = 0; j < items.getLength(); j++) {
|
||||||
|
Element item = (Element) items.item(j);
|
||||||
|
if (idref.equals(item.getAttribute("id"))) {
|
||||||
|
htmlFilesOrder.add(item.getAttribute("href")); // Fetching the actual href
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return htmlFilesOrder;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
@@ -0,0 +1,52 @@
|
|||||||
|
package stirling.software.SPDF.controller.api.converters;
|
||||||
|
|
||||||
|
import org.springframework.http.ResponseEntity;
|
||||||
|
import org.springframework.web.bind.annotation.ModelAttribute;
|
||||||
|
import org.springframework.web.bind.annotation.PostMapping;
|
||||||
|
import org.springframework.web.bind.annotation.RequestMapping;
|
||||||
|
import org.springframework.web.bind.annotation.RestController;
|
||||||
|
import org.springframework.web.multipart.MultipartFile;
|
||||||
|
|
||||||
|
import io.swagger.v3.oas.annotations.Operation;
|
||||||
|
import io.swagger.v3.oas.annotations.tags.Tag;
|
||||||
|
import stirling.software.SPDF.model.api.GeneralFile;
|
||||||
|
import stirling.software.SPDF.utils.FileToPdf;
|
||||||
|
import stirling.software.SPDF.utils.WebResponseUtils;
|
||||||
|
|
||||||
|
@RestController
|
||||||
|
@Tag(name = "Convert", description = "Convert APIs")
|
||||||
|
@RequestMapping("/api/v1/convert")
|
||||||
|
public class ConvertHtmlToPDF {
|
||||||
|
|
||||||
|
|
||||||
|
@PostMapping(consumes = "multipart/form-data", value = "/html/pdf")
|
||||||
|
@Operation(
|
||||||
|
summary = "Convert an HTML or ZIP (containing HTML and CSS) to PDF",
|
||||||
|
description = "This endpoint takes an HTML or ZIP file input and converts it to a PDF format."
|
||||||
|
)
|
||||||
|
public ResponseEntity<byte[]> HtmlToPdf(
|
||||||
|
@ModelAttribute GeneralFile request)
|
||||||
|
throws Exception {
|
||||||
|
MultipartFile fileInput = request.getFileInput();
|
||||||
|
|
||||||
|
if (fileInput == null) {
|
||||||
|
throw new IllegalArgumentException("Please provide an HTML or ZIP file for conversion.");
|
||||||
|
}
|
||||||
|
|
||||||
|
String originalFilename = fileInput.getOriginalFilename();
|
||||||
|
if (originalFilename == null || (!originalFilename.endsWith(".html") && !originalFilename.endsWith(".zip"))) {
|
||||||
|
throw new IllegalArgumentException("File must be either .html or .zip format.");
|
||||||
|
}byte[] pdfBytes = FileToPdf.convertHtmlToPdf( fileInput.getBytes(), originalFilename);
|
||||||
|
|
||||||
|
String outputFilename = originalFilename.replaceFirst("[.][^.]+$", "") + ".pdf"; // Remove file extension and append .pdf
|
||||||
|
|
||||||
|
return WebResponseUtils.bytesToWebResponse(pdfBytes, outputFilename);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
@@ -11,42 +11,35 @@ import org.springframework.http.HttpHeaders;
|
|||||||
import org.springframework.http.HttpStatus;
|
import org.springframework.http.HttpStatus;
|
||||||
import org.springframework.http.MediaType;
|
import org.springframework.http.MediaType;
|
||||||
import org.springframework.http.ResponseEntity;
|
import org.springframework.http.ResponseEntity;
|
||||||
|
import org.springframework.web.bind.annotation.ModelAttribute;
|
||||||
import org.springframework.web.bind.annotation.PostMapping;
|
import org.springframework.web.bind.annotation.PostMapping;
|
||||||
import org.springframework.web.bind.annotation.RequestParam;
|
import org.springframework.web.bind.annotation.RequestMapping;
|
||||||
import org.springframework.web.bind.annotation.RequestPart;
|
|
||||||
import org.springframework.web.bind.annotation.RestController;
|
import org.springframework.web.bind.annotation.RestController;
|
||||||
import org.springframework.web.multipart.MultipartFile;
|
import org.springframework.web.multipart.MultipartFile;
|
||||||
|
|
||||||
import io.swagger.v3.oas.annotations.Operation;
|
import io.swagger.v3.oas.annotations.Operation;
|
||||||
import io.swagger.v3.oas.annotations.Parameter;
|
import io.swagger.v3.oas.annotations.tags.Tag;
|
||||||
import io.swagger.v3.oas.annotations.media.Schema;
|
import stirling.software.SPDF.model.api.converters.ConvertToImageRequest;
|
||||||
|
import stirling.software.SPDF.model.api.converters.ConvertToPdfRequest;
|
||||||
import stirling.software.SPDF.utils.PdfUtils;
|
import stirling.software.SPDF.utils.PdfUtils;
|
||||||
import stirling.software.SPDF.utils.WebResponseUtils;
|
import stirling.software.SPDF.utils.WebResponseUtils;
|
||||||
@RestController
|
@RestController
|
||||||
|
@RequestMapping("/api/v1/convert")
|
||||||
|
@Tag(name = "Convert", description = "Convert APIs")
|
||||||
public class ConvertImgPDFController {
|
public class ConvertImgPDFController {
|
||||||
|
|
||||||
private static final Logger logger = LoggerFactory.getLogger(ConvertImgPDFController.class);
|
private static final Logger logger = LoggerFactory.getLogger(ConvertImgPDFController.class);
|
||||||
|
|
||||||
@PostMapping(consumes = "multipart/form-data", value = "/pdf-to-img")
|
@PostMapping(consumes = "multipart/form-data", value = "/pdf/img")
|
||||||
@Operation(summary = "Convert PDF to image(s)",
|
@Operation(summary = "Convert PDF to image(s)",
|
||||||
description = "This endpoint converts a PDF file to image(s) with the specified image format, color type, and DPI. Users can choose to get a single image or multiple images.")
|
description = "This endpoint converts a PDF file to image(s) with the specified image format, color type, and DPI. Users can choose to get a single image or multiple images. Input:PDF Output:Image Type:SI-Conditional")
|
||||||
public ResponseEntity<Resource> convertToImage(
|
public ResponseEntity<Resource> convertToImage(@ModelAttribute ConvertToImageRequest request) throws IOException {
|
||||||
@RequestPart(required = true, value = "fileInput")
|
MultipartFile file = request.getFileInput();
|
||||||
@Parameter(description = "The input PDF file to be converted")
|
String imageFormat = request.getImageFormat();
|
||||||
MultipartFile file,
|
String singleOrMultiple = request.getSingleOrMultiple();
|
||||||
@RequestParam("imageFormat")
|
String colorType = request.getColorType();
|
||||||
@Parameter(description = "The output image format", schema = @Schema(allowableValues = {"png", "jpeg", "jpg", "gif"}))
|
String dpi = request.getDpi();
|
||||||
String imageFormat,
|
|
||||||
@RequestParam("singleOrMultiple")
|
|
||||||
@Parameter(description = "Choose between a single image containing all pages or separate images for each page", schema = @Schema(allowableValues = {"single", "multiple"}))
|
|
||||||
String singleOrMultiple,
|
|
||||||
@RequestParam("colorType")
|
|
||||||
@Parameter(description = "The color type of the output image(s)", schema = @Schema(allowableValues = {"rgb", "greyscale", "blackwhite"}))
|
|
||||||
String colorType,
|
|
||||||
@RequestParam("dpi")
|
|
||||||
@Parameter(description = "The DPI (dots per inch) for the output image(s)")
|
|
||||||
String dpi) throws IOException {
|
|
||||||
|
|
||||||
byte[] pdfBytes = file.getBytes();
|
byte[] pdfBytes = file.getBytes();
|
||||||
ImageType colorTypeResult = ImageType.RGB;
|
ImageType colorTypeResult = ImageType.RGB;
|
||||||
if ("greyscale".equals(colorType)) {
|
if ("greyscale".equals(colorType)) {
|
||||||
@@ -81,24 +74,17 @@ public class ConvertImgPDFController {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@PostMapping(consumes = "multipart/form-data", value = "/img-to-pdf")
|
@PostMapping(consumes = "multipart/form-data", value = "/img/pdf")
|
||||||
@Operation(summary = "Convert images to a PDF file",
|
@Operation(summary = "Convert images to a PDF file",
|
||||||
description = "This endpoint converts one or more images to a PDF file. Users can specify whether to stretch the images to fit the PDF page, and whether to automatically rotate the images.")
|
description = "This endpoint converts one or more images to a PDF file. Users can specify whether to stretch the images to fit the PDF page, and whether to automatically rotate the images. Input:Image Output:PDF Type:SISO?")
|
||||||
public ResponseEntity<byte[]> convertToPdf(
|
public ResponseEntity<byte[]> convertToPdf(@ModelAttribute ConvertToPdfRequest request) throws IOException {
|
||||||
@RequestPart(required = true, value = "fileInput")
|
MultipartFile[] file = request.getFileInput();
|
||||||
@Parameter(description = "The input images to be converted to a PDF file")
|
String fitOption = request.getFitOption();
|
||||||
MultipartFile[] file,
|
String colorType = request.getColorType();
|
||||||
@RequestParam(defaultValue = "false", name = "stretchToFit")
|
boolean autoRotate = request.isAutoRotate();
|
||||||
@Parameter(description = "Whether to stretch the images to fit the PDF page or maintain the aspect ratio", example = "false")
|
|
||||||
boolean stretchToFit,
|
|
||||||
@RequestParam("colorType")
|
|
||||||
@Parameter(description = "The color type of the output image(s)", schema = @Schema(allowableValues = {"rgb", "greyscale", "blackwhite"}))
|
|
||||||
String colorType,
|
|
||||||
@RequestParam(defaultValue = "false", name = "autoRotate")
|
|
||||||
@Parameter(description = "Whether to automatically rotate the images to better fit the PDF page", example = "true")
|
|
||||||
boolean autoRotate) throws IOException {
|
|
||||||
// Convert the file to PDF and get the resulting bytes
|
// Convert the file to PDF and get the resulting bytes
|
||||||
byte[] bytes = PdfUtils.imageToPdf(file, stretchToFit, autoRotate, colorType);
|
byte[] bytes = PdfUtils.imageToPdf(file, fitOption, autoRotate, colorType);
|
||||||
return WebResponseUtils.bytesToWebResponse(bytes, file[0].getOriginalFilename().replaceFirst("[.][^.]+$", "") + "_converted.pdf");
|
return WebResponseUtils.bytesToWebResponse(bytes, file[0].getOriginalFilename().replaceFirst("[.][^.]+$", "") + "_converted.pdf");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -0,0 +1,54 @@
|
|||||||
|
package stirling.software.SPDF.controller.api.converters;
|
||||||
|
|
||||||
|
import org.commonmark.node.Node;
|
||||||
|
import org.commonmark.parser.Parser;
|
||||||
|
import org.commonmark.renderer.html.HtmlRenderer;
|
||||||
|
import org.springframework.http.ResponseEntity;
|
||||||
|
import org.springframework.web.bind.annotation.ModelAttribute;
|
||||||
|
import org.springframework.web.bind.annotation.PostMapping;
|
||||||
|
import org.springframework.web.bind.annotation.RequestMapping;
|
||||||
|
import org.springframework.web.bind.annotation.RestController;
|
||||||
|
import org.springframework.web.multipart.MultipartFile;
|
||||||
|
|
||||||
|
import io.swagger.v3.oas.annotations.Operation;
|
||||||
|
import io.swagger.v3.oas.annotations.tags.Tag;
|
||||||
|
import stirling.software.SPDF.model.api.GeneralFile;
|
||||||
|
import stirling.software.SPDF.utils.FileToPdf;
|
||||||
|
import stirling.software.SPDF.utils.WebResponseUtils;
|
||||||
|
|
||||||
|
@RestController
|
||||||
|
@Tag(name = "Convert", description = "Convert APIs")
|
||||||
|
@RequestMapping("/api/v1/convert")
|
||||||
|
public class ConvertMarkdownToPdf {
|
||||||
|
|
||||||
|
@PostMapping(consumes = "multipart/form-data", value = "/markdown/pdf")
|
||||||
|
@Operation(
|
||||||
|
summary = "Convert a Markdown file to PDF",
|
||||||
|
description = "This endpoint takes a Markdown file input, converts it to HTML, and then to PDF format."
|
||||||
|
)
|
||||||
|
public ResponseEntity<byte[]> markdownToPdf(
|
||||||
|
@ModelAttribute GeneralFile request)
|
||||||
|
throws Exception {
|
||||||
|
MultipartFile fileInput = request.getFileInput();
|
||||||
|
|
||||||
|
if (fileInput == null) {
|
||||||
|
throw new IllegalArgumentException("Please provide a Markdown file for conversion.");
|
||||||
|
}
|
||||||
|
|
||||||
|
String originalFilename = fileInput.getOriginalFilename();
|
||||||
|
if (originalFilename == null || !originalFilename.endsWith(".md")) {
|
||||||
|
throw new IllegalArgumentException("File must be in .md format.");
|
||||||
|
}
|
||||||
|
|
||||||
|
// Convert Markdown to HTML using CommonMark
|
||||||
|
Parser parser = Parser.builder().build();
|
||||||
|
Node document = parser.parse(new String(fileInput.getBytes()));
|
||||||
|
HtmlRenderer renderer = HtmlRenderer.builder().build();
|
||||||
|
String htmlContent = renderer.render(document);
|
||||||
|
|
||||||
|
byte[] pdfBytes = FileToPdf.convertHtmlToPdf(htmlContent.getBytes(), "converted.html");
|
||||||
|
|
||||||
|
String outputFilename = originalFilename.replaceFirst("[.][^.]+$", "") + ".pdf"; // Remove file extension and append .pdf
|
||||||
|
return WebResponseUtils.bytesToWebResponse(pdfBytes, outputFilename);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -10,17 +10,22 @@ import java.util.List;
|
|||||||
|
|
||||||
import org.apache.commons.io.FilenameUtils;
|
import org.apache.commons.io.FilenameUtils;
|
||||||
import org.springframework.http.ResponseEntity;
|
import org.springframework.http.ResponseEntity;
|
||||||
|
import org.springframework.web.bind.annotation.ModelAttribute;
|
||||||
import org.springframework.web.bind.annotation.PostMapping;
|
import org.springframework.web.bind.annotation.PostMapping;
|
||||||
import org.springframework.web.bind.annotation.RequestPart;
|
import org.springframework.web.bind.annotation.RequestMapping;
|
||||||
import org.springframework.web.bind.annotation.RestController;
|
import org.springframework.web.bind.annotation.RestController;
|
||||||
import org.springframework.web.multipart.MultipartFile;
|
import org.springframework.web.multipart.MultipartFile;
|
||||||
|
|
||||||
import io.swagger.v3.oas.annotations.Operation;
|
import io.swagger.v3.oas.annotations.Operation;
|
||||||
import io.swagger.v3.oas.annotations.Parameter;
|
import io.swagger.v3.oas.annotations.tags.Tag;
|
||||||
|
import stirling.software.SPDF.model.api.GeneralFile;
|
||||||
import stirling.software.SPDF.utils.ProcessExecutor;
|
import stirling.software.SPDF.utils.ProcessExecutor;
|
||||||
|
import stirling.software.SPDF.utils.ProcessExecutor.ProcessExecutorResult;
|
||||||
import stirling.software.SPDF.utils.WebResponseUtils;
|
import stirling.software.SPDF.utils.WebResponseUtils;
|
||||||
|
|
||||||
@RestController
|
@RestController
|
||||||
|
@Tag(name = "Convert", description = "Convert APIs")
|
||||||
|
@RequestMapping("/api/v1/convert")
|
||||||
public class ConvertOfficeController {
|
public class ConvertOfficeController {
|
||||||
|
|
||||||
public byte[] convertToPdf(MultipartFile inputFile) throws IOException, InterruptedException {
|
public byte[] convertToPdf(MultipartFile inputFile) throws IOException, InterruptedException {
|
||||||
@@ -39,7 +44,7 @@ public class ConvertOfficeController {
|
|||||||
|
|
||||||
// Run the LibreOffice command
|
// Run the LibreOffice command
|
||||||
List<String> command = new ArrayList<>(Arrays.asList("unoconv", "-vvv", "-f", "pdf", "-o", tempOutputFile.toString(), tempInputFile.toString()));
|
List<String> command = new ArrayList<>(Arrays.asList("unoconv", "-vvv", "-f", "pdf", "-o", tempOutputFile.toString(), tempInputFile.toString()));
|
||||||
int returnCode = ProcessExecutor.getInstance(ProcessExecutor.Processes.LIBRE_OFFICE).runCommandWithOutputHandling(command);
|
ProcessExecutorResult returnCode = ProcessExecutor.getInstance(ProcessExecutor.Processes.LIBRE_OFFICE).runCommandWithOutputHandling(command);
|
||||||
|
|
||||||
// Read the converted PDF file
|
// Read the converted PDF file
|
||||||
byte[] pdfBytes = Files.readAllBytes(tempOutputFile);
|
byte[] pdfBytes = Files.readAllBytes(tempOutputFile);
|
||||||
@@ -55,19 +60,14 @@ public class ConvertOfficeController {
|
|||||||
return fileExtension.matches(extensionPattern);
|
return fileExtension.matches(extensionPattern);
|
||||||
}
|
}
|
||||||
|
|
||||||
@PostMapping(consumes = "multipart/form-data", value = "/file-to-pdf")
|
@PostMapping(consumes = "multipart/form-data", value = "/file/pdf")
|
||||||
@Operation(
|
@Operation(
|
||||||
summary = "Convert a file to a PDF using OCR",
|
summary = "Convert a file to a PDF using LibreOffice",
|
||||||
description = "This endpoint converts a given file to a PDF using Optical Character Recognition (OCR). The filename of the resulting PDF will be the original filename with '_convertedToPDF.pdf' appended."
|
description = "This endpoint converts a given file to a PDF using LibreOffice API Input:Any Output:PDF Type:SISO"
|
||||||
)
|
)
|
||||||
public ResponseEntity<byte[]> processPdfWithOCR(
|
public ResponseEntity<byte[]> processFileToPDF(@ModelAttribute GeneralFile request)
|
||||||
@RequestPart(required = true, value = "fileInput")
|
throws Exception {
|
||||||
@Parameter(
|
MultipartFile inputFile = request.getFileInput();
|
||||||
description = "The input file to be converted to a PDF file using OCR",
|
|
||||||
required = true
|
|
||||||
)
|
|
||||||
MultipartFile inputFile
|
|
||||||
) throws IOException, InterruptedException {
|
|
||||||
// unused but can start server instance if startup time is to long
|
// unused but can start server instance if startup time is to long
|
||||||
// LibreOfficeListener.getInstance().start();
|
// LibreOfficeListener.getInstance().start();
|
||||||
|
|
||||||
|
|||||||
@@ -3,67 +3,67 @@ package stirling.software.SPDF.controller.api.converters;
|
|||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
|
|
||||||
import org.springframework.http.ResponseEntity;
|
import org.springframework.http.ResponseEntity;
|
||||||
|
import org.springframework.web.bind.annotation.ModelAttribute;
|
||||||
import org.springframework.web.bind.annotation.PostMapping;
|
import org.springframework.web.bind.annotation.PostMapping;
|
||||||
import org.springframework.web.bind.annotation.RequestParam;
|
import org.springframework.web.bind.annotation.RequestMapping;
|
||||||
import org.springframework.web.bind.annotation.RequestPart;
|
|
||||||
import org.springframework.web.bind.annotation.RestController;
|
import org.springframework.web.bind.annotation.RestController;
|
||||||
import org.springframework.web.multipart.MultipartFile;
|
import org.springframework.web.multipart.MultipartFile;
|
||||||
|
|
||||||
import io.swagger.v3.oas.annotations.Operation;
|
import io.swagger.v3.oas.annotations.Operation;
|
||||||
import io.swagger.v3.oas.annotations.Parameter;
|
import io.swagger.v3.oas.annotations.tags.Tag;
|
||||||
import io.swagger.v3.oas.annotations.media.Schema;
|
import stirling.software.SPDF.model.api.PDFFile;
|
||||||
|
import stirling.software.SPDF.model.api.converters.PdfToPresentationRequest;
|
||||||
|
import stirling.software.SPDF.model.api.converters.PdfToTextOrRTFRequest;
|
||||||
|
import stirling.software.SPDF.model.api.converters.PdfToWordRequest;
|
||||||
import stirling.software.SPDF.utils.PDFToFile;
|
import stirling.software.SPDF.utils.PDFToFile;
|
||||||
|
|
||||||
@RestController
|
@RestController
|
||||||
|
@RequestMapping("/api/v1/convert")
|
||||||
|
@Tag(name = "Convert", description = "Convert APIs")
|
||||||
public class ConvertPDFToOffice {
|
public class ConvertPDFToOffice {
|
||||||
|
|
||||||
@PostMapping(consumes = "multipart/form-data", value = "/pdf-to-html")
|
@PostMapping(consumes = "multipart/form-data", value = "/pdf/html")
|
||||||
@Operation(summary = "Convert PDF to HTML", description = "This endpoint converts a PDF file to HTML format.")
|
@Operation(summary = "Convert PDF to HTML", description = "This endpoint converts a PDF file to HTML format. Input:PDF Output:HTML Type:SISO")
|
||||||
public ResponseEntity<byte[]> processPdfToHTML(
|
public ResponseEntity<byte[]> processPdfToHTML(@ModelAttribute PDFFile request)
|
||||||
@RequestPart(required = true, value = "fileInput") @Parameter(description = "The input PDF file to be converted to HTML format", required = true) MultipartFile inputFile)
|
throws Exception {
|
||||||
throws IOException, InterruptedException {
|
MultipartFile inputFile = request.getFileInput();
|
||||||
PDFToFile pdfToFile = new PDFToFile();
|
PDFToFile pdfToFile = new PDFToFile();
|
||||||
return pdfToFile.processPdfToOfficeFormat(inputFile, "html", "writer_pdf_import");
|
return pdfToFile.processPdfToOfficeFormat(inputFile, "html", "writer_pdf_import");
|
||||||
}
|
}
|
||||||
|
|
||||||
@PostMapping(consumes = "multipart/form-data", value = "/pdf-to-presentation")
|
@PostMapping(consumes = "multipart/form-data", value = "/pdf/presentation")
|
||||||
@Operation(summary = "Convert PDF to Presentation format", description = "This endpoint converts a given PDF file to a Presentation format.")
|
@Operation(summary = "Convert PDF to Presentation format", description = "This endpoint converts a given PDF file to a Presentation format. Input:PDF Output:PPT Type:SISO")
|
||||||
public ResponseEntity<byte[]> processPdfToPresentation(
|
public ResponseEntity<byte[]> processPdfToPresentation(@ModelAttribute PdfToPresentationRequest request) throws IOException, InterruptedException {
|
||||||
@RequestPart(required = true, value = "fileInput") @Parameter(description = "The input PDF file") MultipartFile inputFile,
|
MultipartFile inputFile = request.getFileInput();
|
||||||
@RequestParam("outputFormat") @Parameter(description = "The output Presentation format", schema = @Schema(allowableValues = {
|
String outputFormat = request.getOutputFormat();
|
||||||
"ppt", "pptx", "odp" })) String outputFormat)
|
|
||||||
throws IOException, InterruptedException {
|
|
||||||
PDFToFile pdfToFile = new PDFToFile();
|
PDFToFile pdfToFile = new PDFToFile();
|
||||||
return pdfToFile.processPdfToOfficeFormat(inputFile, outputFormat, "impress_pdf_import");
|
return pdfToFile.processPdfToOfficeFormat(inputFile, outputFormat, "impress_pdf_import");
|
||||||
}
|
}
|
||||||
|
|
||||||
@PostMapping(consumes = "multipart/form-data", value = "/pdf-to-text")
|
@PostMapping(consumes = "multipart/form-data", value = "/pdf/text")
|
||||||
@Operation(summary = "Convert PDF to Text or RTF format", description = "This endpoint converts a given PDF file to Text or RTF format.")
|
@Operation(summary = "Convert PDF to Text or RTF format", description = "This endpoint converts a given PDF file to Text or RTF format. Input:PDF Output:TXT Type:SISO")
|
||||||
public ResponseEntity<byte[]> processPdfToRTForTXT(
|
public ResponseEntity<byte[]> processPdfToRTForTXT(@ModelAttribute PdfToTextOrRTFRequest request) throws IOException, InterruptedException {
|
||||||
@RequestPart(required = true, value = "fileInput") @Parameter(description = "The input PDF file") MultipartFile inputFile,
|
MultipartFile inputFile = request.getFileInput();
|
||||||
@RequestParam("outputFormat") @Parameter(description = "The output Text or RTF format", schema = @Schema(allowableValues = {
|
String outputFormat = request.getOutputFormat();
|
||||||
"rtf", "txt:Text" })) String outputFormat)
|
|
||||||
throws IOException, InterruptedException {
|
|
||||||
PDFToFile pdfToFile = new PDFToFile();
|
PDFToFile pdfToFile = new PDFToFile();
|
||||||
return pdfToFile.processPdfToOfficeFormat(inputFile, outputFormat, "writer_pdf_import");
|
return pdfToFile.processPdfToOfficeFormat(inputFile, outputFormat, "writer_pdf_import");
|
||||||
}
|
}
|
||||||
|
|
||||||
@PostMapping(consumes = "multipart/form-data", value = "/pdf-to-word")
|
@PostMapping(consumes = "multipart/form-data", value = "/pdf/word")
|
||||||
@Operation(summary = "Convert PDF to Word document", description = "This endpoint converts a given PDF file to a Word document format.")
|
@Operation(summary = "Convert PDF to Word document", description = "This endpoint converts a given PDF file to a Word document format. Input:PDF Output:WORD Type:SISO")
|
||||||
public ResponseEntity<byte[]> processPdfToWord(
|
public ResponseEntity<byte[]> processPdfToWord(@ModelAttribute PdfToWordRequest request) throws IOException, InterruptedException {
|
||||||
@RequestPart(required = true, value = "fileInput") @Parameter(description = "The input PDF file") MultipartFile inputFile,
|
MultipartFile inputFile = request.getFileInput();
|
||||||
@RequestParam("outputFormat") @Parameter(description = "The output Word document format", schema = @Schema(allowableValues = {
|
String outputFormat = request.getOutputFormat();
|
||||||
"doc", "docx", "odt" })) String outputFormat)
|
|
||||||
throws IOException, InterruptedException {
|
|
||||||
PDFToFile pdfToFile = new PDFToFile();
|
PDFToFile pdfToFile = new PDFToFile();
|
||||||
return pdfToFile.processPdfToOfficeFormat(inputFile, outputFormat, "writer_pdf_import");
|
return pdfToFile.processPdfToOfficeFormat(inputFile, outputFormat, "writer_pdf_import");
|
||||||
}
|
}
|
||||||
|
|
||||||
@PostMapping(consumes = "multipart/form-data", value = "/pdf-to-xml")
|
@PostMapping(consumes = "multipart/form-data", value = "/pdf/xml")
|
||||||
@Operation(summary = "Convert PDF to XML", description = "This endpoint converts a PDF file to an XML file.")
|
@Operation(summary = "Convert PDF to XML", description = "This endpoint converts a PDF file to an XML file. Input:PDF Output:XML Type:SISO")
|
||||||
public ResponseEntity<byte[]> processPdfToXML(
|
public ResponseEntity<byte[]> processPdfToXML(@ModelAttribute PDFFile request)
|
||||||
@RequestPart(required = true, value = "fileInput") @Parameter(description = "The input PDF file to be converted to an XML file", required = true) MultipartFile inputFile)
|
throws Exception {
|
||||||
throws IOException, InterruptedException {
|
MultipartFile inputFile = request.getFileInput();
|
||||||
|
|
||||||
PDFToFile pdfToFile = new PDFToFile();
|
PDFToFile pdfToFile = new PDFToFile();
|
||||||
return pdfToFile.processPdfToOfficeFormat(inputFile, "xml", "writer_pdf_import");
|
return pdfToFile.processPdfToOfficeFormat(inputFile, "xml", "writer_pdf_import");
|
||||||
|
|||||||
@@ -1,34 +1,37 @@
|
|||||||
package stirling.software.SPDF.controller.api.converters;
|
package stirling.software.SPDF.controller.api.converters;
|
||||||
|
|
||||||
import java.io.IOException;
|
|
||||||
import java.nio.file.Files;
|
import java.nio.file.Files;
|
||||||
import java.nio.file.Path;
|
import java.nio.file.Path;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
import org.springframework.http.ResponseEntity;
|
import org.springframework.http.ResponseEntity;
|
||||||
|
import org.springframework.web.bind.annotation.ModelAttribute;
|
||||||
import org.springframework.web.bind.annotation.PostMapping;
|
import org.springframework.web.bind.annotation.PostMapping;
|
||||||
import org.springframework.web.bind.annotation.RequestPart;
|
import org.springframework.web.bind.annotation.RequestMapping;
|
||||||
import org.springframework.web.bind.annotation.RestController;
|
import org.springframework.web.bind.annotation.RestController;
|
||||||
import org.springframework.web.multipart.MultipartFile;
|
import org.springframework.web.multipart.MultipartFile;
|
||||||
|
|
||||||
import io.swagger.v3.oas.annotations.Operation;
|
import io.swagger.v3.oas.annotations.Operation;
|
||||||
import io.swagger.v3.oas.annotations.Parameter;
|
import io.swagger.v3.oas.annotations.tags.Tag;
|
||||||
|
import stirling.software.SPDF.model.api.PDFFile;
|
||||||
import stirling.software.SPDF.utils.ProcessExecutor;
|
import stirling.software.SPDF.utils.ProcessExecutor;
|
||||||
|
import stirling.software.SPDF.utils.ProcessExecutor.ProcessExecutorResult;
|
||||||
import stirling.software.SPDF.utils.WebResponseUtils;
|
import stirling.software.SPDF.utils.WebResponseUtils;
|
||||||
|
|
||||||
@RestController
|
@RestController
|
||||||
|
@RequestMapping("/api/v1/convert")
|
||||||
|
@Tag(name = "Convert", description = "Convert APIs")
|
||||||
public class ConvertPDFToPDFA {
|
public class ConvertPDFToPDFA {
|
||||||
|
|
||||||
@PostMapping(consumes = "multipart/form-data", value = "/pdf-to-pdfa")
|
@PostMapping(consumes = "multipart/form-data", value = "/pdf/pdfa")
|
||||||
@Operation(
|
@Operation(
|
||||||
summary = "Convert a PDF to a PDF/A",
|
summary = "Convert a PDF to a PDF/A",
|
||||||
description = "This endpoint converts a PDF file to a PDF/A file. PDF/A is a format designed for long-term archiving of digital documents."
|
description = "This endpoint converts a PDF file to a PDF/A file. PDF/A is a format designed for long-term archiving of digital documents. Input:PDF Output:PDF Type:SISO"
|
||||||
)
|
)
|
||||||
public ResponseEntity<byte[]> pdfToPdfA(
|
public ResponseEntity<byte[]> pdfToPdfA(@ModelAttribute PDFFile request)
|
||||||
@RequestPart(required = true, value = "fileInput")
|
throws Exception {
|
||||||
@Parameter(description = "The input PDF file to be converted to a PDF/A file", required = true)
|
MultipartFile inputFile = request.getFileInput();
|
||||||
MultipartFile inputFile) throws IOException, InterruptedException {
|
|
||||||
|
|
||||||
// Save the uploaded file to a temporary location
|
// Save the uploaded file to a temporary location
|
||||||
Path tempInputFile = Files.createTempFile("input_", ".pdf");
|
Path tempInputFile = Files.createTempFile("input_", ".pdf");
|
||||||
@@ -47,7 +50,7 @@ public class ConvertPDFToPDFA {
|
|||||||
command.add(tempInputFile.toString());
|
command.add(tempInputFile.toString());
|
||||||
command.add(tempOutputFile.toString());
|
command.add(tempOutputFile.toString());
|
||||||
|
|
||||||
int returnCode = ProcessExecutor.getInstance(ProcessExecutor.Processes.OCR_MY_PDF).runCommandWithOutputHandling(command);
|
ProcessExecutorResult returnCode = ProcessExecutor.getInstance(ProcessExecutor.Processes.OCR_MY_PDF).runCommandWithOutputHandling(command);
|
||||||
|
|
||||||
// Read the optimized PDF file
|
// Read the optimized PDF file
|
||||||
byte[] pdfBytes = Files.readAllBytes(tempOutputFile);
|
byte[] pdfBytes = Files.readAllBytes(tempOutputFile);
|
||||||
|
|||||||
@@ -0,0 +1,76 @@
|
|||||||
|
package stirling.software.SPDF.controller.api.converters;
|
||||||
|
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.nio.file.Files;
|
||||||
|
import java.nio.file.Path;
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
import org.springframework.http.ResponseEntity;
|
||||||
|
import org.springframework.web.bind.annotation.ModelAttribute;
|
||||||
|
import org.springframework.web.bind.annotation.PostMapping;
|
||||||
|
import org.springframework.web.bind.annotation.RequestMapping;
|
||||||
|
import org.springframework.web.bind.annotation.RestController;
|
||||||
|
|
||||||
|
import io.swagger.v3.oas.annotations.Operation;
|
||||||
|
import io.swagger.v3.oas.annotations.tags.Tag;
|
||||||
|
import stirling.software.SPDF.model.api.converters.UrlToPdfRequest;
|
||||||
|
import stirling.software.SPDF.utils.GeneralUtils;
|
||||||
|
import stirling.software.SPDF.utils.ProcessExecutor;
|
||||||
|
import stirling.software.SPDF.utils.ProcessExecutor.ProcessExecutorResult;
|
||||||
|
import stirling.software.SPDF.utils.WebResponseUtils;
|
||||||
|
|
||||||
|
@RestController
|
||||||
|
@Tag(name = "Convert", description = "Convert APIs")
|
||||||
|
@RequestMapping("/api/v1/convert")
|
||||||
|
public class ConvertWebsiteToPDF {
|
||||||
|
|
||||||
|
@PostMapping(consumes = "multipart/form-data", value = "/url/pdf")
|
||||||
|
@Operation(
|
||||||
|
summary = "Convert a URL to a PDF",
|
||||||
|
description = "This endpoint fetches content from a URL and converts it to a PDF format."
|
||||||
|
)
|
||||||
|
public ResponseEntity<byte[]> urlToPdf(@ModelAttribute UrlToPdfRequest request) throws IOException, InterruptedException {
|
||||||
|
String URL = request.getUrlInput();
|
||||||
|
|
||||||
|
// Validate the URL format
|
||||||
|
if(!URL.matches("^https?://.*") || !GeneralUtils.isValidURL(URL)) {
|
||||||
|
throw new IllegalArgumentException("Invalid URL format provided.");
|
||||||
|
}
|
||||||
|
Path tempOutputFile = null;
|
||||||
|
byte[] pdfBytes;
|
||||||
|
try {
|
||||||
|
// Prepare the output file path
|
||||||
|
tempOutputFile = Files.createTempFile("output_", ".pdf");
|
||||||
|
|
||||||
|
// Prepare the OCRmyPDF command
|
||||||
|
List<String> command = new ArrayList<>();
|
||||||
|
command.add("weasyprint");
|
||||||
|
command.add(URL);
|
||||||
|
command.add(tempOutputFile.toString());
|
||||||
|
|
||||||
|
ProcessExecutorResult returnCode = ProcessExecutor.getInstance(ProcessExecutor.Processes.WEASYPRINT).runCommandWithOutputHandling(command);
|
||||||
|
|
||||||
|
// Read the optimized PDF file
|
||||||
|
pdfBytes = Files.readAllBytes(tempOutputFile);
|
||||||
|
}
|
||||||
|
finally {
|
||||||
|
// Clean up the temporary files
|
||||||
|
Files.delete(tempOutputFile);
|
||||||
|
}
|
||||||
|
// Convert URL to a safe filename
|
||||||
|
String outputFilename = convertURLToFileName(URL);
|
||||||
|
|
||||||
|
return WebResponseUtils.bytesToWebResponse(pdfBytes, outputFilename);
|
||||||
|
}
|
||||||
|
|
||||||
|
private String convertURLToFileName(String url) {
|
||||||
|
String safeName = url.replaceAll("[^a-zA-Z0-9]", "_");
|
||||||
|
if(safeName.length() > 50) {
|
||||||
|
safeName = safeName.substring(0, 50); // restrict to 50 characters
|
||||||
|
}
|
||||||
|
return safeName + ".pdf";
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
@@ -0,0 +1,196 @@
|
|||||||
|
package stirling.software.SPDF.controller.api.filters;
|
||||||
|
|
||||||
|
import java.io.IOException;
|
||||||
|
|
||||||
|
import org.apache.pdfbox.pdmodel.PDDocument;
|
||||||
|
import org.apache.pdfbox.pdmodel.PDPage;
|
||||||
|
import org.apache.pdfbox.pdmodel.common.PDRectangle;
|
||||||
|
import org.springframework.http.ResponseEntity;
|
||||||
|
import org.springframework.web.bind.annotation.ModelAttribute;
|
||||||
|
import org.springframework.web.bind.annotation.PostMapping;
|
||||||
|
import org.springframework.web.bind.annotation.RequestMapping;
|
||||||
|
import org.springframework.web.bind.annotation.RestController;
|
||||||
|
import org.springframework.web.multipart.MultipartFile;
|
||||||
|
|
||||||
|
import io.swagger.v3.oas.annotations.Operation;
|
||||||
|
import io.swagger.v3.oas.annotations.tags.Tag;
|
||||||
|
import stirling.software.SPDF.model.api.PDFComparisonAndCount;
|
||||||
|
import stirling.software.SPDF.model.api.PDFWithPageNums;
|
||||||
|
import stirling.software.SPDF.model.api.filter.ContainsTextRequest;
|
||||||
|
import stirling.software.SPDF.model.api.filter.FileSizeRequest;
|
||||||
|
import stirling.software.SPDF.model.api.filter.PageRotationRequest;
|
||||||
|
import stirling.software.SPDF.model.api.filter.PageSizeRequest;
|
||||||
|
import stirling.software.SPDF.utils.PdfUtils;
|
||||||
|
import stirling.software.SPDF.utils.WebResponseUtils;
|
||||||
|
|
||||||
|
@RestController
|
||||||
|
@RequestMapping("/api/v1/filter")
|
||||||
|
@Tag(name = "Filter", description = "Filter APIs")
|
||||||
|
public class FilterController {
|
||||||
|
|
||||||
|
@PostMapping(consumes = "multipart/form-data", value = "/filter-contains-text")
|
||||||
|
@Operation(summary = "Checks if a PDF contains set text, returns true if does", description = "Input:PDF Output:Boolean Type:SISO")
|
||||||
|
public ResponseEntity<byte[]> containsText(@ModelAttribute ContainsTextRequest request) throws IOException, InterruptedException {
|
||||||
|
MultipartFile inputFile = request.getFileInput();
|
||||||
|
String text = request.getText();
|
||||||
|
String pageNumber = request.getPageNumbers();
|
||||||
|
|
||||||
|
PDDocument pdfDocument = PDDocument.load(inputFile.getInputStream());
|
||||||
|
if (PdfUtils.hasText(pdfDocument, pageNumber, text))
|
||||||
|
return WebResponseUtils.pdfDocToWebResponse(pdfDocument, inputFile.getOriginalFilename());
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
// TODO
|
||||||
|
@PostMapping(consumes = "multipart/form-data", value = "/filter-contains-image")
|
||||||
|
@Operation(summary = "Checks if a PDF contains an image", description = "Input:PDF Output:Boolean Type:SISO")
|
||||||
|
public ResponseEntity<byte[]> containsImage(@ModelAttribute PDFWithPageNums request)
|
||||||
|
throws IOException, InterruptedException {
|
||||||
|
MultipartFile inputFile = request.getFileInput();
|
||||||
|
String pageNumber = request.getPageNumbers();
|
||||||
|
|
||||||
|
PDDocument pdfDocument = PDDocument.load(inputFile.getInputStream());
|
||||||
|
if (PdfUtils.hasImages(pdfDocument, pageNumber))
|
||||||
|
return WebResponseUtils.pdfDocToWebResponse(pdfDocument, inputFile.getOriginalFilename());
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
@PostMapping(consumes = "multipart/form-data", value = "/filter-page-count")
|
||||||
|
@Operation(summary = "Checks if a PDF is greater, less or equal to a setPageCount", description = "Input:PDF Output:Boolean Type:SISO")
|
||||||
|
public ResponseEntity<byte[]> pageCount(@ModelAttribute PDFComparisonAndCount request) throws IOException, InterruptedException {
|
||||||
|
MultipartFile inputFile = request.getFileInput();
|
||||||
|
String pageCount = request.getPageCount();
|
||||||
|
String comparator = request.getComparator();
|
||||||
|
// Load the PDF
|
||||||
|
PDDocument document = PDDocument.load(inputFile.getInputStream());
|
||||||
|
int actualPageCount = document.getNumberOfPages();
|
||||||
|
|
||||||
|
boolean valid = false;
|
||||||
|
// Perform the comparison
|
||||||
|
switch (comparator) {
|
||||||
|
case "Greater":
|
||||||
|
valid = actualPageCount > Integer.parseInt(pageCount);
|
||||||
|
break;
|
||||||
|
case "Equal":
|
||||||
|
valid = actualPageCount == Integer.parseInt(pageCount);
|
||||||
|
break;
|
||||||
|
case "Less":
|
||||||
|
valid = actualPageCount < Integer.parseInt(pageCount);
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
throw new IllegalArgumentException("Invalid comparator: " + comparator);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (valid)
|
||||||
|
return WebResponseUtils.multiPartFileToWebResponse(inputFile);
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
@PostMapping(consumes = "multipart/form-data", value = "/filter-page-size")
|
||||||
|
@Operation(summary = "Checks if a PDF is of a certain size", description = "Input:PDF Output:Boolean Type:SISO")
|
||||||
|
public ResponseEntity<byte[]> pageSize(@ModelAttribute PageSizeRequest request) throws IOException, InterruptedException {
|
||||||
|
MultipartFile inputFile = request.getFileInput();
|
||||||
|
String standardPageSize = request.getStandardPageSize();
|
||||||
|
String comparator = request.getComparator();
|
||||||
|
|
||||||
|
// Load the PDF
|
||||||
|
PDDocument document = PDDocument.load(inputFile.getInputStream());
|
||||||
|
|
||||||
|
PDPage firstPage = document.getPage(0);
|
||||||
|
PDRectangle actualPageSize = firstPage.getMediaBox();
|
||||||
|
|
||||||
|
// Calculate the area of the actual page size
|
||||||
|
float actualArea = actualPageSize.getWidth() * actualPageSize.getHeight();
|
||||||
|
|
||||||
|
// Get the standard size and calculate its area
|
||||||
|
PDRectangle standardSize = PdfUtils.textToPageSize(standardPageSize);
|
||||||
|
float standardArea = standardSize.getWidth() * standardSize.getHeight();
|
||||||
|
|
||||||
|
boolean valid = false;
|
||||||
|
// Perform the comparison
|
||||||
|
switch (comparator) {
|
||||||
|
case "Greater":
|
||||||
|
valid = actualArea > standardArea;
|
||||||
|
break;
|
||||||
|
case "Equal":
|
||||||
|
valid = actualArea == standardArea;
|
||||||
|
break;
|
||||||
|
case "Less":
|
||||||
|
valid = actualArea < standardArea;
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
throw new IllegalArgumentException("Invalid comparator: " + comparator);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (valid)
|
||||||
|
return WebResponseUtils.multiPartFileToWebResponse(inputFile);
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
@PostMapping(consumes = "multipart/form-data", value = "/filter-file-size")
|
||||||
|
@Operation(summary = "Checks if a PDF is a set file size", description = "Input:PDF Output:Boolean Type:SISO")
|
||||||
|
public ResponseEntity<byte[]> fileSize(@ModelAttribute FileSizeRequest request) throws IOException, InterruptedException {
|
||||||
|
MultipartFile inputFile = request.getFileInput();
|
||||||
|
String fileSize = request.getFileSize();
|
||||||
|
String comparator = request.getComparator();
|
||||||
|
|
||||||
|
// Get the file size
|
||||||
|
long actualFileSize = inputFile.getSize();
|
||||||
|
|
||||||
|
boolean valid = false;
|
||||||
|
// Perform the comparison
|
||||||
|
switch (comparator) {
|
||||||
|
case "Greater":
|
||||||
|
valid = actualFileSize > Long.parseLong(fileSize);
|
||||||
|
break;
|
||||||
|
case "Equal":
|
||||||
|
valid = actualFileSize == Long.parseLong(fileSize);
|
||||||
|
break;
|
||||||
|
case "Less":
|
||||||
|
valid = actualFileSize < Long.parseLong(fileSize);
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
throw new IllegalArgumentException("Invalid comparator: " + comparator);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (valid)
|
||||||
|
return WebResponseUtils.multiPartFileToWebResponse(inputFile);
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
@PostMapping(consumes = "multipart/form-data", value = "/filter-page-rotation")
|
||||||
|
@Operation(summary = "Checks if a PDF is of a certain rotation", description = "Input:PDF Output:Boolean Type:SISO")
|
||||||
|
public ResponseEntity<byte[]> pageRotation(@ModelAttribute PageRotationRequest request) throws IOException, InterruptedException {
|
||||||
|
MultipartFile inputFile = request.getFileInput();
|
||||||
|
int rotation = request.getRotation();
|
||||||
|
String comparator = request.getComparator();
|
||||||
|
|
||||||
|
// Load the PDF
|
||||||
|
PDDocument document = PDDocument.load(inputFile.getInputStream());
|
||||||
|
|
||||||
|
// Get the rotation of the first page
|
||||||
|
PDPage firstPage = document.getPage(0);
|
||||||
|
int actualRotation = firstPage.getRotation();
|
||||||
|
boolean valid = false;
|
||||||
|
// Perform the comparison
|
||||||
|
switch (comparator) {
|
||||||
|
case "Greater":
|
||||||
|
valid = actualRotation > rotation;
|
||||||
|
break;
|
||||||
|
case "Equal":
|
||||||
|
valid = actualRotation == rotation;
|
||||||
|
break;
|
||||||
|
case "Less":
|
||||||
|
valid = actualRotation < rotation;
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
throw new IllegalArgumentException("Invalid comparator: " + comparator);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (valid)
|
||||||
|
return WebResponseUtils.multiPartFileToWebResponse(inputFile);
|
||||||
|
return null;
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -0,0 +1,128 @@
|
|||||||
|
package stirling.software.SPDF.controller.api.misc;
|
||||||
|
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.Comparator;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
import org.apache.pdfbox.pdmodel.PDDocument;
|
||||||
|
import org.apache.pdfbox.text.PDFTextStripper;
|
||||||
|
import org.apache.pdfbox.text.TextPosition;
|
||||||
|
import org.slf4j.Logger;
|
||||||
|
import org.slf4j.LoggerFactory;
|
||||||
|
import org.springframework.http.ResponseEntity;
|
||||||
|
import org.springframework.web.bind.annotation.ModelAttribute;
|
||||||
|
import org.springframework.web.bind.annotation.PostMapping;
|
||||||
|
import org.springframework.web.bind.annotation.RequestMapping;
|
||||||
|
import org.springframework.web.bind.annotation.RestController;
|
||||||
|
import org.springframework.web.multipart.MultipartFile;
|
||||||
|
|
||||||
|
import io.swagger.v3.oas.annotations.Operation;
|
||||||
|
import io.swagger.v3.oas.annotations.tags.Tag;
|
||||||
|
import stirling.software.SPDF.model.api.misc.ExtractHeaderRequest;
|
||||||
|
import stirling.software.SPDF.utils.WebResponseUtils;
|
||||||
|
@RestController
|
||||||
|
@RequestMapping("/api/v1/misc")
|
||||||
|
@Tag(name = "Misc", description = "Miscellaneous APIs")
|
||||||
|
public class AutoRenameController {
|
||||||
|
|
||||||
|
private static final Logger logger = LoggerFactory.getLogger(AutoRenameController.class);
|
||||||
|
|
||||||
|
private static final float TITLE_FONT_SIZE_THRESHOLD = 20.0f;
|
||||||
|
private static final int LINE_LIMIT = 11;
|
||||||
|
|
||||||
|
@PostMapping(consumes = "multipart/form-data", value = "/auto-rename")
|
||||||
|
@Operation(summary = "Extract header from PDF file", description = "This endpoint accepts a PDF file and attempts to extract its title or header based on heuristics. Input:PDF Output:PDF Type:SISO")
|
||||||
|
public ResponseEntity<byte[]> extractHeader(@ModelAttribute ExtractHeaderRequest request) throws Exception {
|
||||||
|
MultipartFile file = request.getFileInput();
|
||||||
|
Boolean useFirstTextAsFallback = request.isUseFirstTextAsFallback();
|
||||||
|
|
||||||
|
PDDocument document = PDDocument.load(file.getInputStream());
|
||||||
|
PDFTextStripper reader = new PDFTextStripper() {
|
||||||
|
class LineInfo {
|
||||||
|
String text;
|
||||||
|
float fontSize;
|
||||||
|
|
||||||
|
LineInfo(String text, float fontSize) {
|
||||||
|
this.text = text;
|
||||||
|
this.fontSize = fontSize;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
List<LineInfo> lineInfos = new ArrayList<>();
|
||||||
|
StringBuilder lineBuilder = new StringBuilder();
|
||||||
|
float lastY = -1;
|
||||||
|
float maxFontSizeInLine = 0.0f;
|
||||||
|
int lineCount = 0;
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected void processTextPosition(TextPosition text) {
|
||||||
|
if (lastY != text.getY() && lineCount < LINE_LIMIT) {
|
||||||
|
processLine();
|
||||||
|
lineBuilder = new StringBuilder(text.getUnicode());
|
||||||
|
maxFontSizeInLine = text.getFontSizeInPt();
|
||||||
|
lastY = text.getY();
|
||||||
|
lineCount++;
|
||||||
|
} else if (lineCount < LINE_LIMIT) {
|
||||||
|
lineBuilder.append(text.getUnicode());
|
||||||
|
if (text.getFontSizeInPt() > maxFontSizeInLine) {
|
||||||
|
maxFontSizeInLine = text.getFontSizeInPt();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private void processLine() {
|
||||||
|
if (lineBuilder.length() > 0 && lineCount < LINE_LIMIT) {
|
||||||
|
lineInfos.add(new LineInfo(lineBuilder.toString(), maxFontSizeInLine));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String getText(PDDocument doc) throws IOException {
|
||||||
|
this.lineInfos.clear();
|
||||||
|
this.lineBuilder = new StringBuilder();
|
||||||
|
this.lastY = -1;
|
||||||
|
this.maxFontSizeInLine = 0.0f;
|
||||||
|
this.lineCount = 0;
|
||||||
|
super.getText(doc);
|
||||||
|
processLine(); // Process the last line
|
||||||
|
|
||||||
|
// Merge lines with same font size
|
||||||
|
List<LineInfo> mergedLineInfos = new ArrayList<>();
|
||||||
|
for (int i = 0; i < lineInfos.size(); i++) {
|
||||||
|
String mergedText = lineInfos.get(i).text;
|
||||||
|
float fontSize = lineInfos.get(i).fontSize;
|
||||||
|
while (i + 1 < lineInfos.size() && lineInfos.get(i + 1).fontSize == fontSize) {
|
||||||
|
mergedText += " " + lineInfos.get(i + 1).text;
|
||||||
|
i++;
|
||||||
|
}
|
||||||
|
mergedLineInfos.add(new LineInfo(mergedText, fontSize));
|
||||||
|
}
|
||||||
|
|
||||||
|
// Sort lines by font size in descending order and get the first one
|
||||||
|
mergedLineInfos.sort(Comparator.comparing((LineInfo li) -> li.fontSize).reversed());
|
||||||
|
String title = mergedLineInfos.isEmpty() ? null : mergedLineInfos.get(0).text;
|
||||||
|
|
||||||
|
return title != null ? title : (useFirstTextAsFallback ? (mergedLineInfos.isEmpty() ? null : mergedLineInfos.get(mergedLineInfos.size() - 1).text) : null);
|
||||||
|
}
|
||||||
|
|
||||||
|
};
|
||||||
|
|
||||||
|
String header = reader.getText(document);
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
// Sanitize the header string by removing characters not allowed in a filename.
|
||||||
|
if (header != null && header.length() < 255) {
|
||||||
|
header = header.replaceAll("[/\\\\?%*:|\"<>]", "");
|
||||||
|
return WebResponseUtils.pdfDocToWebResponse(document, header + ".pdf");
|
||||||
|
} else {
|
||||||
|
logger.info("File has no good title to be found");
|
||||||
|
return WebResponseUtils.pdfDocToWebResponse(document, file.getOriginalFilename());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
@@ -0,0 +1,144 @@
|
|||||||
|
package stirling.software.SPDF.controller.api.misc;
|
||||||
|
import java.awt.image.BufferedImage;
|
||||||
|
import java.awt.image.DataBufferByte;
|
||||||
|
import java.awt.image.DataBufferInt;
|
||||||
|
import java.io.ByteArrayOutputStream;
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.io.InputStream;
|
||||||
|
import java.nio.file.Files;
|
||||||
|
import java.nio.file.Path;
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.zip.ZipEntry;
|
||||||
|
import java.util.zip.ZipOutputStream;
|
||||||
|
|
||||||
|
import org.apache.pdfbox.pdmodel.PDDocument;
|
||||||
|
import org.apache.pdfbox.rendering.PDFRenderer;
|
||||||
|
import org.springframework.http.MediaType;
|
||||||
|
import org.springframework.http.ResponseEntity;
|
||||||
|
import org.springframework.web.bind.annotation.ModelAttribute;
|
||||||
|
import org.springframework.web.bind.annotation.PostMapping;
|
||||||
|
import org.springframework.web.bind.annotation.RequestMapping;
|
||||||
|
import org.springframework.web.bind.annotation.RestController;
|
||||||
|
import org.springframework.web.multipart.MultipartFile;
|
||||||
|
|
||||||
|
import com.google.zxing.BinaryBitmap;
|
||||||
|
import com.google.zxing.LuminanceSource;
|
||||||
|
import com.google.zxing.MultiFormatReader;
|
||||||
|
import com.google.zxing.NotFoundException;
|
||||||
|
import com.google.zxing.PlanarYUVLuminanceSource;
|
||||||
|
import com.google.zxing.Result;
|
||||||
|
import com.google.zxing.common.HybridBinarizer;
|
||||||
|
|
||||||
|
import io.swagger.v3.oas.annotations.Operation;
|
||||||
|
import io.swagger.v3.oas.annotations.tags.Tag;
|
||||||
|
import stirling.software.SPDF.model.api.misc.AutoSplitPdfRequest;
|
||||||
|
import stirling.software.SPDF.utils.WebResponseUtils;
|
||||||
|
|
||||||
|
@RestController
|
||||||
|
@RequestMapping("/api/v1/misc")
|
||||||
|
@Tag(name = "Misc", description = "Miscellaneous APIs")
|
||||||
|
public class AutoSplitPdfController {
|
||||||
|
|
||||||
|
private static final String QR_CONTENT = "https://github.com/Frooodle/Stirling-PDF";
|
||||||
|
|
||||||
|
@PostMapping(value = "/auto-split-pdf", consumes = "multipart/form-data")
|
||||||
|
@Operation(summary = "Auto split PDF pages into separate documents", description = "This endpoint accepts a PDF file, scans each page for a specific QR code, and splits the document at the QR code boundaries. The output is a zip file containing each separate PDF document. Input:PDF Output:ZIP Type:SISO")
|
||||||
|
public ResponseEntity<byte[]> autoSplitPdf(@ModelAttribute AutoSplitPdfRequest request) throws IOException {
|
||||||
|
MultipartFile file = request.getFileInput();
|
||||||
|
boolean duplexMode = request.isDuplexMode();
|
||||||
|
|
||||||
|
InputStream inputStream = file.getInputStream();
|
||||||
|
PDDocument document = PDDocument.load(inputStream);
|
||||||
|
PDFRenderer pdfRenderer = new PDFRenderer(document);
|
||||||
|
|
||||||
|
List<PDDocument> splitDocuments = new ArrayList<>();
|
||||||
|
List<ByteArrayOutputStream> splitDocumentsBoas = new ArrayList<>();
|
||||||
|
|
||||||
|
for (int page = 0; page < document.getNumberOfPages(); ++page) {
|
||||||
|
BufferedImage bim = pdfRenderer.renderImageWithDPI(page, 150);
|
||||||
|
String result = decodeQRCode(bim);
|
||||||
|
|
||||||
|
if (QR_CONTENT.equals(result) && page != 0) {
|
||||||
|
splitDocuments.add(new PDDocument());
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!splitDocuments.isEmpty() && !QR_CONTENT.equals(result)) {
|
||||||
|
splitDocuments.get(splitDocuments.size() - 1).addPage(document.getPage(page));
|
||||||
|
} else if (page == 0) {
|
||||||
|
PDDocument firstDocument = new PDDocument();
|
||||||
|
firstDocument.addPage(document.getPage(page));
|
||||||
|
splitDocuments.add(firstDocument);
|
||||||
|
}
|
||||||
|
|
||||||
|
// If duplexMode is true and current page is a divider, then skip next page
|
||||||
|
if (duplexMode && QR_CONTENT.equals(result)) {
|
||||||
|
page++;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Remove split documents that have no pages
|
||||||
|
splitDocuments.removeIf(pdDocument -> pdDocument.getNumberOfPages() == 0);
|
||||||
|
|
||||||
|
for (PDDocument splitDocument : splitDocuments) {
|
||||||
|
ByteArrayOutputStream baos = new ByteArrayOutputStream();
|
||||||
|
splitDocument.save(baos);
|
||||||
|
splitDocumentsBoas.add(baos);
|
||||||
|
splitDocument.close();
|
||||||
|
}
|
||||||
|
|
||||||
|
document.close();
|
||||||
|
|
||||||
|
Path zipFile = Files.createTempFile("split_documents", ".zip");
|
||||||
|
String filename = file.getOriginalFilename().replaceFirst("[.][^.]+$", "");
|
||||||
|
byte[] data;
|
||||||
|
|
||||||
|
try (ZipOutputStream zipOut = new ZipOutputStream(Files.newOutputStream(zipFile))) {
|
||||||
|
for (int i = 0; i < splitDocumentsBoas.size(); i++) {
|
||||||
|
String fileName = filename + "_" + (i + 1) + ".pdf";
|
||||||
|
ByteArrayOutputStream baos = splitDocumentsBoas.get(i);
|
||||||
|
byte[] pdf = baos.toByteArray();
|
||||||
|
|
||||||
|
ZipEntry pdfEntry = new ZipEntry(fileName);
|
||||||
|
zipOut.putNextEntry(pdfEntry);
|
||||||
|
zipOut.write(pdf);
|
||||||
|
zipOut.closeEntry();
|
||||||
|
}
|
||||||
|
} catch (Exception e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
} finally {
|
||||||
|
data = Files.readAllBytes(zipFile);
|
||||||
|
Files.delete(zipFile);
|
||||||
|
}
|
||||||
|
|
||||||
|
return WebResponseUtils.bytesToWebResponse(data, filename + ".zip", MediaType.APPLICATION_OCTET_STREAM);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
private static String decodeQRCode(BufferedImage bufferedImage) {
|
||||||
|
LuminanceSource source;
|
||||||
|
|
||||||
|
if (bufferedImage.getRaster().getDataBuffer() instanceof DataBufferByte) {
|
||||||
|
byte[] pixels = ((DataBufferByte) bufferedImage.getRaster().getDataBuffer()).getData();
|
||||||
|
source = new PlanarYUVLuminanceSource(pixels, bufferedImage.getWidth(), bufferedImage.getHeight(), 0, 0, bufferedImage.getWidth(), bufferedImage.getHeight(), false);
|
||||||
|
} else if (bufferedImage.getRaster().getDataBuffer() instanceof DataBufferInt) {
|
||||||
|
int[] pixels = ((DataBufferInt) bufferedImage.getRaster().getDataBuffer()).getData();
|
||||||
|
byte[] newPixels = new byte[pixels.length];
|
||||||
|
for (int i = 0; i < pixels.length; i++) {
|
||||||
|
newPixels[i] = (byte) (pixels[i] & 0xff);
|
||||||
|
}
|
||||||
|
source = new PlanarYUVLuminanceSource(newPixels, bufferedImage.getWidth(), bufferedImage.getHeight(), 0, 0, bufferedImage.getWidth(), bufferedImage.getHeight(), false);
|
||||||
|
} else {
|
||||||
|
throw new IllegalArgumentException("BufferedImage must have 8-bit gray scale, 24-bit RGB, 32-bit ARGB (packed int), byte gray, or 3-byte/4-byte RGB image data");
|
||||||
|
}
|
||||||
|
|
||||||
|
BinaryBitmap bitmap = new BinaryBitmap(new HybridBinarizer(source));
|
||||||
|
|
||||||
|
try {
|
||||||
|
Result result = new MultiFormatReader().decode(bitmap);
|
||||||
|
return result.getText();
|
||||||
|
} catch (NotFoundException e) {
|
||||||
|
return null; // there is no QR code in the image
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,4 +1,4 @@
|
|||||||
package stirling.software.SPDF.controller.api.other;
|
package stirling.software.SPDF.controller.api.misc;
|
||||||
|
|
||||||
import java.awt.image.BufferedImage;
|
import java.awt.image.BufferedImage;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
@@ -20,36 +20,34 @@ import org.apache.pdfbox.rendering.PDFRenderer;
|
|||||||
import org.apache.pdfbox.text.PDFTextStripper;
|
import org.apache.pdfbox.text.PDFTextStripper;
|
||||||
import org.springframework.http.HttpStatus;
|
import org.springframework.http.HttpStatus;
|
||||||
import org.springframework.http.ResponseEntity;
|
import org.springframework.http.ResponseEntity;
|
||||||
|
import org.springframework.web.bind.annotation.ModelAttribute;
|
||||||
import org.springframework.web.bind.annotation.PostMapping;
|
import org.springframework.web.bind.annotation.PostMapping;
|
||||||
import org.springframework.web.bind.annotation.RequestParam;
|
import org.springframework.web.bind.annotation.RequestMapping;
|
||||||
import org.springframework.web.bind.annotation.RequestPart;
|
|
||||||
import org.springframework.web.bind.annotation.RestController;
|
import org.springframework.web.bind.annotation.RestController;
|
||||||
import org.springframework.web.multipart.MultipartFile;
|
import org.springframework.web.multipart.MultipartFile;
|
||||||
|
|
||||||
import io.swagger.v3.oas.annotations.Operation;
|
import io.swagger.v3.oas.annotations.Operation;
|
||||||
import io.swagger.v3.oas.annotations.Parameter;
|
import io.swagger.v3.oas.annotations.tags.Tag;
|
||||||
import stirling.software.SPDF.pdf.ImageFinder;
|
import stirling.software.SPDF.model.api.misc.RemoveBlankPagesRequest;
|
||||||
|
import stirling.software.SPDF.utils.PdfUtils;
|
||||||
import stirling.software.SPDF.utils.ProcessExecutor;
|
import stirling.software.SPDF.utils.ProcessExecutor;
|
||||||
|
import stirling.software.SPDF.utils.ProcessExecutor.ProcessExecutorResult;
|
||||||
import stirling.software.SPDF.utils.WebResponseUtils;
|
import stirling.software.SPDF.utils.WebResponseUtils;
|
||||||
|
|
||||||
@RestController
|
@RestController
|
||||||
|
@RequestMapping("/api/v1/misc")
|
||||||
|
@Tag(name = "Misc", description = "Miscellaneous APIs")
|
||||||
public class BlankPageController {
|
public class BlankPageController {
|
||||||
|
|
||||||
@PostMapping(consumes = "multipart/form-data", value = "/remove-blanks")
|
@PostMapping(consumes = "multipart/form-data", value = "/remove-blanks")
|
||||||
@Operation(
|
@Operation(
|
||||||
summary = "Remove blank pages from a PDF file",
|
summary = "Remove blank pages from a PDF file",
|
||||||
description = "This endpoint removes blank pages from a given PDF file. Users can specify the threshold and white percentage to tune the detection of blank pages."
|
description = "This endpoint removes blank pages from a given PDF file. Users can specify the threshold and white percentage to tune the detection of blank pages. Input:PDF Output:PDF Type:SISO"
|
||||||
)
|
)
|
||||||
public ResponseEntity<byte[]> removeBlankPages(
|
public ResponseEntity<byte[]> removeBlankPages(@ModelAttribute RemoveBlankPagesRequest request) throws IOException, InterruptedException {
|
||||||
@RequestPart(required = true, value = "fileInput")
|
MultipartFile inputFile = request.getFileInput();
|
||||||
@Parameter(description = "The input PDF file from which blank pages will be removed", required = true)
|
int threshold = request.getThreshold();
|
||||||
MultipartFile inputFile,
|
float whitePercent = request.getWhitePercent();
|
||||||
@RequestParam(defaultValue = "10", name = "threshold")
|
|
||||||
@Parameter(description = "The threshold value to determine blank pages", example = "10")
|
|
||||||
int threshold,
|
|
||||||
@RequestParam(defaultValue = "99.9", name = "whitePercent")
|
|
||||||
@Parameter(description = "The percentage of white color on a page to consider it as blank", example = "99.9")
|
|
||||||
float whitePercent) throws IOException, InterruptedException {
|
|
||||||
|
|
||||||
PDDocument document = null;
|
PDDocument document = null;
|
||||||
try {
|
try {
|
||||||
@@ -71,7 +69,7 @@ public class BlankPageController {
|
|||||||
pagesToKeepIndex.add(pageIndex);
|
pagesToKeepIndex.add(pageIndex);
|
||||||
System.out.println("page " + pageIndex + " has text");
|
System.out.println("page " + pageIndex + " has text");
|
||||||
} else {
|
} else {
|
||||||
boolean hasImages = hasImagesOnPage(page);
|
boolean hasImages = PdfUtils.hasImagesOnPage(page);
|
||||||
if (hasImages) {
|
if (hasImages) {
|
||||||
System.out.println("page " + pageIndex + " has image");
|
System.out.println("page " + pageIndex + " has image");
|
||||||
|
|
||||||
@@ -84,10 +82,10 @@ public class BlankPageController {
|
|||||||
List<String> command = new ArrayList<>(Arrays.asList("python3", System.getProperty("user.dir") + "/scripts/detect-blank-pages.py", tempFile.toString() ,"--threshold", String.valueOf(threshold), "--white_percent", String.valueOf(whitePercent)));
|
List<String> command = new ArrayList<>(Arrays.asList("python3", System.getProperty("user.dir") + "/scripts/detect-blank-pages.py", tempFile.toString() ,"--threshold", String.valueOf(threshold), "--white_percent", String.valueOf(whitePercent)));
|
||||||
|
|
||||||
// Run CLI command
|
// Run CLI command
|
||||||
int returnCode = ProcessExecutor.getInstance(ProcessExecutor.Processes.PYTHON_OPENCV).runCommandWithOutputHandling(command);
|
ProcessExecutorResult returnCode = ProcessExecutor.getInstance(ProcessExecutor.Processes.PYTHON_OPENCV).runCommandWithOutputHandling(command);
|
||||||
|
|
||||||
// does contain data
|
// does contain data
|
||||||
if (returnCode == 0) {
|
if (returnCode.getRc() == 0) {
|
||||||
System.out.println("page " + pageIndex + " has image which is not blank");
|
System.out.println("page " + pageIndex + " has image which is not blank");
|
||||||
pagesToKeepIndex.add(pageIndex);
|
pagesToKeepIndex.add(pageIndex);
|
||||||
} else {
|
} else {
|
||||||
@@ -120,9 +118,5 @@ public class BlankPageController {
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
private static boolean hasImagesOnPage(PDPage page) throws IOException {
|
|
||||||
ImageFinder imageFinder = new ImageFinder(page);
|
|
||||||
imageFinder.processPage(page);
|
|
||||||
return imageFinder.hasImages();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
@@ -1,4 +1,4 @@
|
|||||||
package stirling.software.SPDF.controller.api.other;
|
package stirling.software.SPDF.controller.api.misc;
|
||||||
|
|
||||||
import java.awt.Image;
|
import java.awt.Image;
|
||||||
import java.awt.image.BufferedImage;
|
import java.awt.image.BufferedImage;
|
||||||
@@ -22,32 +22,34 @@ import org.apache.pdfbox.pdmodel.graphics.image.PDImageXObject;
|
|||||||
import org.slf4j.Logger;
|
import org.slf4j.Logger;
|
||||||
import org.slf4j.LoggerFactory;
|
import org.slf4j.LoggerFactory;
|
||||||
import org.springframework.http.ResponseEntity;
|
import org.springframework.http.ResponseEntity;
|
||||||
|
import org.springframework.web.bind.annotation.ModelAttribute;
|
||||||
import org.springframework.web.bind.annotation.PostMapping;
|
import org.springframework.web.bind.annotation.PostMapping;
|
||||||
import org.springframework.web.bind.annotation.RequestParam;
|
import org.springframework.web.bind.annotation.RequestMapping;
|
||||||
import org.springframework.web.bind.annotation.RequestPart;
|
|
||||||
import org.springframework.web.bind.annotation.RestController;
|
import org.springframework.web.bind.annotation.RestController;
|
||||||
import org.springframework.web.multipart.MultipartFile;
|
import org.springframework.web.multipart.MultipartFile;
|
||||||
|
|
||||||
import io.swagger.v3.oas.annotations.Operation;
|
import io.swagger.v3.oas.annotations.Operation;
|
||||||
import io.swagger.v3.oas.annotations.Parameter;
|
import io.swagger.v3.oas.annotations.tags.Tag;
|
||||||
import io.swagger.v3.oas.annotations.media.Schema;
|
import stirling.software.SPDF.model.api.misc.OptimizePdfRequest;
|
||||||
import stirling.software.SPDF.utils.GeneralUtils;
|
import stirling.software.SPDF.utils.GeneralUtils;
|
||||||
import stirling.software.SPDF.utils.ProcessExecutor;
|
import stirling.software.SPDF.utils.ProcessExecutor;
|
||||||
|
import stirling.software.SPDF.utils.ProcessExecutor.ProcessExecutorResult;
|
||||||
import stirling.software.SPDF.utils.WebResponseUtils;
|
import stirling.software.SPDF.utils.WebResponseUtils;
|
||||||
|
|
||||||
@RestController
|
@RestController
|
||||||
|
@RequestMapping("/api/v1/misc")
|
||||||
|
@Tag(name = "Misc", description = "Miscellaneous APIs")
|
||||||
public class CompressController {
|
public class CompressController {
|
||||||
|
|
||||||
private static final Logger logger = LoggerFactory.getLogger(CompressController.class);
|
private static final Logger logger = LoggerFactory.getLogger(CompressController.class);
|
||||||
|
|
||||||
@PostMapping(consumes = "multipart/form-data", value = "/compress-pdf")
|
@PostMapping(consumes = "multipart/form-data", value = "/compress-pdf")
|
||||||
@Operation(summary = "Optimize PDF file", description = "This endpoint accepts a PDF file and optimizes it based on the provided parameters.")
|
@Operation(summary = "Optimize PDF file", description = "This endpoint accepts a PDF file and optimizes it based on the provided parameters. Input:PDF Output:PDF Type:SISO")
|
||||||
public ResponseEntity<byte[]> optimizePdf(
|
public ResponseEntity<byte[]> optimizePdf(@ModelAttribute OptimizePdfRequest request) throws Exception {
|
||||||
@RequestPart(value = "fileInput") @Parameter(description = "The input PDF file to be optimized.", required = true) MultipartFile inputFile,
|
MultipartFile inputFile = request.getFileInput();
|
||||||
@RequestParam(required = false, value = "optimizeLevel") @Parameter(description = "The level of optimization to apply to the PDF file. Higher values indicate greater compression but may reduce quality.", schema = @Schema(allowableValues = {
|
Integer optimizeLevel = request.getOptimizeLevel();
|
||||||
"1", "2", "3", "4", "5" })) Integer optimizeLevel,
|
String expectedOutputSizeString = request.getExpectedOutputSize();
|
||||||
@RequestParam(value = "expectedOutputSize", required = false) @Parameter(description = "The expected output size, e.g. '100MB', '25KB', etc.", required = false) String expectedOutputSizeString)
|
|
||||||
throws Exception {
|
|
||||||
|
|
||||||
if(expectedOutputSizeString == null && optimizeLevel == null) {
|
if(expectedOutputSizeString == null && optimizeLevel == null) {
|
||||||
throw new Exception("Both expected output size and optimize level are not specified");
|
throw new Exception("Both expected output size and optimize level are not specified");
|
||||||
@@ -114,7 +116,7 @@ public class CompressController {
|
|||||||
command.add("-sOutputFile=" + tempOutputFile.toString());
|
command.add("-sOutputFile=" + tempOutputFile.toString());
|
||||||
command.add(tempInputFile.toString());
|
command.add(tempInputFile.toString());
|
||||||
|
|
||||||
int returnCode = ProcessExecutor.getInstance(ProcessExecutor.Processes.GHOSTSCRIPT).runCommandWithOutputHandling(command);
|
ProcessExecutorResult returnCode = ProcessExecutor.getInstance(ProcessExecutor.Processes.GHOSTSCRIPT).runCommandWithOutputHandling(command);
|
||||||
|
|
||||||
// Check if file size is within expected size or not auto mode so instantly finish
|
// Check if file size is within expected size or not auto mode so instantly finish
|
||||||
long outputFileSize = Files.size(tempOutputFile);
|
long outputFileSize = Files.size(tempOutputFile);
|
||||||
@@ -219,6 +221,15 @@ public class CompressController {
|
|||||||
// Read the optimized PDF file
|
// Read the optimized PDF file
|
||||||
byte[] pdfBytes = Files.readAllBytes(tempOutputFile);
|
byte[] pdfBytes = Files.readAllBytes(tempOutputFile);
|
||||||
|
|
||||||
|
// Check if optimized file is larger than the original
|
||||||
|
if(pdfBytes.length > inputFileSize) {
|
||||||
|
// Log the occurrence
|
||||||
|
logger.warn("Optimized file is larger than the original. Returning the original file instead.");
|
||||||
|
|
||||||
|
// Read the original file again
|
||||||
|
pdfBytes = Files.readAllBytes(tempInputFile);
|
||||||
|
}
|
||||||
|
|
||||||
// Clean up the temporary files
|
// Clean up the temporary files
|
||||||
Files.delete(tempInputFile);
|
Files.delete(tempInputFile);
|
||||||
Files.delete(tempOutputFile);
|
Files.delete(tempOutputFile);
|
||||||
@@ -1,4 +1,4 @@
|
|||||||
package stirling.software.SPDF.controller.api.other;
|
package stirling.software.SPDF.controller.api.misc;
|
||||||
|
|
||||||
import java.awt.image.BufferedImage;
|
import java.awt.image.BufferedImage;
|
||||||
import java.io.ByteArrayInputStream;
|
import java.io.ByteArrayInputStream;
|
||||||
@@ -24,45 +24,39 @@ import org.slf4j.LoggerFactory;
|
|||||||
import org.springframework.http.MediaType;
|
import org.springframework.http.MediaType;
|
||||||
import org.springframework.http.ResponseEntity;
|
import org.springframework.http.ResponseEntity;
|
||||||
import org.springframework.web.bind.annotation.PostMapping;
|
import org.springframework.web.bind.annotation.PostMapping;
|
||||||
import org.springframework.web.bind.annotation.RequestParam;
|
import org.springframework.web.bind.annotation.RequestMapping;
|
||||||
import org.springframework.web.bind.annotation.RequestPart;
|
|
||||||
import org.springframework.web.bind.annotation.RestController;
|
import org.springframework.web.bind.annotation.RestController;
|
||||||
import org.springframework.web.multipart.MultipartFile;
|
|
||||||
|
|
||||||
import io.swagger.v3.oas.annotations.Operation;
|
import io.swagger.v3.oas.annotations.Operation;
|
||||||
import io.swagger.v3.oas.annotations.Parameter;
|
import io.swagger.v3.oas.annotations.media.Content;
|
||||||
|
import io.swagger.v3.oas.annotations.media.Schema;
|
||||||
|
import io.swagger.v3.oas.annotations.parameters.RequestBody;
|
||||||
|
import io.swagger.v3.oas.annotations.tags.Tag;
|
||||||
|
import stirling.software.SPDF.model.api.misc.ExtractImageScansRequest;
|
||||||
import stirling.software.SPDF.utils.ProcessExecutor;
|
import stirling.software.SPDF.utils.ProcessExecutor;
|
||||||
|
import stirling.software.SPDF.utils.ProcessExecutor.ProcessExecutorResult;
|
||||||
import stirling.software.SPDF.utils.WebResponseUtils;
|
import stirling.software.SPDF.utils.WebResponseUtils;
|
||||||
|
|
||||||
@RestController
|
@RestController
|
||||||
|
@RequestMapping("/api/v1/misc")
|
||||||
|
@Tag(name = "Misc", description = "Miscellaneous APIs")
|
||||||
public class ExtractImageScansController {
|
public class ExtractImageScansController {
|
||||||
|
|
||||||
private static final Logger logger = LoggerFactory.getLogger(ExtractImageScansController.class);
|
private static final Logger logger = LoggerFactory.getLogger(ExtractImageScansController.class);
|
||||||
|
|
||||||
@PostMapping(consumes = "multipart/form-data", value = "/extract-image-scans")
|
@PostMapping(consumes = "multipart/form-data", value = "/extract-image-scans")
|
||||||
@Operation(summary = "Extract image scans from an input file",
|
@Operation(summary = "Extract image scans from an input file",
|
||||||
description = "This endpoint extracts image scans from a given file based on certain parameters. Users can specify angle threshold, tolerance, minimum area, minimum contour area, and border size.")
|
description = "This endpoint extracts image scans from a given file based on certain parameters. Users can specify angle threshold, tolerance, minimum area, minimum contour area, and border size. Input:PDF Output:IMAGE/ZIP Type:SIMO")
|
||||||
public ResponseEntity<byte[]> extractImageScans(
|
public ResponseEntity<byte[]> extractImageScans(
|
||||||
@RequestPart(required = true, value = "fileInput")
|
@RequestBody(
|
||||||
@Parameter(description = "The input file containing image scans")
|
description = "Form data containing file and extraction parameters",
|
||||||
MultipartFile inputFile,
|
required = true,
|
||||||
@RequestParam(name = "angle_threshold", defaultValue = "5")
|
content = @Content(
|
||||||
@Parameter(description = "The angle threshold for the image scan extraction", example = "5")
|
mediaType = "multipart/form-data",
|
||||||
int angleThreshold,
|
schema = @Schema(implementation = ExtractImageScansRequest.class) // This should represent your form's structure
|
||||||
@RequestParam(name = "tolerance", defaultValue = "20")
|
)
|
||||||
@Parameter(description = "The tolerance for the image scan extraction", example = "20")
|
)
|
||||||
int tolerance,
|
ExtractImageScansRequest form) throws IOException, InterruptedException {
|
||||||
@RequestParam(name = "min_area", defaultValue = "8000")
|
String fileName = form.getFileInput().getOriginalFilename();
|
||||||
@Parameter(description = "The minimum area for the image scan extraction", example = "8000")
|
|
||||||
int minArea,
|
|
||||||
@RequestParam(name = "min_contour_area", defaultValue = "500")
|
|
||||||
@Parameter(description = "The minimum contour area for the image scan extraction", example = "500")
|
|
||||||
int minContourArea,
|
|
||||||
@RequestParam(name = "border_size", defaultValue = "1")
|
|
||||||
@Parameter(description = "The border size for the image scan extraction", example = "1")
|
|
||||||
int borderSize) throws IOException, InterruptedException {
|
|
||||||
|
|
||||||
String fileName = inputFile.getOriginalFilename();
|
|
||||||
String extension = fileName.substring(fileName.lastIndexOf(".") + 1);
|
String extension = fileName.substring(fileName.lastIndexOf(".") + 1);
|
||||||
|
|
||||||
List<String> images = new ArrayList<>();
|
List<String> images = new ArrayList<>();
|
||||||
@@ -70,7 +64,7 @@ public class ExtractImageScansController {
|
|||||||
// Check if input file is a PDF
|
// Check if input file is a PDF
|
||||||
if (extension.equalsIgnoreCase("pdf")) {
|
if (extension.equalsIgnoreCase("pdf")) {
|
||||||
// Load PDF document
|
// Load PDF document
|
||||||
try (PDDocument document = PDDocument.load(new ByteArrayInputStream(inputFile.getBytes()))) {
|
try (PDDocument document = PDDocument.load(new ByteArrayInputStream(form.getFileInput().getBytes()))) {
|
||||||
PDFRenderer pdfRenderer = new PDFRenderer(document);
|
PDFRenderer pdfRenderer = new PDFRenderer(document);
|
||||||
int pageCount = document.getNumberOfPages();
|
int pageCount = document.getNumberOfPages();
|
||||||
images = new ArrayList<>();
|
images = new ArrayList<>();
|
||||||
@@ -90,7 +84,7 @@ public class ExtractImageScansController {
|
|||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
Path tempInputFile = Files.createTempFile("input_", "." + extension);
|
Path tempInputFile = Files.createTempFile("input_", "." + extension);
|
||||||
Files.copy(inputFile.getInputStream(), tempInputFile, StandardCopyOption.REPLACE_EXISTING);
|
Files.copy(form.getFileInput().getInputStream(), tempInputFile, StandardCopyOption.REPLACE_EXISTING);
|
||||||
// Add input file path to images list
|
// Add input file path to images list
|
||||||
images.add(tempInputFile.toString());
|
images.add(tempInputFile.toString());
|
||||||
}
|
}
|
||||||
@@ -106,16 +100,16 @@ public class ExtractImageScansController {
|
|||||||
"./scripts/split_photos.py",
|
"./scripts/split_photos.py",
|
||||||
images.get(i),
|
images.get(i),
|
||||||
tempDir.toString(),
|
tempDir.toString(),
|
||||||
"--angle_threshold", String.valueOf(angleThreshold),
|
"--angle_threshold", String.valueOf(form.getAngleThreshold()),
|
||||||
"--tolerance", String.valueOf(tolerance),
|
"--tolerance", String.valueOf(form.getTolerance()),
|
||||||
"--min_area", String.valueOf(minArea),
|
"--min_area", String.valueOf(form.getMinArea()),
|
||||||
"--min_contour_area", String.valueOf(minContourArea),
|
"--min_contour_area", String.valueOf(form.getMinContourArea()),
|
||||||
"--border_size", String.valueOf(borderSize)
|
"--border_size", String.valueOf(form.getBorderSize())
|
||||||
));
|
));
|
||||||
|
|
||||||
|
|
||||||
// Run CLI command
|
// Run CLI command
|
||||||
int returnCode = ProcessExecutor.getInstance(ProcessExecutor.Processes.PYTHON_OPENCV).runCommandWithOutputHandling(command);
|
ProcessExecutorResult returnCode = ProcessExecutor.getInstance(ProcessExecutor.Processes.PYTHON_OPENCV).runCommandWithOutputHandling(command);
|
||||||
|
|
||||||
// Read the output photos in temp directory
|
// Read the output photos in temp directory
|
||||||
List<Path> tempOutputFiles = Files.list(tempDir).sorted().collect(Collectors.toList());
|
List<Path> tempOutputFiles = Files.list(tempDir).sorted().collect(Collectors.toList());
|
||||||
@@ -1,5 +1,8 @@
|
|||||||
package stirling.software.SPDF.controller.api.other;
|
package stirling.software.SPDF.controller.api.misc;
|
||||||
|
import java.util.HashSet;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Map;
|
||||||
|
import java.util.Set;
|
||||||
import java.awt.Graphics2D;
|
import java.awt.Graphics2D;
|
||||||
import java.awt.Image;
|
import java.awt.Image;
|
||||||
import java.awt.image.BufferedImage;
|
import java.awt.image.BufferedImage;
|
||||||
@@ -20,31 +23,29 @@ import org.slf4j.Logger;
|
|||||||
import org.slf4j.LoggerFactory;
|
import org.slf4j.LoggerFactory;
|
||||||
import org.springframework.http.MediaType;
|
import org.springframework.http.MediaType;
|
||||||
import org.springframework.http.ResponseEntity;
|
import org.springframework.http.ResponseEntity;
|
||||||
|
import org.springframework.web.bind.annotation.ModelAttribute;
|
||||||
import org.springframework.web.bind.annotation.PostMapping;
|
import org.springframework.web.bind.annotation.PostMapping;
|
||||||
import org.springframework.web.bind.annotation.RequestParam;
|
import org.springframework.web.bind.annotation.RequestMapping;
|
||||||
import org.springframework.web.bind.annotation.RequestPart;
|
|
||||||
import org.springframework.web.bind.annotation.RestController;
|
import org.springframework.web.bind.annotation.RestController;
|
||||||
import org.springframework.web.multipart.MultipartFile;
|
import org.springframework.web.multipart.MultipartFile;
|
||||||
|
|
||||||
import io.swagger.v3.oas.annotations.Operation;
|
import io.swagger.v3.oas.annotations.Operation;
|
||||||
import io.swagger.v3.oas.annotations.Parameter;
|
import io.swagger.v3.oas.annotations.tags.Tag;
|
||||||
import io.swagger.v3.oas.annotations.media.Schema;
|
import stirling.software.SPDF.model.api.PDFWithImageFormatRequest;
|
||||||
import stirling.software.SPDF.utils.WebResponseUtils;
|
import stirling.software.SPDF.utils.WebResponseUtils;
|
||||||
@RestController
|
@RestController
|
||||||
|
@RequestMapping("/api/v1/misc")
|
||||||
|
@Tag(name = "Misc", description = "Miscellaneous APIs")
|
||||||
public class ExtractImagesController {
|
public class ExtractImagesController {
|
||||||
|
|
||||||
private static final Logger logger = LoggerFactory.getLogger(ExtractImagesController.class);
|
private static final Logger logger = LoggerFactory.getLogger(ExtractImagesController.class);
|
||||||
|
|
||||||
@PostMapping(consumes = "multipart/form-data", value = "/extract-images")
|
@PostMapping(consumes = "multipart/form-data", value = "/extract-images")
|
||||||
@Operation(summary = "Extract images from a PDF file",
|
@Operation(summary = "Extract images from a PDF file",
|
||||||
description = "This endpoint extracts images from a given PDF file and returns them in a zip file. Users can specify the output image format.")
|
description = "This endpoint extracts images from a given PDF file and returns them in a zip file. Users can specify the output image format. Input:PDF Output:IMAGE/ZIP Type:SIMO")
|
||||||
public ResponseEntity<byte[]> extractImages(
|
public ResponseEntity<byte[]> extractImages(@ModelAttribute PDFWithImageFormatRequest request) throws IOException {
|
||||||
@RequestPart(required = true, value = "fileInput")
|
MultipartFile file = request.getFileInput();
|
||||||
@Parameter(description = "The input PDF file containing images")
|
String format = request.getFormat();
|
||||||
MultipartFile file,
|
|
||||||
@RequestParam("format")
|
|
||||||
@Parameter(description = "The output image format e.g., 'png', 'jpeg', or 'gif'", schema = @Schema(allowableValues = {"png", "jpeg", "gif"}))
|
|
||||||
String format) throws IOException {
|
|
||||||
|
|
||||||
System.out.println(System.currentTimeMillis() + "file=" + file.getName() + ", format=" + format);
|
System.out.println(System.currentTimeMillis() + "file=" + file.getName() + ", format=" + format);
|
||||||
PDDocument document = PDDocument.load(file.getBytes());
|
PDDocument document = PDDocument.load(file.getBytes());
|
||||||
@@ -60,7 +61,8 @@ public class ExtractImagesController {
|
|||||||
|
|
||||||
int imageIndex = 1;
|
int imageIndex = 1;
|
||||||
String filename = file.getOriginalFilename().replaceFirst("[.][^.]+$", "");
|
String filename = file.getOriginalFilename().replaceFirst("[.][^.]+$", "");
|
||||||
int pageNum = 1;
|
int pageNum = 0;
|
||||||
|
Set<Integer> processedImages = new HashSet<>();
|
||||||
// Iterate over each page
|
// Iterate over each page
|
||||||
for (PDPage page : document.getPages()) {
|
for (PDPage page : document.getPages()) {
|
||||||
++pageNum;
|
++pageNum;
|
||||||
@@ -68,7 +70,12 @@ public class ExtractImagesController {
|
|||||||
for (COSName name : page.getResources().getXObjectNames()) {
|
for (COSName name : page.getResources().getXObjectNames()) {
|
||||||
if (page.getResources().isImageXObject(name)) {
|
if (page.getResources().isImageXObject(name)) {
|
||||||
PDImageXObject image = (PDImageXObject) page.getResources().getXObject(name);
|
PDImageXObject image = (PDImageXObject) page.getResources().getXObject(name);
|
||||||
|
int imageHash = image.hashCode();
|
||||||
|
if(processedImages.contains(imageHash)) {
|
||||||
|
continue; // Skip already processed images
|
||||||
|
}
|
||||||
|
processedImages.add(imageHash);
|
||||||
|
|
||||||
// Convert image to desired format
|
// Convert image to desired format
|
||||||
RenderedImage renderedImage = image.getImage();
|
RenderedImage renderedImage = image.getImage();
|
||||||
BufferedImage bufferedImage = null;
|
BufferedImage bufferedImage = null;
|
||||||
@@ -1,62 +1,53 @@
|
|||||||
package stirling.software.SPDF.controller.api.other;
|
package stirling.software.SPDF.controller.api.misc;
|
||||||
|
|
||||||
import java.io.IOException;
|
|
||||||
import java.nio.file.Files;
|
|
||||||
import java.nio.file.Path;
|
|
||||||
import java.util.ArrayList;
|
|
||||||
import java.util.List;
|
|
||||||
|
|
||||||
import org.apache.pdfbox.pdmodel.PDDocument;
|
|
||||||
import org.slf4j.Logger;
|
|
||||||
import org.slf4j.LoggerFactory;
|
|
||||||
import org.springframework.http.ResponseEntity;
|
|
||||||
import org.springframework.web.bind.annotation.PostMapping;
|
|
||||||
import org.springframework.web.bind.annotation.RequestPart;
|
|
||||||
import org.springframework.web.bind.annotation.RestController;
|
|
||||||
import org.springframework.web.multipart.MultipartFile;
|
|
||||||
|
|
||||||
import com.itextpdf.io.source.ByteArrayOutputStream;
|
|
||||||
|
|
||||||
import io.swagger.v3.oas.annotations.Operation;
|
|
||||||
import io.swagger.v3.oas.annotations.Parameter;
|
|
||||||
import stirling.software.SPDF.utils.ProcessExecutor;
|
|
||||||
import stirling.software.SPDF.utils.WebResponseUtils;
|
|
||||||
//Required for PDF manipulation
|
|
||||||
import org.apache.pdfbox.pdmodel.PDDocument;
|
|
||||||
import org.apache.pdfbox.pdmodel.PDPage;
|
|
||||||
import org.apache.pdfbox.pdmodel.PDPageContentStream;
|
|
||||||
import org.apache.pdfbox.pdmodel.common.PDRectangle;
|
|
||||||
import org.apache.pdfbox.pdmodel.graphics.image.PDImageXObject;
|
|
||||||
import org.apache.pdfbox.pdmodel.graphics.image.LosslessFactory;
|
|
||||||
import org.apache.pdfbox.rendering.ImageType;
|
|
||||||
import org.apache.pdfbox.rendering.PDFRenderer;
|
|
||||||
|
|
||||||
|
|
||||||
|
import java.awt.Color;
|
||||||
|
import java.awt.geom.AffineTransform;
|
||||||
|
import java.awt.image.AffineTransformOp;
|
||||||
//Required for image manipulation
|
//Required for image manipulation
|
||||||
import java.awt.image.BufferedImage;
|
import java.awt.image.BufferedImage;
|
||||||
import java.awt.image.BufferedImageOp;
|
import java.awt.image.BufferedImageOp;
|
||||||
import java.awt.image.RescaleOp;
|
|
||||||
import java.awt.image.AffineTransformOp;
|
|
||||||
import java.awt.image.ConvolveOp;
|
import java.awt.image.ConvolveOp;
|
||||||
import java.awt.image.Kernel;
|
import java.awt.image.Kernel;
|
||||||
import java.awt.Color;
|
import java.awt.image.RescaleOp;
|
||||||
import java.awt.geom.AffineTransform;
|
import java.io.ByteArrayOutputStream;
|
||||||
|
//Required for file input/output
|
||||||
|
import java.io.File;
|
||||||
|
import java.io.IOException;
|
||||||
|
//Other required classes
|
||||||
|
import java.util.Random;
|
||||||
|
|
||||||
//Required for image input/output
|
//Required for image input/output
|
||||||
import javax.imageio.ImageIO;
|
import javax.imageio.ImageIO;
|
||||||
|
|
||||||
//Required for file input/output
|
import org.apache.pdfbox.pdmodel.PDDocument;
|
||||||
import java.io.File;
|
import org.apache.pdfbox.pdmodel.PDPage;
|
||||||
|
import org.apache.pdfbox.pdmodel.PDPageContentStream;
|
||||||
//Other required classes
|
import org.apache.pdfbox.pdmodel.common.PDRectangle;
|
||||||
import java.util.Random;
|
import org.apache.pdfbox.pdmodel.graphics.image.LosslessFactory;
|
||||||
|
import org.apache.pdfbox.pdmodel.graphics.image.PDImageXObject;
|
||||||
|
import org.apache.pdfbox.rendering.ImageType;
|
||||||
|
import org.apache.pdfbox.rendering.PDFRenderer;
|
||||||
|
import org.slf4j.Logger;
|
||||||
|
import org.slf4j.LoggerFactory;
|
||||||
|
import org.springframework.http.ResponseEntity;
|
||||||
|
import org.springframework.web.bind.annotation.ModelAttribute;
|
||||||
|
import org.springframework.web.bind.annotation.PostMapping;
|
||||||
|
import org.springframework.web.bind.annotation.RequestMapping;
|
||||||
|
import org.springframework.web.bind.annotation.RestController;
|
||||||
|
import org.springframework.web.multipart.MultipartFile;
|
||||||
|
|
||||||
import io.swagger.v3.oas.annotations.Hidden;
|
import io.swagger.v3.oas.annotations.Hidden;
|
||||||
|
import io.swagger.v3.oas.annotations.Operation;
|
||||||
|
import io.swagger.v3.oas.annotations.tags.Tag;
|
||||||
|
import stirling.software.SPDF.model.api.PDFFile;
|
||||||
|
import stirling.software.SPDF.utils.WebResponseUtils;
|
||||||
|
|
||||||
@RestController
|
@RestController
|
||||||
public class FakeScanController {
|
@RequestMapping("/api/v1/misc")
|
||||||
|
@Tag(name = "Misc", description = "Miscellaneous APIs")
|
||||||
|
public class FakeScanControllerWIP {
|
||||||
|
|
||||||
private static final Logger logger = LoggerFactory.getLogger(FakeScanController.class);
|
private static final Logger logger = LoggerFactory.getLogger(FakeScanControllerWIP.class);
|
||||||
|
|
||||||
//TODO
|
//TODO
|
||||||
@Hidden
|
@Hidden
|
||||||
@@ -65,10 +56,8 @@ public class FakeScanController {
|
|||||||
summary = "Repair a PDF file",
|
summary = "Repair a PDF file",
|
||||||
description = "This endpoint repairs a given PDF file by running Ghostscript command. The PDF is first saved to a temporary location, repaired, read back, and then returned as a response."
|
description = "This endpoint repairs a given PDF file by running Ghostscript command. The PDF is first saved to a temporary location, repaired, read back, and then returned as a response."
|
||||||
)
|
)
|
||||||
public ResponseEntity<byte[]> repairPdf(
|
public ResponseEntity<byte[]> repairPdf(@ModelAttribute PDFFile request) throws IOException {
|
||||||
@RequestPart(required = true, value = "fileInput")
|
MultipartFile inputFile = request.getFileInput();
|
||||||
@Parameter(description = "The input PDF file to be repaired", required = true)
|
|
||||||
MultipartFile inputFile) throws IOException, InterruptedException {
|
|
||||||
|
|
||||||
PDDocument document = PDDocument.load(inputFile.getBytes());
|
PDDocument document = PDDocument.load(inputFile.getBytes());
|
||||||
PDFRenderer pdfRenderer = new PDFRenderer(document);
|
PDFRenderer pdfRenderer = new PDFRenderer(document);
|
||||||
@@ -1,4 +1,4 @@
|
|||||||
package stirling.software.SPDF.controller.api.other;
|
package stirling.software.SPDF.controller.api.misc;
|
||||||
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.text.ParseException;
|
import java.text.ParseException;
|
||||||
@@ -11,17 +11,20 @@ import org.apache.pdfbox.cos.COSName;
|
|||||||
import org.apache.pdfbox.pdmodel.PDDocument;
|
import org.apache.pdfbox.pdmodel.PDDocument;
|
||||||
import org.apache.pdfbox.pdmodel.PDDocumentInformation;
|
import org.apache.pdfbox.pdmodel.PDDocumentInformation;
|
||||||
import org.springframework.http.ResponseEntity;
|
import org.springframework.http.ResponseEntity;
|
||||||
|
import org.springframework.web.bind.annotation.ModelAttribute;
|
||||||
import org.springframework.web.bind.annotation.PostMapping;
|
import org.springframework.web.bind.annotation.PostMapping;
|
||||||
import org.springframework.web.bind.annotation.RequestParam;
|
import org.springframework.web.bind.annotation.RequestMapping;
|
||||||
import org.springframework.web.bind.annotation.RequestPart;
|
|
||||||
import org.springframework.web.bind.annotation.RestController;
|
import org.springframework.web.bind.annotation.RestController;
|
||||||
import org.springframework.web.multipart.MultipartFile;
|
import org.springframework.web.multipart.MultipartFile;
|
||||||
|
|
||||||
import io.swagger.v3.oas.annotations.Operation;
|
import io.swagger.v3.oas.annotations.Operation;
|
||||||
import io.swagger.v3.oas.annotations.Parameter;
|
import io.swagger.v3.oas.annotations.tags.Tag;
|
||||||
|
import stirling.software.SPDF.model.api.misc.MetadataRequest;
|
||||||
import stirling.software.SPDF.utils.WebResponseUtils;
|
import stirling.software.SPDF.utils.WebResponseUtils;
|
||||||
|
|
||||||
@RestController
|
@RestController
|
||||||
|
@RequestMapping("/api/v1/misc")
|
||||||
|
@Tag(name = "Misc", description = "Miscellaneous APIs")
|
||||||
public class MetadataController {
|
public class MetadataController {
|
||||||
|
|
||||||
|
|
||||||
@@ -38,44 +41,29 @@ public class MetadataController {
|
|||||||
|
|
||||||
@PostMapping(consumes = "multipart/form-data", value = "/update-metadata")
|
@PostMapping(consumes = "multipart/form-data", value = "/update-metadata")
|
||||||
@Operation(summary = "Update metadata of a PDF file",
|
@Operation(summary = "Update metadata of a PDF file",
|
||||||
description = "This endpoint allows you to update the metadata of a given PDF file. You can add, modify, or delete standard and custom metadata fields.")
|
description = "This endpoint allows you to update the metadata of a given PDF file. You can add, modify, or delete standard and custom metadata fields. Input:PDF Output:PDF Type:SISO")
|
||||||
public ResponseEntity<byte[]> metadata(
|
public ResponseEntity<byte[]> metadata(@ModelAttribute MetadataRequest request) throws IOException {
|
||||||
@RequestPart(required = true, value = "fileInput")
|
|
||||||
@Parameter(description = "The input PDF file to update metadata")
|
// Extract PDF file from the request object
|
||||||
MultipartFile pdfFile,
|
MultipartFile pdfFile = request.getFileInput();
|
||||||
@RequestParam(value = "deleteAll", required = false, defaultValue = "false")
|
|
||||||
@Parameter(description = "Delete all metadata if set to true")
|
|
||||||
Boolean deleteAll,
|
|
||||||
@RequestParam(value = "author", required = false)
|
|
||||||
@Parameter(description = "The author of the document")
|
|
||||||
String author,
|
|
||||||
@RequestParam(value = "creationDate", required = false)
|
|
||||||
@Parameter(description = "The creation date of the document (format: yyyy/MM/dd HH:mm:ss)")
|
|
||||||
String creationDate,
|
|
||||||
@RequestParam(value = "creator", required = false)
|
|
||||||
@Parameter(description = "The creator of the document")
|
|
||||||
String creator,
|
|
||||||
@RequestParam(value = "keywords", required = false)
|
|
||||||
@Parameter(description = "The keywords for the document")
|
|
||||||
String keywords,
|
|
||||||
@RequestParam(value = "modificationDate", required = false)
|
|
||||||
@Parameter(description = "The modification date of the document (format: yyyy/MM/dd HH:mm:ss)")
|
|
||||||
String modificationDate,
|
|
||||||
@RequestParam(value = "producer", required = false)
|
|
||||||
@Parameter(description = "The producer of the document")
|
|
||||||
String producer,
|
|
||||||
@RequestParam(value = "subject", required = false)
|
|
||||||
@Parameter(description = "The subject of the document")
|
|
||||||
String subject,
|
|
||||||
@RequestParam(value = "title", required = false)
|
|
||||||
@Parameter(description = "The title of the document")
|
|
||||||
String title,
|
|
||||||
@RequestParam(value = "trapped", required = false)
|
|
||||||
@Parameter(description = "The trapped status of the document")
|
|
||||||
String trapped,
|
|
||||||
@RequestParam Map<String, String> allRequestParams)
|
|
||||||
throws IOException {
|
|
||||||
|
|
||||||
|
// Extract metadata information
|
||||||
|
Boolean deleteAll = request.isDeleteAll();
|
||||||
|
String author = request.getAuthor();
|
||||||
|
String creationDate = request.getCreationDate();
|
||||||
|
String creator = request.getCreator();
|
||||||
|
String keywords = request.getKeywords();
|
||||||
|
String modificationDate = request.getModificationDate();
|
||||||
|
String producer = request.getProducer();
|
||||||
|
String subject = request.getSubject();
|
||||||
|
String title = request.getTitle();
|
||||||
|
String trapped = request.getTrapped();
|
||||||
|
|
||||||
|
// Extract additional custom parameters
|
||||||
|
Map<String, String> allRequestParams = request.getAllRequestParams();
|
||||||
|
if(allRequestParams == null) {
|
||||||
|
allRequestParams = new java.util.HashMap<String, String>();
|
||||||
|
}
|
||||||
// Load the PDF file into a PDDocument
|
// Load the PDF file into a PDDocument
|
||||||
PDDocument document = PDDocument.load(pdfFile.getBytes());
|
PDDocument document = PDDocument.load(pdfFile.getBytes());
|
||||||
|
|
||||||
@@ -1,4 +1,4 @@
|
|||||||
package stirling.software.SPDF.controller.api.other;
|
package stirling.software.SPDF.controller.api.misc;
|
||||||
|
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
import java.io.FileOutputStream;
|
import java.io.FileOutputStream;
|
||||||
@@ -18,19 +18,22 @@ import org.slf4j.Logger;
|
|||||||
import org.slf4j.LoggerFactory;
|
import org.slf4j.LoggerFactory;
|
||||||
import org.springframework.http.MediaType;
|
import org.springframework.http.MediaType;
|
||||||
import org.springframework.http.ResponseEntity;
|
import org.springframework.http.ResponseEntity;
|
||||||
|
import org.springframework.web.bind.annotation.ModelAttribute;
|
||||||
import org.springframework.web.bind.annotation.PostMapping;
|
import org.springframework.web.bind.annotation.PostMapping;
|
||||||
import org.springframework.web.bind.annotation.RequestParam;
|
import org.springframework.web.bind.annotation.RequestMapping;
|
||||||
import org.springframework.web.bind.annotation.RequestPart;
|
|
||||||
import org.springframework.web.bind.annotation.RestController;
|
import org.springframework.web.bind.annotation.RestController;
|
||||||
import org.springframework.web.multipart.MultipartFile;
|
import org.springframework.web.multipart.MultipartFile;
|
||||||
|
|
||||||
import io.swagger.v3.oas.annotations.Operation;
|
import io.swagger.v3.oas.annotations.Operation;
|
||||||
import io.swagger.v3.oas.annotations.Parameter;
|
import io.swagger.v3.oas.annotations.tags.Tag;
|
||||||
import io.swagger.v3.oas.annotations.media.Schema;
|
import stirling.software.SPDF.model.api.misc.ProcessPdfWithOcrRequest;
|
||||||
import stirling.software.SPDF.utils.ProcessExecutor;
|
import stirling.software.SPDF.utils.ProcessExecutor;
|
||||||
|
import stirling.software.SPDF.utils.ProcessExecutor.ProcessExecutorResult;
|
||||||
import stirling.software.SPDF.utils.WebResponseUtils;
|
import stirling.software.SPDF.utils.WebResponseUtils;
|
||||||
|
|
||||||
@RestController
|
@RestController
|
||||||
|
@RequestMapping("/api/v1/misc")
|
||||||
|
@Tag(name = "Misc", description = "Miscellaneous APIs")
|
||||||
public class OCRController {
|
public class OCRController {
|
||||||
|
|
||||||
private static final Logger logger = LoggerFactory.getLogger(OCRController.class);
|
private static final Logger logger = LoggerFactory.getLogger(OCRController.class);
|
||||||
@@ -47,36 +50,17 @@ public class OCRController {
|
|||||||
|
|
||||||
@PostMapping(consumes = "multipart/form-data", value = "/ocr-pdf")
|
@PostMapping(consumes = "multipart/form-data", value = "/ocr-pdf")
|
||||||
@Operation(summary = "Process a PDF file with OCR",
|
@Operation(summary = "Process a PDF file with OCR",
|
||||||
description = "This endpoint processes a PDF file using OCR (Optical Character Recognition). Users can specify languages, sidecar, deskew, clean, cleanFinal, ocrType, ocrRenderType, and removeImagesAfter options.")
|
description = "This endpoint processes a PDF file using OCR (Optical Character Recognition). Users can specify languages, sidecar, deskew, clean, cleanFinal, ocrType, ocrRenderType, and removeImagesAfter options. Input:PDF Output:PDF Type:SI-Conditional")
|
||||||
public ResponseEntity<byte[]> processPdfWithOCR(
|
public ResponseEntity<byte[]> processPdfWithOCR(@ModelAttribute ProcessPdfWithOcrRequest request) throws IOException, InterruptedException {
|
||||||
@RequestPart(required = true, value = "fileInput")
|
MultipartFile inputFile = request.getFileInput();
|
||||||
@Parameter(description = "The input PDF file to be processed with OCR")
|
List<String> selectedLanguages = request.getLanguages();
|
||||||
MultipartFile inputFile,
|
Boolean sidecar = request.isSidecar();
|
||||||
@RequestParam("languages")
|
Boolean deskew = request.isDeskew();
|
||||||
@Parameter(description = "List of languages to use in OCR processing")
|
Boolean clean = request.isClean();
|
||||||
List<String> selectedLanguages,
|
Boolean cleanFinal = request.isCleanFinal();
|
||||||
@RequestParam(name = "sidecar", required = false)
|
String ocrType = request.getOcrType();
|
||||||
@Parameter(description = "Include OCR text in a sidecar text file if set to true")
|
String ocrRenderType = request.getOcrRenderType();
|
||||||
Boolean sidecar,
|
Boolean removeImagesAfter = request.isRemoveImagesAfter();
|
||||||
@RequestParam(name = "deskew", required = false)
|
|
||||||
@Parameter(description = "Deskew the input file if set to true")
|
|
||||||
Boolean deskew,
|
|
||||||
@RequestParam(name = "clean", required = false)
|
|
||||||
@Parameter(description = "Clean the input file if set to true")
|
|
||||||
Boolean clean,
|
|
||||||
@RequestParam(name = "clean-final", required = false)
|
|
||||||
@Parameter(description = "Clean the final output if set to true")
|
|
||||||
Boolean cleanFinal,
|
|
||||||
@RequestParam(name = "ocrType", required = false)
|
|
||||||
@Parameter(description = "Specify the OCR type, e.g., 'skip-text', 'force-ocr', or 'Normal'", schema = @Schema(allowableValues = {"skip-text", "force-ocr", "Normal"}))
|
|
||||||
String ocrType,
|
|
||||||
@RequestParam(name = "ocrRenderType", required = false, defaultValue = "hocr")
|
|
||||||
@Parameter(description = "Specify the OCR render type, either 'hocr' or 'sandwich'", schema = @Schema(allowableValues = {"hocr", "sandwich"}))
|
|
||||||
String ocrRenderType,
|
|
||||||
@RequestParam(name = "removeImagesAfter", required = false)
|
|
||||||
@Parameter(description = "Remove images from the output PDF if set to true")
|
|
||||||
Boolean removeImagesAfter) throws IOException, InterruptedException {
|
|
||||||
|
|
||||||
// --output-type pdfa
|
// --output-type pdfa
|
||||||
if (selectedLanguages == null || selectedLanguages.isEmpty()) {
|
if (selectedLanguages == null || selectedLanguages.isEmpty()) {
|
||||||
throw new IOException("Please select at least one language.");
|
throw new IOException("Please select at least one language.");
|
||||||
@@ -139,8 +123,12 @@ public class OCRController {
|
|||||||
command.addAll(Arrays.asList("--language", languageOption, tempInputFile.toString(), tempOutputFile.toString()));
|
command.addAll(Arrays.asList("--language", languageOption, tempInputFile.toString(), tempOutputFile.toString()));
|
||||||
|
|
||||||
// Run CLI command
|
// Run CLI command
|
||||||
int returnCode = ProcessExecutor.getInstance(ProcessExecutor.Processes.OCR_MY_PDF).runCommandWithOutputHandling(command);
|
ProcessExecutorResult result = ProcessExecutor.getInstance(ProcessExecutor.Processes.OCR_MY_PDF).runCommandWithOutputHandling(command);
|
||||||
|
if(result.getRc() != 0 && result.getMessages().contains("multiprocessing/synchronize.py") && result.getMessages().contains("OSError: [Errno 38] Function not implemented")) {
|
||||||
|
command.add("--jobs");
|
||||||
|
command.add("1");
|
||||||
|
result = ProcessExecutor.getInstance(ProcessExecutor.Processes.OCR_MY_PDF).runCommandWithOutputHandling(command);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
@@ -151,7 +139,7 @@ public class OCRController {
|
|||||||
|
|
||||||
List<String> gsCommand = Arrays.asList("gs", "-sDEVICE=pdfwrite", "-dFILTERIMAGE", "-o", tempPdfWithoutImages.toString(), tempOutputFile.toString());
|
List<String> gsCommand = Arrays.asList("gs", "-sDEVICE=pdfwrite", "-dFILTERIMAGE", "-o", tempPdfWithoutImages.toString(), tempOutputFile.toString());
|
||||||
|
|
||||||
int gsReturnCode = ProcessExecutor.getInstance(ProcessExecutor.Processes.GHOSTSCRIPT).runCommandWithOutputHandling(gsCommand);
|
ProcessExecutor.getInstance(ProcessExecutor.Processes.GHOSTSCRIPT).runCommandWithOutputHandling(gsCommand);
|
||||||
tempOutputFile = tempPdfWithoutImages;
|
tempOutputFile = tempPdfWithoutImages;
|
||||||
}
|
}
|
||||||
// Read the OCR processed PDF file
|
// Read the OCR processed PDF file
|
||||||
@@ -1,4 +1,4 @@
|
|||||||
package stirling.software.SPDF.controller.api.other;
|
package stirling.software.SPDF.controller.api.misc;
|
||||||
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
|
|
||||||
@@ -6,18 +6,21 @@ import org.slf4j.Logger;
|
|||||||
import org.slf4j.LoggerFactory;
|
import org.slf4j.LoggerFactory;
|
||||||
import org.springframework.http.HttpStatus;
|
import org.springframework.http.HttpStatus;
|
||||||
import org.springframework.http.ResponseEntity;
|
import org.springframework.http.ResponseEntity;
|
||||||
|
import org.springframework.web.bind.annotation.ModelAttribute;
|
||||||
import org.springframework.web.bind.annotation.PostMapping;
|
import org.springframework.web.bind.annotation.PostMapping;
|
||||||
import org.springframework.web.bind.annotation.RequestParam;
|
import org.springframework.web.bind.annotation.RequestMapping;
|
||||||
import org.springframework.web.bind.annotation.RequestPart;
|
|
||||||
import org.springframework.web.bind.annotation.RestController;
|
import org.springframework.web.bind.annotation.RestController;
|
||||||
import org.springframework.web.multipart.MultipartFile;
|
import org.springframework.web.multipart.MultipartFile;
|
||||||
|
|
||||||
import io.swagger.v3.oas.annotations.Operation;
|
import io.swagger.v3.oas.annotations.Operation;
|
||||||
import io.swagger.v3.oas.annotations.Parameter;
|
import io.swagger.v3.oas.annotations.tags.Tag;
|
||||||
|
import stirling.software.SPDF.model.api.misc.OverlayImageRequest;
|
||||||
import stirling.software.SPDF.utils.PdfUtils;
|
import stirling.software.SPDF.utils.PdfUtils;
|
||||||
import stirling.software.SPDF.utils.WebResponseUtils;
|
import stirling.software.SPDF.utils.WebResponseUtils;
|
||||||
|
|
||||||
@RestController
|
@RestController
|
||||||
|
@RequestMapping("/api/v1/misc")
|
||||||
|
@Tag(name = "Misc", description = "Miscellaneous APIs")
|
||||||
public class OverlayImageController {
|
public class OverlayImageController {
|
||||||
|
|
||||||
private static final Logger logger = LoggerFactory.getLogger(OverlayImageController.class);
|
private static final Logger logger = LoggerFactory.getLogger(OverlayImageController.class);
|
||||||
@@ -25,24 +28,14 @@ public class OverlayImageController {
|
|||||||
@PostMapping(consumes = "multipart/form-data", value = "/add-image")
|
@PostMapping(consumes = "multipart/form-data", value = "/add-image")
|
||||||
@Operation(
|
@Operation(
|
||||||
summary = "Overlay image onto a PDF file",
|
summary = "Overlay image onto a PDF file",
|
||||||
description = "This endpoint overlays an image onto a PDF file at the specified coordinates. The image can be overlaid on every page of the PDF if specified."
|
description = "This endpoint overlays an image onto a PDF file at the specified coordinates. The image can be overlaid on every page of the PDF if specified. Input:PDF/IMAGE Output:PDF Type:MF-SISO"
|
||||||
)
|
)
|
||||||
public ResponseEntity<byte[]> overlayImage(
|
public ResponseEntity<byte[]> overlayImage(@ModelAttribute OverlayImageRequest request) {
|
||||||
@RequestPart(required = true, value = "fileInput")
|
MultipartFile pdfFile = request.getFileInput();
|
||||||
@Parameter(description = "The input PDF file to overlay the image onto.", required = true)
|
MultipartFile imageFile = request.getImageFile();
|
||||||
MultipartFile pdfFile,
|
float x = request.getX();
|
||||||
@RequestParam("fileInput2")
|
float y = request.getY();
|
||||||
@Parameter(description = "The image file to be overlaid onto the PDF.", required = true)
|
boolean everyPage = request.isEveryPage();
|
||||||
MultipartFile imageFile,
|
|
||||||
@RequestParam("x")
|
|
||||||
@Parameter(description = "The x-coordinate at which to place the top-left corner of the image.", example = "0")
|
|
||||||
float x,
|
|
||||||
@RequestParam("y")
|
|
||||||
@Parameter(description = "The y-coordinate at which to place the top-left corner of the image.", example = "0")
|
|
||||||
float y,
|
|
||||||
@RequestParam("everyPage")
|
|
||||||
@Parameter(description = "Whether to overlay the image onto every page of the PDF.", example = "false")
|
|
||||||
boolean everyPage) {
|
|
||||||
try {
|
try {
|
||||||
byte[] pdfBytes = pdfFile.getBytes();
|
byte[] pdfBytes = pdfFile.getBytes();
|
||||||
byte[] imageBytes = imageFile.getBytes();
|
byte[] imageBytes = imageFile.getBytes();
|
||||||
@@ -0,0 +1,135 @@
|
|||||||
|
package stirling.software.SPDF.controller.api.misc;
|
||||||
|
|
||||||
|
import java.io.ByteArrayOutputStream;
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
import org.apache.pdfbox.pdmodel.PDDocument;
|
||||||
|
import org.apache.pdfbox.pdmodel.PDPage;
|
||||||
|
import org.apache.pdfbox.pdmodel.PDPageContentStream;
|
||||||
|
import org.apache.pdfbox.pdmodel.common.PDRectangle;
|
||||||
|
import org.apache.pdfbox.pdmodel.font.PDType1Font;
|
||||||
|
import org.slf4j.Logger;
|
||||||
|
import org.slf4j.LoggerFactory;
|
||||||
|
import org.springframework.http.MediaType;
|
||||||
|
import org.springframework.http.ResponseEntity;
|
||||||
|
import org.springframework.web.bind.annotation.ModelAttribute;
|
||||||
|
import org.springframework.web.bind.annotation.PostMapping;
|
||||||
|
import org.springframework.web.bind.annotation.RequestMapping;
|
||||||
|
import org.springframework.web.bind.annotation.RestController;
|
||||||
|
import org.springframework.web.multipart.MultipartFile;
|
||||||
|
|
||||||
|
import io.swagger.v3.oas.annotations.Operation;
|
||||||
|
import io.swagger.v3.oas.annotations.tags.Tag;
|
||||||
|
import stirling.software.SPDF.model.api.misc.AddPageNumbersRequest;
|
||||||
|
import stirling.software.SPDF.utils.GeneralUtils;
|
||||||
|
import stirling.software.SPDF.utils.WebResponseUtils;
|
||||||
|
|
||||||
|
@RestController
|
||||||
|
@RequestMapping("/api/v1/misc")
|
||||||
|
@Tag(name = "Misc", description = "Miscellaneous APIs")
|
||||||
|
public class PageNumbersController {
|
||||||
|
|
||||||
|
private static final Logger logger = LoggerFactory.getLogger(PageNumbersController.class);
|
||||||
|
|
||||||
|
@PostMapping(value = "/add-page-numbers", consumes = "multipart/form-data")
|
||||||
|
@Operation(summary = "Add page numbers to a PDF document", description = "This operation takes an input PDF file and adds page numbers to it. Input:PDF Output:PDF Type:SISO")
|
||||||
|
public ResponseEntity<byte[]> addPageNumbers(@ModelAttribute AddPageNumbersRequest request) throws IOException {
|
||||||
|
MultipartFile file = request.getFileInput();
|
||||||
|
String customMargin = request.getCustomMargin();
|
||||||
|
int position = request.getPosition();
|
||||||
|
int startingNumber = request.getStartingNumber();
|
||||||
|
String pagesToNumber = request.getPagesToNumber();
|
||||||
|
String customText = request.getCustomText();
|
||||||
|
int pageNumber = startingNumber;
|
||||||
|
byte[] fileBytes = file.getBytes();
|
||||||
|
PDDocument document = PDDocument.load(fileBytes);
|
||||||
|
|
||||||
|
float marginFactor;
|
||||||
|
switch (customMargin.toLowerCase()) {
|
||||||
|
case "small":
|
||||||
|
marginFactor = 0.02f;
|
||||||
|
break;
|
||||||
|
case "medium":
|
||||||
|
marginFactor = 0.035f;
|
||||||
|
break;
|
||||||
|
case "large":
|
||||||
|
marginFactor = 0.05f;
|
||||||
|
break;
|
||||||
|
case "x-large":
|
||||||
|
marginFactor = 0.075f;
|
||||||
|
break;
|
||||||
|
|
||||||
|
|
||||||
|
default:
|
||||||
|
marginFactor = 0.035f;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
float fontSize = 12.0f;
|
||||||
|
PDType1Font font = PDType1Font.HELVETICA;
|
||||||
|
if(pagesToNumber == null || pagesToNumber.length() == 0) {
|
||||||
|
pagesToNumber = "all";
|
||||||
|
}
|
||||||
|
if(customText == null || customText.length() == 0) {
|
||||||
|
customText = "{n}";
|
||||||
|
}
|
||||||
|
List<Integer> pagesToNumberList = GeneralUtils.parsePageList(pagesToNumber.split(","), document.getNumberOfPages());
|
||||||
|
|
||||||
|
for (int i : pagesToNumberList) {
|
||||||
|
PDPage page = document.getPage(i);
|
||||||
|
PDRectangle pageSize = page.getMediaBox();
|
||||||
|
|
||||||
|
String text = customText != null ? customText.replace("{n}", String.valueOf(pageNumber)).replace("{total}", String.valueOf(document.getNumberOfPages())).replace("{filename}", file.getOriginalFilename().replaceFirst("[.][^.]+$", "")) : String.valueOf(pageNumber);
|
||||||
|
|
||||||
|
float x, y;
|
||||||
|
|
||||||
|
int xGroup = (position - 1) % 3;
|
||||||
|
int yGroup = 2 - (position - 1) / 3;
|
||||||
|
|
||||||
|
switch (xGroup) {
|
||||||
|
case 0: // left
|
||||||
|
x = pageSize.getLowerLeftX() + marginFactor * pageSize.getWidth();
|
||||||
|
break;
|
||||||
|
case 1: // center
|
||||||
|
x = pageSize.getLowerLeftX() + (pageSize.getWidth() / 2);
|
||||||
|
break;
|
||||||
|
default: // right
|
||||||
|
x = pageSize.getUpperRightX() - marginFactor * pageSize.getWidth();
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
switch (yGroup) {
|
||||||
|
case 0: // bottom
|
||||||
|
y = pageSize.getLowerLeftY() + marginFactor * pageSize.getHeight();
|
||||||
|
break;
|
||||||
|
case 1: // middle
|
||||||
|
y = pageSize.getLowerLeftY() + (pageSize.getHeight() / 2);
|
||||||
|
break;
|
||||||
|
default: // top
|
||||||
|
y = pageSize.getUpperRightY() - marginFactor * pageSize.getHeight();
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
PDPageContentStream contentStream = new PDPageContentStream(document, page, PDPageContentStream.AppendMode.APPEND, true);
|
||||||
|
contentStream.beginText();
|
||||||
|
contentStream.setFont(font, fontSize);
|
||||||
|
contentStream.newLineAtOffset(x, y);
|
||||||
|
contentStream.showText(text);
|
||||||
|
contentStream.endText();
|
||||||
|
contentStream.close();
|
||||||
|
|
||||||
|
pageNumber++;
|
||||||
|
}
|
||||||
|
|
||||||
|
ByteArrayOutputStream baos = new ByteArrayOutputStream();
|
||||||
|
document.save(baos);
|
||||||
|
document.close();
|
||||||
|
|
||||||
|
return WebResponseUtils.bytesToWebResponse(baos.toByteArray(), file.getOriginalFilename().replaceFirst("[.][^.]+$", "") + "_numbersAdded.pdf", MediaType.APPLICATION_PDF);
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
@@ -1,4 +1,4 @@
|
|||||||
package stirling.software.SPDF.controller.api.other;
|
package stirling.software.SPDF.controller.api.misc;
|
||||||
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.nio.file.Files;
|
import java.nio.file.Files;
|
||||||
@@ -9,17 +9,22 @@ import java.util.List;
|
|||||||
import org.slf4j.Logger;
|
import org.slf4j.Logger;
|
||||||
import org.slf4j.LoggerFactory;
|
import org.slf4j.LoggerFactory;
|
||||||
import org.springframework.http.ResponseEntity;
|
import org.springframework.http.ResponseEntity;
|
||||||
|
import org.springframework.web.bind.annotation.ModelAttribute;
|
||||||
import org.springframework.web.bind.annotation.PostMapping;
|
import org.springframework.web.bind.annotation.PostMapping;
|
||||||
import org.springframework.web.bind.annotation.RequestPart;
|
import org.springframework.web.bind.annotation.RequestMapping;
|
||||||
import org.springframework.web.bind.annotation.RestController;
|
import org.springframework.web.bind.annotation.RestController;
|
||||||
import org.springframework.web.multipart.MultipartFile;
|
import org.springframework.web.multipart.MultipartFile;
|
||||||
|
|
||||||
import io.swagger.v3.oas.annotations.Operation;
|
import io.swagger.v3.oas.annotations.Operation;
|
||||||
import io.swagger.v3.oas.annotations.Parameter;
|
import io.swagger.v3.oas.annotations.tags.Tag;
|
||||||
|
import stirling.software.SPDF.model.api.PDFFile;
|
||||||
import stirling.software.SPDF.utils.ProcessExecutor;
|
import stirling.software.SPDF.utils.ProcessExecutor;
|
||||||
|
import stirling.software.SPDF.utils.ProcessExecutor.ProcessExecutorResult;
|
||||||
import stirling.software.SPDF.utils.WebResponseUtils;
|
import stirling.software.SPDF.utils.WebResponseUtils;
|
||||||
|
|
||||||
@RestController
|
@RestController
|
||||||
|
@RequestMapping("/api/v1/misc")
|
||||||
|
@Tag(name = "Misc", description = "Miscellaneous APIs")
|
||||||
public class RepairController {
|
public class RepairController {
|
||||||
|
|
||||||
private static final Logger logger = LoggerFactory.getLogger(RepairController.class);
|
private static final Logger logger = LoggerFactory.getLogger(RepairController.class);
|
||||||
@@ -27,13 +32,10 @@ public class RepairController {
|
|||||||
@PostMapping(consumes = "multipart/form-data", value = "/repair")
|
@PostMapping(consumes = "multipart/form-data", value = "/repair")
|
||||||
@Operation(
|
@Operation(
|
||||||
summary = "Repair a PDF file",
|
summary = "Repair a PDF file",
|
||||||
description = "This endpoint repairs a given PDF file by running Ghostscript command. The PDF is first saved to a temporary location, repaired, read back, and then returned as a response."
|
description = "This endpoint repairs a given PDF file by running Ghostscript command. The PDF is first saved to a temporary location, repaired, read back, and then returned as a response. Input:PDF Output:PDF Type:SISO"
|
||||||
)
|
)
|
||||||
public ResponseEntity<byte[]> repairPdf(
|
public ResponseEntity<byte[]> repairPdf(@ModelAttribute PDFFile request) throws IOException, InterruptedException {
|
||||||
@RequestPart(required = true, value = "fileInput")
|
MultipartFile inputFile = request.getFileInput();
|
||||||
@Parameter(description = "The input PDF file to be repaired", required = true)
|
|
||||||
MultipartFile inputFile) throws IOException, InterruptedException {
|
|
||||||
|
|
||||||
// Save the uploaded file to a temporary location
|
// Save the uploaded file to a temporary location
|
||||||
Path tempInputFile = Files.createTempFile("input_", ".pdf");
|
Path tempInputFile = Files.createTempFile("input_", ".pdf");
|
||||||
inputFile.transferTo(tempInputFile.toFile());
|
inputFile.transferTo(tempInputFile.toFile());
|
||||||
@@ -49,7 +51,7 @@ public class RepairController {
|
|||||||
command.add(tempInputFile.toString());
|
command.add(tempInputFile.toString());
|
||||||
|
|
||||||
|
|
||||||
int returnCode = ProcessExecutor.getInstance(ProcessExecutor.Processes.GHOSTSCRIPT).runCommandWithOutputHandling(command);
|
ProcessExecutorResult returnCode = ProcessExecutor.getInstance(ProcessExecutor.Processes.GHOSTSCRIPT).runCommandWithOutputHandling(command);
|
||||||
|
|
||||||
// Read the optimized PDF file
|
// Read the optimized PDF file
|
||||||
byte[] pdfBytes = Files.readAllBytes(tempOutputFile);
|
byte[] pdfBytes = Files.readAllBytes(tempOutputFile);
|
||||||
@@ -0,0 +1,61 @@
|
|||||||
|
package stirling.software.SPDF.controller.api.misc;
|
||||||
|
|
||||||
|
import java.nio.charset.StandardCharsets;
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
|
import org.apache.pdfbox.pdmodel.PDDocument;
|
||||||
|
import org.apache.pdfbox.pdmodel.common.PDNameTreeNode;
|
||||||
|
import org.apache.pdfbox.pdmodel.interactive.action.PDActionJavaScript;
|
||||||
|
import org.slf4j.Logger;
|
||||||
|
import org.slf4j.LoggerFactory;
|
||||||
|
import org.springframework.http.ResponseEntity;
|
||||||
|
import org.springframework.web.bind.annotation.ModelAttribute;
|
||||||
|
import org.springframework.web.bind.annotation.PostMapping;
|
||||||
|
import org.springframework.web.bind.annotation.RequestMapping;
|
||||||
|
import org.springframework.web.bind.annotation.RestController;
|
||||||
|
import org.springframework.web.multipart.MultipartFile;
|
||||||
|
|
||||||
|
import io.swagger.v3.oas.annotations.tags.Tag;
|
||||||
|
import stirling.software.SPDF.model.api.PDFFile;
|
||||||
|
import stirling.software.SPDF.utils.WebResponseUtils;
|
||||||
|
@RestController
|
||||||
|
@RequestMapping("/api/v1/misc")
|
||||||
|
@Tag(name = "Misc", description = "Miscellaneous APIs")
|
||||||
|
public class ShowJavascript {
|
||||||
|
|
||||||
|
private static final Logger logger = LoggerFactory.getLogger(ShowJavascript.class);
|
||||||
|
@PostMapping(consumes = "multipart/form-data", value = "/show-javascript")
|
||||||
|
public ResponseEntity<byte[]> extractHeader(@ModelAttribute PDFFile request) throws Exception {
|
||||||
|
MultipartFile inputFile = request.getFileInput();
|
||||||
|
String script = "";
|
||||||
|
|
||||||
|
try (PDDocument document = PDDocument.load(inputFile.getInputStream())) {
|
||||||
|
|
||||||
|
if(document.getDocumentCatalog() != null && document.getDocumentCatalog().getNames() != null) {
|
||||||
|
PDNameTreeNode<PDActionJavaScript> jsTree = document.getDocumentCatalog().getNames().getJavaScript();
|
||||||
|
|
||||||
|
if (jsTree != null) {
|
||||||
|
Map<String, PDActionJavaScript> jsEntries = jsTree.getNames();
|
||||||
|
|
||||||
|
for (Map.Entry<String, PDActionJavaScript> entry : jsEntries.entrySet()) {
|
||||||
|
String name = entry.getKey();
|
||||||
|
PDActionJavaScript jsAction = entry.getValue();
|
||||||
|
String jsCodeStr = jsAction.getAction();
|
||||||
|
|
||||||
|
script += "// File: " + inputFile.getOriginalFilename() + ", Script: " + name + "\n" + jsCodeStr + "\n";
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (script.isEmpty()) {
|
||||||
|
script = "PDF '" + inputFile.getOriginalFilename() + "' does not contain Javascript";
|
||||||
|
}
|
||||||
|
|
||||||
|
return WebResponseUtils.bytesToWebResponse(script.getBytes(StandardCharsets.UTF_8), inputFile.getOriginalFilename() + ".js");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
@@ -0,0 +1,524 @@
|
|||||||
|
package stirling.software.SPDF.controller.api.pipeline;
|
||||||
|
|
||||||
|
import java.io.ByteArrayInputStream;
|
||||||
|
import java.io.ByteArrayOutputStream;
|
||||||
|
import java.io.File;
|
||||||
|
import java.io.FileOutputStream;
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.io.InputStream;
|
||||||
|
import java.io.OutputStream;
|
||||||
|
import java.io.PrintStream;
|
||||||
|
import java.nio.file.Files;
|
||||||
|
import java.nio.file.Path;
|
||||||
|
import java.nio.file.Paths;
|
||||||
|
import java.time.LocalDate;
|
||||||
|
import java.time.LocalTime;
|
||||||
|
import java.time.format.DateTimeFormatter;
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.Iterator;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Map;
|
||||||
|
import java.util.stream.Stream;
|
||||||
|
import java.util.zip.ZipEntry;
|
||||||
|
import java.util.zip.ZipInputStream;
|
||||||
|
import java.util.zip.ZipOutputStream;
|
||||||
|
|
||||||
|
import org.slf4j.Logger;
|
||||||
|
import org.slf4j.LoggerFactory;
|
||||||
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
import org.springframework.core.io.ByteArrayResource;
|
||||||
|
import org.springframework.core.io.Resource;
|
||||||
|
import org.springframework.http.HttpEntity;
|
||||||
|
import org.springframework.http.HttpHeaders;
|
||||||
|
import org.springframework.http.HttpMethod;
|
||||||
|
import org.springframework.http.HttpStatus;
|
||||||
|
import org.springframework.http.MediaType;
|
||||||
|
import org.springframework.http.ResponseEntity;
|
||||||
|
import org.springframework.scheduling.annotation.Scheduled;
|
||||||
|
import org.springframework.util.LinkedMultiValueMap;
|
||||||
|
import org.springframework.util.MultiValueMap;
|
||||||
|
import org.springframework.web.bind.annotation.ModelAttribute;
|
||||||
|
import org.springframework.web.bind.annotation.PostMapping;
|
||||||
|
import org.springframework.web.bind.annotation.RequestMapping;
|
||||||
|
import org.springframework.web.bind.annotation.RestController;
|
||||||
|
import org.springframework.web.client.RestTemplate;
|
||||||
|
import org.springframework.web.multipart.MultipartFile;
|
||||||
|
|
||||||
|
import com.fasterxml.jackson.databind.JsonNode;
|
||||||
|
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||||
|
|
||||||
|
import io.swagger.v3.oas.annotations.tags.Tag;
|
||||||
|
import stirling.software.SPDF.model.ApplicationProperties;
|
||||||
|
import stirling.software.SPDF.model.PipelineConfig;
|
||||||
|
import stirling.software.SPDF.model.PipelineOperation;
|
||||||
|
import stirling.software.SPDF.model.api.HandleDataRequest;
|
||||||
|
import stirling.software.SPDF.utils.WebResponseUtils;
|
||||||
|
|
||||||
|
@RestController
|
||||||
|
@RequestMapping("/api/v1/pipeline")
|
||||||
|
@Tag(name = "Pipeline", description = "Pipeline APIs")
|
||||||
|
public class PipelineController {
|
||||||
|
|
||||||
|
private static final Logger logger = LoggerFactory.getLogger(PipelineController.class);
|
||||||
|
@Autowired
|
||||||
|
private ObjectMapper objectMapper;
|
||||||
|
|
||||||
|
final String jsonFileName = "pipelineConfig.json";
|
||||||
|
final String watchedFoldersDir = "./pipeline/watchedFolders/";
|
||||||
|
final String finishedFoldersDir = "./pipeline/finishedFolders/";
|
||||||
|
|
||||||
|
@Scheduled(fixedRate = 25000)
|
||||||
|
public void scanFolders() {
|
||||||
|
logger.info("Scanning folders...");
|
||||||
|
Path watchedFolderPath = Paths.get(watchedFoldersDir);
|
||||||
|
if (!Files.exists(watchedFolderPath)) {
|
||||||
|
try {
|
||||||
|
Files.createDirectories(watchedFolderPath);
|
||||||
|
logger.info("Created directory: {}", watchedFolderPath);
|
||||||
|
} catch (IOException e) {
|
||||||
|
logger.error("Error creating directory: {}", watchedFolderPath, e);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
try (Stream<Path> paths = Files.walk(watchedFolderPath)) {
|
||||||
|
paths.filter(Files::isDirectory).forEach(t -> {
|
||||||
|
try {
|
||||||
|
if (!t.equals(watchedFolderPath) && !t.endsWith("processing")) {
|
||||||
|
handleDirectory(t);
|
||||||
|
}
|
||||||
|
} catch (Exception e) {
|
||||||
|
logger.error("Error handling directory: {}", t, e);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
} catch (Exception e) {
|
||||||
|
logger.error("Error walking through directory: {}", watchedFolderPath, e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
ApplicationProperties applicationProperties;
|
||||||
|
|
||||||
|
|
||||||
|
private void handleDirectory(Path dir) throws Exception {
|
||||||
|
logger.info("Handling directory: {}", dir);
|
||||||
|
Path jsonFile = dir.resolve(jsonFileName);
|
||||||
|
Path processingDir = dir.resolve("processing"); // Directory to move files during processing
|
||||||
|
if (!Files.exists(processingDir)) {
|
||||||
|
Files.createDirectory(processingDir);
|
||||||
|
logger.info("Created processing directory: {}", processingDir);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (Files.exists(jsonFile)) {
|
||||||
|
// Read JSON file
|
||||||
|
String jsonString;
|
||||||
|
try {
|
||||||
|
jsonString = new String(Files.readAllBytes(jsonFile));
|
||||||
|
logger.info("Read JSON file: {}", jsonFile);
|
||||||
|
} catch (IOException e) {
|
||||||
|
logger.error("Error reading JSON file: {}", jsonFile, e);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Decode JSON to PipelineConfig
|
||||||
|
PipelineConfig config;
|
||||||
|
try {
|
||||||
|
config = objectMapper.readValue(jsonString, PipelineConfig.class);
|
||||||
|
// Assuming your PipelineConfig class has getters for all necessary fields, you
|
||||||
|
// can perform checks here
|
||||||
|
if (config.getOperations() == null || config.getOutputDir() == null || config.getName() == null) {
|
||||||
|
throw new IOException("Invalid JSON format");
|
||||||
|
}
|
||||||
|
} catch (IOException e) {
|
||||||
|
logger.error("Error parsing PipelineConfig: {}", jsonString, e);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
// For each operation in the pipeline
|
||||||
|
for (PipelineOperation operation : config.getOperations()) {
|
||||||
|
// Collect all files based on fileInput
|
||||||
|
File[] files;
|
||||||
|
String fileInput = (String) operation.getParameters().get("fileInput");
|
||||||
|
if ("automated".equals(fileInput)) {
|
||||||
|
// If fileInput is "automated", process all files in the directory
|
||||||
|
try (Stream<Path> paths = Files.list(dir)) {
|
||||||
|
files = paths
|
||||||
|
.filter(path -> !Files.isDirectory(path)) // exclude directories
|
||||||
|
.filter(path -> !path.equals(jsonFile)) // exclude jsonFile
|
||||||
|
.map(Path::toFile)
|
||||||
|
.toArray(File[]::new);
|
||||||
|
|
||||||
|
} catch (IOException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
// If fileInput contains a path, process only this file
|
||||||
|
files = new File[] { new File(fileInput) };
|
||||||
|
}
|
||||||
|
|
||||||
|
// Prepare the files for processing
|
||||||
|
List<File> filesToProcess = new ArrayList<>();
|
||||||
|
for (File file : files) {
|
||||||
|
logger.info(file.getName());
|
||||||
|
logger.info("{} to {}",file.toPath(), processingDir.resolve(file.getName()));
|
||||||
|
Files.move(file.toPath(), processingDir.resolve(file.getName()));
|
||||||
|
filesToProcess.add(processingDir.resolve(file.getName()).toFile());
|
||||||
|
}
|
||||||
|
|
||||||
|
// Process the files
|
||||||
|
try {
|
||||||
|
List<Resource> resources = handleFiles(filesToProcess.toArray(new File[0]), jsonString);
|
||||||
|
|
||||||
|
if(resources == null) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
// Move resultant files and rename them as per config in JSON file
|
||||||
|
for (Resource resource : resources) {
|
||||||
|
String resourceName = resource.getFilename();
|
||||||
|
String baseName = resourceName.substring(0, resourceName.lastIndexOf("."));
|
||||||
|
String extension = resourceName.substring(resourceName.lastIndexOf(".")+1);
|
||||||
|
|
||||||
|
String outputFileName = config.getOutputPattern().replace("{filename}", baseName);
|
||||||
|
|
||||||
|
outputFileName = outputFileName.replace("{pipelineName}", config.getName());
|
||||||
|
DateTimeFormatter dateFormatter = DateTimeFormatter.ofPattern("yyyyMMdd");
|
||||||
|
outputFileName = outputFileName.replace("{date}", LocalDate.now().format(dateFormatter));
|
||||||
|
DateTimeFormatter timeFormatter = DateTimeFormatter.ofPattern("HHmmss");
|
||||||
|
outputFileName = outputFileName.replace("{time}", LocalTime.now().format(timeFormatter));
|
||||||
|
|
||||||
|
outputFileName += "." + extension;
|
||||||
|
// {filename} {folder} {date} {tmime} {pipeline}
|
||||||
|
String outputDir = config.getOutputDir();
|
||||||
|
|
||||||
|
String outputFolder = applicationProperties.getAutoPipeline().getOutputFolder();
|
||||||
|
|
||||||
|
if (outputFolder == null || outputFolder.isEmpty()) {
|
||||||
|
// If the environment variable is not set, use the default value
|
||||||
|
outputFolder = finishedFoldersDir;
|
||||||
|
}
|
||||||
|
logger.info("outputDir 0={}", outputDir);
|
||||||
|
// Replace the placeholders in the outputDir string
|
||||||
|
outputDir = outputDir.replace("{outputFolder}", outputFolder);
|
||||||
|
outputDir = outputDir.replace("{folderName}", dir.toString());
|
||||||
|
logger.info("outputDir 1={}", outputDir);
|
||||||
|
outputDir = outputDir.replace("\\watchedFolders", "");
|
||||||
|
outputDir = outputDir.replace("//watchedFolders", "");
|
||||||
|
outputDir = outputDir.replace("\\\\watchedFolders", "");
|
||||||
|
outputDir = outputDir.replace("/watchedFolders", "");
|
||||||
|
|
||||||
|
Path outputPath;
|
||||||
|
logger.info("outputDir 2={}", outputDir);
|
||||||
|
if (Paths.get(outputDir).isAbsolute()) {
|
||||||
|
// If it's an absolute path, use it directly
|
||||||
|
outputPath = Paths.get(outputDir);
|
||||||
|
} else {
|
||||||
|
// If it's a relative path, make it relative to the current working directory
|
||||||
|
outputPath = Paths.get(".", outputDir);
|
||||||
|
}
|
||||||
|
|
||||||
|
logger.info("outputPath={}", outputPath);
|
||||||
|
|
||||||
|
if (!Files.exists(outputPath)) {
|
||||||
|
try {
|
||||||
|
Files.createDirectories(outputPath);
|
||||||
|
logger.info("Created directory: {}", outputPath);
|
||||||
|
} catch (IOException e) {
|
||||||
|
logger.error("Error creating directory: {}", outputPath, e);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
logger.info("outputPath {}", outputPath);
|
||||||
|
logger.info("outputPath.resolve(outputFileName).toString() {}", outputPath.resolve(outputFileName).toString());
|
||||||
|
File newFile = new File(outputPath.resolve(outputFileName).toString());
|
||||||
|
OutputStream os = new FileOutputStream(newFile);
|
||||||
|
os.write(((ByteArrayResource)resource).getByteArray());
|
||||||
|
os.close();
|
||||||
|
logger.info("made {}", outputPath.resolve(outputFileName));
|
||||||
|
}
|
||||||
|
|
||||||
|
// If successful, delete the original files
|
||||||
|
for (File file : filesToProcess) {
|
||||||
|
Files.deleteIfExists(processingDir.resolve(file.getName()));
|
||||||
|
}
|
||||||
|
} catch (Exception e) {
|
||||||
|
// If an error occurs, move the original files back
|
||||||
|
for (File file : filesToProcess) {
|
||||||
|
Files.move(processingDir.resolve(file.getName()), file.toPath());
|
||||||
|
}
|
||||||
|
throw e;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
List<Resource> processFiles(List<Resource> outputFiles, String jsonString) throws Exception {
|
||||||
|
|
||||||
|
ObjectMapper mapper = new ObjectMapper();
|
||||||
|
JsonNode jsonNode = mapper.readTree(jsonString);
|
||||||
|
|
||||||
|
JsonNode pipelineNode = jsonNode.get("pipeline");
|
||||||
|
logger.info("Running pipelineNode: {}", pipelineNode);
|
||||||
|
ByteArrayOutputStream logStream = new ByteArrayOutputStream();
|
||||||
|
PrintStream logPrintStream = new PrintStream(logStream);
|
||||||
|
|
||||||
|
boolean hasErrors = false;
|
||||||
|
|
||||||
|
for (JsonNode operationNode : pipelineNode) {
|
||||||
|
String operation = operationNode.get("operation").asText();
|
||||||
|
logger.info("Running operation: {}", operation);
|
||||||
|
JsonNode parametersNode = operationNode.get("parameters");
|
||||||
|
String inputFileExtension = "";
|
||||||
|
if (operationNode.has("inputFileType")) {
|
||||||
|
inputFileExtension = operationNode.get("inputFileType").asText();
|
||||||
|
} else {
|
||||||
|
inputFileExtension = ".pdf";
|
||||||
|
}
|
||||||
|
|
||||||
|
List<Resource> newOutputFiles = new ArrayList<>();
|
||||||
|
boolean hasInputFileType = false;
|
||||||
|
|
||||||
|
for (Resource file : outputFiles) {
|
||||||
|
if (file.getFilename().endsWith(inputFileExtension)) {
|
||||||
|
hasInputFileType = true;
|
||||||
|
MultiValueMap<String, Object> body = new LinkedMultiValueMap<>();
|
||||||
|
body.add("fileInput", file);
|
||||||
|
|
||||||
|
Iterator<Map.Entry<String, JsonNode>> parameters = parametersNode.fields();
|
||||||
|
while (parameters.hasNext()) {
|
||||||
|
Map.Entry<String, JsonNode> parameter = parameters.next();
|
||||||
|
body.add(parameter.getKey(), parameter.getValue().asText());
|
||||||
|
}
|
||||||
|
|
||||||
|
HttpHeaders headers = new HttpHeaders();
|
||||||
|
headers.setContentType(MediaType.MULTIPART_FORM_DATA);
|
||||||
|
|
||||||
|
HttpEntity<MultiValueMap<String, Object>> entity = new HttpEntity<>(body, headers);
|
||||||
|
|
||||||
|
RestTemplate restTemplate = new RestTemplate();
|
||||||
|
String url = "http://localhost:8080/" + operation;
|
||||||
|
|
||||||
|
ResponseEntity<byte[]> response = restTemplate.exchange(url, HttpMethod.POST, entity, byte[].class);
|
||||||
|
|
||||||
|
// If the operation is filter and the response body is null or empty, skip this file
|
||||||
|
if (operation.startsWith("filter-") && (response.getBody() == null || response.getBody().length == 0)) {
|
||||||
|
logger.info("Skipping file due to failing {}", operation);
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!response.getStatusCode().equals(HttpStatus.OK)) {
|
||||||
|
logPrintStream.println("Error: " + response.getBody());
|
||||||
|
hasErrors = true;
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
// Define filename
|
||||||
|
String filename;
|
||||||
|
if ("auto-rename".equals(operation)) {
|
||||||
|
// If the operation is "auto-rename", generate a new filename.
|
||||||
|
// This is a simple example of generating a filename using current timestamp.
|
||||||
|
// Modify as per your needs.
|
||||||
|
filename = "file_" + System.currentTimeMillis();
|
||||||
|
} else {
|
||||||
|
// Otherwise, keep the original filename.
|
||||||
|
filename = file.getFilename();
|
||||||
|
}
|
||||||
|
|
||||||
|
// Check if the response body is a zip file
|
||||||
|
if (isZip(response.getBody())) {
|
||||||
|
// Unzip the file and add all the files to the new output files
|
||||||
|
newOutputFiles.addAll(unzip(response.getBody()));
|
||||||
|
} else {
|
||||||
|
Resource outputResource = new ByteArrayResource(response.getBody()) {
|
||||||
|
@Override
|
||||||
|
public String getFilename() {
|
||||||
|
return filename;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
newOutputFiles.add(outputResource);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!hasInputFileType) {
|
||||||
|
logPrintStream.println(
|
||||||
|
"No files with extension " + inputFileExtension + " found for operation " + operation);
|
||||||
|
hasErrors = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
outputFiles = newOutputFiles;
|
||||||
|
}
|
||||||
|
logPrintStream.close();
|
||||||
|
|
||||||
|
}
|
||||||
|
if (hasErrors) {
|
||||||
|
logger.error("Errors occurred during processing. Log: {}", logStream.toString());
|
||||||
|
}
|
||||||
|
return outputFiles;
|
||||||
|
}
|
||||||
|
|
||||||
|
List<Resource> handleFiles(File[] files, String jsonString) throws Exception {
|
||||||
|
if(files == null || files.length == 0) {
|
||||||
|
logger.info("No files");
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
logger.info("Handling files: {} files, with JSON string of length: {}", files.length, jsonString.length());
|
||||||
|
|
||||||
|
ObjectMapper mapper = new ObjectMapper();
|
||||||
|
JsonNode jsonNode = mapper.readTree(jsonString);
|
||||||
|
|
||||||
|
JsonNode pipelineNode = jsonNode.get("pipeline");
|
||||||
|
|
||||||
|
boolean hasErrors = false;
|
||||||
|
List<Resource> outputFiles = new ArrayList<>();
|
||||||
|
|
||||||
|
for (File file : files) {
|
||||||
|
Path path = Paths.get(file.getAbsolutePath());
|
||||||
|
System.out.println("Reading file: " + path); // debug statement
|
||||||
|
|
||||||
|
if (Files.exists(path)) {
|
||||||
|
Resource fileResource = new ByteArrayResource(Files.readAllBytes(path)) {
|
||||||
|
@Override
|
||||||
|
public String getFilename() {
|
||||||
|
return file.getName();
|
||||||
|
}
|
||||||
|
};
|
||||||
|
outputFiles.add(fileResource);
|
||||||
|
} else {
|
||||||
|
System.out.println("File not found: " + path); // debug statement
|
||||||
|
}
|
||||||
|
}
|
||||||
|
logger.info("Files successfully loaded. Starting processing...");
|
||||||
|
return processFiles(outputFiles, jsonString);
|
||||||
|
}
|
||||||
|
|
||||||
|
List<Resource> handleFiles(MultipartFile[] files, String jsonString) throws Exception {
|
||||||
|
if(files == null || files.length == 0) {
|
||||||
|
logger.info("No files");
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
logger.info("Handling files: {} files, with JSON string of length: {}", files.length, jsonString.length());
|
||||||
|
ObjectMapper mapper = new ObjectMapper();
|
||||||
|
JsonNode jsonNode = mapper.readTree(jsonString);
|
||||||
|
|
||||||
|
JsonNode pipelineNode = jsonNode.get("pipeline");
|
||||||
|
|
||||||
|
boolean hasErrors = false;
|
||||||
|
List<Resource> outputFiles = new ArrayList<>();
|
||||||
|
|
||||||
|
for (MultipartFile file : files) {
|
||||||
|
Resource fileResource = new ByteArrayResource(file.getBytes()) {
|
||||||
|
@Override
|
||||||
|
public String getFilename() {
|
||||||
|
return file.getOriginalFilename();
|
||||||
|
}
|
||||||
|
};
|
||||||
|
outputFiles.add(fileResource);
|
||||||
|
}
|
||||||
|
logger.info("Files successfully loaded. Starting processing...");
|
||||||
|
return processFiles(outputFiles, jsonString);
|
||||||
|
}
|
||||||
|
|
||||||
|
@PostMapping("/handleData")
|
||||||
|
public ResponseEntity<byte[]> handleData(@ModelAttribute HandleDataRequest request) {
|
||||||
|
MultipartFile[] files = request.getFileInputs();
|
||||||
|
String jsonString = request.getJsonString();
|
||||||
|
logger.info("Received POST request to /handleData with {} files", files.length);
|
||||||
|
try {
|
||||||
|
List<Resource> outputFiles = handleFiles(files, jsonString);
|
||||||
|
|
||||||
|
if (outputFiles != null && outputFiles.size() == 1) {
|
||||||
|
// If there is only one file, return it directly
|
||||||
|
Resource singleFile = outputFiles.get(0);
|
||||||
|
InputStream is = singleFile.getInputStream();
|
||||||
|
byte[] bytes = new byte[(int) singleFile.contentLength()];
|
||||||
|
is.read(bytes);
|
||||||
|
is.close();
|
||||||
|
|
||||||
|
logger.info("Returning single file response...");
|
||||||
|
return WebResponseUtils.bytesToWebResponse(bytes, singleFile.getFilename(),
|
||||||
|
MediaType.APPLICATION_OCTET_STREAM);
|
||||||
|
} else if (outputFiles == null) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Create a ByteArrayOutputStream to hold the zip
|
||||||
|
ByteArrayOutputStream baos = new ByteArrayOutputStream();
|
||||||
|
ZipOutputStream zipOut = new ZipOutputStream(baos);
|
||||||
|
|
||||||
|
// Loop through each file and add it to the zip
|
||||||
|
for (Resource file : outputFiles) {
|
||||||
|
ZipEntry zipEntry = new ZipEntry(file.getFilename());
|
||||||
|
zipOut.putNextEntry(zipEntry);
|
||||||
|
|
||||||
|
// Read the file into a byte array
|
||||||
|
InputStream is = file.getInputStream();
|
||||||
|
byte[] bytes = new byte[(int) file.contentLength()];
|
||||||
|
is.read(bytes);
|
||||||
|
|
||||||
|
// Write the bytes of the file to the zip
|
||||||
|
zipOut.write(bytes, 0, bytes.length);
|
||||||
|
zipOut.closeEntry();
|
||||||
|
|
||||||
|
is.close();
|
||||||
|
}
|
||||||
|
|
||||||
|
zipOut.close();
|
||||||
|
|
||||||
|
logger.info("Returning zipped file response...");
|
||||||
|
return WebResponseUtils.boasToWebResponse(baos, "output.zip", MediaType.APPLICATION_OCTET_STREAM);
|
||||||
|
} catch (Exception e) {
|
||||||
|
logger.error("Error handling data: ", e);
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private boolean isZip(byte[] data) {
|
||||||
|
if (data == null || data.length < 4) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Check the first four bytes of the data against the standard zip magic number
|
||||||
|
return data[0] == 0x50 && data[1] == 0x4B && data[2] == 0x03 && data[3] == 0x04;
|
||||||
|
}
|
||||||
|
|
||||||
|
private List<Resource> unzip(byte[] data) throws IOException {
|
||||||
|
logger.info("Unzipping data of length: {}", data.length);
|
||||||
|
List<Resource> unzippedFiles = new ArrayList<>();
|
||||||
|
|
||||||
|
try (ByteArrayInputStream bais = new ByteArrayInputStream(data);
|
||||||
|
ZipInputStream zis = new ZipInputStream(bais)) {
|
||||||
|
|
||||||
|
ZipEntry entry;
|
||||||
|
while ((entry = zis.getNextEntry()) != null) {
|
||||||
|
ByteArrayOutputStream baos = new ByteArrayOutputStream();
|
||||||
|
byte[] buffer = new byte[1024];
|
||||||
|
int count;
|
||||||
|
|
||||||
|
while ((count = zis.read(buffer)) != -1) {
|
||||||
|
baos.write(buffer, 0, count);
|
||||||
|
}
|
||||||
|
|
||||||
|
final String filename = entry.getName();
|
||||||
|
Resource fileResource = new ByteArrayResource(baos.toByteArray()) {
|
||||||
|
@Override
|
||||||
|
public String getFilename() {
|
||||||
|
return filename;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
// If the unzipped file is a zip file, unzip it
|
||||||
|
if (isZip(baos.toByteArray())) {
|
||||||
|
logger.info("File {} is a zip file. Unzipping...", filename);
|
||||||
|
unzippedFiles.addAll(unzip(baos.toByteArray()));
|
||||||
|
} else {
|
||||||
|
unzippedFiles.add(fileResource);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
logger.info("Unzipping completed. {} files were unzipped.", unzippedFiles.size());
|
||||||
|
return unzippedFiles;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -3,292 +3,256 @@ package stirling.software.SPDF.controller.api.security;
|
|||||||
import java.io.ByteArrayInputStream;
|
import java.io.ByteArrayInputStream;
|
||||||
import java.io.ByteArrayOutputStream;
|
import java.io.ByteArrayOutputStream;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.io.InputStream;
|
|
||||||
import java.io.InputStreamReader;
|
import java.io.InputStreamReader;
|
||||||
import java.security.KeyFactory;
|
import java.security.KeyFactory;
|
||||||
import java.security.KeyStore;
|
import java.security.KeyStore;
|
||||||
import java.security.Principal;
|
|
||||||
import java.security.PrivateKey;
|
import java.security.PrivateKey;
|
||||||
import java.security.Security;
|
import java.security.Security;
|
||||||
import java.security.cert.Certificate;
|
|
||||||
import java.security.cert.CertificateFactory;
|
import java.security.cert.CertificateFactory;
|
||||||
import java.security.cert.X509Certificate;
|
import java.security.cert.X509Certificate;
|
||||||
import java.security.spec.PKCS8EncodedKeySpec;
|
import java.security.spec.PKCS8EncodedKeySpec;
|
||||||
import java.text.SimpleDateFormat;
|
import java.text.SimpleDateFormat;
|
||||||
import java.util.Arrays;
|
import java.util.Calendar;
|
||||||
|
import java.util.Collections;
|
||||||
import java.util.Date;
|
import java.util.Date;
|
||||||
import java.util.List;
|
|
||||||
|
|
||||||
|
import org.apache.commons.io.IOUtils;
|
||||||
|
import org.apache.pdfbox.pdmodel.PDDocument;
|
||||||
|
import org.apache.pdfbox.pdmodel.PDPage;
|
||||||
|
import org.apache.pdfbox.pdmodel.PDPageContentStream;
|
||||||
|
import org.apache.pdfbox.pdmodel.PDResources;
|
||||||
|
import org.apache.pdfbox.pdmodel.common.PDRectangle;
|
||||||
|
import org.apache.pdfbox.pdmodel.font.PDType1Font;
|
||||||
|
import org.apache.pdfbox.pdmodel.interactive.annotation.PDAnnotationWidget;
|
||||||
|
import org.apache.pdfbox.pdmodel.interactive.annotation.PDAppearanceDictionary;
|
||||||
|
import org.apache.pdfbox.pdmodel.interactive.annotation.PDAppearanceStream;
|
||||||
|
import org.apache.pdfbox.pdmodel.interactive.digitalsignature.ExternalSigningSupport;
|
||||||
|
import org.apache.pdfbox.pdmodel.interactive.digitalsignature.PDSignature;
|
||||||
|
import org.apache.pdfbox.pdmodel.interactive.digitalsignature.SignatureOptions;
|
||||||
|
import org.apache.pdfbox.pdmodel.interactive.form.PDAcroForm;
|
||||||
|
import org.apache.pdfbox.pdmodel.interactive.form.PDSignatureField;
|
||||||
|
import org.bouncycastle.cert.jcajce.JcaCertStore;
|
||||||
|
import org.bouncycastle.cms.CMSProcessableByteArray;
|
||||||
|
import org.bouncycastle.cms.CMSSignedData;
|
||||||
|
import org.bouncycastle.cms.CMSSignedDataGenerator;
|
||||||
|
import org.bouncycastle.cms.CMSTypedData;
|
||||||
|
import org.bouncycastle.cms.jcajce.JcaSignerInfoGeneratorBuilder;
|
||||||
import org.bouncycastle.jce.provider.BouncyCastleProvider;
|
import org.bouncycastle.jce.provider.BouncyCastleProvider;
|
||||||
|
import org.bouncycastle.operator.ContentSigner;
|
||||||
|
import org.bouncycastle.operator.jcajce.JcaContentSignerBuilder;
|
||||||
|
import org.bouncycastle.operator.jcajce.JcaDigestCalculatorProviderBuilder;
|
||||||
import org.bouncycastle.util.io.pem.PemReader;
|
import org.bouncycastle.util.io.pem.PemReader;
|
||||||
import org.slf4j.Logger;
|
import org.slf4j.Logger;
|
||||||
import org.slf4j.LoggerFactory;
|
import org.slf4j.LoggerFactory;
|
||||||
import org.springframework.http.ResponseEntity;
|
import org.springframework.http.ResponseEntity;
|
||||||
|
import org.springframework.web.bind.annotation.ModelAttribute;
|
||||||
import org.springframework.web.bind.annotation.PostMapping;
|
import org.springframework.web.bind.annotation.PostMapping;
|
||||||
import org.springframework.web.bind.annotation.RequestParam;
|
import org.springframework.web.bind.annotation.RequestMapping;
|
||||||
import org.springframework.web.bind.annotation.RequestPart;
|
|
||||||
import org.springframework.web.bind.annotation.RestController;
|
import org.springframework.web.bind.annotation.RestController;
|
||||||
import org.springframework.web.multipart.MultipartFile;
|
import org.springframework.web.multipart.MultipartFile;
|
||||||
|
|
||||||
import com.itextpdf.io.font.constants.StandardFonts;
|
|
||||||
import com.itextpdf.kernel.font.PdfFont;
|
|
||||||
import com.itextpdf.kernel.font.PdfFontFactory;
|
|
||||||
import com.itextpdf.kernel.geom.Rectangle;
|
|
||||||
import com.itextpdf.kernel.pdf.PdfDocument;
|
|
||||||
import com.itextpdf.kernel.pdf.PdfPage;
|
|
||||||
import com.itextpdf.kernel.pdf.PdfReader;
|
|
||||||
import com.itextpdf.kernel.pdf.StampingProperties;
|
|
||||||
import com.itextpdf.signatures.BouncyCastleDigest;
|
|
||||||
import com.itextpdf.signatures.DigestAlgorithms;
|
|
||||||
import com.itextpdf.signatures.IExternalDigest;
|
|
||||||
import com.itextpdf.signatures.IExternalSignature;
|
|
||||||
import com.itextpdf.signatures.PdfPKCS7;
|
|
||||||
import com.itextpdf.signatures.PdfSignatureAppearance;
|
|
||||||
import com.itextpdf.signatures.PdfSigner;
|
|
||||||
import com.itextpdf.signatures.PrivateKeySignature;
|
|
||||||
import com.itextpdf.signatures.SignatureUtil;
|
|
||||||
|
|
||||||
import io.swagger.v3.oas.annotations.Operation;
|
import io.swagger.v3.oas.annotations.Operation;
|
||||||
import io.swagger.v3.oas.annotations.Parameter;
|
import io.swagger.v3.oas.annotations.tags.Tag;
|
||||||
import io.swagger.v3.oas.annotations.media.Schema;
|
import stirling.software.SPDF.model.api.security.SignPDFWithCertRequest;
|
||||||
import stirling.software.SPDF.utils.WebResponseUtils;
|
import stirling.software.SPDF.utils.WebResponseUtils;
|
||||||
|
|
||||||
@RestController
|
@RestController
|
||||||
|
@RequestMapping("/api/v1/security")
|
||||||
|
@Tag(name = "Security", description = "Security APIs")
|
||||||
public class CertSignController {
|
public class CertSignController {
|
||||||
|
|
||||||
private static final Logger logger = LoggerFactory.getLogger(CertSignController.class);
|
private static final Logger logger = LoggerFactory.getLogger(CertSignController.class);
|
||||||
|
|
||||||
static {
|
static {
|
||||||
Security.addProvider(new BouncyCastleProvider());
|
Security.addProvider(new BouncyCastleProvider());
|
||||||
}
|
}
|
||||||
|
|
||||||
@PostMapping(consumes = "multipart/form-data", value = "/cert-sign")
|
@PostMapping(consumes = "multipart/form-data", value = "/cert-sign")
|
||||||
@Operation(summary = "Sign PDF with a Digital Certificate",
|
@Operation(summary = "Sign PDF with a Digital Certificate", description = "This endpoint accepts a PDF file, a digital certificate and related information to sign the PDF. It then returns the digitally signed PDF file. Input:PDF Output:PDF Type:MF-SISO")
|
||||||
description = "This endpoint accepts a PDF file, a digital certificate and related information to sign the PDF. It then returns the digitally signed PDF file.")
|
public ResponseEntity<byte[]> signPDFWithCert(@ModelAttribute SignPDFWithCertRequest request) throws Exception {
|
||||||
public ResponseEntity<byte[]> signPDF(
|
MultipartFile pdf = request.getFileInput();
|
||||||
@RequestPart(required = true, value = "fileInput")
|
String certType = request.getCertType();
|
||||||
@Parameter(description = "The input PDF file to be signed")
|
MultipartFile privateKeyFile = request.getPrivateKeyFile();
|
||||||
MultipartFile pdf,
|
MultipartFile certFile = request.getCertFile();
|
||||||
|
MultipartFile p12File = request.getP12File();
|
||||||
|
String password = request.getPassword();
|
||||||
|
Boolean showSignature = request.isShowSignature();
|
||||||
|
String reason = request.getReason();
|
||||||
|
String location = request.getLocation();
|
||||||
|
String name = request.getName();
|
||||||
|
Integer pageNumber = request.getPageNumber();
|
||||||
|
|
||||||
@RequestParam(value = "certType", required = false)
|
PrivateKey privateKey = null;
|
||||||
@Parameter(description = "The type of the digital certificate", schema = @Schema(allowableValues = {"PKCS12", "PEM"}))
|
X509Certificate cert = null;
|
||||||
String certType,
|
|
||||||
|
|
||||||
@RequestParam(value = "key", required = false)
|
if (certType != null) {
|
||||||
@Parameter(description = "The private key for the digital certificate (required for PEM type certificates)")
|
logger.info("Cert type provided: {}", certType);
|
||||||
MultipartFile privateKeyFile,
|
switch (certType) {
|
||||||
|
case "PKCS12":
|
||||||
|
if (p12File != null) {
|
||||||
|
KeyStore ks = KeyStore.getInstance("PKCS12");
|
||||||
|
ks.load(new ByteArrayInputStream(p12File.getBytes()), password.toCharArray());
|
||||||
|
String alias = ks.aliases().nextElement();
|
||||||
|
if (!ks.isKeyEntry(alias)) {
|
||||||
|
throw new IllegalArgumentException("The provided PKCS12 file does not contain a private key.");
|
||||||
|
}
|
||||||
|
privateKey = (PrivateKey) ks.getKey(alias, password.toCharArray());
|
||||||
|
cert = (X509Certificate) ks.getCertificate(alias);
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
case "PEM":
|
||||||
|
if (privateKeyFile != null && certFile != null) {
|
||||||
|
// Load private key
|
||||||
|
KeyFactory keyFactory = KeyFactory.getInstance("RSA", BouncyCastleProvider.PROVIDER_NAME);
|
||||||
|
if (isPEM(privateKeyFile.getBytes())) {
|
||||||
|
privateKey = keyFactory
|
||||||
|
.generatePrivate(new PKCS8EncodedKeySpec(parsePEM(privateKeyFile.getBytes())));
|
||||||
|
} else {
|
||||||
|
privateKey = keyFactory.generatePrivate(new PKCS8EncodedKeySpec(privateKeyFile.getBytes()));
|
||||||
|
}
|
||||||
|
|
||||||
@RequestParam(value = "cert", required = false)
|
// Load certificate
|
||||||
@Parameter(description = "The digital certificate (required for PEM type certificates)")
|
CertificateFactory certFactory = CertificateFactory.getInstance("X.509",
|
||||||
MultipartFile certFile,
|
BouncyCastleProvider.PROVIDER_NAME);
|
||||||
|
if (isPEM(certFile.getBytes())) {
|
||||||
|
cert = (X509Certificate) certFactory
|
||||||
|
.generateCertificate(new ByteArrayInputStream(parsePEM(certFile.getBytes())));
|
||||||
|
} else {
|
||||||
|
cert = (X509Certificate) certFactory
|
||||||
|
.generateCertificate(new ByteArrayInputStream(certFile.getBytes()));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
PDSignature signature = new PDSignature();
|
||||||
|
signature.setFilter(PDSignature.FILTER_ADOBE_PPKLITE); // default filter
|
||||||
|
signature.setSubFilter(PDSignature.SUBFILTER_ADBE_PKCS7_SHA1);
|
||||||
|
signature.setName(name);
|
||||||
|
signature.setLocation(location);
|
||||||
|
signature.setReason(reason);
|
||||||
|
signature.setSignDate(Calendar.getInstance());
|
||||||
|
|
||||||
|
// Load the PDF
|
||||||
|
try (PDDocument document = PDDocument.load(pdf.getBytes())) {
|
||||||
|
logger.info("Successfully loaded the provided PDF");
|
||||||
|
SignatureOptions signatureOptions = new SignatureOptions();
|
||||||
|
|
||||||
@RequestParam(value = "p12", required = false)
|
// If you want to show the signature
|
||||||
@Parameter(description = "The PKCS12 keystore file (required for PKCS12 type certificates)")
|
|
||||||
MultipartFile p12File,
|
|
||||||
|
|
||||||
@RequestParam(value = "password", required = false)
|
// ATTEMPT 2
|
||||||
@Parameter(description = "The password for the keystore or the private key")
|
if (showSignature != null && showSignature) {
|
||||||
String password,
|
PDPage page = document.getPage(pageNumber - 1);
|
||||||
|
|
||||||
@RequestParam(value = "showSignature", required = false)
|
PDAcroForm acroForm = document.getDocumentCatalog().getAcroForm();
|
||||||
@Parameter(description = "Whether to visually show the signature in the PDF file")
|
if (acroForm == null) {
|
||||||
Boolean showSignature,
|
acroForm = new PDAcroForm(document);
|
||||||
|
document.getDocumentCatalog().setAcroForm(acroForm);
|
||||||
|
}
|
||||||
|
|
||||||
@RequestParam(value = "reason", required = false)
|
// Create a new signature field and widget
|
||||||
@Parameter(description = "The reason for signing the PDF")
|
|
||||||
String reason,
|
|
||||||
|
|
||||||
@RequestParam(value = "location", required = false)
|
PDSignatureField signatureField = new PDSignatureField(acroForm);
|
||||||
@Parameter(description = "The location where the PDF is signed")
|
PDAnnotationWidget widget = signatureField.getWidgets().get(0);
|
||||||
String location,
|
PDRectangle rect = new PDRectangle(100, 100, 200, 50); // Define the rectangle size here
|
||||||
|
widget.setRectangle(rect);
|
||||||
|
page.getAnnotations().add(widget);
|
||||||
|
|
||||||
@RequestParam(value = "name", required = false)
|
// Set the appearance for the signature field
|
||||||
@Parameter(description = "The name of the signer")
|
PDAppearanceDictionary appearanceDict = new PDAppearanceDictionary();
|
||||||
String name,
|
PDAppearanceStream appearanceStream = new PDAppearanceStream(document);
|
||||||
|
appearanceStream.setResources(new PDResources());
|
||||||
|
appearanceStream.setBBox(rect);
|
||||||
|
appearanceDict.setNormalAppearance(appearanceStream);
|
||||||
|
widget.setAppearance(appearanceDict);
|
||||||
|
|
||||||
@RequestParam(value = "pageNumber", required = false)
|
try (PDPageContentStream contentStream = new PDPageContentStream(document, appearanceStream)) {
|
||||||
@Parameter(description = "The page number where the signature should be visible. This is required if showSignature is set to true")
|
contentStream.beginText();
|
||||||
Integer pageNumber) throws Exception {
|
contentStream.setFont(PDType1Font.HELVETICA_BOLD, 12);
|
||||||
|
contentStream.newLineAtOffset(110, 130);
|
||||||
BouncyCastleProvider provider = new BouncyCastleProvider();
|
contentStream.showText("Digitally signed by: " + (name != null ? name : "Unknown"));
|
||||||
Security.addProvider(provider);
|
contentStream.newLineAtOffset(0, -15);
|
||||||
|
contentStream.showText("Date: " + new SimpleDateFormat("yyyy.MM.dd HH:mm:ss z").format(new Date()));
|
||||||
|
contentStream.newLineAtOffset(0, -15);
|
||||||
|
if (reason != null && !reason.isEmpty()) {
|
||||||
|
contentStream.showText("Reason: " + reason);
|
||||||
|
contentStream.newLineAtOffset(0, -15);
|
||||||
|
}
|
||||||
|
if (location != null && !location.isEmpty()) {
|
||||||
|
contentStream.showText("Location: " + location);
|
||||||
|
contentStream.newLineAtOffset(0, -15);
|
||||||
|
}
|
||||||
|
contentStream.endText();
|
||||||
|
}
|
||||||
|
|
||||||
PrivateKey privateKey = null;
|
// Add the widget annotation to the page
|
||||||
X509Certificate cert = null;
|
page.getAnnotations().add(widget);
|
||||||
|
|
||||||
if (certType != null) {
|
|
||||||
switch (certType) {
|
|
||||||
case "PKCS12":
|
|
||||||
if (p12File != null) {
|
|
||||||
KeyStore ks = KeyStore.getInstance("PKCS12");
|
|
||||||
ks.load(new ByteArrayInputStream(p12File.getBytes()), password.toCharArray());
|
|
||||||
String alias = ks.aliases().nextElement();
|
|
||||||
privateKey = (PrivateKey) ks.getKey(alias, password.toCharArray());
|
|
||||||
cert = (X509Certificate) ks.getCertificate(alias);
|
|
||||||
}
|
|
||||||
break;
|
|
||||||
case "PEM":
|
|
||||||
if (privateKeyFile != null && certFile != null) {
|
|
||||||
// Load private key
|
|
||||||
KeyFactory keyFactory = KeyFactory.getInstance("RSA", provider);
|
|
||||||
if (isPEM(privateKeyFile.getBytes())) {
|
|
||||||
privateKey = keyFactory.generatePrivate(new PKCS8EncodedKeySpec(parsePEM(privateKeyFile.getBytes())));
|
|
||||||
} else {
|
|
||||||
privateKey = keyFactory.generatePrivate(new PKCS8EncodedKeySpec(privateKeyFile.getBytes()));
|
|
||||||
}
|
|
||||||
|
|
||||||
// Load certificate
|
// Add the signature field to the acroform
|
||||||
CertificateFactory certFactory = CertificateFactory.getInstance("X.509", provider);
|
acroForm.getFields().add(signatureField);
|
||||||
if (isPEM(certFile.getBytes())) {
|
|
||||||
cert = (X509Certificate) certFactory.generateCertificate(new ByteArrayInputStream(parsePEM(certFile.getBytes())));
|
|
||||||
} else {
|
|
||||||
cert = (X509Certificate) certFactory.generateCertificate(new ByteArrayInputStream(certFile.getBytes()));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
Principal principal = cert.getSubjectDN();
|
// Handle multiple signatures by ensuring a unique field name
|
||||||
String dn = principal.getName();
|
String baseFieldName = "Signature";
|
||||||
|
String signatureFieldName = baseFieldName;
|
||||||
|
int suffix = 1;
|
||||||
|
while (acroForm.getField(signatureFieldName) != null) {
|
||||||
|
suffix++;
|
||||||
|
signatureFieldName = baseFieldName + suffix;
|
||||||
|
}
|
||||||
|
signatureField.setPartialName(signatureFieldName);
|
||||||
|
}
|
||||||
|
|
||||||
|
document.addSignature(signature, signatureOptions);
|
||||||
|
logger.info("Signature added to the PDF document");
|
||||||
|
// External signing
|
||||||
|
ExternalSigningSupport externalSigning = document
|
||||||
|
.saveIncrementalForExternalSigning(new ByteArrayOutputStream());
|
||||||
|
|
||||||
// Extract the "CN" (Common Name) field from the distinguished name (if it's present)
|
byte[] content = IOUtils.toByteArray(externalSigning.getContent());
|
||||||
String cn = null;
|
|
||||||
for (String part : dn.split(",")) {
|
|
||||||
if (part.trim().startsWith("CN=")) {
|
|
||||||
cn = part.trim().substring("CN=".length());
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Set up the PDF reader and stamper
|
|
||||||
PdfReader reader = new PdfReader(new ByteArrayInputStream(pdf.getBytes()));
|
|
||||||
ByteArrayOutputStream signedPdf = new ByteArrayOutputStream();
|
|
||||||
PdfSigner signer = new PdfSigner(reader, signedPdf, new StampingProperties());
|
|
||||||
|
|
||||||
// Set up the signing appearance
|
// Using BouncyCastle to sign
|
||||||
PdfSignatureAppearance appearance = signer.getSignatureAppearance()
|
CMSTypedData cmsData = new CMSProcessableByteArray(content);
|
||||||
.setReason("Test")
|
|
||||||
.setLocation("TestLocation");
|
|
||||||
|
|
||||||
if (showSignature != null && showSignature) {
|
CMSSignedDataGenerator gen = new CMSSignedDataGenerator();
|
||||||
float fontSize = 4; // the font size of the signature
|
ContentSigner signer = new JcaContentSignerBuilder("SHA256withRSA")
|
||||||
float marginRight = 36; // Margin from the right
|
.setProvider(BouncyCastleProvider.PROVIDER_NAME).build(privateKey);
|
||||||
float marginBottom = 36; // Margin from the bottom
|
|
||||||
String signingDate = new SimpleDateFormat("yyyy.MM.dd HH:mm:ss z").format(new Date());
|
|
||||||
|
|
||||||
// Prepare the text for the digital signature
|
gen.addSignerInfoGenerator(new JcaSignerInfoGeneratorBuilder(
|
||||||
StringBuilder layer2TextBuilder = new StringBuilder(String.format("Digitally signed by: %s\nDate: %s",
|
new JcaDigestCalculatorProviderBuilder().setProvider(BouncyCastleProvider.PROVIDER_NAME).build())
|
||||||
name != null ? name : "Unknown", signingDate));
|
.build(signer, cert));
|
||||||
|
|
||||||
if (reason != null && !reason.isEmpty()) {
|
gen.addCertificates(new JcaCertStore(Collections.singletonList(cert)));
|
||||||
layer2TextBuilder.append("\nReason: ").append(reason);
|
CMSSignedData signedData = gen.generate(cmsData, false);
|
||||||
}
|
|
||||||
|
|
||||||
if (location != null && !location.isEmpty()) {
|
byte[] cmsSignature = signedData.getEncoded();
|
||||||
layer2TextBuilder.append("\nLocation: ").append(location);
|
logger.info("About to sign content using BouncyCastle");
|
||||||
}
|
externalSigning.setSignature(cmsSignature);
|
||||||
String layer2Text = layer2TextBuilder.toString();
|
logger.info("Signature set successfully");
|
||||||
// Get the PDF font and measure the width and height of the text block
|
|
||||||
PdfFont font = PdfFontFactory.createFont(StandardFonts.HELVETICA_BOLD);
|
|
||||||
float textWidth = Arrays.stream(layer2Text.split("\n"))
|
|
||||||
.map(line -> font.getWidth(line, fontSize))
|
|
||||||
.max(Float::compare)
|
|
||||||
.orElse(0f);
|
|
||||||
int numLines = layer2Text.split("\n").length;
|
|
||||||
float textHeight = numLines * fontSize;
|
|
||||||
|
|
||||||
// Calculate the signature rectangle size
|
// After setting the signature, return the resultant PDF
|
||||||
float sigWidth = textWidth + marginRight * 2;
|
try (ByteArrayOutputStream signedPdfOutput = new ByteArrayOutputStream()) {
|
||||||
float sigHeight = textHeight + marginBottom * 2;
|
document.save(signedPdfOutput);
|
||||||
|
return WebResponseUtils.boasToWebResponse(signedPdfOutput,
|
||||||
|
pdf.getOriginalFilename().replaceFirst("[.][^.]+$", "") + "_signed.pdf");
|
||||||
|
|
||||||
// Get the page size
|
} catch (Exception e) {
|
||||||
PdfPage page = signer.getDocument().getPage(1);
|
e.printStackTrace();
|
||||||
Rectangle pageSize = page.getPageSize();
|
}
|
||||||
|
} catch (Exception e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
|
||||||
// Define the position and dimension of the signature field
|
return null;
|
||||||
Rectangle rect = new Rectangle(
|
}
|
||||||
pageSize.getRight() - sigWidth - marginRight,
|
|
||||||
pageSize.getBottom() + marginBottom,
|
|
||||||
sigWidth,
|
|
||||||
sigHeight
|
|
||||||
);
|
|
||||||
|
|
||||||
// Configure the appearance of the digital signature
|
private byte[] parsePEM(byte[] content) throws IOException {
|
||||||
appearance.setPageRect(rect)
|
PemReader pemReader = new PemReader(new InputStreamReader(new ByteArrayInputStream(content)));
|
||||||
.setContact(name != null ? name : "")
|
return pemReader.readPemObject().getContent();
|
||||||
.setPageNumber(pageNumber)
|
}
|
||||||
.setReason(reason != null ? reason : "")
|
|
||||||
.setLocation(location != null ? location : "")
|
|
||||||
.setReuseAppearance(false)
|
|
||||||
.setLayer2Text(layer2Text.toString());
|
|
||||||
|
|
||||||
signer.setFieldName("sig");
|
|
||||||
} else {
|
|
||||||
appearance.setRenderingMode(PdfSignatureAppearance.RenderingMode.DESCRIPTION);
|
|
||||||
}
|
|
||||||
|
|
||||||
// Set up the signer
|
|
||||||
PrivateKeySignature pks = new PrivateKeySignature(privateKey, DigestAlgorithms.SHA256, provider.getName());
|
|
||||||
IExternalSignature pss = new PrivateKeySignature(privateKey, DigestAlgorithms.SHA256, provider.getName());
|
|
||||||
IExternalDigest digest = new BouncyCastleDigest();
|
|
||||||
|
|
||||||
// Call iTex7 to sign the PDF
|
|
||||||
signer.signDetached(digest, pks, new Certificate[] {cert}, null, null, null, 0, PdfSigner.CryptoStandard.CMS);
|
|
||||||
|
|
||||||
|
|
||||||
System.out.println("Signed PDF size: " + signedPdf.size());
|
|
||||||
|
|
||||||
System.out.println("PDF signed = " + isPdfSigned(signedPdf.toByteArray()));
|
|
||||||
return WebResponseUtils.bytesToWebResponse(signedPdf.toByteArray(), "example.pdf");
|
|
||||||
}
|
|
||||||
|
|
||||||
public boolean isPdfSigned(byte[] pdfData) throws IOException {
|
|
||||||
InputStream pdfStream = new ByteArrayInputStream(pdfData);
|
|
||||||
PdfDocument pdfDoc = new PdfDocument(new PdfReader(pdfStream));
|
|
||||||
SignatureUtil signatureUtil = new SignatureUtil(pdfDoc);
|
|
||||||
List<String> names = signatureUtil.getSignatureNames();
|
|
||||||
|
|
||||||
boolean isSigned = false;
|
|
||||||
|
|
||||||
for (String name : names) {
|
|
||||||
PdfPKCS7 pkcs7 = signatureUtil.readSignatureData(name);
|
|
||||||
if (pkcs7 != null) {
|
|
||||||
System.out.println("Signature found.");
|
|
||||||
|
|
||||||
// Log certificate details
|
|
||||||
Certificate[] signChain = pkcs7.getSignCertificateChain();
|
|
||||||
for (Certificate cert : signChain) {
|
|
||||||
if (cert instanceof X509Certificate) {
|
|
||||||
X509Certificate x509 = (X509Certificate) cert;
|
|
||||||
System.out.println("Certificate Details:");
|
|
||||||
System.out.println("Subject: " + x509.getSubjectDN());
|
|
||||||
System.out.println("Issuer: " + x509.getIssuerDN());
|
|
||||||
System.out.println("Serial: " + x509.getSerialNumber());
|
|
||||||
System.out.println("Not Before: " + x509.getNotBefore());
|
|
||||||
System.out.println("Not After: " + x509.getNotAfter());
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
isSigned = true;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
pdfDoc.close();
|
|
||||||
|
|
||||||
return isSigned;
|
|
||||||
}
|
|
||||||
private byte[] parsePEM(byte[] content) throws IOException {
|
|
||||||
PemReader pemReader = new PemReader(new InputStreamReader(new ByteArrayInputStream(content)));
|
|
||||||
return pemReader.readPemObject().getContent();
|
|
||||||
}
|
|
||||||
|
|
||||||
private boolean isPEM(byte[] content) {
|
|
||||||
String contentStr = new String(content);
|
|
||||||
return contentStr.contains("-----BEGIN") && contentStr.contains("-----END");
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
private boolean isPEM(byte[] content) {
|
||||||
|
String contentStr = new String(content);
|
||||||
|
return contentStr.contains("-----BEGIN") && contentStr.contains("-----END");
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,807 @@
|
|||||||
|
package stirling.software.SPDF.controller.api.security;
|
||||||
|
|
||||||
|
import java.io.ByteArrayOutputStream;
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.nio.charset.StandardCharsets;
|
||||||
|
import java.text.SimpleDateFormat;
|
||||||
|
import java.util.Calendar;
|
||||||
|
import java.util.HashMap;
|
||||||
|
import java.util.HashSet;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Map;
|
||||||
|
import java.util.Set;
|
||||||
|
|
||||||
|
import org.apache.pdfbox.cos.COSDocument;
|
||||||
|
import org.apache.pdfbox.cos.COSInputStream;
|
||||||
|
import org.apache.pdfbox.cos.COSName;
|
||||||
|
import org.apache.pdfbox.cos.COSObject;
|
||||||
|
import org.apache.pdfbox.cos.COSStream;
|
||||||
|
import org.apache.pdfbox.cos.COSString;
|
||||||
|
import org.apache.pdfbox.pdmodel.PDDocument;
|
||||||
|
import org.apache.pdfbox.pdmodel.PDDocumentCatalog;
|
||||||
|
import org.apache.pdfbox.pdmodel.PDDocumentInformation;
|
||||||
|
import org.apache.pdfbox.pdmodel.PDDocumentNameDictionary;
|
||||||
|
import org.apache.pdfbox.pdmodel.PDEmbeddedFilesNameTreeNode;
|
||||||
|
import org.apache.pdfbox.pdmodel.PDJavascriptNameTreeNode;
|
||||||
|
import org.apache.pdfbox.pdmodel.PDPage;
|
||||||
|
import org.apache.pdfbox.pdmodel.PDResources;
|
||||||
|
import org.apache.pdfbox.pdmodel.common.PDMetadata;
|
||||||
|
import org.apache.pdfbox.pdmodel.common.PDRectangle;
|
||||||
|
import org.apache.pdfbox.pdmodel.common.PDStream;
|
||||||
|
import org.apache.pdfbox.pdmodel.common.filespecification.PDComplexFileSpecification;
|
||||||
|
import org.apache.pdfbox.pdmodel.common.filespecification.PDEmbeddedFile;
|
||||||
|
import org.apache.pdfbox.pdmodel.documentinterchange.logicalstructure.PDStructureElement;
|
||||||
|
import org.apache.pdfbox.pdmodel.documentinterchange.logicalstructure.PDStructureNode;
|
||||||
|
import org.apache.pdfbox.pdmodel.documentinterchange.logicalstructure.PDStructureTreeRoot;
|
||||||
|
import org.apache.pdfbox.pdmodel.encryption.AccessPermission;
|
||||||
|
import org.apache.pdfbox.pdmodel.encryption.PDEncryption;
|
||||||
|
import org.apache.pdfbox.pdmodel.font.PDFont;
|
||||||
|
import org.apache.pdfbox.pdmodel.font.PDFontDescriptor;
|
||||||
|
import org.apache.pdfbox.pdmodel.graphics.PDXObject;
|
||||||
|
import org.apache.pdfbox.pdmodel.graphics.color.PDColorSpace;
|
||||||
|
import org.apache.pdfbox.pdmodel.graphics.color.PDICCBased;
|
||||||
|
import org.apache.pdfbox.pdmodel.graphics.form.PDFormXObject;
|
||||||
|
import org.apache.pdfbox.pdmodel.graphics.image.PDImageXObject;
|
||||||
|
import org.apache.pdfbox.pdmodel.graphics.optionalcontent.PDOptionalContentGroup;
|
||||||
|
import org.apache.pdfbox.pdmodel.graphics.optionalcontent.PDOptionalContentProperties;
|
||||||
|
import org.apache.pdfbox.pdmodel.interactive.action.PDActionJavaScript;
|
||||||
|
import org.apache.pdfbox.pdmodel.interactive.action.PDActionURI;
|
||||||
|
import org.apache.pdfbox.pdmodel.interactive.annotation.PDAnnotation;
|
||||||
|
import org.apache.pdfbox.pdmodel.interactive.annotation.PDAnnotationFileAttachment;
|
||||||
|
import org.apache.pdfbox.pdmodel.interactive.annotation.PDAnnotationLink;
|
||||||
|
import org.apache.pdfbox.pdmodel.interactive.documentnavigation.outline.PDOutlineItem;
|
||||||
|
import org.apache.pdfbox.pdmodel.interactive.documentnavigation.outline.PDOutlineNode;
|
||||||
|
import org.apache.pdfbox.pdmodel.interactive.form.PDAcroForm;
|
||||||
|
import org.apache.pdfbox.pdmodel.interactive.form.PDField;
|
||||||
|
import org.apache.pdfbox.text.PDFTextStripper;
|
||||||
|
import org.apache.xmpbox.XMPMetadata;
|
||||||
|
import org.apache.xmpbox.xml.DomXmpParser;
|
||||||
|
import org.apache.xmpbox.xml.XmpParsingException;
|
||||||
|
import org.apache.xmpbox.xml.XmpSerializer;
|
||||||
|
import org.springframework.http.MediaType;
|
||||||
|
import org.springframework.http.ResponseEntity;
|
||||||
|
import org.springframework.web.bind.annotation.ModelAttribute;
|
||||||
|
import org.springframework.web.bind.annotation.PostMapping;
|
||||||
|
import org.springframework.web.bind.annotation.RequestMapping;
|
||||||
|
import org.springframework.web.bind.annotation.RestController;
|
||||||
|
import org.springframework.web.multipart.MultipartFile;
|
||||||
|
|
||||||
|
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||||
|
import com.fasterxml.jackson.databind.node.ArrayNode;
|
||||||
|
import com.fasterxml.jackson.databind.node.ObjectNode;
|
||||||
|
|
||||||
|
import io.swagger.v3.oas.annotations.Operation;
|
||||||
|
import io.swagger.v3.oas.annotations.tags.Tag;
|
||||||
|
import stirling.software.SPDF.model.api.PDFFile;
|
||||||
|
import stirling.software.SPDF.utils.WebResponseUtils;
|
||||||
|
@RestController
|
||||||
|
@RequestMapping("/api/v1/security")
|
||||||
|
@Tag(name = "Security", description = "Security APIs")
|
||||||
|
public class GetInfoOnPDF {
|
||||||
|
|
||||||
|
static ObjectMapper objectMapper = new ObjectMapper();
|
||||||
|
|
||||||
|
@PostMapping(consumes = "multipart/form-data", value = "/get-info-on-pdf")
|
||||||
|
@Operation(summary = "Summary here", description = "desc. Input:PDF Output:JSON Type:SISO")
|
||||||
|
public ResponseEntity<byte[]> getPdfInfo(@ModelAttribute PDFFile request)
|
||||||
|
throws IOException {
|
||||||
|
MultipartFile inputFile = request.getFileInput();
|
||||||
|
try (
|
||||||
|
PDDocument pdfBoxDoc = PDDocument.load(inputFile.getInputStream());
|
||||||
|
) {
|
||||||
|
ObjectMapper objectMapper = new ObjectMapper();
|
||||||
|
ObjectNode jsonOutput = objectMapper.createObjectNode();
|
||||||
|
|
||||||
|
// Metadata using PDFBox
|
||||||
|
PDDocumentInformation info = pdfBoxDoc.getDocumentInformation();
|
||||||
|
ObjectNode metadata = objectMapper.createObjectNode();
|
||||||
|
ObjectNode basicInfo = objectMapper.createObjectNode();
|
||||||
|
ObjectNode docInfoNode = objectMapper.createObjectNode();
|
||||||
|
ObjectNode compliancy = objectMapper.createObjectNode();
|
||||||
|
ObjectNode encryption = objectMapper.createObjectNode();
|
||||||
|
ObjectNode other = objectMapper.createObjectNode();
|
||||||
|
|
||||||
|
|
||||||
|
metadata.put("Title", info.getTitle());
|
||||||
|
metadata.put("Author", info.getAuthor());
|
||||||
|
metadata.put("Subject", info.getSubject());
|
||||||
|
metadata.put("Keywords", info.getKeywords());
|
||||||
|
metadata.put("Producer", info.getProducer());
|
||||||
|
metadata.put("Creator", info.getCreator());
|
||||||
|
metadata.put("CreationDate", formatDate(info.getCreationDate()));
|
||||||
|
metadata.put("ModificationDate", formatDate(info.getModificationDate()));
|
||||||
|
jsonOutput.set("Metadata", metadata);
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
// Total file size of the PDF
|
||||||
|
long fileSizeInBytes = inputFile.getSize();
|
||||||
|
basicInfo.put("FileSizeInBytes", fileSizeInBytes);
|
||||||
|
|
||||||
|
// Number of words, paragraphs, and images in the entire document
|
||||||
|
String fullText = new PDFTextStripper().getText(pdfBoxDoc);
|
||||||
|
String[] words = fullText.split("\\s+");
|
||||||
|
int wordCount = words.length;
|
||||||
|
int paragraphCount = fullText.split("\r\n|\r|\n").length;
|
||||||
|
basicInfo.put("WordCount", wordCount);
|
||||||
|
basicInfo.put("ParagraphCount", paragraphCount);
|
||||||
|
// Number of characters in the entire document (including spaces and special characters)
|
||||||
|
int charCount = fullText.length();
|
||||||
|
basicInfo.put("CharacterCount", charCount);
|
||||||
|
|
||||||
|
|
||||||
|
// Initialize the flags and types
|
||||||
|
boolean hasCompression = false;
|
||||||
|
String compressionType = "None";
|
||||||
|
|
||||||
|
COSDocument cosDoc = pdfBoxDoc.getDocument();
|
||||||
|
for (COSObject cosObject : cosDoc.getObjects()) {
|
||||||
|
if (cosObject.getObject() instanceof COSStream) {
|
||||||
|
COSStream cosStream = (COSStream) cosObject.getObject();
|
||||||
|
if (COSName.OBJ_STM.equals(cosStream.getItem(COSName.TYPE))) {
|
||||||
|
hasCompression = true;
|
||||||
|
compressionType = "Object Streams";
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
basicInfo.put("Compression", hasCompression);
|
||||||
|
if(hasCompression)
|
||||||
|
basicInfo.put("CompressionType", compressionType);
|
||||||
|
|
||||||
|
String language = pdfBoxDoc.getDocumentCatalog().getLanguage();
|
||||||
|
basicInfo.put("Language", language);
|
||||||
|
basicInfo.put("Number of pages", pdfBoxDoc.getNumberOfPages());
|
||||||
|
|
||||||
|
|
||||||
|
PDDocumentCatalog catalog = pdfBoxDoc.getDocumentCatalog();
|
||||||
|
String pageMode = catalog.getPageMode().name();
|
||||||
|
|
||||||
|
// Document Information using PDFBox
|
||||||
|
docInfoNode.put("PDF version", pdfBoxDoc.getVersion());
|
||||||
|
docInfoNode.put("Trapped", info.getTrapped());
|
||||||
|
docInfoNode.put("Page Mode", getPageModeDescription(pageMode));;
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
PDAcroForm acroForm = pdfBoxDoc.getDocumentCatalog().getAcroForm();
|
||||||
|
|
||||||
|
ObjectNode formFieldsNode = objectMapper.createObjectNode();
|
||||||
|
if (acroForm != null) {
|
||||||
|
for (PDField field : acroForm.getFieldTree()) {
|
||||||
|
formFieldsNode.put(field.getFullyQualifiedName(), field.getValueAsString());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
jsonOutput.set("FormFields", formFieldsNode);
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
//embeed files TODO size
|
||||||
|
if(catalog.getNames() != null) {
|
||||||
|
PDEmbeddedFilesNameTreeNode efTree = catalog.getNames().getEmbeddedFiles();
|
||||||
|
|
||||||
|
ArrayNode embeddedFilesArray = objectMapper.createArrayNode();
|
||||||
|
if (efTree != null) {
|
||||||
|
Map<String, PDComplexFileSpecification> efMap = efTree.getNames();
|
||||||
|
if (efMap != null) {
|
||||||
|
for (Map.Entry<String, PDComplexFileSpecification> entry : efMap.entrySet()) {
|
||||||
|
ObjectNode embeddedFileNode = objectMapper.createObjectNode();
|
||||||
|
embeddedFileNode.put("Name", entry.getKey());
|
||||||
|
PDEmbeddedFile embeddedFile = entry.getValue().getEmbeddedFile();
|
||||||
|
if (embeddedFile != null) {
|
||||||
|
embeddedFileNode.put("FileSize", embeddedFile.getLength()); // size in bytes
|
||||||
|
}
|
||||||
|
embeddedFilesArray.add(embeddedFileNode);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
other.set("EmbeddedFiles", embeddedFilesArray);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
//attachments TODO size
|
||||||
|
ArrayNode attachmentsArray = objectMapper.createArrayNode();
|
||||||
|
for (PDPage page : pdfBoxDoc.getPages()) {
|
||||||
|
for (PDAnnotation annotation : page.getAnnotations()) {
|
||||||
|
if (annotation instanceof PDAnnotationFileAttachment) {
|
||||||
|
PDAnnotationFileAttachment fileAttachmentAnnotation = (PDAnnotationFileAttachment) annotation;
|
||||||
|
|
||||||
|
ObjectNode attachmentNode = objectMapper.createObjectNode();
|
||||||
|
attachmentNode.put("Name", fileAttachmentAnnotation.getAttachmentName());
|
||||||
|
attachmentNode.put("Description", fileAttachmentAnnotation.getContents());
|
||||||
|
|
||||||
|
attachmentsArray.add(attachmentNode);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
other.set("Attachments", attachmentsArray);
|
||||||
|
|
||||||
|
//Javascript
|
||||||
|
PDDocumentNameDictionary namesDict = catalog.getNames();
|
||||||
|
ArrayNode javascriptArray = objectMapper.createArrayNode();
|
||||||
|
|
||||||
|
if (namesDict != null) {
|
||||||
|
PDJavascriptNameTreeNode javascriptDict = namesDict.getJavaScript();
|
||||||
|
if (javascriptDict != null) {
|
||||||
|
try {
|
||||||
|
Map<String, PDActionJavaScript> jsEntries = javascriptDict.getNames();
|
||||||
|
|
||||||
|
for (Map.Entry<String, PDActionJavaScript> entry : jsEntries.entrySet()) {
|
||||||
|
ObjectNode jsNode = objectMapper.createObjectNode();
|
||||||
|
jsNode.put("JS Name", entry.getKey());
|
||||||
|
|
||||||
|
PDActionJavaScript jsAction = entry.getValue();
|
||||||
|
if (jsAction != null) {
|
||||||
|
String jsCodeStr = jsAction.getAction();
|
||||||
|
if (jsCodeStr != null) {
|
||||||
|
jsNode.put("JS Script Length", jsCodeStr.length());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
javascriptArray.add(jsNode);
|
||||||
|
}
|
||||||
|
} catch (IOException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
other.set("JavaScript", javascriptArray);
|
||||||
|
|
||||||
|
|
||||||
|
//TODO size
|
||||||
|
PDOptionalContentProperties ocProperties = pdfBoxDoc.getDocumentCatalog().getOCProperties();
|
||||||
|
ArrayNode layersArray = objectMapper.createArrayNode();
|
||||||
|
|
||||||
|
if (ocProperties != null) {
|
||||||
|
for (PDOptionalContentGroup ocg : ocProperties.getOptionalContentGroups()) {
|
||||||
|
ObjectNode layerNode = objectMapper.createObjectNode();
|
||||||
|
layerNode.put("Name", ocg.getName());
|
||||||
|
layersArray.add(layerNode);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
other.set("Layers", layersArray);
|
||||||
|
|
||||||
|
//TODO Security
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
PDStructureTreeRoot structureTreeRoot = pdfBoxDoc.getDocumentCatalog().getStructureTreeRoot();
|
||||||
|
ArrayNode structureTreeArray;
|
||||||
|
try {
|
||||||
|
if(structureTreeRoot != null) {
|
||||||
|
structureTreeArray = exploreStructureTree(structureTreeRoot.getKids());
|
||||||
|
other.set("StructureTree", structureTreeArray);
|
||||||
|
}
|
||||||
|
} catch (Exception e) {
|
||||||
|
// TODO Auto-generated catch block
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
boolean isPdfACompliant = checkForStandard(pdfBoxDoc, "PDF/A");
|
||||||
|
boolean isPdfXCompliant = checkForStandard(pdfBoxDoc, "PDF/X");
|
||||||
|
boolean isPdfECompliant = checkForStandard(pdfBoxDoc, "PDF/E");
|
||||||
|
boolean isPdfVTCompliant = checkForStandard(pdfBoxDoc, "PDF/VT");
|
||||||
|
boolean isPdfUACompliant = checkForStandard(pdfBoxDoc, "PDF/UA");
|
||||||
|
boolean isPdfBCompliant = checkForStandard(pdfBoxDoc, "PDF/B"); // If you want to check for PDF/Broadcast, though this isn't an official ISO standard.
|
||||||
|
boolean isPdfSECCompliant = checkForStandard(pdfBoxDoc, "PDF/SEC"); // This might not be effective since PDF/SEC was under development in 2021.
|
||||||
|
|
||||||
|
compliancy.put("IsPDF/ACompliant", isPdfACompliant);
|
||||||
|
compliancy.put("IsPDF/XCompliant", isPdfXCompliant);
|
||||||
|
compliancy.put("IsPDF/ECompliant", isPdfECompliant);
|
||||||
|
compliancy.put("IsPDF/VTCompliant", isPdfVTCompliant);
|
||||||
|
compliancy.put("IsPDF/UACompliant", isPdfUACompliant);
|
||||||
|
compliancy.put("IsPDF/BCompliant", isPdfBCompliant);
|
||||||
|
compliancy.put("IsPDF/SECCompliant", isPdfSECCompliant);
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
PDOutlineNode root = pdfBoxDoc.getDocumentCatalog().getDocumentOutline();
|
||||||
|
ArrayNode bookmarksArray = objectMapper.createArrayNode();
|
||||||
|
|
||||||
|
if (root != null) {
|
||||||
|
for (PDOutlineItem child : root.children()) {
|
||||||
|
addOutlinesToArray(child, bookmarksArray);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
other.set("Bookmarks/Outline/TOC", bookmarksArray);
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
PDMetadata pdMetadata = pdfBoxDoc.getDocumentCatalog().getMetadata();
|
||||||
|
|
||||||
|
String xmpString = null;
|
||||||
|
|
||||||
|
if (pdMetadata != null) {
|
||||||
|
try {
|
||||||
|
COSInputStream is = pdMetadata.createInputStream();
|
||||||
|
DomXmpParser domXmpParser = new DomXmpParser();
|
||||||
|
XMPMetadata xmpMeta = domXmpParser.parse(is);
|
||||||
|
|
||||||
|
ByteArrayOutputStream os = new ByteArrayOutputStream();
|
||||||
|
new XmpSerializer().serialize(xmpMeta, os, true);
|
||||||
|
xmpString = new String(os.toByteArray(), StandardCharsets.UTF_8);
|
||||||
|
} catch (XmpParsingException | IOException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
other.put("XMPMetadata", xmpString);
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
if (pdfBoxDoc.isEncrypted()) {
|
||||||
|
encryption.put("IsEncrypted", true);
|
||||||
|
|
||||||
|
// Retrieve encryption details using getEncryption()
|
||||||
|
PDEncryption pdfEncryption = pdfBoxDoc.getEncryption();
|
||||||
|
encryption.put("EncryptionAlgorithm", pdfEncryption.getFilter());
|
||||||
|
encryption.put("KeyLength", pdfEncryption.getLength());
|
||||||
|
AccessPermission ap = pdfBoxDoc.getCurrentAccessPermission();
|
||||||
|
if (ap != null) {
|
||||||
|
ObjectNode permissionsNode = objectMapper.createObjectNode();
|
||||||
|
|
||||||
|
permissionsNode.put("CanAssembleDocument", ap.canAssembleDocument());
|
||||||
|
permissionsNode.put("CanExtractContent", ap.canExtractContent());
|
||||||
|
permissionsNode.put("CanExtractForAccessibility", ap.canExtractForAccessibility());
|
||||||
|
permissionsNode.put("CanFillInForm", ap.canFillInForm());
|
||||||
|
permissionsNode.put("CanModify", ap.canModify());
|
||||||
|
permissionsNode.put("CanModifyAnnotations", ap.canModifyAnnotations());
|
||||||
|
permissionsNode.put("CanPrint", ap.canPrint());
|
||||||
|
permissionsNode.put("CanPrintDegraded", ap.canPrintDegraded());
|
||||||
|
|
||||||
|
encryption.set("Permissions", permissionsNode); // set the node under "Permissions"
|
||||||
|
}
|
||||||
|
// Add other encryption-related properties as needed
|
||||||
|
} else {
|
||||||
|
encryption.put("IsEncrypted", false);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
ObjectNode pageInfoParent = objectMapper.createObjectNode();
|
||||||
|
for (int pageNum = 0; pageNum < pdfBoxDoc.getNumberOfPages(); pageNum++) {
|
||||||
|
ObjectNode pageInfo = objectMapper.createObjectNode();
|
||||||
|
|
||||||
|
// Retrieve the page
|
||||||
|
PDPage page = pdfBoxDoc.getPage(pageNum);
|
||||||
|
|
||||||
|
// Page-level Information
|
||||||
|
PDRectangle mediaBox = page.getMediaBox();
|
||||||
|
|
||||||
|
float width = mediaBox.getWidth();
|
||||||
|
float height = mediaBox.getHeight();
|
||||||
|
|
||||||
|
ObjectNode sizeInfo = objectMapper.createObjectNode();
|
||||||
|
|
||||||
|
getDimensionInfo(sizeInfo, width, height);
|
||||||
|
|
||||||
|
sizeInfo.put("Standard Page", getPageSize(width, height));
|
||||||
|
pageInfo.set("Size", sizeInfo);
|
||||||
|
|
||||||
|
pageInfo.put("Rotation", page.getRotation());
|
||||||
|
pageInfo.put("Page Orientation", getPageOrientation(width, height));
|
||||||
|
|
||||||
|
|
||||||
|
// Boxes
|
||||||
|
pageInfo.put("MediaBox", mediaBox.toString());
|
||||||
|
|
||||||
|
// Assuming the following boxes are defined for your document; if not, you may get null values.
|
||||||
|
PDRectangle cropBox = page.getCropBox();
|
||||||
|
pageInfo.put("CropBox", cropBox == null ? "Undefined" : cropBox.toString());
|
||||||
|
|
||||||
|
PDRectangle bleedBox = page.getBleedBox();
|
||||||
|
pageInfo.put("BleedBox", bleedBox == null ? "Undefined" : bleedBox.toString());
|
||||||
|
|
||||||
|
PDRectangle trimBox = page.getTrimBox();
|
||||||
|
pageInfo.put("TrimBox", trimBox == null ? "Undefined" : trimBox.toString());
|
||||||
|
|
||||||
|
PDRectangle artBox = page.getArtBox();
|
||||||
|
pageInfo.put("ArtBox", artBox == null ? "Undefined" : artBox.toString());
|
||||||
|
|
||||||
|
// Content Extraction
|
||||||
|
PDFTextStripper textStripper = new PDFTextStripper();
|
||||||
|
textStripper.setStartPage(pageNum + 1);
|
||||||
|
textStripper.setEndPage(pageNum +1);
|
||||||
|
String pageText = textStripper.getText(pdfBoxDoc);
|
||||||
|
|
||||||
|
pageInfo.put("Text Characters Count", pageText.length()); //
|
||||||
|
|
||||||
|
// Annotations
|
||||||
|
|
||||||
|
List<PDAnnotation> annotations = page.getAnnotations();
|
||||||
|
|
||||||
|
int subtypeCount = 0;
|
||||||
|
int contentsCount = 0;
|
||||||
|
|
||||||
|
for (PDAnnotation annotation : annotations) {
|
||||||
|
if (annotation.getSubtype() != null) {
|
||||||
|
subtypeCount++; // Increase subtype count
|
||||||
|
}
|
||||||
|
if (annotation.getContents() != null) {
|
||||||
|
contentsCount++; // Increase contents count
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
ObjectNode annotationsObject = objectMapper.createObjectNode();
|
||||||
|
annotationsObject.put("AnnotationsCount", annotations.size());
|
||||||
|
annotationsObject.put("SubtypeCount", subtypeCount);
|
||||||
|
annotationsObject.put("ContentsCount", contentsCount);
|
||||||
|
pageInfo.set("Annotations", annotationsObject);
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
// Images (simplified)
|
||||||
|
// This part is non-trivial as images can be embedded in multiple ways in a PDF.
|
||||||
|
// Here is a basic structure to recognize image XObjects on a page.
|
||||||
|
ArrayNode imagesArray = objectMapper.createArrayNode();
|
||||||
|
PDResources resources = page.getResources();
|
||||||
|
|
||||||
|
|
||||||
|
for (COSName name : resources.getXObjectNames()) {
|
||||||
|
PDXObject xObject = resources.getXObject(name);
|
||||||
|
if (xObject instanceof PDImageXObject) {
|
||||||
|
PDImageXObject image = (PDImageXObject) xObject;
|
||||||
|
|
||||||
|
ObjectNode imageNode = objectMapper.createObjectNode();
|
||||||
|
imageNode.put("Width", image.getWidth());
|
||||||
|
imageNode.put("Height", image.getHeight());
|
||||||
|
if(image.getMetadata() != null && image.getMetadata().getFile() != null && image.getMetadata().getFile().getFile() != null) {
|
||||||
|
imageNode.put("Name", image.getMetadata().getFile().getFile());
|
||||||
|
}
|
||||||
|
if (image.getColorSpace() != null) {
|
||||||
|
imageNode.put("ColorSpace", image.getColorSpace().getName());
|
||||||
|
}
|
||||||
|
|
||||||
|
imagesArray.add(imageNode);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
pageInfo.set("Images", imagesArray);
|
||||||
|
|
||||||
|
|
||||||
|
// Links
|
||||||
|
ArrayNode linksArray = objectMapper.createArrayNode();
|
||||||
|
Set<String> uniqueURIs = new HashSet<>(); // To store unique URIs
|
||||||
|
|
||||||
|
for (PDAnnotation annotation : annotations) {
|
||||||
|
if (annotation instanceof PDAnnotationLink) {
|
||||||
|
PDAnnotationLink linkAnnotation = (PDAnnotationLink) annotation;
|
||||||
|
if (linkAnnotation.getAction() instanceof PDActionURI) {
|
||||||
|
PDActionURI uriAction = (PDActionURI) linkAnnotation.getAction();
|
||||||
|
String uri = uriAction.getURI();
|
||||||
|
uniqueURIs.add(uri); // Add to set to ensure uniqueness
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Add unique URIs to linksArray
|
||||||
|
for (String uri : uniqueURIs) {
|
||||||
|
ObjectNode linkNode = objectMapper.createObjectNode();
|
||||||
|
linkNode.put("URI", uri);
|
||||||
|
linksArray.add(linkNode);
|
||||||
|
}
|
||||||
|
pageInfo.set("Links", linksArray);
|
||||||
|
|
||||||
|
|
||||||
|
// Fonts
|
||||||
|
ArrayNode fontsArray = objectMapper.createArrayNode();
|
||||||
|
Map<String, ObjectNode> uniqueFontsMap = new HashMap<>();
|
||||||
|
|
||||||
|
for (COSName fontName : resources.getFontNames()) {
|
||||||
|
PDFont font = resources.getFont(fontName);
|
||||||
|
ObjectNode fontNode = objectMapper.createObjectNode();
|
||||||
|
|
||||||
|
fontNode.put("IsEmbedded", font.isEmbedded());
|
||||||
|
|
||||||
|
// PDFBox provides Font's BaseFont (i.e., the font name) directly
|
||||||
|
fontNode.put("Name", font.getName());
|
||||||
|
|
||||||
|
fontNode.put("Subtype", font.getType());
|
||||||
|
|
||||||
|
PDFontDescriptor fontDescriptor = font.getFontDescriptor();
|
||||||
|
|
||||||
|
if (fontDescriptor != null) {
|
||||||
|
fontNode.put("ItalicAngle", fontDescriptor.getItalicAngle());
|
||||||
|
int flags = fontDescriptor.getFlags();
|
||||||
|
fontNode.put("IsItalic", (flags & 1) != 0);
|
||||||
|
fontNode.put("IsBold", (flags & 64) != 0);
|
||||||
|
fontNode.put("IsFixedPitch", (flags & 2) != 0);
|
||||||
|
fontNode.put("IsSerif", (flags & 4) != 0);
|
||||||
|
fontNode.put("IsSymbolic", (flags & 8) != 0);
|
||||||
|
fontNode.put("IsScript", (flags & 16) != 0);
|
||||||
|
fontNode.put("IsNonsymbolic", (flags & 32) != 0);
|
||||||
|
|
||||||
|
fontNode.put("FontFamily", fontDescriptor.getFontFamily());
|
||||||
|
// Font stretch and BBox are not directly available in PDFBox's API, so these are omitted for simplicity
|
||||||
|
fontNode.put("FontWeight", fontDescriptor.getFontWeight());
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
// Create a unique key for this font node based on its attributes
|
||||||
|
String uniqueKey = fontNode.toString();
|
||||||
|
|
||||||
|
// Increment count if this font exists, or initialize it if new
|
||||||
|
if (uniqueFontsMap.containsKey(uniqueKey)) {
|
||||||
|
ObjectNode existingFontNode = uniqueFontsMap.get(uniqueKey);
|
||||||
|
int count = existingFontNode.get("Count").asInt() + 1;
|
||||||
|
existingFontNode.put("Count", count);
|
||||||
|
} else {
|
||||||
|
fontNode.put("Count", 1);
|
||||||
|
uniqueFontsMap.put(uniqueKey, fontNode);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Add unique font entries to fontsArray
|
||||||
|
for (ObjectNode uniqueFontNode : uniqueFontsMap.values()) {
|
||||||
|
fontsArray.add(uniqueFontNode);
|
||||||
|
}
|
||||||
|
|
||||||
|
pageInfo.set("Fonts", fontsArray);
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
// Access resources dictionary
|
||||||
|
ArrayNode colorSpacesArray = objectMapper.createArrayNode();
|
||||||
|
|
||||||
|
Iterable<COSName> colorSpaceNames = resources.getColorSpaceNames();
|
||||||
|
for (COSName name : colorSpaceNames) {
|
||||||
|
PDColorSpace colorSpace = resources.getColorSpace(name);
|
||||||
|
if (colorSpace instanceof PDICCBased) {
|
||||||
|
PDICCBased iccBased = (PDICCBased) colorSpace;
|
||||||
|
PDStream iccData = iccBased.getPDStream();
|
||||||
|
byte[] iccBytes = iccData.toByteArray();
|
||||||
|
|
||||||
|
// TODO: Further decode and analyze the ICC data if needed
|
||||||
|
ObjectNode iccProfileNode = objectMapper.createObjectNode();
|
||||||
|
iccProfileNode.put("ICC Profile Length", iccBytes.length);
|
||||||
|
colorSpacesArray.add(iccProfileNode);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
pageInfo.set("Color Spaces & ICC Profiles", colorSpacesArray);
|
||||||
|
|
||||||
|
|
||||||
|
// Other XObjects
|
||||||
|
Map<String, Integer> xObjectCountMap = new HashMap<>(); // To store the count for each type
|
||||||
|
for (COSName name : resources.getXObjectNames()) {
|
||||||
|
PDXObject xObject = resources.getXObject(name);
|
||||||
|
String xObjectType;
|
||||||
|
|
||||||
|
if (xObject instanceof PDImageXObject) {
|
||||||
|
xObjectType = "Image";
|
||||||
|
} else if (xObject instanceof PDFormXObject) {
|
||||||
|
xObjectType = "Form";
|
||||||
|
} else {
|
||||||
|
xObjectType = "Other";
|
||||||
|
}
|
||||||
|
|
||||||
|
// Increment the count for this type in the map
|
||||||
|
xObjectCountMap.put(xObjectType, xObjectCountMap.getOrDefault(xObjectType, 0) + 1);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Add the count map to pageInfo (or wherever you want to store it)
|
||||||
|
ObjectNode xObjectCountNode = objectMapper.createObjectNode();
|
||||||
|
for (Map.Entry<String, Integer> entry : xObjectCountMap.entrySet()) {
|
||||||
|
xObjectCountNode.put(entry.getKey(), entry.getValue());
|
||||||
|
}
|
||||||
|
pageInfo.set("XObjectCounts", xObjectCountNode);
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
ArrayNode multimediaArray = objectMapper.createArrayNode();
|
||||||
|
|
||||||
|
for (PDAnnotation annotation : annotations) {
|
||||||
|
if ("RichMedia".equals(annotation.getSubtype())) {
|
||||||
|
ObjectNode multimediaNode = objectMapper.createObjectNode();
|
||||||
|
// Extract details from the annotation as needed
|
||||||
|
multimediaArray.add(multimediaNode);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pageInfo.set("Multimedia", multimediaArray);
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
pageInfoParent.set("Page " + (pageNum+1), pageInfo);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
jsonOutput.set("BasicInfo", basicInfo);
|
||||||
|
jsonOutput.set("DocumentInfo", docInfoNode);
|
||||||
|
jsonOutput.set("Compliancy", compliancy);
|
||||||
|
jsonOutput.set("Encryption", encryption);
|
||||||
|
jsonOutput.set("Other", other);
|
||||||
|
jsonOutput.set("PerPageInfo", pageInfoParent);
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
// Save JSON to file
|
||||||
|
String jsonString = objectMapper.writerWithDefaultPrettyPrinter().writeValueAsString(jsonOutput);
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
return WebResponseUtils.bytesToWebResponse(jsonString.getBytes(StandardCharsets.UTF_8), "response.json", MediaType.APPLICATION_JSON);
|
||||||
|
|
||||||
|
} catch (Exception e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
private static void addOutlinesToArray(PDOutlineItem outline, ArrayNode arrayNode) {
|
||||||
|
if (outline == null) return;
|
||||||
|
|
||||||
|
ObjectNode outlineNode = objectMapper.createObjectNode();
|
||||||
|
outlineNode.put("Title", outline.getTitle());
|
||||||
|
// You can add other properties if needed
|
||||||
|
arrayNode.add(outlineNode);
|
||||||
|
|
||||||
|
PDOutlineItem child = outline.getFirstChild();
|
||||||
|
while (child != null) {
|
||||||
|
addOutlinesToArray(child, arrayNode);
|
||||||
|
child = child.getNextSibling();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getPageOrientation(double width, double height) {
|
||||||
|
if (width > height) {
|
||||||
|
return "Landscape";
|
||||||
|
} else if (height > width) {
|
||||||
|
return "Portrait";
|
||||||
|
} else {
|
||||||
|
return "Square";
|
||||||
|
}
|
||||||
|
}
|
||||||
|
public String getPageSize(float width, float height) {
|
||||||
|
// Define standard page sizes
|
||||||
|
Map<String, PDRectangle> standardSizes = new HashMap<>();
|
||||||
|
standardSizes.put("Letter", PDRectangle.LETTER);
|
||||||
|
standardSizes.put("LEGAL", PDRectangle.LEGAL);
|
||||||
|
standardSizes.put("A0", PDRectangle.A0);
|
||||||
|
standardSizes.put("A1", PDRectangle.A1);
|
||||||
|
standardSizes.put("A2", PDRectangle.A2);
|
||||||
|
standardSizes.put("A3", PDRectangle.A3);
|
||||||
|
standardSizes.put("A4", PDRectangle.A4);
|
||||||
|
standardSizes.put("A5", PDRectangle.A5);
|
||||||
|
standardSizes.put("A6", PDRectangle.A6);
|
||||||
|
|
||||||
|
for (Map.Entry<String, PDRectangle> entry : standardSizes.entrySet()) {
|
||||||
|
PDRectangle size = entry.getValue();
|
||||||
|
if (isCloseToSize(width, height, size.getWidth(), size.getHeight())) {
|
||||||
|
return entry.getKey();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return "Custom";
|
||||||
|
}
|
||||||
|
|
||||||
|
private boolean isCloseToSize(float width, float height, float standardWidth, float standardHeight) {
|
||||||
|
float tolerance = 1.0f; // You can adjust the tolerance as needed
|
||||||
|
return Math.abs(width - standardWidth) <= tolerance && Math.abs(height - standardHeight) <= tolerance;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
public ObjectNode getDimensionInfo(ObjectNode dimensionInfo, float width, float height) {
|
||||||
|
float ppi = 72; // Points Per Inch
|
||||||
|
|
||||||
|
float widthInInches = width / ppi;
|
||||||
|
float heightInInches = height / ppi;
|
||||||
|
|
||||||
|
float widthInCm = widthInInches * 2.54f;
|
||||||
|
float heightInCm = heightInInches * 2.54f;
|
||||||
|
|
||||||
|
dimensionInfo.put("Width (px)", String.format("%.2f", width));
|
||||||
|
dimensionInfo.put("Height (px)", String.format("%.2f", height));
|
||||||
|
dimensionInfo.put("Width (in)", String.format("%.2f", widthInInches));
|
||||||
|
dimensionInfo.put("Height (in)", String.format("%.2f", heightInInches));
|
||||||
|
dimensionInfo.put("Width (cm)", String.format("%.2f", widthInCm));
|
||||||
|
dimensionInfo.put("Height (cm)", String.format("%.2f", heightInCm));
|
||||||
|
return dimensionInfo;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
public static boolean checkForStandard(PDDocument document, String standardKeyword) {
|
||||||
|
// Check XMP Metadata
|
||||||
|
try {
|
||||||
|
PDMetadata pdMetadata = document.getDocumentCatalog().getMetadata();
|
||||||
|
if (pdMetadata != null) {
|
||||||
|
COSInputStream metaStream = pdMetadata.createInputStream();
|
||||||
|
DomXmpParser domXmpParser = new DomXmpParser();
|
||||||
|
XMPMetadata xmpMeta = domXmpParser.parse(metaStream);
|
||||||
|
|
||||||
|
ByteArrayOutputStream baos = new ByteArrayOutputStream();
|
||||||
|
new XmpSerializer().serialize(xmpMeta, baos, true);
|
||||||
|
String xmpString = new String(baos.toByteArray(), StandardCharsets.UTF_8);
|
||||||
|
|
||||||
|
if (xmpString.contains(standardKeyword)) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} catch (Exception e) { // Catching general exception for brevity, ideally you'd catch specific exceptions.
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
public ArrayNode exploreStructureTree(List<Object> nodes) {
|
||||||
|
ArrayNode elementsArray = objectMapper.createArrayNode();
|
||||||
|
if (nodes != null) {
|
||||||
|
for (Object obj : nodes) {
|
||||||
|
if (obj instanceof PDStructureNode) {
|
||||||
|
PDStructureNode node = (PDStructureNode) obj;
|
||||||
|
ObjectNode elementNode = objectMapper.createObjectNode();
|
||||||
|
|
||||||
|
if (node instanceof PDStructureElement) {
|
||||||
|
PDStructureElement structureElement = (PDStructureElement) node;
|
||||||
|
elementNode.put("Type", structureElement.getStructureType());
|
||||||
|
elementNode.put("Content", getContent(structureElement));
|
||||||
|
|
||||||
|
// Recursively explore child elements
|
||||||
|
ArrayNode childElements = exploreStructureTree(structureElement.getKids());
|
||||||
|
if (childElements.size() > 0) {
|
||||||
|
elementNode.set("Children", childElements);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
elementsArray.add(elementNode);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return elementsArray;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
public String getContent(PDStructureElement structureElement) {
|
||||||
|
StringBuilder contentBuilder = new StringBuilder();
|
||||||
|
|
||||||
|
for (Object item : structureElement.getKids()) {
|
||||||
|
if (item instanceof COSString) {
|
||||||
|
COSString cosString = (COSString) item;
|
||||||
|
contentBuilder.append(cosString.getString());
|
||||||
|
} else if (item instanceof PDStructureElement) {
|
||||||
|
// For simplicity, we're handling only COSString and PDStructureElement here
|
||||||
|
// but a more comprehensive method would handle other types too
|
||||||
|
contentBuilder.append(getContent((PDStructureElement) item));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return contentBuilder.toString();
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
private String formatDate(Calendar calendar) {
|
||||||
|
if (calendar != null) {
|
||||||
|
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
|
||||||
|
return sdf.format(calendar.getTime());
|
||||||
|
} else {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private String getPageModeDescription(String pageMode) {
|
||||||
|
return pageMode != null ? pageMode.toString().replaceFirst("/", "") : "Unknown";
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -8,17 +8,20 @@ import org.apache.pdfbox.pdmodel.encryption.StandardProtectionPolicy;
|
|||||||
import org.slf4j.Logger;
|
import org.slf4j.Logger;
|
||||||
import org.slf4j.LoggerFactory;
|
import org.slf4j.LoggerFactory;
|
||||||
import org.springframework.http.ResponseEntity;
|
import org.springframework.http.ResponseEntity;
|
||||||
|
import org.springframework.web.bind.annotation.ModelAttribute;
|
||||||
import org.springframework.web.bind.annotation.PostMapping;
|
import org.springframework.web.bind.annotation.PostMapping;
|
||||||
import org.springframework.web.bind.annotation.RequestParam;
|
import org.springframework.web.bind.annotation.RequestMapping;
|
||||||
import org.springframework.web.bind.annotation.RequestPart;
|
|
||||||
import org.springframework.web.bind.annotation.RestController;
|
import org.springframework.web.bind.annotation.RestController;
|
||||||
import org.springframework.web.multipart.MultipartFile;
|
import org.springframework.web.multipart.MultipartFile;
|
||||||
|
|
||||||
import io.swagger.v3.oas.annotations.Operation;
|
import io.swagger.v3.oas.annotations.Operation;
|
||||||
import io.swagger.v3.oas.annotations.Parameter;
|
import io.swagger.v3.oas.annotations.tags.Tag;
|
||||||
import io.swagger.v3.oas.annotations.media.Schema;
|
import stirling.software.SPDF.model.api.security.AddPasswordRequest;
|
||||||
|
import stirling.software.SPDF.model.api.security.PDFPasswordRequest;
|
||||||
import stirling.software.SPDF.utils.WebResponseUtils;
|
import stirling.software.SPDF.utils.WebResponseUtils;
|
||||||
@RestController
|
@RestController
|
||||||
|
@RequestMapping("/api/v1/security")
|
||||||
|
@Tag(name = "Security", description = "Security APIs")
|
||||||
public class PasswordController {
|
public class PasswordController {
|
||||||
|
|
||||||
private static final Logger logger = LoggerFactory.getLogger(PasswordController.class);
|
private static final Logger logger = LoggerFactory.getLogger(PasswordController.class);
|
||||||
@@ -27,15 +30,14 @@ public class PasswordController {
|
|||||||
@PostMapping(consumes = "multipart/form-data", value = "/remove-password")
|
@PostMapping(consumes = "multipart/form-data", value = "/remove-password")
|
||||||
@Operation(
|
@Operation(
|
||||||
summary = "Remove password from a PDF file",
|
summary = "Remove password from a PDF file",
|
||||||
description = "This endpoint removes the password from a protected PDF file. Users need to provide the existing password."
|
description = "This endpoint removes the password from a protected PDF file. Users need to provide the existing password. Input:PDF Output:PDF Type:SISO"
|
||||||
)
|
)
|
||||||
public ResponseEntity<byte[]> removePassword(
|
public ResponseEntity<byte[]> removePassword(@ModelAttribute PDFPasswordRequest request) throws IOException {
|
||||||
@RequestPart(required = true, value = "fileInput")
|
MultipartFile fileInput = request.getFileInput();
|
||||||
@Parameter(description = "The input PDF file from which the password should be removed", required = true)
|
String password = request.getPassword();
|
||||||
MultipartFile fileInput,
|
|
||||||
@RequestParam(name = "password")
|
|
||||||
@Parameter(description = "The password of the PDF file", required = true)
|
|
||||||
String password) throws IOException {
|
|
||||||
PDDocument document = PDDocument.load(fileInput.getBytes(), password);
|
PDDocument document = PDDocument.load(fileInput.getBytes(), password);
|
||||||
document.setAllSecurityToBeRemoved(true);
|
document.setAllSecurityToBeRemoved(true);
|
||||||
return WebResponseUtils.pdfDocToWebResponse(document, fileInput.getOriginalFilename().replaceFirst("[.][^.]+$", "") + "_password_removed.pdf");
|
return WebResponseUtils.pdfDocToWebResponse(document, fileInput.getOriginalFilename().replaceFirst("[.][^.]+$", "") + "_password_removed.pdf");
|
||||||
@@ -44,46 +46,21 @@ public class PasswordController {
|
|||||||
@PostMapping(consumes = "multipart/form-data", value = "/add-password")
|
@PostMapping(consumes = "multipart/form-data", value = "/add-password")
|
||||||
@Operation(
|
@Operation(
|
||||||
summary = "Add password to a PDF file",
|
summary = "Add password to a PDF file",
|
||||||
description = "This endpoint adds password protection to a PDF file. Users can specify a set of permissions that should be applied to the file."
|
description = "This endpoint adds password protection to a PDF file. Users can specify a set of permissions that should be applied to the file. Input:PDF Output:PDF"
|
||||||
)
|
)
|
||||||
public ResponseEntity<byte[]> addPassword(
|
public ResponseEntity<byte[]> addPassword(@ModelAttribute AddPasswordRequest request) throws IOException {
|
||||||
@RequestPart(required = true, value = "fileInput")
|
MultipartFile fileInput = request.getFileInput();
|
||||||
@Parameter(description = "The input PDF file to which the password should be added", required = true)
|
String ownerPassword = request.getOwnerPassword();
|
||||||
MultipartFile fileInput,
|
String password = request.getPassword();
|
||||||
@RequestParam(defaultValue = "", name = "ownerPassword")
|
int keyLength = request.getKeyLength();
|
||||||
@Parameter(description = "The owner password to be added to the PDF file (Restricts what can be done with the document once it is opened)")
|
boolean canAssembleDocument = request.isCanAssembleDocument();
|
||||||
String ownerPassword,
|
boolean canExtractContent = request.isCanExtractContent();
|
||||||
@RequestParam(defaultValue = "", name = "password")
|
boolean canExtractForAccessibility = request.isCanExtractForAccessibility();
|
||||||
@Parameter(description = "The password to be added to the PDF file (Restricts the opening of the document itself.)")
|
boolean canFillInForm = request.isCanFillInForm();
|
||||||
String password,
|
boolean canModify = request.isCanModify();
|
||||||
@RequestParam(defaultValue = "128", name = "keyLength")
|
boolean canModifyAnnotations = request.isCanModifyAnnotations();
|
||||||
@Parameter(description = "The length of the encryption key", schema = @Schema(allowableValues = {"40", "128", "256"}))
|
boolean canPrint = request.isCanPrint();
|
||||||
int keyLength,
|
boolean canPrintFaithful = request.isCanPrintFaithful();
|
||||||
@RequestParam(defaultValue = "false", name = "canAssembleDocument")
|
|
||||||
@Parameter(description = "Whether the document assembly is allowed", example = "false")
|
|
||||||
boolean canAssembleDocument,
|
|
||||||
@RequestParam(defaultValue = "false", name = "canExtractContent")
|
|
||||||
@Parameter(description = "Whether content extraction for accessibility is allowed", example = "false")
|
|
||||||
boolean canExtractContent,
|
|
||||||
@RequestParam(defaultValue = "false", name = "canExtractForAccessibility")
|
|
||||||
@Parameter(description = "Whether content extraction for accessibility is allowed", example = "false")
|
|
||||||
boolean canExtractForAccessibility,
|
|
||||||
@RequestParam(defaultValue = "false", name = "canFillInForm")
|
|
||||||
@Parameter(description = "Whether form filling is allowed", example = "false")
|
|
||||||
boolean canFillInForm,
|
|
||||||
@RequestParam(defaultValue = "false", name = "canModify")
|
|
||||||
@Parameter(description = "Whether the document modification is allowed", example = "false")
|
|
||||||
boolean canModify,
|
|
||||||
@RequestParam(defaultValue = "false", name = "canModifyAnnotations")
|
|
||||||
@Parameter(description = "Whether modification of annotations is allowed", example = "false")
|
|
||||||
boolean canModifyAnnotations,
|
|
||||||
@RequestParam(defaultValue = "false", name = "canPrint")
|
|
||||||
@Parameter(description = "Whether printing of the document is allowed", example = "false")
|
|
||||||
boolean canPrint,
|
|
||||||
@RequestParam(defaultValue = "false", name = "canPrintFaithful")
|
|
||||||
@Parameter(description = "Whether faithful printing is allowed", example = "false")
|
|
||||||
boolean canPrintFaithful
|
|
||||||
) throws IOException {
|
|
||||||
|
|
||||||
PDDocument document = PDDocument.load(fileInput.getBytes());
|
PDDocument document = PDDocument.load(fileInput.getBytes());
|
||||||
AccessPermission ap = new AccessPermission();
|
AccessPermission ap = new AccessPermission();
|
||||||
@@ -96,15 +73,15 @@ public class PasswordController {
|
|||||||
ap.setCanPrint(!canPrint);
|
ap.setCanPrint(!canPrint);
|
||||||
ap.setCanPrintFaithful(!canPrintFaithful);
|
ap.setCanPrintFaithful(!canPrintFaithful);
|
||||||
StandardProtectionPolicy spp = new StandardProtectionPolicy(ownerPassword, password, ap);
|
StandardProtectionPolicy spp = new StandardProtectionPolicy(ownerPassword, password, ap);
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
spp.setEncryptionKeyLength(keyLength);
|
|
||||||
|
|
||||||
|
if(!"".equals(ownerPassword) || !"".equals(password)) {
|
||||||
|
spp.setEncryptionKeyLength(keyLength);
|
||||||
|
}
|
||||||
spp.setPermissions(ap);
|
spp.setPermissions(ap);
|
||||||
|
|
||||||
document.protect(spp);
|
document.protect(spp);
|
||||||
|
|
||||||
|
if("".equals(ownerPassword) && "".equals(password))
|
||||||
|
return WebResponseUtils.pdfDocToWebResponse(document, fileInput.getOriginalFilename().replaceFirst("[.][^.]+$", "") + "_permissions.pdf");
|
||||||
return WebResponseUtils.pdfDocToWebResponse(document, fileInput.getOriginalFilename().replaceFirst("[.][^.]+$", "") + "_passworded.pdf");
|
return WebResponseUtils.pdfDocToWebResponse(document, fileInput.getOriginalFilename().replaceFirst("[.][^.]+$", "") + "_passworded.pdf");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -0,0 +1,123 @@
|
|||||||
|
package stirling.software.SPDF.controller.api.security;
|
||||||
|
|
||||||
|
import java.awt.Color;
|
||||||
|
import java.awt.image.BufferedImage;
|
||||||
|
import java.io.ByteArrayInputStream;
|
||||||
|
import java.io.ByteArrayOutputStream;
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
import org.apache.pdfbox.pdmodel.PDDocument;
|
||||||
|
import org.apache.pdfbox.pdmodel.PDPage;
|
||||||
|
import org.apache.pdfbox.pdmodel.PDPageContentStream;
|
||||||
|
import org.apache.pdfbox.pdmodel.common.PDRectangle;
|
||||||
|
import org.apache.pdfbox.pdmodel.graphics.image.LosslessFactory;
|
||||||
|
import org.apache.pdfbox.pdmodel.graphics.image.PDImageXObject;
|
||||||
|
import org.apache.pdfbox.rendering.ImageType;
|
||||||
|
import org.apache.pdfbox.rendering.PDFRenderer;
|
||||||
|
import org.slf4j.Logger;
|
||||||
|
import org.slf4j.LoggerFactory;
|
||||||
|
import org.springframework.http.ResponseEntity;
|
||||||
|
import org.springframework.web.bind.annotation.ModelAttribute;
|
||||||
|
import org.springframework.web.bind.annotation.PostMapping;
|
||||||
|
import org.springframework.web.bind.annotation.RequestMapping;
|
||||||
|
import org.springframework.web.bind.annotation.RestController;
|
||||||
|
import org.springframework.web.multipart.MultipartFile;
|
||||||
|
|
||||||
|
import io.swagger.v3.oas.annotations.Operation;
|
||||||
|
import io.swagger.v3.oas.annotations.tags.Tag;
|
||||||
|
import stirling.software.SPDF.model.PDFText;
|
||||||
|
import stirling.software.SPDF.model.api.security.RedactPdfRequest;
|
||||||
|
import stirling.software.SPDF.pdf.TextFinder;
|
||||||
|
import stirling.software.SPDF.utils.WebResponseUtils;
|
||||||
|
@RestController
|
||||||
|
@RequestMapping("/api/v1/security")
|
||||||
|
@Tag(name = "Security", description = "Security APIs")
|
||||||
|
public class RedactController {
|
||||||
|
|
||||||
|
private static final Logger logger = LoggerFactory.getLogger(RedactController.class);
|
||||||
|
|
||||||
|
|
||||||
|
@PostMapping(value = "/auto-redact", consumes = "multipart/form-data")
|
||||||
|
@Operation(summary = "Redacts listOfText in a PDF document",
|
||||||
|
description = "This operation takes an input PDF file and redacts the provided listOfText. Input:PDF, Output:PDF, Type:SISO")
|
||||||
|
public ResponseEntity<byte[]> redactPdf(@ModelAttribute RedactPdfRequest request) throws Exception {
|
||||||
|
MultipartFile file = request.getFileInput();
|
||||||
|
String listOfTextString = request.getListOfText();
|
||||||
|
boolean useRegex = request.isUseRegex();
|
||||||
|
boolean wholeWordSearchBool = request.isWholeWordSearch();
|
||||||
|
String colorString = request.getRedactColor();
|
||||||
|
float customPadding = request.getCustomPadding();
|
||||||
|
boolean convertPDFToImage = request.isConvertPDFToImage();
|
||||||
|
|
||||||
|
System.out.println(listOfTextString);
|
||||||
|
String[] listOfText = listOfTextString.split("\n");
|
||||||
|
byte[] bytes = file.getBytes();
|
||||||
|
PDDocument document = PDDocument.load(new ByteArrayInputStream(bytes));
|
||||||
|
|
||||||
|
Color redactColor;
|
||||||
|
try {
|
||||||
|
if (!colorString.startsWith("#")) {
|
||||||
|
colorString = "#" + colorString;
|
||||||
|
}
|
||||||
|
redactColor = Color.decode(colorString);
|
||||||
|
} catch (NumberFormatException e) {
|
||||||
|
logger.warn("Invalid color string provided. Using default color BLACK for redaction.");
|
||||||
|
redactColor = Color.BLACK;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
for (String text : listOfText) {
|
||||||
|
text = text.trim();
|
||||||
|
System.out.println(text);
|
||||||
|
TextFinder textFinder = new TextFinder(text, useRegex, wholeWordSearchBool);
|
||||||
|
List<PDFText> foundTexts = textFinder.getTextLocations(document);
|
||||||
|
redactFoundText(document, foundTexts, customPadding,redactColor);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
if (convertPDFToImage) {
|
||||||
|
PDDocument imageDocument = new PDDocument();
|
||||||
|
PDFRenderer pdfRenderer = new PDFRenderer(document);
|
||||||
|
for (int page = 0; page < document.getNumberOfPages(); ++page) {
|
||||||
|
BufferedImage bim = pdfRenderer.renderImageWithDPI(page, 300, ImageType.RGB);
|
||||||
|
PDPage newPage = new PDPage(new PDRectangle(bim.getWidth(), bim.getHeight()));
|
||||||
|
imageDocument.addPage(newPage);
|
||||||
|
PDImageXObject pdImage = LosslessFactory.createFromImage(imageDocument, bim);
|
||||||
|
PDPageContentStream contentStream = new PDPageContentStream(imageDocument, newPage);
|
||||||
|
contentStream.drawImage(pdImage, 0, 0);
|
||||||
|
contentStream.close();
|
||||||
|
}
|
||||||
|
document.close();
|
||||||
|
document = imageDocument;
|
||||||
|
}
|
||||||
|
|
||||||
|
ByteArrayOutputStream baos = new ByteArrayOutputStream();
|
||||||
|
document.save(baos);
|
||||||
|
document.close();
|
||||||
|
|
||||||
|
byte[] pdfContent = baos.toByteArray();
|
||||||
|
return WebResponseUtils.bytesToWebResponse(pdfContent,
|
||||||
|
file.getOriginalFilename().replaceFirst("[.][^.]+$", "") + "_redacted.pdf");
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
private void redactFoundText(PDDocument document, List<PDFText> blocks, float customPadding, Color redactColor) throws IOException {
|
||||||
|
var allPages = document.getDocumentCatalog().getPages();
|
||||||
|
|
||||||
|
for (PDFText block : blocks) {
|
||||||
|
var page = allPages.get(block.getPageIndex());
|
||||||
|
PDPageContentStream contentStream = new PDPageContentStream(document, page, PDPageContentStream.AppendMode.APPEND, true, true);
|
||||||
|
contentStream.setNonStrokingColor(redactColor);
|
||||||
|
float padding = (block.getY2() - block.getY1()) * 0.3f + customPadding;
|
||||||
|
PDRectangle pageBox = page.getBBox();
|
||||||
|
contentStream.addRect(block.getX1(), pageBox.getHeight() - block.getY1() - padding, block.getX2() - block.getX1(), block.getY2() - block.getY1() + 2 * padding);
|
||||||
|
contentStream.fill();
|
||||||
|
contentStream.close();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
@@ -0,0 +1,167 @@
|
|||||||
|
package stirling.software.SPDF.controller.api.security;
|
||||||
|
import java.io.IOException;
|
||||||
|
|
||||||
|
import org.apache.pdfbox.cos.COSDictionary;
|
||||||
|
import org.apache.pdfbox.cos.COSName;
|
||||||
|
import org.apache.pdfbox.pdmodel.PDDocument;
|
||||||
|
import org.apache.pdfbox.pdmodel.PDDocumentCatalog;
|
||||||
|
import org.apache.pdfbox.pdmodel.PDPage;
|
||||||
|
import org.apache.pdfbox.pdmodel.PDPageTree;
|
||||||
|
import org.apache.pdfbox.pdmodel.PDResources;
|
||||||
|
import org.apache.pdfbox.pdmodel.common.PDMetadata;
|
||||||
|
import org.apache.pdfbox.pdmodel.interactive.action.PDAction;
|
||||||
|
import org.apache.pdfbox.pdmodel.interactive.action.PDActionJavaScript;
|
||||||
|
import org.apache.pdfbox.pdmodel.interactive.action.PDActionLaunch;
|
||||||
|
import org.apache.pdfbox.pdmodel.interactive.action.PDActionURI;
|
||||||
|
import org.apache.pdfbox.pdmodel.interactive.action.PDFormFieldAdditionalActions;
|
||||||
|
import org.apache.pdfbox.pdmodel.interactive.annotation.PDAnnotation;
|
||||||
|
import org.apache.pdfbox.pdmodel.interactive.annotation.PDAnnotationLink;
|
||||||
|
import org.apache.pdfbox.pdmodel.interactive.annotation.PDAnnotationWidget;
|
||||||
|
import org.apache.pdfbox.pdmodel.interactive.form.PDAcroForm;
|
||||||
|
import org.apache.pdfbox.pdmodel.interactive.form.PDField;
|
||||||
|
import org.springframework.http.ResponseEntity;
|
||||||
|
import org.springframework.web.bind.annotation.ModelAttribute;
|
||||||
|
import org.springframework.web.bind.annotation.PostMapping;
|
||||||
|
import org.springframework.web.bind.annotation.RequestMapping;
|
||||||
|
import org.springframework.web.bind.annotation.RestController;
|
||||||
|
import org.springframework.web.multipart.MultipartFile;
|
||||||
|
|
||||||
|
import io.swagger.v3.oas.annotations.Operation;
|
||||||
|
import io.swagger.v3.oas.annotations.tags.Tag;
|
||||||
|
import stirling.software.SPDF.model.api.security.SanitizePdfRequest;
|
||||||
|
import stirling.software.SPDF.utils.WebResponseUtils;
|
||||||
|
|
||||||
|
@RestController
|
||||||
|
@RequestMapping("/api/v1/security")
|
||||||
|
@Tag(name = "Security", description = "Security APIs")
|
||||||
|
public class SanitizeController {
|
||||||
|
|
||||||
|
@PostMapping(consumes = "multipart/form-data", value = "/sanitize-pdf")
|
||||||
|
@Operation(summary = "Sanitize a PDF file",
|
||||||
|
description = "This endpoint processes a PDF file and removes specific elements based on the provided options. Input:PDF Output:PDF Type:SISO")
|
||||||
|
public ResponseEntity<byte[]> sanitizePDF(@ModelAttribute SanitizePdfRequest request) throws IOException {
|
||||||
|
MultipartFile inputFile = request.getFileInput();
|
||||||
|
boolean removeJavaScript = request.isRemoveJavaScript();
|
||||||
|
boolean removeEmbeddedFiles = request.isRemoveEmbeddedFiles();
|
||||||
|
boolean removeMetadata = request.isRemoveMetadata();
|
||||||
|
boolean removeLinks = request.isRemoveLinks();
|
||||||
|
boolean removeFonts = request.isRemoveFonts();
|
||||||
|
|
||||||
|
try (PDDocument document = PDDocument.load(inputFile.getInputStream())) {
|
||||||
|
if (removeJavaScript) {
|
||||||
|
sanitizeJavaScript(document);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (removeEmbeddedFiles) {
|
||||||
|
sanitizeEmbeddedFiles(document);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (removeMetadata) {
|
||||||
|
sanitizeMetadata(document);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (removeLinks) {
|
||||||
|
sanitizeLinks(document);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (removeFonts) {
|
||||||
|
sanitizeFonts(document);
|
||||||
|
}
|
||||||
|
|
||||||
|
return WebResponseUtils.pdfDocToWebResponse(document, inputFile.getOriginalFilename().replaceFirst("[.][^.]+$", "") + "_sanitized.pdf");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
private void sanitizeJavaScript(PDDocument document) throws IOException {
|
||||||
|
// Get the root dictionary (catalog) of the PDF
|
||||||
|
PDDocumentCatalog catalog = document.getDocumentCatalog();
|
||||||
|
|
||||||
|
// Get the Names dictionary
|
||||||
|
COSDictionary namesDict = (COSDictionary) catalog.getCOSObject().getDictionaryObject(COSName.NAMES);
|
||||||
|
|
||||||
|
if (namesDict != null) {
|
||||||
|
// Get the JavaScript dictionary
|
||||||
|
COSDictionary javaScriptDict = (COSDictionary) namesDict.getDictionaryObject(COSName.getPDFName("JavaScript"));
|
||||||
|
|
||||||
|
if (javaScriptDict != null) {
|
||||||
|
// Remove the JavaScript dictionary
|
||||||
|
namesDict.removeItem(COSName.getPDFName("JavaScript"));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
for (PDPage page : document.getPages()) {
|
||||||
|
for (PDAnnotation annotation : page.getAnnotations()) {
|
||||||
|
if (annotation instanceof PDAnnotationWidget) {
|
||||||
|
PDAnnotationWidget widget = (PDAnnotationWidget) annotation;
|
||||||
|
PDAction action = widget.getAction();
|
||||||
|
if (action instanceof PDActionJavaScript) {
|
||||||
|
widget.setAction(null);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
PDAcroForm acroForm = document.getDocumentCatalog().getAcroForm();
|
||||||
|
if (acroForm != null) {
|
||||||
|
for (PDField field : acroForm.getFields()) {
|
||||||
|
PDFormFieldAdditionalActions actions = field.getActions();
|
||||||
|
if(actions != null) {
|
||||||
|
if (actions.getC() instanceof PDActionJavaScript) {
|
||||||
|
actions.setC(null);
|
||||||
|
}
|
||||||
|
if (actions.getF() instanceof PDActionJavaScript) {
|
||||||
|
actions.setF(null);
|
||||||
|
}
|
||||||
|
if (actions.getK() instanceof PDActionJavaScript) {
|
||||||
|
actions.setK(null);
|
||||||
|
}
|
||||||
|
if (actions.getV() instanceof PDActionJavaScript) {
|
||||||
|
actions.setV(null);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
private void sanitizeEmbeddedFiles(PDDocument document) {
|
||||||
|
PDPageTree allPages = document.getPages();
|
||||||
|
|
||||||
|
for (PDPage page : allPages) {
|
||||||
|
PDResources res = page.getResources();
|
||||||
|
|
||||||
|
// Remove embedded files from the PDF
|
||||||
|
res.getCOSObject().removeItem(COSName.getPDFName("EmbeddedFiles"));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
private void sanitizeMetadata(PDDocument document) {
|
||||||
|
PDMetadata metadata = document.getDocumentCatalog().getMetadata();
|
||||||
|
if (metadata != null) {
|
||||||
|
document.getDocumentCatalog().setMetadata(null);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
private void sanitizeLinks(PDDocument document) throws IOException {
|
||||||
|
for (PDPage page : document.getPages()) {
|
||||||
|
for (PDAnnotation annotation : page.getAnnotations()) {
|
||||||
|
if (annotation instanceof PDAnnotationLink) {
|
||||||
|
PDAction action = ((PDAnnotationLink) annotation).getAction();
|
||||||
|
if (action instanceof PDActionLaunch || action instanceof PDActionURI) {
|
||||||
|
((PDAnnotationLink) annotation).setAction(null);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private void sanitizeFonts(PDDocument document) {
|
||||||
|
for (PDPage page : document.getPages()) {
|
||||||
|
page.getResources().getCOSObject().removeItem(COSName.getPDFName("Font"));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -1,97 +1,191 @@
|
|||||||
package stirling.software.SPDF.controller.api.security;
|
package stirling.software.SPDF.controller.api.security;
|
||||||
|
|
||||||
import java.awt.Color;
|
import java.awt.Color;
|
||||||
|
import java.awt.image.BufferedImage;
|
||||||
|
import java.io.File;
|
||||||
|
import java.io.FileOutputStream;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
|
import java.io.InputStream;
|
||||||
|
|
||||||
|
import javax.imageio.ImageIO;
|
||||||
|
|
||||||
|
import org.apache.commons.io.IOUtils;
|
||||||
import org.apache.pdfbox.pdmodel.PDDocument;
|
import org.apache.pdfbox.pdmodel.PDDocument;
|
||||||
import org.apache.pdfbox.pdmodel.PDPage;
|
import org.apache.pdfbox.pdmodel.PDPage;
|
||||||
import org.apache.pdfbox.pdmodel.PDPageContentStream;
|
import org.apache.pdfbox.pdmodel.PDPageContentStream;
|
||||||
import org.apache.pdfbox.pdmodel.font.PDFont;
|
import org.apache.pdfbox.pdmodel.font.PDFont;
|
||||||
|
import org.apache.pdfbox.pdmodel.font.PDType0Font;
|
||||||
import org.apache.pdfbox.pdmodel.font.PDType1Font;
|
import org.apache.pdfbox.pdmodel.font.PDType1Font;
|
||||||
|
import org.apache.pdfbox.pdmodel.graphics.image.LosslessFactory;
|
||||||
|
import org.apache.pdfbox.pdmodel.graphics.image.PDImageXObject;
|
||||||
import org.apache.pdfbox.pdmodel.graphics.state.PDExtendedGraphicsState;
|
import org.apache.pdfbox.pdmodel.graphics.state.PDExtendedGraphicsState;
|
||||||
import org.apache.pdfbox.util.Matrix;
|
import org.apache.pdfbox.util.Matrix;
|
||||||
|
import org.springframework.core.io.ClassPathResource;
|
||||||
import org.springframework.http.ResponseEntity;
|
import org.springframework.http.ResponseEntity;
|
||||||
|
import org.springframework.web.bind.annotation.ModelAttribute;
|
||||||
import org.springframework.web.bind.annotation.PostMapping;
|
import org.springframework.web.bind.annotation.PostMapping;
|
||||||
import org.springframework.web.bind.annotation.RequestParam;
|
import org.springframework.web.bind.annotation.RequestMapping;
|
||||||
import org.springframework.web.bind.annotation.RequestPart;
|
|
||||||
import org.springframework.web.bind.annotation.RestController;
|
import org.springframework.web.bind.annotation.RestController;
|
||||||
import org.springframework.web.multipart.MultipartFile;
|
import org.springframework.web.multipart.MultipartFile;
|
||||||
|
|
||||||
import io.swagger.v3.oas.annotations.Operation;
|
import io.swagger.v3.oas.annotations.Operation;
|
||||||
import io.swagger.v3.oas.annotations.Parameter;
|
import io.swagger.v3.oas.annotations.tags.Tag;
|
||||||
|
import stirling.software.SPDF.model.api.security.AddWatermarkRequest;
|
||||||
import stirling.software.SPDF.utils.WebResponseUtils;
|
import stirling.software.SPDF.utils.WebResponseUtils;
|
||||||
|
|
||||||
@RestController
|
@RestController
|
||||||
|
@RequestMapping("/api/v1/security")
|
||||||
|
@Tag(name = "Security", description = "Security APIs")
|
||||||
public class WatermarkController {
|
public class WatermarkController {
|
||||||
|
|
||||||
@PostMapping(consumes = "multipart/form-data", value = "/add-watermark")
|
@PostMapping(consumes = "multipart/form-data", value = "/add-watermark")
|
||||||
@Operation(summary = "Add watermark to a PDF file",
|
@Operation(summary = "Add watermark to a PDF file", description = "This endpoint adds a watermark to a given PDF file. Users can specify the watermark type (text or image), rotation, opacity, width spacer, and height spacer. Input:PDF Output:PDF Type:SISO")
|
||||||
description = "This endpoint adds a watermark to a given PDF file. Users can specify the watermark text, font size, rotation, opacity, width spacer, and height spacer.")
|
public ResponseEntity<byte[]> addWatermark(@ModelAttribute AddWatermarkRequest request) throws IOException, Exception {
|
||||||
public ResponseEntity<byte[]> addWatermark(
|
MultipartFile pdfFile = request.getFileInput();
|
||||||
@RequestPart(required = true, value = "fileInput")
|
String watermarkType = request.getWatermarkType();
|
||||||
@Parameter(description = "The input PDF file to add a watermark")
|
String watermarkText = request.getWatermarkText();
|
||||||
MultipartFile pdfFile,
|
MultipartFile watermarkImage = request.getWatermarkImage();
|
||||||
@RequestParam("watermarkText")
|
String alphabet = request.getAlphabet();
|
||||||
@Parameter(description = "The watermark text to add to the PDF file")
|
float fontSize = request.getFontSize();
|
||||||
String watermarkText,
|
float rotation = request.getRotation();
|
||||||
@RequestParam(defaultValue = "30", name = "fontSize")
|
float opacity = request.getOpacity();
|
||||||
@Parameter(description = "The font size of the watermark text", example = "30")
|
int widthSpacer = request.getWidthSpacer();
|
||||||
float fontSize,
|
int heightSpacer = request.getHeightSpacer();
|
||||||
@RequestParam(defaultValue = "0", name = "rotation")
|
|
||||||
@Parameter(description = "The rotation of the watermark text in degrees", example = "0")
|
|
||||||
float rotation,
|
|
||||||
@RequestParam(defaultValue = "0.5", name = "opacity")
|
|
||||||
@Parameter(description = "The opacity of the watermark text (0.0 - 1.0)", example = "0.5")
|
|
||||||
float opacity,
|
|
||||||
@RequestParam(defaultValue = "50", name = "widthSpacer")
|
|
||||||
@Parameter(description = "The width spacer between watermark texts", example = "50")
|
|
||||||
int widthSpacer,
|
|
||||||
@RequestParam(defaultValue = "50", name = "heightSpacer")
|
|
||||||
@Parameter(description = "The height spacer between watermark texts", example = "50")
|
|
||||||
int heightSpacer) throws IOException {
|
|
||||||
|
|
||||||
// Load the input PDF
|
// Load the input PDF
|
||||||
PDDocument document = PDDocument.load(pdfFile.getInputStream());
|
PDDocument document = PDDocument.load(pdfFile.getInputStream());
|
||||||
|
|
||||||
// Create a page in the document
|
// Create a page in the document
|
||||||
for (PDPage page : document.getPages()) {
|
for (PDPage page : document.getPages()) {
|
||||||
|
|
||||||
// Get the page's content stream
|
// Get the page's content stream
|
||||||
PDPageContentStream contentStream = new PDPageContentStream(document, page, PDPageContentStream.AppendMode.APPEND, true);
|
PDPageContentStream contentStream = new PDPageContentStream(document, page,
|
||||||
|
PDPageContentStream.AppendMode.APPEND, true);
|
||||||
|
|
||||||
// Set transparency
|
// Set transparency
|
||||||
PDExtendedGraphicsState graphicsState = new PDExtendedGraphicsState();
|
PDExtendedGraphicsState graphicsState = new PDExtendedGraphicsState();
|
||||||
graphicsState.setNonStrokingAlphaConstant(opacity);
|
graphicsState.setNonStrokingAlphaConstant(opacity);
|
||||||
contentStream.setGraphicsStateParameters(graphicsState);
|
contentStream.setGraphicsStateParameters(graphicsState);
|
||||||
|
|
||||||
// Set font of watermark
|
if (watermarkType.equalsIgnoreCase("text")) {
|
||||||
PDFont font = PDType1Font.HELVETICA_BOLD;
|
addTextWatermark(contentStream, watermarkText, document, page, rotation, widthSpacer, heightSpacer,
|
||||||
contentStream.beginText();
|
fontSize, alphabet);
|
||||||
contentStream.setFont(font, fontSize);
|
} else if (watermarkType.equalsIgnoreCase("image")) {
|
||||||
contentStream.setNonStrokingColor(Color.LIGHT_GRAY);
|
addImageWatermark(contentStream, watermarkImage, document, page, rotation, widthSpacer, heightSpacer,
|
||||||
|
fontSize);
|
||||||
|
}
|
||||||
|
|
||||||
// Set size and location of watermark
|
// Close the content stream
|
||||||
float pageWidth = page.getMediaBox().getWidth();
|
contentStream.close();
|
||||||
float pageHeight = page.getMediaBox().getHeight();
|
}
|
||||||
float watermarkWidth = widthSpacer + font.getStringWidth(watermarkText) * fontSize / 1000;
|
|
||||||
float watermarkHeight = heightSpacer + fontSize;
|
|
||||||
int watermarkRows = (int) (pageHeight / watermarkHeight + 1);
|
|
||||||
int watermarkCols = (int) (pageWidth / watermarkWidth + 1);
|
|
||||||
|
|
||||||
// Add the watermark text
|
return WebResponseUtils.pdfDocToWebResponse(document,
|
||||||
for (int i = 0; i < watermarkRows; i++) {
|
pdfFile.getOriginalFilename().replaceFirst("[.][^.]+$", "") + "_watermarked.pdf");
|
||||||
for (int j = 0; j < watermarkCols; j++) {
|
}
|
||||||
contentStream.setTextMatrix(Matrix.getRotateInstance((float) Math.toRadians(rotation), j * watermarkWidth, i * watermarkHeight));
|
|
||||||
contentStream.showTextWithPositioning(new Object[] { watermarkText });
|
private void addTextWatermark(PDPageContentStream contentStream, String watermarkText, PDDocument document,
|
||||||
}
|
PDPage page, float rotation, int widthSpacer, int heightSpacer, float fontSize, String alphabet) throws IOException {
|
||||||
|
String resourceDir = "";
|
||||||
|
PDFont font = PDType1Font.HELVETICA_BOLD;
|
||||||
|
switch (alphabet) {
|
||||||
|
case "arabic":
|
||||||
|
resourceDir = "static/fonts/NotoSansArabic-Regular.ttf";
|
||||||
|
break;
|
||||||
|
case "japanese":
|
||||||
|
resourceDir = "static/fonts/Meiryo.ttf";
|
||||||
|
break;
|
||||||
|
case "korean":
|
||||||
|
resourceDir = "static/fonts/malgun.ttf";
|
||||||
|
break;
|
||||||
|
case "chinese":
|
||||||
|
resourceDir = "static/fonts/SimSun.ttf";
|
||||||
|
break;
|
||||||
|
case "roman":
|
||||||
|
default:
|
||||||
|
resourceDir = "static/fonts/NotoSans-Regular.ttf";
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
if(!resourceDir.equals("")) {
|
||||||
|
ClassPathResource classPathResource = new ClassPathResource(resourceDir);
|
||||||
|
String fileExtension = resourceDir.substring(resourceDir.lastIndexOf("."));
|
||||||
|
File tempFile = File.createTempFile("NotoSansFont", fileExtension);
|
||||||
|
try (InputStream is = classPathResource.getInputStream(); FileOutputStream os = new FileOutputStream(tempFile)) {
|
||||||
|
IOUtils.copy(is, os);
|
||||||
}
|
}
|
||||||
|
|
||||||
contentStream.endText();
|
font = PDType0Font.load(document, tempFile);
|
||||||
|
tempFile.deleteOnExit();
|
||||||
// Close the content stream
|
|
||||||
contentStream.close();
|
|
||||||
}
|
}
|
||||||
return WebResponseUtils.pdfDocToWebResponse(document, pdfFile.getOriginalFilename().replaceFirst("[.][^.]+$", "") + "_watermarked.pdf");
|
|
||||||
}
|
contentStream.setFont(font, fontSize);
|
||||||
|
contentStream.setNonStrokingColor(Color.LIGHT_GRAY);
|
||||||
|
|
||||||
|
// Set size and location of text watermark
|
||||||
|
float watermarkWidth = widthSpacer + font.getStringWidth(watermarkText) * fontSize / 1000;
|
||||||
|
float watermarkHeight = heightSpacer + fontSize;
|
||||||
|
float pageWidth = page.getMediaBox().getWidth();
|
||||||
|
float pageHeight = page.getMediaBox().getHeight();
|
||||||
|
int watermarkRows = (int) (pageHeight / watermarkHeight + 1);
|
||||||
|
int watermarkCols = (int) (pageWidth / watermarkWidth + 1);
|
||||||
|
|
||||||
|
// Add the text watermark
|
||||||
|
for (int i = 0; i < watermarkRows; i++) {
|
||||||
|
for (int j = 0; j < watermarkCols; j++) {
|
||||||
|
contentStream.beginText();
|
||||||
|
contentStream.setTextMatrix(Matrix.getRotateInstance((float) Math.toRadians(rotation),
|
||||||
|
j * watermarkWidth, i * watermarkHeight));
|
||||||
|
contentStream.showText(watermarkText);
|
||||||
|
contentStream.endText();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private void addImageWatermark(PDPageContentStream contentStream, MultipartFile watermarkImage, PDDocument document, PDPage page, float rotation,
|
||||||
|
int widthSpacer, int heightSpacer, float fontSize) throws IOException {
|
||||||
|
|
||||||
|
// Load the watermark image
|
||||||
|
BufferedImage image = ImageIO.read(watermarkImage.getInputStream());
|
||||||
|
|
||||||
|
// Compute width based on original aspect ratio
|
||||||
|
float aspectRatio = (float) image.getWidth() / (float) image.getHeight();
|
||||||
|
|
||||||
|
// Desired physical height (in PDF points)
|
||||||
|
float desiredPhysicalHeight = fontSize ;
|
||||||
|
|
||||||
|
// Desired physical width based on the aspect ratio
|
||||||
|
float desiredPhysicalWidth = desiredPhysicalHeight * aspectRatio;
|
||||||
|
|
||||||
|
// Convert the BufferedImage to PDImageXObject
|
||||||
|
PDImageXObject xobject = LosslessFactory.createFromImage(document, image);
|
||||||
|
|
||||||
|
// Calculate the number of rows and columns for watermarks
|
||||||
|
float pageWidth = page.getMediaBox().getWidth();
|
||||||
|
float pageHeight = page.getMediaBox().getHeight();
|
||||||
|
int watermarkRows = (int) ((pageHeight + heightSpacer) / (desiredPhysicalHeight + heightSpacer));
|
||||||
|
int watermarkCols = (int) ((pageWidth + widthSpacer) / (desiredPhysicalWidth + widthSpacer));
|
||||||
|
|
||||||
|
for (int i = 0; i < watermarkRows; i++) {
|
||||||
|
for (int j = 0; j < watermarkCols; j++) {
|
||||||
|
float x = j * (desiredPhysicalWidth + widthSpacer);
|
||||||
|
float y = i * (desiredPhysicalHeight + heightSpacer);
|
||||||
|
|
||||||
|
// Save the graphics state
|
||||||
|
contentStream.saveGraphicsState();
|
||||||
|
|
||||||
|
// Create rotation matrix and rotate
|
||||||
|
contentStream.transform(Matrix.getTranslateInstance(x + desiredPhysicalWidth / 2, y + desiredPhysicalHeight / 2));
|
||||||
|
contentStream.transform(Matrix.getRotateInstance(Math.toRadians(rotation), 0, 0));
|
||||||
|
contentStream.transform(Matrix.getTranslateInstance(-desiredPhysicalWidth / 2, -desiredPhysicalHeight / 2));
|
||||||
|
|
||||||
|
// Draw the image and restore the graphics state
|
||||||
|
contentStream.drawImage(xobject, 0, 0, desiredPhysicalWidth, desiredPhysicalHeight);
|
||||||
|
contentStream.restoreGraphicsState();
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,135 @@
|
|||||||
|
package stirling.software.SPDF.controller.web;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Optional;
|
||||||
|
|
||||||
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
import org.springframework.security.access.prepost.PreAuthorize;
|
||||||
|
import org.springframework.security.core.Authentication;
|
||||||
|
import org.springframework.security.core.userdetails.UserDetails;
|
||||||
|
import org.springframework.stereotype.Controller;
|
||||||
|
import org.springframework.ui.Model;
|
||||||
|
import org.springframework.web.bind.annotation.GetMapping;
|
||||||
|
|
||||||
|
import com.fasterxml.jackson.core.JsonProcessingException;
|
||||||
|
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||||
|
|
||||||
|
import io.swagger.v3.oas.annotations.tags.Tag;
|
||||||
|
import jakarta.servlet.http.HttpServletRequest;
|
||||||
|
import stirling.software.SPDF.model.User;
|
||||||
|
import stirling.software.SPDF.repository.UserRepository;
|
||||||
|
@Controller
|
||||||
|
@Tag(name = "Account Security", description = "Account Security APIs")
|
||||||
|
public class AccountWebController {
|
||||||
|
|
||||||
|
|
||||||
|
@GetMapping("/login")
|
||||||
|
public String login(HttpServletRequest request, Model model, Authentication authentication) {
|
||||||
|
if (authentication != null && authentication.isAuthenticated()) {
|
||||||
|
return "redirect:/";
|
||||||
|
}
|
||||||
|
|
||||||
|
if (request.getParameter("error") != null) {
|
||||||
|
|
||||||
|
model.addAttribute("error", request.getParameter("error"));
|
||||||
|
}
|
||||||
|
if (request.getParameter("logout") != null) {
|
||||||
|
|
||||||
|
model.addAttribute("logoutMessage", "You have been logged out.");
|
||||||
|
}
|
||||||
|
|
||||||
|
return "login";
|
||||||
|
}
|
||||||
|
@Autowired
|
||||||
|
private UserRepository userRepository; // Assuming you have a repository for user operations
|
||||||
|
|
||||||
|
|
||||||
|
@PreAuthorize("hasRole('ROLE_ADMIN')")
|
||||||
|
@GetMapping("/addUsers")
|
||||||
|
public String showAddUserForm(Model model, Authentication authentication) {
|
||||||
|
List<User> allUsers = userRepository.findAll();
|
||||||
|
model.addAttribute("users", allUsers);
|
||||||
|
model.addAttribute("currentUsername", authentication.getName());
|
||||||
|
return "addUsers";
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
@GetMapping("/account")
|
||||||
|
public String account(HttpServletRequest request, Model model, Authentication authentication) {
|
||||||
|
if (authentication == null || !authentication.isAuthenticated()) {
|
||||||
|
return "redirect:/";
|
||||||
|
}
|
||||||
|
if (authentication != null && authentication.isAuthenticated()) {
|
||||||
|
Object principal = authentication.getPrincipal();
|
||||||
|
|
||||||
|
if (principal instanceof UserDetails) {
|
||||||
|
// Cast the principal object to UserDetails
|
||||||
|
UserDetails userDetails = (UserDetails) principal;
|
||||||
|
|
||||||
|
// Retrieve username and other attributes
|
||||||
|
String username = userDetails.getUsername();
|
||||||
|
|
||||||
|
// Fetch user details from the database
|
||||||
|
Optional<User> user = userRepository.findByUsername(username); // Assuming findByUsername method exists
|
||||||
|
if (!user.isPresent()) {
|
||||||
|
// Handle error appropriately
|
||||||
|
return "redirect:/error"; // Example redirection in case of error
|
||||||
|
}
|
||||||
|
|
||||||
|
// Convert settings map to JSON string
|
||||||
|
ObjectMapper objectMapper = new ObjectMapper();
|
||||||
|
String settingsJson;
|
||||||
|
try {
|
||||||
|
settingsJson = objectMapper.writeValueAsString(user.get().getSettings());
|
||||||
|
} catch (JsonProcessingException e) {
|
||||||
|
// Handle JSON conversion error
|
||||||
|
e.printStackTrace();
|
||||||
|
return "redirect:/error"; // Example redirection in case of error
|
||||||
|
}
|
||||||
|
|
||||||
|
// Add attributes to the model
|
||||||
|
model.addAttribute("username", username);
|
||||||
|
model.addAttribute("role", user.get().getRolesAsString());
|
||||||
|
model.addAttribute("settings", settingsJson);
|
||||||
|
model.addAttribute("changeCredsFlag", user.get().isFirstLogin());
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
return "redirect:/";
|
||||||
|
}
|
||||||
|
return "account";
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
@GetMapping("/change-creds")
|
||||||
|
public String changeCreds(HttpServletRequest request, Model model, Authentication authentication) {
|
||||||
|
if (authentication == null || !authentication.isAuthenticated()) {
|
||||||
|
return "redirect:/";
|
||||||
|
}
|
||||||
|
if (authentication != null && authentication.isAuthenticated()) {
|
||||||
|
Object principal = authentication.getPrincipal();
|
||||||
|
|
||||||
|
if (principal instanceof UserDetails) {
|
||||||
|
// Cast the principal object to UserDetails
|
||||||
|
UserDetails userDetails = (UserDetails) principal;
|
||||||
|
|
||||||
|
// Retrieve username and other attributes
|
||||||
|
String username = userDetails.getUsername();
|
||||||
|
|
||||||
|
// Fetch user details from the database
|
||||||
|
Optional<User> user = userRepository.findByUsername(username); // Assuming findByUsername method exists
|
||||||
|
if (!user.isPresent()) {
|
||||||
|
// Handle error appropriately
|
||||||
|
return "redirect:/error"; // Example redirection in case of error
|
||||||
|
}
|
||||||
|
// Add attributes to the model
|
||||||
|
model.addAttribute("username", username);
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
return "redirect:/";
|
||||||
|
}
|
||||||
|
return "change-creds";
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
@@ -1,86 +1,109 @@
|
|||||||
package stirling.software.SPDF.controller.web;
|
package stirling.software.SPDF.controller.web;
|
||||||
|
|
||||||
import org.springframework.stereotype.Controller;
|
import org.springframework.stereotype.Controller;
|
||||||
import org.springframework.ui.Model;
|
import org.springframework.ui.Model;
|
||||||
import org.springframework.web.bind.annotation.GetMapping;
|
import org.springframework.web.bind.annotation.GetMapping;
|
||||||
import org.springframework.web.servlet.ModelAndView;
|
import org.springframework.web.servlet.ModelAndView;
|
||||||
|
|
||||||
import io.swagger.v3.oas.annotations.Hidden;
|
import io.swagger.v3.oas.annotations.Hidden;
|
||||||
|
import io.swagger.v3.oas.annotations.tags.Tag;
|
||||||
@Controller
|
|
||||||
public class ConverterWebController {
|
@Controller
|
||||||
|
@Tag(name = "Convert", description = "Convert APIs")
|
||||||
@GetMapping("/img-to-pdf")
|
public class ConverterWebController {
|
||||||
@Hidden
|
|
||||||
public String convertImgToPdfForm(Model model) {
|
@GetMapping("/img-to-pdf")
|
||||||
model.addAttribute("currentPage", "img-to-pdf");
|
@Hidden
|
||||||
return "convert/img-to-pdf";
|
public String convertImgToPdfForm(Model model) {
|
||||||
}
|
model.addAttribute("currentPage", "img-to-pdf");
|
||||||
|
return "convert/img-to-pdf";
|
||||||
|
}
|
||||||
@GetMapping("/pdf-to-img")
|
|
||||||
@Hidden
|
@GetMapping("/html-to-pdf")
|
||||||
public String pdfToimgForm(Model model) {
|
@Hidden
|
||||||
model.addAttribute("currentPage", "pdf-to-img");
|
public String convertHTMLToPdfForm(Model model) {
|
||||||
return "convert/pdf-to-img";
|
model.addAttribute("currentPage", "html-to-pdf");
|
||||||
}
|
return "convert/html-to-pdf";
|
||||||
|
}
|
||||||
@GetMapping("/file-to-pdf")
|
@GetMapping("/markdown-to-pdf")
|
||||||
@Hidden
|
@Hidden
|
||||||
public String convertToPdfForm(Model model) {
|
public String convertMarkdownToPdfForm(Model model) {
|
||||||
model.addAttribute("currentPage", "file-to-pdf");
|
model.addAttribute("currentPage", "markdown-to-pdf");
|
||||||
return "convert/file-to-pdf";
|
return "convert/markdown-to-pdf";
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@GetMapping("/url-to-pdf")
|
||||||
//PDF TO......
|
@Hidden
|
||||||
|
public String convertURLToPdfForm(Model model) {
|
||||||
@GetMapping("/pdf-to-html")
|
model.addAttribute("currentPage", "url-to-pdf");
|
||||||
@Hidden
|
return "convert/url-to-pdf";
|
||||||
public ModelAndView pdfToHTML() {
|
}
|
||||||
ModelAndView modelAndView = new ModelAndView("convert/pdf-to-html");
|
|
||||||
modelAndView.addObject("currentPage", "pdf-to-html");
|
|
||||||
return modelAndView;
|
@GetMapping("/pdf-to-img")
|
||||||
}
|
@Hidden
|
||||||
|
public String pdfToimgForm(Model model) {
|
||||||
@GetMapping("/pdf-to-presentation")
|
model.addAttribute("currentPage", "pdf-to-img");
|
||||||
@Hidden
|
return "convert/pdf-to-img";
|
||||||
public ModelAndView pdfToPresentation() {
|
}
|
||||||
ModelAndView modelAndView = new ModelAndView("convert/pdf-to-presentation");
|
|
||||||
modelAndView.addObject("currentPage", "pdf-to-presentation");
|
@GetMapping("/file-to-pdf")
|
||||||
return modelAndView;
|
@Hidden
|
||||||
}
|
public String convertToPdfForm(Model model) {
|
||||||
|
model.addAttribute("currentPage", "file-to-pdf");
|
||||||
@GetMapping("/pdf-to-text")
|
return "convert/file-to-pdf";
|
||||||
@Hidden
|
}
|
||||||
public ModelAndView pdfToText() {
|
|
||||||
ModelAndView modelAndView = new ModelAndView("convert/pdf-to-text");
|
|
||||||
modelAndView.addObject("currentPage", "pdf-to-text");
|
|
||||||
return modelAndView;
|
//PDF TO......
|
||||||
}
|
|
||||||
|
@GetMapping("/pdf-to-html")
|
||||||
@GetMapping("/pdf-to-word")
|
@Hidden
|
||||||
@Hidden
|
public ModelAndView pdfToHTML() {
|
||||||
public ModelAndView pdfToWord() {
|
ModelAndView modelAndView = new ModelAndView("convert/pdf-to-html");
|
||||||
ModelAndView modelAndView = new ModelAndView("convert/pdf-to-word");
|
modelAndView.addObject("currentPage", "pdf-to-html");
|
||||||
modelAndView.addObject("currentPage", "pdf-to-word");
|
return modelAndView;
|
||||||
return modelAndView;
|
}
|
||||||
}
|
|
||||||
|
@GetMapping("/pdf-to-presentation")
|
||||||
@GetMapping("/pdf-to-xml")
|
@Hidden
|
||||||
@Hidden
|
public ModelAndView pdfToPresentation() {
|
||||||
public ModelAndView pdfToXML() {
|
ModelAndView modelAndView = new ModelAndView("convert/pdf-to-presentation");
|
||||||
ModelAndView modelAndView = new ModelAndView("convert/pdf-to-xml");
|
modelAndView.addObject("currentPage", "pdf-to-presentation");
|
||||||
modelAndView.addObject("currentPage", "pdf-to-xml");
|
return modelAndView;
|
||||||
return modelAndView;
|
}
|
||||||
}
|
|
||||||
|
@GetMapping("/pdf-to-text")
|
||||||
|
@Hidden
|
||||||
@GetMapping("/pdf-to-pdfa")
|
public ModelAndView pdfToText() {
|
||||||
@Hidden
|
ModelAndView modelAndView = new ModelAndView("convert/pdf-to-text");
|
||||||
public String pdfToPdfAForm(Model model) {
|
modelAndView.addObject("currentPage", "pdf-to-text");
|
||||||
model.addAttribute("currentPage", "pdf-to-pdfa");
|
return modelAndView;
|
||||||
return "convert/pdf-to-pdfa";
|
}
|
||||||
}
|
|
||||||
}
|
@GetMapping("/pdf-to-word")
|
||||||
|
@Hidden
|
||||||
|
public ModelAndView pdfToWord() {
|
||||||
|
ModelAndView modelAndView = new ModelAndView("convert/pdf-to-word");
|
||||||
|
modelAndView.addObject("currentPage", "pdf-to-word");
|
||||||
|
return modelAndView;
|
||||||
|
}
|
||||||
|
|
||||||
|
@GetMapping("/pdf-to-xml")
|
||||||
|
@Hidden
|
||||||
|
public ModelAndView pdfToXML() {
|
||||||
|
ModelAndView modelAndView = new ModelAndView("convert/pdf-to-xml");
|
||||||
|
modelAndView.addObject("currentPage", "pdf-to-xml");
|
||||||
|
return modelAndView;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@GetMapping("/pdf-to-pdfa")
|
||||||
|
@Hidden
|
||||||
|
public String pdfToPdfAForm(Model model) {
|
||||||
|
model.addAttribute("currentPage", "pdf-to-pdfa");
|
||||||
|
return "convert/pdf-to-pdfa";
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
@@ -1,27 +1,86 @@
|
|||||||
package stirling.software.SPDF.controller.web;
|
package stirling.software.SPDF.controller.web;
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.nio.charset.StandardCharsets;
|
||||||
|
import java.nio.file.Files;
|
||||||
|
import java.nio.file.Path;
|
||||||
|
import java.nio.file.Paths;
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.Arrays;
|
||||||
|
import java.util.HashMap;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Map;
|
||||||
|
import java.util.Objects;
|
||||||
|
import java.util.stream.Collectors;
|
||||||
|
import java.util.stream.Stream;
|
||||||
|
|
||||||
import org.springframework.http.MediaType;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
import org.springframework.core.io.Resource;
|
||||||
|
import org.springframework.core.io.ResourceLoader;
|
||||||
|
import org.springframework.core.io.support.ResourcePatternUtils;
|
||||||
import org.springframework.stereotype.Controller;
|
import org.springframework.stereotype.Controller;
|
||||||
import org.springframework.ui.Model;
|
import org.springframework.ui.Model;
|
||||||
import org.springframework.web.bind.annotation.GetMapping;
|
import org.springframework.web.bind.annotation.GetMapping;
|
||||||
import org.springframework.web.bind.annotation.ResponseBody;
|
|
||||||
|
import com.fasterxml.jackson.core.type.TypeReference;
|
||||||
|
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||||
|
|
||||||
import io.swagger.v3.oas.annotations.Hidden;
|
import io.swagger.v3.oas.annotations.Hidden;
|
||||||
|
import io.swagger.v3.oas.annotations.tags.Tag;
|
||||||
|
|
||||||
@Controller
|
@Controller
|
||||||
|
@Tag(name = "General", description = "General APIs")
|
||||||
public class GeneralWebController {
|
public class GeneralWebController {
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
@GetMapping("/pipeline")
|
||||||
|
@Hidden
|
||||||
|
public String pipelineForm(Model model) {
|
||||||
|
model.addAttribute("currentPage", "pipeline");
|
||||||
|
|
||||||
|
List<String> pipelineConfigs = new ArrayList<>();
|
||||||
|
try (Stream<Path> paths = Files.walk(Paths.get("./pipeline/defaultWebUIConfigs/"))) {
|
||||||
|
List<Path> jsonFiles = paths
|
||||||
|
.filter(Files::isRegularFile)
|
||||||
|
.filter(p -> p.toString().endsWith(".json"))
|
||||||
|
.collect(Collectors.toList());
|
||||||
|
|
||||||
|
for (Path jsonFile : jsonFiles) {
|
||||||
|
String content = Files.readString(jsonFile, StandardCharsets.UTF_8);
|
||||||
|
pipelineConfigs.add(content);
|
||||||
|
}
|
||||||
|
List<Map<String, String>> pipelineConfigsWithNames = new ArrayList<>();
|
||||||
|
for (String config : pipelineConfigs) {
|
||||||
|
Map<String, Object> jsonContent = new ObjectMapper().readValue(config, new TypeReference<Map<String, Object>>(){});
|
||||||
|
|
||||||
|
String name = (String) jsonContent.get("name");
|
||||||
|
Map<String, String> configWithName = new HashMap<>();
|
||||||
|
configWithName.put("json", config);
|
||||||
|
configWithName.put("name", name);
|
||||||
|
pipelineConfigsWithNames.add(configWithName);
|
||||||
|
}
|
||||||
|
model.addAttribute("pipelineConfigsWithNames", pipelineConfigsWithNames);
|
||||||
|
|
||||||
|
} catch (IOException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
|
||||||
|
model.addAttribute("pipelineConfigs", pipelineConfigs);
|
||||||
|
|
||||||
|
return "pipeline";
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
@GetMapping("/merge-pdfs")
|
@GetMapping("/merge-pdfs")
|
||||||
@Hidden
|
@Hidden
|
||||||
public String mergePdfForm(Model model) {
|
public String mergePdfForm(Model model) {
|
||||||
model.addAttribute("currentPage", "merge-pdfs");
|
model.addAttribute("currentPage", "merge-pdfs");
|
||||||
return "merge-pdfs";
|
return "merge-pdfs";
|
||||||
}
|
}
|
||||||
@GetMapping("/about")
|
|
||||||
@Hidden
|
|
||||||
public String gameForm(Model model) {
|
|
||||||
model.addAttribute("currentPage", "about");
|
|
||||||
return "about";
|
|
||||||
}
|
|
||||||
|
|
||||||
@GetMapping("/multi-tool")
|
@GetMapping("/multi-tool")
|
||||||
@Hidden
|
@Hidden
|
||||||
@@ -29,17 +88,7 @@ public class GeneralWebController {
|
|||||||
model.addAttribute("currentPage", "multi-tool");
|
model.addAttribute("currentPage", "multi-tool");
|
||||||
return "multi-tool";
|
return "multi-tool";
|
||||||
}
|
}
|
||||||
|
|
||||||
@GetMapping("/")
|
|
||||||
public String home(Model model) {
|
|
||||||
model.addAttribute("currentPage", "home");
|
|
||||||
return "home";
|
|
||||||
}
|
|
||||||
|
|
||||||
@GetMapping("/home")
|
|
||||||
public String root(Model model) {
|
|
||||||
return "redirect:/";
|
|
||||||
}
|
|
||||||
|
|
||||||
@GetMapping("/remove-pages")
|
@GetMapping("/remove-pages")
|
||||||
@Hidden
|
@Hidden
|
||||||
@@ -55,6 +104,20 @@ public class GeneralWebController {
|
|||||||
return "pdf-organizer";
|
return "pdf-organizer";
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@GetMapping("/extract-page")
|
||||||
|
@Hidden
|
||||||
|
public String extractPages(Model model) {
|
||||||
|
model.addAttribute("currentPage", "extract-page");
|
||||||
|
return "extract-page";
|
||||||
|
}
|
||||||
|
|
||||||
|
@GetMapping("/pdf-to-single-page")
|
||||||
|
@Hidden
|
||||||
|
public String pdfToSinglePage(Model model) {
|
||||||
|
model.addAttribute("currentPage", "pdf-to-single-page");
|
||||||
|
return "pdf-to-single-page";
|
||||||
|
}
|
||||||
|
|
||||||
@GetMapping("/rotate-pdf")
|
@GetMapping("/rotate-pdf")
|
||||||
@Hidden
|
@Hidden
|
||||||
public String rotatePdfForm(Model model) {
|
public String rotatePdfForm(Model model) {
|
||||||
@@ -73,23 +136,133 @@ public class GeneralWebController {
|
|||||||
@Hidden
|
@Hidden
|
||||||
public String signForm(Model model) {
|
public String signForm(Model model) {
|
||||||
model.addAttribute("currentPage", "sign");
|
model.addAttribute("currentPage", "sign");
|
||||||
|
model.addAttribute("fonts", getFontNames());
|
||||||
return "sign";
|
return "sign";
|
||||||
}
|
}
|
||||||
|
|
||||||
@GetMapping(value = "/robots.txt", produces = MediaType.TEXT_PLAIN_VALUE)
|
@GetMapping("/multi-page-layout")
|
||||||
@ResponseBody
|
|
||||||
@Hidden
|
@Hidden
|
||||||
public String getRobotsTxt() {
|
public String multiPageLayoutForm(Model model) {
|
||||||
String allowGoogleVisibility = System.getProperty("ALLOW_GOOGLE_VISIBILITY");
|
model.addAttribute("currentPage", "multi-page-layout");
|
||||||
if (allowGoogleVisibility == null)
|
return "multi-page-layout";
|
||||||
allowGoogleVisibility = System.getenv("ALLOW_GOOGLE_VISIBILITY");
|
|
||||||
if (allowGoogleVisibility == null)
|
|
||||||
allowGoogleVisibility = "false";
|
|
||||||
if (Boolean.parseBoolean(allowGoogleVisibility)) {
|
|
||||||
return "User-agent: Googlebot\nAllow: /\n\nUser-agent: *\nAllow: /";
|
|
||||||
} else {
|
|
||||||
return "User-agent: Googlebot\nDisallow: /\n\nUser-agent: *\nDisallow: /";
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@GetMapping("/scale-pages")
|
||||||
|
@Hidden
|
||||||
|
public String scalePagesFrom(Model model) {
|
||||||
|
model.addAttribute("currentPage", "scale-pages");
|
||||||
|
return "scale-pages";
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
private ResourceLoader resourceLoader;
|
||||||
|
|
||||||
|
private List<FontResource> getFontNames() {
|
||||||
|
List<FontResource> fontNames = new ArrayList<>();
|
||||||
|
|
||||||
|
// Extract font names from classpath
|
||||||
|
fontNames.addAll(getFontNamesFromLocation("classpath:static/fonts/*.woff2"));
|
||||||
|
|
||||||
|
// Extract font names from external directory
|
||||||
|
fontNames.addAll(getFontNamesFromLocation("file:customFiles/static/fonts/*"));
|
||||||
|
|
||||||
|
return fontNames;
|
||||||
|
}
|
||||||
|
|
||||||
|
private List<FontResource> getFontNamesFromLocation(String locationPattern) {
|
||||||
|
try {
|
||||||
|
Resource[] resources = ResourcePatternUtils.getResourcePatternResolver(resourceLoader)
|
||||||
|
.getResources(locationPattern);
|
||||||
|
return Arrays.stream(resources)
|
||||||
|
.map(resource -> {
|
||||||
|
try {
|
||||||
|
String filename = resource.getFilename();
|
||||||
|
if (filename != null) {
|
||||||
|
int lastDotIndex = filename.lastIndexOf('.');
|
||||||
|
if (lastDotIndex != -1) {
|
||||||
|
String name = filename.substring(0, lastDotIndex);
|
||||||
|
String extension = filename.substring(lastDotIndex + 1);
|
||||||
|
return new FontResource(name, extension);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return null;
|
||||||
|
} catch (Exception e) {
|
||||||
|
throw new RuntimeException("Error processing filename", e);
|
||||||
|
}
|
||||||
|
})
|
||||||
|
.filter(Objects::nonNull)
|
||||||
|
.collect(Collectors.toList());
|
||||||
|
} catch (Exception e) {
|
||||||
|
throw new RuntimeException("Failed to read font directory from " + locationPattern, e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
public String getFormatFromExtension(String extension) {
|
||||||
|
switch (extension) {
|
||||||
|
case "ttf": return "truetype";
|
||||||
|
case "woff": return "woff";
|
||||||
|
case "woff2": return "woff2";
|
||||||
|
case "eot": return "embedded-opentype";
|
||||||
|
case "svg": return "svg";
|
||||||
|
default: return ""; // or throw an exception if an unexpected extension is encountered
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
public class FontResource {
|
||||||
|
private String name;
|
||||||
|
private String extension;
|
||||||
|
private String type;
|
||||||
|
public FontResource(String name, String extension) {
|
||||||
|
this.name = name;
|
||||||
|
this.extension = extension;
|
||||||
|
this.type = getFormatFromExtension(extension);
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getName() {
|
||||||
|
return name;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setName(String name) {
|
||||||
|
this.name = name;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getExtension() {
|
||||||
|
return extension;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setExtension(String extension) {
|
||||||
|
this.extension = extension;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getType() {
|
||||||
|
return type;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setType(String type) {
|
||||||
|
this.type = type;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@GetMapping("/crop")
|
||||||
|
@Hidden
|
||||||
|
public String cropForm(Model model) {
|
||||||
|
model.addAttribute("currentPage", "crop");
|
||||||
|
return "crop";
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@GetMapping("/auto-split-pdf")
|
||||||
|
@Hidden
|
||||||
|
public String autoSPlitPDFForm(Model model) {
|
||||||
|
model.addAttribute("currentPage", "auto-split-pdf");
|
||||||
|
return "auto-split-pdf";
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,52 @@
|
|||||||
|
package stirling.software.SPDF.controller.web;
|
||||||
|
|
||||||
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
import org.springframework.http.MediaType;
|
||||||
|
import org.springframework.stereotype.Controller;
|
||||||
|
import org.springframework.ui.Model;
|
||||||
|
import org.springframework.web.bind.annotation.GetMapping;
|
||||||
|
import org.springframework.web.bind.annotation.ResponseBody;
|
||||||
|
|
||||||
|
import io.swagger.v3.oas.annotations.Hidden;
|
||||||
|
import stirling.software.SPDF.model.ApplicationProperties;
|
||||||
|
|
||||||
|
@Controller
|
||||||
|
public class HomeWebController {
|
||||||
|
|
||||||
|
@GetMapping("/about")
|
||||||
|
@Hidden
|
||||||
|
public String gameForm(Model model) {
|
||||||
|
model.addAttribute("currentPage", "about");
|
||||||
|
return "about";
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
@GetMapping("/")
|
||||||
|
public String home(Model model) {
|
||||||
|
model.addAttribute("currentPage", "home");
|
||||||
|
return "home";
|
||||||
|
}
|
||||||
|
|
||||||
|
@GetMapping("/home")
|
||||||
|
public String root(Model model) {
|
||||||
|
return "redirect:/";
|
||||||
|
}
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
ApplicationProperties applicationProperties;
|
||||||
|
|
||||||
|
|
||||||
|
@GetMapping(value = "/robots.txt", produces = MediaType.TEXT_PLAIN_VALUE)
|
||||||
|
@ResponseBody
|
||||||
|
@Hidden
|
||||||
|
public String getRobotsTxt() {
|
||||||
|
Boolean allowGoogle = applicationProperties.getSystem().getGooglevisibility();
|
||||||
|
if(Boolean.TRUE.equals(allowGoogle)) {
|
||||||
|
return "User-agent: Googlebot\nAllow: /\n\nUser-agent: *\nAllow: /";
|
||||||
|
} else {
|
||||||
|
return "User-agent: Googlebot\nDisallow: /\n\nUser-agent: *\nDisallow: /";
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -1,8 +1,16 @@
|
|||||||
package stirling.software.SPDF.controller.web;
|
package stirling.software.SPDF.controller.web;
|
||||||
|
import java.time.Duration;
|
||||||
|
import java.time.LocalDateTime;
|
||||||
|
import java.util.Comparator;
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
|
import java.util.List;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
import java.util.Optional;
|
import java.util.Optional;
|
||||||
|
import java.util.stream.Collectors;
|
||||||
|
|
||||||
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
import org.springframework.http.HttpStatus;
|
||||||
|
import org.springframework.http.ResponseEntity;
|
||||||
import org.springframework.web.bind.annotation.GetMapping;
|
import org.springframework.web.bind.annotation.GetMapping;
|
||||||
import org.springframework.web.bind.annotation.RequestMapping;
|
import org.springframework.web.bind.annotation.RequestMapping;
|
||||||
import org.springframework.web.bind.annotation.RequestParam;
|
import org.springframework.web.bind.annotation.RequestParam;
|
||||||
@@ -12,13 +20,34 @@ import io.micrometer.core.instrument.Counter;
|
|||||||
import io.micrometer.core.instrument.Meter;
|
import io.micrometer.core.instrument.Meter;
|
||||||
import io.micrometer.core.instrument.MeterRegistry;
|
import io.micrometer.core.instrument.MeterRegistry;
|
||||||
import io.swagger.v3.oas.annotations.Operation;
|
import io.swagger.v3.oas.annotations.Operation;
|
||||||
|
import io.swagger.v3.oas.annotations.Parameter;
|
||||||
|
import io.swagger.v3.oas.annotations.tags.Tag;
|
||||||
|
import jakarta.annotation.PostConstruct;
|
||||||
|
import stirling.software.SPDF.config.StartupApplicationListener;
|
||||||
|
import stirling.software.SPDF.model.ApplicationProperties;
|
||||||
|
|
||||||
@RestController
|
@RestController
|
||||||
@RequestMapping("/api/v1")
|
@RequestMapping("/api/v1")
|
||||||
|
@Tag(name = "API", description = "Info APIs")
|
||||||
public class MetricsController {
|
public class MetricsController {
|
||||||
|
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
ApplicationProperties applicationProperties;
|
||||||
|
|
||||||
|
|
||||||
private final MeterRegistry meterRegistry;
|
private final MeterRegistry meterRegistry;
|
||||||
|
|
||||||
|
private boolean metricsEnabled;
|
||||||
|
|
||||||
|
@PostConstruct
|
||||||
|
public void init() {
|
||||||
|
Boolean metricsEnabled = applicationProperties.getMetrics().getEnabled();
|
||||||
|
if(metricsEnabled == null)
|
||||||
|
metricsEnabled = true;
|
||||||
|
this.metricsEnabled = metricsEnabled;
|
||||||
|
}
|
||||||
|
|
||||||
public MetricsController(MeterRegistry meterRegistry) {
|
public MetricsController(MeterRegistry meterRegistry) {
|
||||||
this.meterRegistry = meterRegistry;
|
this.meterRegistry = meterRegistry;
|
||||||
}
|
}
|
||||||
@@ -26,55 +55,211 @@ public class MetricsController {
|
|||||||
@GetMapping("/status")
|
@GetMapping("/status")
|
||||||
@Operation(summary = "Application status and version",
|
@Operation(summary = "Application status and version",
|
||||||
description = "This endpoint returns the status of the application and its version number.")
|
description = "This endpoint returns the status of the application and its version number.")
|
||||||
public Map<String, String> getStatus() {
|
public ResponseEntity<?> getStatus() {
|
||||||
|
if (!metricsEnabled) {
|
||||||
|
return ResponseEntity.status(HttpStatus.FORBIDDEN).body("This endpoint is disabled.");
|
||||||
|
}
|
||||||
|
|
||||||
Map<String, String> status = new HashMap<>();
|
Map<String, String> status = new HashMap<>();
|
||||||
status.put("status", "UP");
|
status.put("status", "UP");
|
||||||
status.put("version", getClass().getPackage().getImplementationVersion());
|
status.put("version", getClass().getPackage().getImplementationVersion());
|
||||||
return status;
|
return ResponseEntity.ok(status);
|
||||||
}
|
}
|
||||||
|
|
||||||
@GetMapping("/loads")
|
@GetMapping("/loads")
|
||||||
@Operation(summary = "GET request count",
|
@Operation(summary = "GET request count",
|
||||||
description = "This endpoint returns the total count of GET requests or the count of GET requests for a specific endpoint.")
|
description = "This endpoint returns the total count of GET requests or the count of GET requests for a specific endpoint.")
|
||||||
public Double getPageLoads(@RequestParam Optional<String> endpoint) {
|
public ResponseEntity<?> getPageLoads(@RequestParam(required = false, name = "endpoint") @Parameter(description = "endpoint") Optional<String> endpoint) {
|
||||||
try {
|
if (!metricsEnabled) {
|
||||||
|
return ResponseEntity.status(HttpStatus.FORBIDDEN).body("This endpoint is disabled.");
|
||||||
|
}
|
||||||
|
try {
|
||||||
|
|
||||||
double count = 0.0;
|
double count = 0.0;
|
||||||
|
|
||||||
|
for (Meter meter : meterRegistry.getMeters()) {
|
||||||
|
if (meter.getId().getName().equals("http.requests")) {
|
||||||
|
String method = meter.getId().getTag("method");
|
||||||
|
if (method != null && method.equals("GET")) {
|
||||||
|
|
||||||
|
if (endpoint.isPresent() && !endpoint.get().isBlank()) {
|
||||||
|
if(!endpoint.get().startsWith("/")) {
|
||||||
|
endpoint = Optional.of("/" + endpoint.get());
|
||||||
|
}
|
||||||
|
System.out.println("loads " + endpoint.get() + " vs " + meter.getId().getTag("uri"));
|
||||||
|
if(endpoint.get().equals(meter.getId().getTag("uri"))){
|
||||||
|
if (meter instanceof Counter) {
|
||||||
|
count += ((Counter) meter).count();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
if (meter instanceof Counter) {
|
||||||
|
count += ((Counter) meter).count();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return ResponseEntity.ok(count);
|
||||||
|
} catch (Exception e) {
|
||||||
|
return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@GetMapping("/loads/all")
|
||||||
|
@Operation(summary = "GET requests count for all endpoints",
|
||||||
|
description = "This endpoint returns the count of GET requests for each endpoint.")
|
||||||
|
public ResponseEntity<?> getAllEndpointLoads() {
|
||||||
|
if (!metricsEnabled) {
|
||||||
|
return ResponseEntity.status(HttpStatus.FORBIDDEN).body("This endpoint is disabled.");
|
||||||
|
}
|
||||||
|
try {
|
||||||
|
Map<String, Double> counts = new HashMap<>();
|
||||||
|
|
||||||
for (Meter meter : meterRegistry.getMeters()) {
|
for (Meter meter : meterRegistry.getMeters()) {
|
||||||
if (meter.getId().getName().equals("http.requests")) {
|
if (meter.getId().getName().equals("http.requests")) {
|
||||||
String method = meter.getId().getTag("method");
|
String method = meter.getId().getTag("method");
|
||||||
if (method != null && method.equals("GET")) {
|
if (method != null && method.equals("GET")) {
|
||||||
if (meter instanceof Counter) {
|
String uri = meter.getId().getTag("uri");
|
||||||
count += ((Counter) meter).count();
|
if (uri != null) {
|
||||||
|
double currentCount = counts.getOrDefault(uri, 0.0);
|
||||||
|
if (meter instanceof Counter) {
|
||||||
|
currentCount += ((Counter) meter).count();
|
||||||
|
}
|
||||||
|
counts.put(uri, currentCount);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return count;
|
List<EndpointCount> results = counts.entrySet().stream()
|
||||||
|
.map(entry -> new EndpointCount(entry.getKey(), entry.getValue()))
|
||||||
|
.sorted(Comparator.comparing(EndpointCount::getCount).reversed())
|
||||||
|
.collect(Collectors.toList());
|
||||||
|
|
||||||
|
return ResponseEntity.ok(results);
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
return -1.0;
|
return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public class EndpointCount {
|
||||||
|
private String endpoint;
|
||||||
|
private double count;
|
||||||
|
|
||||||
|
public EndpointCount(String endpoint, double count) {
|
||||||
|
this.endpoint = endpoint;
|
||||||
|
this.count = count;
|
||||||
|
}
|
||||||
|
public String getEndpoint() {
|
||||||
|
return endpoint;
|
||||||
|
}
|
||||||
|
public void setEndpoint(String endpoint) {
|
||||||
|
this.endpoint = endpoint;
|
||||||
|
}
|
||||||
|
public double getCount() {
|
||||||
|
return count;
|
||||||
|
}
|
||||||
|
public void setCount(double count) {
|
||||||
|
this.count = count;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
@GetMapping("/requests")
|
@GetMapping("/requests")
|
||||||
@Operation(summary = "POST request count",
|
@Operation(summary = "POST request count",
|
||||||
description = "This endpoint returns the total count of POST requests or the count of POST requests for a specific endpoint.")
|
description = "This endpoint returns the total count of POST requests or the count of POST requests for a specific endpoint.")
|
||||||
public Double getTotalRequests(@RequestParam Optional<String> endpoint) {
|
public ResponseEntity<?> getTotalRequests(@RequestParam(required = false, name = "endpoint") @Parameter(description = "endpoint") Optional<String> endpoint) {
|
||||||
try {
|
if (!metricsEnabled) {
|
||||||
Counter counter;
|
return ResponseEntity.status(HttpStatus.FORBIDDEN).body("This endpoint is disabled.");
|
||||||
if (endpoint.isPresent()) {
|
}
|
||||||
counter = meterRegistry.get("http.requests")
|
try {
|
||||||
.tags("method", "POST", "uri", endpoint.get()).counter();
|
double count = 0.0;
|
||||||
} else {
|
|
||||||
counter = meterRegistry.get("http.requests")
|
for (Meter meter : meterRegistry.getMeters()) {
|
||||||
.tags("method", "POST").counter();
|
if (meter.getId().getName().equals("http.requests")) {
|
||||||
}
|
String method = meter.getId().getTag("method");
|
||||||
return counter.count();
|
if (method != null && method.equals("POST")) {
|
||||||
} catch (Exception e) {
|
if (endpoint.isPresent() && !endpoint.get().isBlank()) {
|
||||||
return -1.0;
|
if (!endpoint.get().startsWith("/")) {
|
||||||
|
endpoint = Optional.of("/" + endpoint.get());
|
||||||
|
}
|
||||||
|
if (endpoint.get().equals(meter.getId().getTag("uri"))) {
|
||||||
|
if (meter instanceof Counter) {
|
||||||
|
count += ((Counter) meter).count();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
if (meter instanceof Counter) {
|
||||||
|
count += ((Counter) meter).count();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return ResponseEntity.ok(count);
|
||||||
|
} catch (Exception e) {
|
||||||
|
return ResponseEntity.ok(-1);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@GetMapping("/requests/all")
|
||||||
|
@Operation(summary = "POST requests count for all endpoints",
|
||||||
|
description = "This endpoint returns the count of POST requests for each endpoint.")
|
||||||
|
public ResponseEntity<?> getAllPostRequests() {
|
||||||
|
if (!metricsEnabled) {
|
||||||
|
return ResponseEntity.status(HttpStatus.FORBIDDEN).body("This endpoint is disabled.");
|
||||||
|
}
|
||||||
|
try {
|
||||||
|
Map<String, Double> counts = new HashMap<>();
|
||||||
|
|
||||||
|
for (Meter meter : meterRegistry.getMeters()) {
|
||||||
|
if (meter.getId().getName().equals("http.requests")) {
|
||||||
|
String method = meter.getId().getTag("method");
|
||||||
|
if (method != null && method.equals("POST")) {
|
||||||
|
String uri = meter.getId().getTag("uri");
|
||||||
|
if (uri != null) {
|
||||||
|
double currentCount = counts.getOrDefault(uri, 0.0);
|
||||||
|
if (meter instanceof Counter) {
|
||||||
|
currentCount += ((Counter) meter).count();
|
||||||
|
}
|
||||||
|
counts.put(uri, currentCount);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
List<EndpointCount> results = counts.entrySet().stream()
|
||||||
|
.map(entry -> new EndpointCount(entry.getKey(), entry.getValue()))
|
||||||
|
.sorted(Comparator.comparing(EndpointCount::getCount).reversed())
|
||||||
|
.collect(Collectors.toList());
|
||||||
|
|
||||||
|
return ResponseEntity.ok(results);
|
||||||
|
} catch (Exception e) {
|
||||||
|
return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@GetMapping("/uptime")
|
||||||
|
public ResponseEntity<?> getUptime() {
|
||||||
|
if (!metricsEnabled) {
|
||||||
|
return ResponseEntity.status(HttpStatus.FORBIDDEN).body("This endpoint is disabled.");
|
||||||
|
}
|
||||||
|
|
||||||
|
LocalDateTime now = LocalDateTime.now();
|
||||||
|
Duration uptime = Duration.between(StartupApplicationListener.startTime, now);
|
||||||
|
return ResponseEntity.ok(formatDuration(uptime));
|
||||||
|
}
|
||||||
|
|
||||||
|
private String formatDuration(Duration duration) {
|
||||||
|
long days = duration.toDays();
|
||||||
|
long hours = duration.toHoursPart();
|
||||||
|
long minutes = duration.toMinutesPart();
|
||||||
|
long seconds = duration.toSecondsPart();
|
||||||
|
return String.format("%dd %dh %dm %ds", days, hours, minutes, seconds);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user