sopdf 0.1.0__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (72) hide show
  1. sopdf-0.1.0/.gitignore +52 -0
  2. sopdf-0.1.0/LICENSE +183 -0
  3. sopdf-0.1.0/PKG-INFO +215 -0
  4. sopdf-0.1.0/README.md +186 -0
  5. sopdf-0.1.0/README_CN.md +199 -0
  6. sopdf-0.1.0/docs/assets/wechat.png +0 -0
  7. sopdf-0.1.0/docs/en/API.md +878 -0
  8. sopdf-0.1.0/docs/zh/API.md +878 -0
  9. sopdf-0.1.0/examples/01_open/open_from_bytes.py +12 -0
  10. sopdf-0.1.0/examples/01_open/open_from_path.py +15 -0
  11. sopdf-0.1.0/examples/01_open/open_with_password.py +7 -0
  12. sopdf-0.1.0/examples/02_render/render_batch.py +9 -0
  13. sopdf-0.1.0/examples/02_render/render_batch_parallel.py +9 -0
  14. sopdf-0.1.0/examples/02_render/render_single_page.py +8 -0
  15. sopdf-0.1.0/examples/02_render/render_to_file.py +9 -0
  16. sopdf-0.1.0/examples/03_extract_text/get_plain_text.py +9 -0
  17. sopdf-0.1.0/examples/03_extract_text/get_text_blocks.py +14 -0
  18. sopdf-0.1.0/examples/03_extract_text/get_text_in_region.py +13 -0
  19. sopdf-0.1.0/examples/04_search_text/search_basic.py +10 -0
  20. sopdf-0.1.0/examples/04_search_text/search_case_sensitive.py +14 -0
  21. sopdf-0.1.0/examples/04_search_text/search_with_context.py +13 -0
  22. sopdf-0.1.0/examples/05_split/split_each.py +8 -0
  23. sopdf-0.1.0/examples/05_split/split_pages.py +15 -0
  24. sopdf-0.1.0/examples/06_merge/append_doc.py +13 -0
  25. sopdf-0.1.0/examples/06_merge/merge_files.py +12 -0
  26. sopdf-0.1.0/examples/07_save_compress/save_basic.py +8 -0
  27. sopdf-0.1.0/examples/07_save_compress/save_to_bytes.py +12 -0
  28. sopdf-0.1.0/examples/07_save_compress/save_with_options.py +13 -0
  29. sopdf-0.1.0/examples/08_rotate/rotate_pages.py +14 -0
  30. sopdf-0.1.0/examples/09_decrypt/decrypt_and_save.py +11 -0
  31. sopdf-0.1.0/examples/10_repair/repair_corrupted.py +16 -0
  32. sopdf-0.1.0/pyproject.toml +54 -0
  33. sopdf-0.1.0/sopdf/__init__.py +76 -0
  34. sopdf-0.1.0/sopdf/_document.py +352 -0
  35. sopdf-0.1.0/sopdf/_exceptions.py +19 -0
  36. sopdf-0.1.0/sopdf/_merge.py +46 -0
  37. sopdf-0.1.0/sopdf/_page.py +283 -0
  38. sopdf-0.1.0/sopdf/_rect.py +134 -0
  39. sopdf-0.1.0/sopdf/_render.py +159 -0
  40. sopdf-0.1.0/sopdf/_text.py +34 -0
  41. sopdf-0.1.0/sopdf/_utils.py +137 -0
  42. sopdf-0.1.0/tests/__init__.py +0 -0
  43. sopdf-0.1.0/tests/benchmark/__init__.py +1 -0
  44. sopdf-0.1.0/tests/benchmark/bench_render.py +248 -0
  45. sopdf-0.1.0/tests/benchmark/bench_text.py +237 -0
  46. sopdf-0.1.0/tests/benchmark/fixtures/bench_50pages.pdf +708 -0
  47. sopdf-0.1.0/tests/benchmark/fixtures.py +201 -0
  48. sopdf-0.1.0/tests/benchmark/reports/report_20260330T123142Z.md +59 -0
  49. sopdf-0.1.0/tests/benchmark/run_benchmarks.py +232 -0
  50. sopdf-0.1.0/tests/conftest.py +167 -0
  51. sopdf-0.1.0/tests/fixtures/corrupted.pdf +0 -0
  52. sopdf-0.1.0/tests/fixtures/encrypted.pdf +33 -0
  53. sopdf-0.1.0/tests/fixtures/mixed.pdf +0 -0
  54. sopdf-0.1.0/tests/fixtures/multipage.pdf +0 -0
  55. sopdf-0.1.0/tests/fixtures/rotated.pdf +0 -0
  56. sopdf-0.1.0/tests/fixtures/simple.pdf +30 -0
  57. sopdf-0.1.0/tests/test_coverage_gaps.py +129 -0
  58. sopdf-0.1.0/tests/test_decrypt.py +34 -0
  59. sopdf-0.1.0/tests/test_document.py +135 -0
  60. sopdf-0.1.0/tests/test_exceptions.py +32 -0
  61. sopdf-0.1.0/tests/test_merge.py +51 -0
  62. sopdf-0.1.0/tests/test_page.py +57 -0
  63. sopdf-0.1.0/tests/test_parallel.py +40 -0
  64. sopdf-0.1.0/tests/test_rect.py +138 -0
  65. sopdf-0.1.0/tests/test_render.py +69 -0
  66. sopdf-0.1.0/tests/test_repair.py +32 -0
  67. sopdf-0.1.0/tests/test_rotate.py +52 -0
  68. sopdf-0.1.0/tests/test_save.py +70 -0
  69. sopdf-0.1.0/tests/test_search.py +68 -0
  70. sopdf-0.1.0/tests/test_split.py +60 -0
  71. sopdf-0.1.0/tests/test_sync.py +60 -0
  72. sopdf-0.1.0/tests/test_text.py +68 -0
sopdf-0.1.0/.gitignore ADDED
@@ -0,0 +1,52 @@
1
+ # Python
2
+ __pycache__/
3
+ *.py[cod]
4
+ *.pyo
5
+ *.pyd
6
+
7
+ # Virtual environments
8
+ .venv/
9
+ venv/
10
+ env/
11
+ ENV/
12
+
13
+ # Distribution / packaging
14
+ dist/
15
+ build/
16
+ *.egg-info/
17
+ *.egg
18
+ MANIFEST
19
+
20
+ # Testing & coverage
21
+ .pytest_cache/
22
+ .coverage
23
+ .coverage.*
24
+ htmlcov/
25
+ coverage.xml
26
+
27
+ # Type checking
28
+ .mypy_cache/
29
+ .ruff_cache/
30
+
31
+ # Editors
32
+ .idea/
33
+ .vscode/
34
+ *.swp
35
+ *.swo
36
+ *~
37
+
38
+ # macOS
39
+ .DS_Store
40
+ .AppleDouble
41
+ .LSOverride
42
+
43
+ # Windows
44
+ Thumbs.db
45
+ Desktop.ini
46
+
47
+ # Temporary output from tests (render output, split pages, etc.)
48
+ tests/output/
49
+
50
+ # Build artifacts
51
+ *.so
52
+ *.dylib
sopdf-0.1.0/LICENSE ADDED
@@ -0,0 +1,183 @@
1
+ Apache License
2
+ Version 2.0, January 2004
3
+ http://www.apache.org/licenses/
4
+
5
+ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
6
+
7
+ 1. Definitions.
8
+
9
+ "License" shall mean the terms and conditions for use, reproduction,
10
+ and distribution as defined by Sections 1 through 9 of this document.
11
+
12
+ "Licensor" shall mean the copyright owner or entity authorized by
13
+ the copyright owner that is granting the License.
14
+
15
+ "Legal Entity" shall mean the union of the acting entity and all
16
+ other entities that control, are controlled by, or are under common
17
+ control with that entity. For the purposes of this definition,
18
+ "control" means (i) the power, direct or indirect, to cause the
19
+ direction or management of such entity, whether by contract or
20
+ otherwise, or (ii) ownership of fifty percent (50%) or more of the
21
+ outstanding shares, or (iii) beneficial ownership of such entity.
22
+
23
+ "You" (or "Your") shall mean an individual or Legal Entity
24
+ exercising permissions granted by this License.
25
+
26
+ "Source" form shall mean the preferred form for making modifications,
27
+ including but not limited to software source code, documentation
28
+ source, and configuration files.
29
+
30
+ "Object" form shall mean any form resulting from mechanical
31
+ transformation or translation of a Source form, including but
32
+ not limited to compiled object code, generated documentation,
33
+ and conversions to other media types.
34
+
35
+ "Work" shall mean the work of authorship made available under
36
+ the License, as indicated by a copyright notice that is included in
37
+ or attached to the work (an example is provided in the Appendix below).
38
+
39
+ "Derivative Works" shall mean any work, whether in Source or Object
40
+ form, that is based on (or derived from) the Work and for which the
41
+ editorial revisions, annotations, elaborations, or other modifications
42
+ represent, as a whole, an original work of authorship. For the purposes
43
+ of this License, Derivative Works shall not include works that remain
44
+ separable from, or merely link (or bind by name) to the interfaces of,
45
+ the Work and Derivative Works thereof.
46
+
47
+ "Contribution" shall mean, as submitted to the Licensor for inclusion
48
+ in the Work by the copyright owner or by an individual or Legal Entity
49
+ authorized to submit on behalf of the copyright owner. For the purposes
50
+ of this definition, "submitted" means any form of electronic, verbal,
51
+ or written communication sent to the Licensor or its representatives,
52
+ including but not limited to communication on electronic mailing lists,
53
+ source code control systems, and issue tracking systems that are managed
54
+ by, or on behalf of, the Licensor for the purpose of discussing and
55
+ improving the Work, but excluding communication that is conspicuously
56
+ marked or designated in writing by the copyright owner as "Not a
57
+ Contribution."
58
+
59
+ "Contributor" shall mean Licensor and any Legal Entity on behalf of
60
+ whom a Contribution has been received by the Licensor and incorporated
61
+ within the Work.
62
+
63
+ 2. Grant of Copyright License. Subject to the terms and conditions of
64
+ this License, each Contributor hereby grants to You a perpetual,
65
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
66
+ copyright license to reproduce, prepare Derivative Works of,
67
+ publicly display, publicly perform, sublicense, and distribute the
68
+ Work and such Derivative Works in Source or Object form.
69
+
70
+ 3. Grant of Patent License. Subject to the terms and conditions of
71
+ this License, each Contributor hereby grants to You a perpetual,
72
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
73
+ (except as stated in this section) patent license to make, have made,
74
+ use, offer to sell, sell, import, and otherwise transfer the Work,
75
+ where such license applies only to those patent claims licensable
76
+ by such Contributor that are necessarily infringed by their
77
+ Contribution(s) alone or by the combination of their Contribution(s)
78
+ with the Work to which such Contribution(s) was submitted. If You
79
+ institute patent litigation against any entity (including a cross-claim
80
+ or counterclaim in a lawsuit) alleging that the Work or any
81
+ Contribution embodied within the Work constitutes direct or
82
+ contributory patent infringement, then any patent licenses granted to
83
+ You under this License for that Work shall terminate as of the date
84
+ such litigation is filed.
85
+
86
+ 4. Redistribution. You may reproduce and distribute copies of the
87
+ Work or Derivative Works thereof in any medium, with or without
88
+ modifications, and in Source or Object form, provided that You
89
+ meet the following conditions:
90
+
91
+ (a) You must give any other recipients of the Work or Derivative
92
+ Works a copy of this License; and
93
+
94
+ (b) You must cause any modified files to carry prominent notices
95
+ stating that You changed the files; and
96
+
97
+ (c) You must retain, in the Source form of any Derivative Works
98
+ that You distribute, all copyright, patent, trademark, and
99
+ attribution notices from the Source form of the Work,
100
+ excluding those notices that do not pertain to any part of
101
+ the Derivative Works; and
102
+
103
+ (d) If the Work includes a "NOTICE" text file as part of its
104
+ distribution, You must include a readable copy of the
105
+ attribution notices contained within such NOTICE file, in
106
+ at least one of the following places: within a NOTICE text
107
+ file distributed as part of the Derivative Works; within
108
+ the Source form or documentation, if provided along with the
109
+ Derivative Works; or, within a display generated by the
110
+ Derivative Works, if and wherever such third-party notices
111
+ normally appear. The contents of the NOTICE file are for
112
+ informational purposes only and do not modify the License.
113
+ You may add Your own attribution notices within Derivative
114
+ Works that You distribute, alongside or in addition to the
115
+ NOTICE text from the Work, provided that such additional
116
+ attribution notices cannot be construed as modifying the License.
117
+
118
+ You may add Your own license statement for Your modifications and
119
+ may provide additional grant of rights to use, copy, modify, merge,
120
+ publish, distribute, sublicense, and/or sell copies of the Work,
121
+ and to permit persons to whom the Work is furnished to do so.
122
+
123
+ 5. Submission of Contributions. Unless You explicitly state otherwise,
124
+ any Contribution intentionally submitted for inclusion in the Work
125
+ by You to the Licensor shall be under the terms and conditions of
126
+ this License, without any additional terms or conditions.
127
+ Notwithstanding the above, nothing herein shall supersede or modify
128
+ the terms of any separate license agreement you may have executed
129
+ with Licensor regarding such Contributions.
130
+
131
+ 6. Trademarks. This License does not grant permission to use the trade
132
+ names, trademarks, service marks, or product names of the Licensor,
133
+ except as required for reasonable and customary use in describing the
134
+ origin of the Work and reproducing the content of the NOTICE file.
135
+
136
+ 7. Disclaimer of Warranty. Unless required by applicable law or
137
+ agreed to in writing, Licensor provides the Work (and each
138
+ Contributor provides its Contributions) on an "AS IS" BASIS,
139
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
140
+ implied, including, without limitation, any warranties or conditions
141
+ of TITLE, MERCHANTIBILITY, NON-INFRINGEMENT, or FITNESS FOR A
142
+ PARTICULAR PURPOSE. You are solely responsible for determining the
143
+ appropriateness of using or reproducing the Work and assume any
144
+ risks associated with Your exercise of permissions under this License.
145
+
146
+ 8. Limitation of Liability. In no event and under no legal theory,
147
+ whether in tort (including negligence), contract, or otherwise,
148
+ unless required by applicable law (such as deliberate and grossly
149
+ negligent acts) or agreed to in writing, shall any Contributor be
150
+ liable to You for damages, including any direct, indirect, special,
151
+ incidental, or exemplary damages of any character arising as a
152
+ result of this License or out of the use or inability to use the
153
+ Work (including but not limited to damages for loss of goodwill,
154
+ work stoppage, computer failure or malfunction, or all other
155
+ commercial damages or losses), even if such Contributor has been
156
+ advised of the possibility of such damages.
157
+
158
+ 9. Accepting Warranty or Additional Liability. While redistributing
159
+ the Work or Derivative Works thereof, You may choose to offer,
160
+ and charge a fee for, acceptance of support, warranty, indemnity,
161
+ or other liability obligations and/or rights consistent with this
162
+ License. However, in accepting such obligations, You may act only
163
+ on Your own behalf and on Your sole responsibility, not on behalf
164
+ of any other Contributor, and only if You agree to indemnify,
165
+ defend, and hold each Contributor harmless for any liability
166
+ incurred by, or claims asserted against, such Contributor by reason
167
+ of your accepting any such warranty or additional liability.
168
+
169
+ END OF TERMS AND CONDITIONS
170
+
171
+ Copyright 2025 Kevin Qiu
172
+
173
+ Licensed under the Apache License, Version 2.0 (the "License");
174
+ you may not use this file except in compliance with the License.
175
+ You may obtain a copy of the License at
176
+
177
+ http://www.apache.org/licenses/LICENSE-2.0
178
+
179
+ Unless required by applicable law or agreed to in writing, software
180
+ distributed under the License is distributed on an "AS IS" BASIS,
181
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
182
+ See the License for the specific language governing permissions and
183
+ limitations under the License.
sopdf-0.1.0/PKG-INFO ADDED
@@ -0,0 +1,215 @@
1
+ Metadata-Version: 2.4
2
+ Name: sopdf
3
+ Version: 0.1.0
4
+ Summary: A high-performance PDF processing library with a permissive license.
5
+ Project-URL: Homepage, https://github.com/SoMarkAI/SoPDF
6
+ Project-URL: Repository, https://github.com/SoMarkAI/SoPDF
7
+ Project-URL: Bug Tracker, https://github.com/SoMarkAI/SoPDF/issues
8
+ License: Apache-2.0
9
+ License-File: LICENSE
10
+ Keywords: document,pdf,pikepdf,pypdfium2,render,text-extraction
11
+ Classifier: Development Status :: 3 - Alpha
12
+ Classifier: Intended Audience :: Developers
13
+ Classifier: License :: OSI Approved :: Apache Software License
14
+ Classifier: Programming Language :: Python :: 3
15
+ Classifier: Programming Language :: Python :: 3.10
16
+ Classifier: Programming Language :: Python :: 3.11
17
+ Classifier: Programming Language :: Python :: 3.12
18
+ Classifier: Topic :: Multimedia :: Graphics
19
+ Classifier: Topic :: Software Development :: Libraries :: Python Modules
20
+ Classifier: Topic :: Text Processing
21
+ Requires-Python: >=3.10
22
+ Requires-Dist: pikepdf>=8.0
23
+ Requires-Dist: pillow>=9.0
24
+ Requires-Dist: pypdfium2>=4.0
25
+ Provides-Extra: dev
26
+ Requires-Dist: pytest-cov>=4.0; extra == 'dev'
27
+ Requires-Dist: pytest>=7.0; extra == 'dev'
28
+ Description-Content-Type: text/markdown
29
+
30
+ <div align="center">
31
+
32
+ # SoPDF
33
+
34
+ **The PDF processing library that belongs to everyone.**
35
+
36
+ [![PyPI version](https://img.shields.io/pypi/v/sopdf.svg)](https://pypi.org/project/sopdf/)
37
+ [![Python versions](https://img.shields.io/pypi/pyversions/sopdf.svg)](https://pypi.org/project/sopdf/)
38
+ [![License](https://img.shields.io/badge/License-Apache%202.0-blue.svg)](LICENSE)
39
+
40
+ ```
41
+ pip install sopdf
42
+ ```
43
+
44
+ English | [中文](README_CN.md)
45
+
46
+ </div>
47
+
48
+ ---
49
+
50
+ ## Why SoPDF?
51
+
52
+ **1. 🚀 High Performance**
53
+
54
+ With parallel processing and other optimizations, SoPDF significantly outperforms alternatives: rendering up to **1.56x faster**, plain text extraction **2.7x faster**, and full-text search **3x faster** — while maintaining **99% word-level accuracy consistency** with PyMuPDF. See the [Performance Benchmark](#performance-benchmark) section, or run it yourself.
55
+
56
+ **2. ✨ Feature-Rich**
57
+
58
+ Built on [`pypdfium2`](https://github.com/pypdfium2-team/pypdfium2) (Google PDFium, for rendering and text) and [`pikepdf`](https://github.com/pikepdf/pikepdf) (libqpdf, for structure and writing). SoPDF covers the entire workflow from rendering and text extraction to structural editing.
59
+
60
+ **3. 🎯 Clean API**
61
+
62
+ Intuition as documentation. You would have designed it the same way.
63
+
64
+ **4. 🔓 Permissive License**
65
+
66
+ In PDF processing, feature-rich + open source often comes with a license unfriendly to the open-source ecosystem. But SoPDF delivers equivalent core capabilities under the **Apache 2.0 License** — no strings attached, no license audit, zero friction. Embed it, ship it, fork it. It's yours.
67
+
68
+ > If you find SoPDF helpful, please consider giving it a ⭐ Star — it really means a lot to us. Every star fuels our motivation to keep improving.
69
+
70
+ ---
71
+
72
+ ## Benchmarks
73
+
74
+ > Measured on Apple M-series (arm64, 10-core), Python 3.10, against a 50-page PDF fixture.
75
+ > Run the suite yourself: `python tests/benchmark/run_benchmarks.py`
76
+
77
+ ### Rendering vs PyMuPDF
78
+
79
+ | Scenario | SoPDF | PyMuPDF | Speedup |
80
+ | --- | --- | --- | --- |
81
+ | Open document | 0.1 ms | 0.2 ms | **1.39× faster** |
82
+ | Render 1 page @ 72 DPI | 6.6 ms | 9.1 ms | **1.38× faster** |
83
+ | Render 1 page @ 150 DPI | 20.0 ms | 30.3 ms | **1.51× faster** |
84
+ | Render 1 page @ 300 DPI | 64.6 ms | 101.1 ms | **1.56× faster** |
85
+ | 50 pages sequential @ 150 DPI | 966.9 ms | 1470.3 ms | **1.52× faster** |
86
+ | 50 pages parallel @ 150 DPI | 410.7 ms | 447.2 ms | **1.09× faster** |
87
+
88
+ SoPDF wins at every DPI — and the margin widens at higher resolutions. In parallel mode, SoPDF achieves a genuine **2.35× speedup** over its own sequential baseline. PyMuPDF's thread-parallel path, on the other hand, actually *regresses* to 1548.9 ms (slower than sequential) because MuPDF serialises concurrent renders behind a global lock.
89
+
90
+ ### Text Extraction vs PyMuPDF
91
+
92
+ | Scenario | SoPDF | PyMuPDF | Speedup |
93
+ | --- | --- | --- | --- |
94
+ | Plain text — 50 pages | 26.0 ms | 70.0 ms | **2.70× faster** |
95
+ | Text blocks — 50 pages | 63.6 ms | 70.4 ms | **1.11× faster** |
96
+ | Search 'benchmark' — 50 pages | 30.2 ms | 91.0 ms | **3.01× faster** |
97
+ | Region extract — 50 pages | 27.6 ms | 39.6 ms | **1.43× faster** |
98
+
99
+ Text search is the standout: **3× faster** than PyMuPDF. Plain-text extraction follows at **2.7×**. Correctness is verified — sopdf and PyMuPDF produce 99% word-level overlap on the same document, so the speed advantage carries no accuracy trade-off.
100
+
101
+ ---
102
+
103
+ ## Architecture
104
+
105
+ SoPDF runs two best-in-class C/C++ engines in tandem:
106
+
107
+ ```
108
+ ┌──────────────────────────────────────────┐
109
+ │ SoPDF Python API │
110
+ ├───────────────────┬──────────────────────┤
111
+ │ pypdfium2 │ pikepdf │
112
+ │ (Google PDFium) │ (libqpdf) │
113
+ │ │ │
114
+ │ • Rendering │ • Structure reads │
115
+ │ • Text extract │ • All writes │
116
+ │ • Search │ • Save / compress │
117
+ └───────────────────┴──────────────────────┘
118
+ ```
119
+
120
+ A **dirty-flag + hot-reload** mechanism keeps the two engines in sync:
121
+ when you write via pikepdf (e.g. rotate a page), the next read operation
122
+ (e.g. render) automatically reserialises the document into pypdfium2 —
123
+ zero manual sync required.
124
+
125
+ Files are opened with **lazy loading / mmap** — a 500 MB PDF opens in
126
+ milliseconds and only the pages you actually access are loaded.
127
+
128
+ ---
129
+
130
+ ## Quick Start
131
+
132
+ ```bash
133
+ pip install sopdf
134
+ ```
135
+
136
+ Requires Python 3.10+. The two native dependencies (`pypdfium2`, `pikepdf`) ship pre-built wheels for macOS, Linux, and Windows — no compiler needed.
137
+
138
+ ```python
139
+ import sopdf
140
+
141
+ # --- Open ---
142
+ # from a file path (near-instant thanks to lazy loading & mmap)
143
+ with sopdf.open("document.pdf") as doc:
144
+
145
+ # --- Render ---
146
+ img_bytes = doc[0].render(dpi=150) # PNG bytes
147
+ doc[0].render_to_file("page0.png", dpi=300) # write to disk
148
+
149
+ # parallel rendering across all pages
150
+ images = sopdf.render_pages(doc.pages, dpi=150, parallel=True)
151
+
152
+ # --- Extract text ---
153
+ text = doc[0].get_text()
154
+ blocks = doc[0].get_text_blocks() # list[TextBlock] with bounding boxes
155
+
156
+ # --- Search ---
157
+ hits = doc[0].search("invoice", match_case=False) # list[Rect]
158
+
159
+ # --- Split & merge ---
160
+ new_doc = doc.split(pages=[0, 1, 2], output="chapter1.pdf")
161
+ doc.split_each(output_dir="pages/")
162
+ sopdf.merge(["intro.pdf", "body.pdf"], output="book.pdf")
163
+
164
+ # --- Save ---
165
+ doc.append(new_doc)
166
+ doc.save("out.pdf", compress=True, garbage=True)
167
+ raw = doc.to_bytes() # no disk write
168
+
169
+ # --- Rotate ---
170
+ doc[0].rotation = 90
171
+
172
+ # --- Encrypted PDFs ---
173
+ with sopdf.open("protected.pdf", password="hunter2") as doc:
174
+ doc.save("unlocked.pdf") # encryption stripped on save
175
+
176
+ # --- Open from bytes / stream ---
177
+ with open("document.pdf", "rb") as f:
178
+ with sopdf.open(stream=f.read()) as doc:
179
+ print(doc.page_count)
180
+
181
+ # --- Auto-repair corrupted PDFs ---
182
+ with sopdf.open("corrupted.pdf") as doc:
183
+ doc.save("repaired.pdf")
184
+ ```
185
+
186
+ ---
187
+
188
+ ## Features
189
+
190
+ | Capability | Examples |
191
+ |---|---|
192
+ | Open from path / bytes / stream | [01_open](examples/01_open) |
193
+ | Render pages to PNG / JPEG | [02_render](examples/02_render) |
194
+ | Batch & parallel rendering | [02_render](examples/02_render) |
195
+ | Extract plain text | [03_extract_text](examples/03_extract_text) |
196
+ | Extract text with bounding boxes | [03_extract_text](examples/03_extract_text) |
197
+ | Full-text search with hit rects | [04_search_text](examples/04_search_text) |
198
+ | Split pages into new document | [05_split](examples/05_split) |
199
+ | Merge multiple PDFs | [06_merge](examples/06_merge) |
200
+ | Save with compression | [07_save_compress](examples/07_save_compress) |
201
+ | Serialise to bytes (no disk write) | [07_save_compress](examples/07_save_compress) |
202
+ | Rotate pages | [08_rotate](examples/08_rotate) |
203
+ | Open & save encrypted PDFs | [09_decrypt](examples/09_decrypt) |
204
+ | Auto-repair corrupted PDFs | [10_repair](examples/10_repair) |
205
+
206
+ ---
207
+
208
+ ## License
209
+
210
+ Apache 2.0 — see [LICENSE](LICENSE).
211
+
212
+ SoPDF is free to use in personal projects, commercial products, and open-source libraries. No licensing fees, no attribution requirements beyond the standard Apache 2.0 notice.
213
+
214
+ ## WeChat Group
215
+ <img src="./docs/assets/wechat.png" width="100">
sopdf-0.1.0/README.md ADDED
@@ -0,0 +1,186 @@
1
+ <div align="center">
2
+
3
+ # SoPDF
4
+
5
+ **The PDF processing library that belongs to everyone.**
6
+
7
+ [![PyPI version](https://img.shields.io/pypi/v/sopdf.svg)](https://pypi.org/project/sopdf/)
8
+ [![Python versions](https://img.shields.io/pypi/pyversions/sopdf.svg)](https://pypi.org/project/sopdf/)
9
+ [![License](https://img.shields.io/badge/License-Apache%202.0-blue.svg)](LICENSE)
10
+
11
+ ```
12
+ pip install sopdf
13
+ ```
14
+
15
+ English | [中文](README_CN.md)
16
+
17
+ </div>
18
+
19
+ ---
20
+
21
+ ## Why SoPDF?
22
+
23
+ **1. 🚀 High Performance**
24
+
25
+ With parallel processing and other optimizations, SoPDF significantly outperforms alternatives: rendering up to **1.56x faster**, plain text extraction **2.7x faster**, and full-text search **3x faster** — while maintaining **99% word-level accuracy consistency** with PyMuPDF. See the [Performance Benchmark](#performance-benchmark) section, or run it yourself.
26
+
27
+ **2. ✨ Feature-Rich**
28
+
29
+ Built on [`pypdfium2`](https://github.com/pypdfium2-team/pypdfium2) (Google PDFium, for rendering and text) and [`pikepdf`](https://github.com/pikepdf/pikepdf) (libqpdf, for structure and writing). SoPDF covers the entire workflow from rendering and text extraction to structural editing.
30
+
31
+ **3. 🎯 Clean API**
32
+
33
+ Intuition as documentation. You would have designed it the same way.
34
+
35
+ **4. 🔓 Permissive License**
36
+
37
+ In PDF processing, feature-rich + open source often comes with a license unfriendly to the open-source ecosystem. But SoPDF delivers equivalent core capabilities under the **Apache 2.0 License** — no strings attached, no license audit, zero friction. Embed it, ship it, fork it. It's yours.
38
+
39
+ > If you find SoPDF helpful, please consider giving it a ⭐ Star — it really means a lot to us. Every star fuels our motivation to keep improving.
40
+
41
+ ---
42
+
43
+ ## Benchmarks
44
+
45
+ > Measured on Apple M-series (arm64, 10-core), Python 3.10, against a 50-page PDF fixture.
46
+ > Run the suite yourself: `python tests/benchmark/run_benchmarks.py`
47
+
48
+ ### Rendering vs PyMuPDF
49
+
50
+ | Scenario | SoPDF | PyMuPDF | Speedup |
51
+ | --- | --- | --- | --- |
52
+ | Open document | 0.1 ms | 0.2 ms | **1.39× faster** |
53
+ | Render 1 page @ 72 DPI | 6.6 ms | 9.1 ms | **1.38× faster** |
54
+ | Render 1 page @ 150 DPI | 20.0 ms | 30.3 ms | **1.51× faster** |
55
+ | Render 1 page @ 300 DPI | 64.6 ms | 101.1 ms | **1.56× faster** |
56
+ | 50 pages sequential @ 150 DPI | 966.9 ms | 1470.3 ms | **1.52× faster** |
57
+ | 50 pages parallel @ 150 DPI | 410.7 ms | 447.2 ms | **1.09× faster** |
58
+
59
+ SoPDF wins at every DPI — and the margin widens at higher resolutions. In parallel mode, SoPDF achieves a genuine **2.35× speedup** over its own sequential baseline. PyMuPDF's thread-parallel path, on the other hand, actually *regresses* to 1548.9 ms (slower than sequential) because MuPDF serialises concurrent renders behind a global lock.
60
+
61
+ ### Text Extraction vs PyMuPDF
62
+
63
+ | Scenario | SoPDF | PyMuPDF | Speedup |
64
+ | --- | --- | --- | --- |
65
+ | Plain text — 50 pages | 26.0 ms | 70.0 ms | **2.70× faster** |
66
+ | Text blocks — 50 pages | 63.6 ms | 70.4 ms | **1.11× faster** |
67
+ | Search 'benchmark' — 50 pages | 30.2 ms | 91.0 ms | **3.01× faster** |
68
+ | Region extract — 50 pages | 27.6 ms | 39.6 ms | **1.43× faster** |
69
+
70
+ Text search is the standout: **3× faster** than PyMuPDF. Plain-text extraction follows at **2.7×**. Correctness is verified — sopdf and PyMuPDF produce 99% word-level overlap on the same document, so the speed advantage carries no accuracy trade-off.
71
+
72
+ ---
73
+
74
+ ## Architecture
75
+
76
+ SoPDF runs two best-in-class C/C++ engines in tandem:
77
+
78
+ ```
79
+ ┌──────────────────────────────────────────┐
80
+ │ SoPDF Python API │
81
+ ├───────────────────┬──────────────────────┤
82
+ │ pypdfium2 │ pikepdf │
83
+ │ (Google PDFium) │ (libqpdf) │
84
+ │ │ │
85
+ │ • Rendering │ • Structure reads │
86
+ │ • Text extract │ • All writes │
87
+ │ • Search │ • Save / compress │
88
+ └───────────────────┴──────────────────────┘
89
+ ```
90
+
91
+ A **dirty-flag + hot-reload** mechanism keeps the two engines in sync:
92
+ when you write via pikepdf (e.g. rotate a page), the next read operation
93
+ (e.g. render) automatically reserialises the document into pypdfium2 —
94
+ zero manual sync required.
95
+
96
+ Files are opened with **lazy loading / mmap** — a 500 MB PDF opens in
97
+ milliseconds and only the pages you actually access are loaded.
98
+
99
+ ---
100
+
101
+ ## Quick Start
102
+
103
+ ```bash
104
+ pip install sopdf
105
+ ```
106
+
107
+ Requires Python 3.10+. The two native dependencies (`pypdfium2`, `pikepdf`) ship pre-built wheels for macOS, Linux, and Windows — no compiler needed.
108
+
109
+ ```python
110
+ import sopdf
111
+
112
+ # --- Open ---
113
+ # from a file path (near-instant thanks to lazy loading & mmap)
114
+ with sopdf.open("document.pdf") as doc:
115
+
116
+ # --- Render ---
117
+ img_bytes = doc[0].render(dpi=150) # PNG bytes
118
+ doc[0].render_to_file("page0.png", dpi=300) # write to disk
119
+
120
+ # parallel rendering across all pages
121
+ images = sopdf.render_pages(doc.pages, dpi=150, parallel=True)
122
+
123
+ # --- Extract text ---
124
+ text = doc[0].get_text()
125
+ blocks = doc[0].get_text_blocks() # list[TextBlock] with bounding boxes
126
+
127
+ # --- Search ---
128
+ hits = doc[0].search("invoice", match_case=False) # list[Rect]
129
+
130
+ # --- Split & merge ---
131
+ new_doc = doc.split(pages=[0, 1, 2], output="chapter1.pdf")
132
+ doc.split_each(output_dir="pages/")
133
+ sopdf.merge(["intro.pdf", "body.pdf"], output="book.pdf")
134
+
135
+ # --- Save ---
136
+ doc.append(new_doc)
137
+ doc.save("out.pdf", compress=True, garbage=True)
138
+ raw = doc.to_bytes() # no disk write
139
+
140
+ # --- Rotate ---
141
+ doc[0].rotation = 90
142
+
143
+ # --- Encrypted PDFs ---
144
+ with sopdf.open("protected.pdf", password="hunter2") as doc:
145
+ doc.save("unlocked.pdf") # encryption stripped on save
146
+
147
+ # --- Open from bytes / stream ---
148
+ with open("document.pdf", "rb") as f:
149
+ with sopdf.open(stream=f.read()) as doc:
150
+ print(doc.page_count)
151
+
152
+ # --- Auto-repair corrupted PDFs ---
153
+ with sopdf.open("corrupted.pdf") as doc:
154
+ doc.save("repaired.pdf")
155
+ ```
156
+
157
+ ---
158
+
159
+ ## Features
160
+
161
+ | Capability | Examples |
162
+ |---|---|
163
+ | Open from path / bytes / stream | [01_open](examples/01_open) |
164
+ | Render pages to PNG / JPEG | [02_render](examples/02_render) |
165
+ | Batch & parallel rendering | [02_render](examples/02_render) |
166
+ | Extract plain text | [03_extract_text](examples/03_extract_text) |
167
+ | Extract text with bounding boxes | [03_extract_text](examples/03_extract_text) |
168
+ | Full-text search with hit rects | [04_search_text](examples/04_search_text) |
169
+ | Split pages into new document | [05_split](examples/05_split) |
170
+ | Merge multiple PDFs | [06_merge](examples/06_merge) |
171
+ | Save with compression | [07_save_compress](examples/07_save_compress) |
172
+ | Serialise to bytes (no disk write) | [07_save_compress](examples/07_save_compress) |
173
+ | Rotate pages | [08_rotate](examples/08_rotate) |
174
+ | Open & save encrypted PDFs | [09_decrypt](examples/09_decrypt) |
175
+ | Auto-repair corrupted PDFs | [10_repair](examples/10_repair) |
176
+
177
+ ---
178
+
179
+ ## License
180
+
181
+ Apache 2.0 — see [LICENSE](LICENSE).
182
+
183
+ SoPDF is free to use in personal projects, commercial products, and open-source libraries. No licensing fees, no attribution requirements beyond the standard Apache 2.0 notice.
184
+
185
+ ## WeChat Group
186
+ <img src="./docs/assets/wechat.png" width="100">