react-native-pdf-sdk 0.0.0-stage → 0.1.1
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.
- package/CHANGELOG.md +24 -0
- package/LICENSE +21 -0
- package/PdfSdk.podspec +32 -0
- package/README.md +270 -2
- package/android/CMakeLists.txt +24 -0
- package/android/build.gradle +132 -0
- package/android/gradle.properties +4 -0
- package/android/src/main/AndroidManifest.xml +1 -0
- package/android/src/main/cpp/cpp-adapter.cpp +9 -0
- package/android/src/main/java/com/margelo/nitro/pdfsdk/HybridPdfDocument.kt +72 -0
- package/android/src/main/java/com/margelo/nitro/pdfsdk/HybridPdfSdk.kt +17 -0
- package/android/src/main/java/com/margelo/nitro/pdfsdk/HybridPdfView.kt +194 -0
- package/android/src/main/java/com/margelo/nitro/pdfsdk/PdfDocumentLayout.kt +109 -0
- package/android/src/main/java/com/margelo/nitro/pdfsdk/PdfFailure.kt +36 -0
- package/android/src/main/java/com/margelo/nitro/pdfsdk/PdfPageSource.kt +141 -0
- package/android/src/main/java/com/margelo/nitro/pdfsdk/PdfSdkPackage.kt +35 -0
- package/android/src/main/java/com/margelo/nitro/pdfsdk/PdfSourceLoader.kt +117 -0
- package/android/src/main/java/com/margelo/nitro/pdfsdk/PdfSurface.kt +839 -0
- package/android/src/main/java/com/margelo/nitro/pdfsdk/PdfTileStore.kt +210 -0
- package/android/src/main/java/com/margelo/nitro/pdfsdk/PdfViewport.kt +255 -0
- package/android/src/main/java/com/margelo/nitro/pdfsdk/TileGrid.kt +276 -0
- package/ios/HybridPdfDocument.swift +71 -0
- package/ios/HybridPdfSdk.swift +10 -0
- package/ios/HybridPdfView.swift +202 -0
- package/ios/PdfDocumentLayout.swift +174 -0
- package/ios/PdfFailure.swift +46 -0
- package/ios/PdfImageCache.swift +83 -0
- package/ios/PdfPageSource.swift +221 -0
- package/ios/PdfScrollPlan.swift +108 -0
- package/ios/PdfSourceLoader.swift +120 -0
- package/ios/PdfSurface.swift +711 -0
- package/ios/PdfTileCanvas.swift +148 -0
- package/ios/PdfTileGrid.swift +518 -0
- package/ios/PdfTileStore.swift +346 -0
- package/ios/PdfViewport.swift +200 -0
- package/lib/commonjs/PdfView.js +149 -0
- package/lib/commonjs/PdfView.js.map +1 -0
- package/lib/commonjs/document.js +38 -0
- package/lib/commonjs/document.js.map +1 -0
- package/lib/commonjs/index.js +26 -0
- package/lib/commonjs/index.js.map +1 -0
- package/lib/commonjs/package.json +1 -0
- package/lib/commonjs/specs/PdfSdk.nitro.js +6 -0
- package/lib/commonjs/specs/PdfSdk.nitro.js.map +1 -0
- package/lib/commonjs/specs/PdfView.nitro.js +6 -0
- package/lib/commonjs/specs/PdfView.nitro.js.map +1 -0
- package/lib/module/PdfView.js +143 -0
- package/lib/module/PdfView.js.map +1 -0
- package/lib/module/document.js +34 -0
- package/lib/module/document.js.map +1 -0
- package/lib/module/index.js +5 -0
- package/lib/module/index.js.map +1 -0
- package/lib/module/specs/PdfSdk.nitro.js +4 -0
- package/lib/module/specs/PdfSdk.nitro.js.map +1 -0
- package/lib/module/specs/PdfView.nitro.js +4 -0
- package/lib/module/specs/PdfView.nitro.js.map +1 -0
- package/lib/typescript/src/PdfView.d.ts +65 -0
- package/lib/typescript/src/PdfView.d.ts.map +1 -0
- package/lib/typescript/src/document.d.ts +22 -0
- package/lib/typescript/src/document.d.ts.map +1 -0
- package/lib/typescript/src/index.d.ts +6 -0
- package/lib/typescript/src/index.d.ts.map +1 -0
- package/lib/typescript/src/specs/PdfSdk.nitro.d.ts +60 -0
- package/lib/typescript/src/specs/PdfSdk.nitro.d.ts.map +1 -0
- package/lib/typescript/src/specs/PdfView.nitro.d.ts +131 -0
- package/lib/typescript/src/specs/PdfView.nitro.d.ts.map +1 -0
- package/nitro.json +40 -0
- package/nitrogen/generated/.gitattributes +1 -0
- package/nitrogen/generated/android/PdfSdk+autolinking.cmake +87 -0
- package/nitrogen/generated/android/PdfSdk+autolinking.gradle +27 -0
- package/nitrogen/generated/android/PdfSdkOnLoad.cpp +84 -0
- package/nitrogen/generated/android/PdfSdkOnLoad.hpp +34 -0
- package/nitrogen/generated/android/c++/JFunc_void_PdfErrorEvent.hpp +80 -0
- package/nitrogen/generated/android/c++/JFunc_void_PdfLoadEvent.hpp +77 -0
- package/nitrogen/generated/android/c++/JFunc_void_PdfPageEvent.hpp +77 -0
- package/nitrogen/generated/android/c++/JFunc_void_PdfTapEvent.hpp +77 -0
- package/nitrogen/generated/android/c++/JFunc_void_PdfZoomEvent.hpp +77 -0
- package/nitrogen/generated/android/c++/JHybridNativePdfViewSpec.cpp +266 -0
- package/nitrogen/generated/android/c++/JHybridNativePdfViewSpec.hpp +92 -0
- package/nitrogen/generated/android/c++/JHybridPdfDocumentSpec.cpp +87 -0
- package/nitrogen/generated/android/c++/JHybridPdfDocumentSpec.hpp +65 -0
- package/nitrogen/generated/android/c++/JHybridPdfSdkSpec.cpp +58 -0
- package/nitrogen/generated/android/c++/JHybridPdfSdkSpec.hpp +63 -0
- package/nitrogen/generated/android/c++/JPdfErrorCode.hpp +67 -0
- package/nitrogen/generated/android/c++/JPdfErrorEvent.hpp +63 -0
- package/nitrogen/generated/android/c++/JPdfFit.hpp +64 -0
- package/nitrogen/generated/android/c++/JPdfLoadEvent.hpp +65 -0
- package/nitrogen/generated/android/c++/JPdfPageEvent.hpp +61 -0
- package/nitrogen/generated/android/c++/JPdfPageSize.hpp +61 -0
- package/nitrogen/generated/android/c++/JPdfRenderRequest.hpp +86 -0
- package/nitrogen/generated/android/c++/JPdfTapEvent.hpp +65 -0
- package/nitrogen/generated/android/c++/JPdfViewSource.hpp +84 -0
- package/nitrogen/generated/android/c++/JPdfZoomEvent.hpp +61 -0
- package/nitrogen/generated/android/c++/views/JHybridNativePdfViewStateUpdater.cpp +108 -0
- package/nitrogen/generated/android/c++/views/JHybridNativePdfViewStateUpdater.hpp +49 -0
- package/nitrogen/generated/android/kotlin/com/margelo/nitro/pdfsdk/Func_void_PdfErrorEvent.kt +80 -0
- package/nitrogen/generated/android/kotlin/com/margelo/nitro/pdfsdk/Func_void_PdfLoadEvent.kt +80 -0
- package/nitrogen/generated/android/kotlin/com/margelo/nitro/pdfsdk/Func_void_PdfPageEvent.kt +80 -0
- package/nitrogen/generated/android/kotlin/com/margelo/nitro/pdfsdk/Func_void_PdfTapEvent.kt +80 -0
- package/nitrogen/generated/android/kotlin/com/margelo/nitro/pdfsdk/Func_void_PdfZoomEvent.kt +80 -0
- package/nitrogen/generated/android/kotlin/com/margelo/nitro/pdfsdk/HybridNativePdfViewSpec.kt +185 -0
- package/nitrogen/generated/android/kotlin/com/margelo/nitro/pdfsdk/HybridPdfDocumentSpec.kt +65 -0
- package/nitrogen/generated/android/kotlin/com/margelo/nitro/pdfsdk/HybridPdfSdkSpec.kt +54 -0
- package/nitrogen/generated/android/kotlin/com/margelo/nitro/pdfsdk/PdfErrorCode.kt +26 -0
- package/nitrogen/generated/android/kotlin/com/margelo/nitro/pdfsdk/PdfErrorEvent.kt +56 -0
- package/nitrogen/generated/android/kotlin/com/margelo/nitro/pdfsdk/PdfFit.kt +25 -0
- package/nitrogen/generated/android/kotlin/com/margelo/nitro/pdfsdk/PdfLoadEvent.kt +61 -0
- package/nitrogen/generated/android/kotlin/com/margelo/nitro/pdfsdk/PdfPageEvent.kt +56 -0
- package/nitrogen/generated/android/kotlin/com/margelo/nitro/pdfsdk/PdfPageSize.kt +56 -0
- package/nitrogen/generated/android/kotlin/com/margelo/nitro/pdfsdk/PdfRenderRequest.kt +86 -0
- package/nitrogen/generated/android/kotlin/com/margelo/nitro/pdfsdk/PdfSdkOnLoad.kt +35 -0
- package/nitrogen/generated/android/kotlin/com/margelo/nitro/pdfsdk/PdfTapEvent.kt +61 -0
- package/nitrogen/generated/android/kotlin/com/margelo/nitro/pdfsdk/PdfViewSource.kt +66 -0
- package/nitrogen/generated/android/kotlin/com/margelo/nitro/pdfsdk/PdfZoomEvent.kt +56 -0
- package/nitrogen/generated/android/kotlin/com/margelo/nitro/pdfsdk/views/HybridNativePdfViewManager.kt +80 -0
- package/nitrogen/generated/android/kotlin/com/margelo/nitro/pdfsdk/views/HybridNativePdfViewStateUpdater.kt +23 -0
- package/nitrogen/generated/ios/PdfSdk+autolinking.rb +62 -0
- package/nitrogen/generated/ios/PdfSdk-Swift-Cxx-Bridge.cpp +123 -0
- package/nitrogen/generated/ios/PdfSdk-Swift-Cxx-Bridge.hpp +474 -0
- package/nitrogen/generated/ios/PdfSdk-Swift-Cxx-Umbrella.hpp +88 -0
- package/nitrogen/generated/ios/PdfSdkAutolinking.mm +41 -0
- package/nitrogen/generated/ios/PdfSdkAutolinking.swift +38 -0
- package/nitrogen/generated/ios/c++/HybridNativePdfViewSpecSwift.cpp +11 -0
- package/nitrogen/generated/ios/c++/HybridNativePdfViewSpecSwift.hpp +209 -0
- package/nitrogen/generated/ios/c++/HybridPdfDocumentSpecSwift.cpp +11 -0
- package/nitrogen/generated/ios/c++/HybridPdfDocumentSpecSwift.hpp +105 -0
- package/nitrogen/generated/ios/c++/HybridPdfSdkSpecSwift.cpp +11 -0
- package/nitrogen/generated/ios/c++/HybridPdfSdkSpecSwift.hpp +86 -0
- package/nitrogen/generated/ios/c++/views/HybridNativePdfViewComponent.mm +187 -0
- package/nitrogen/generated/ios/swift/Func_void_PdfErrorEvent.swift +46 -0
- package/nitrogen/generated/ios/swift/Func_void_PdfLoadEvent.swift +46 -0
- package/nitrogen/generated/ios/swift/Func_void_PdfPageEvent.swift +46 -0
- package/nitrogen/generated/ios/swift/Func_void_PdfTapEvent.swift +46 -0
- package/nitrogen/generated/ios/swift/Func_void_PdfZoomEvent.swift +46 -0
- package/nitrogen/generated/ios/swift/Func_void_std__exception_ptr.swift +46 -0
- package/nitrogen/generated/ios/swift/Func_void_std__string.swift +46 -0
- package/nitrogen/generated/ios/swift/HybridNativePdfViewSpec.swift +70 -0
- package/nitrogen/generated/ios/swift/HybridNativePdfViewSpec_cxx.swift +438 -0
- package/nitrogen/generated/ios/swift/HybridPdfDocumentSpec.swift +57 -0
- package/nitrogen/generated/ios/swift/HybridPdfDocumentSpec_cxx.swift +173 -0
- package/nitrogen/generated/ios/swift/HybridPdfSdkSpec.swift +55 -0
- package/nitrogen/generated/ios/swift/HybridPdfSdkSpec_cxx.swift +148 -0
- package/nitrogen/generated/ios/swift/PdfErrorCode.swift +52 -0
- package/nitrogen/generated/ios/swift/PdfErrorEvent.swift +34 -0
- package/nitrogen/generated/ios/swift/PdfFit.swift +48 -0
- package/nitrogen/generated/ios/swift/PdfLoadEvent.swift +39 -0
- package/nitrogen/generated/ios/swift/PdfPageEvent.swift +34 -0
- package/nitrogen/generated/ios/swift/PdfPageSize.swift +34 -0
- package/nitrogen/generated/ios/swift/PdfRenderRequest.swift +129 -0
- package/nitrogen/generated/ios/swift/PdfTapEvent.swift +39 -0
- package/nitrogen/generated/ios/swift/PdfViewSource.swift +97 -0
- package/nitrogen/generated/ios/swift/PdfZoomEvent.swift +34 -0
- package/nitrogen/generated/shared/c++/HybridNativePdfViewSpec.cpp +51 -0
- package/nitrogen/generated/shared/c++/HybridNativePdfViewSpec.hpp +112 -0
- package/nitrogen/generated/shared/c++/HybridPdfDocumentSpec.cpp +24 -0
- package/nitrogen/generated/shared/c++/HybridPdfDocumentSpec.hpp +70 -0
- package/nitrogen/generated/shared/c++/HybridPdfSdkSpec.cpp +21 -0
- package/nitrogen/generated/shared/c++/HybridPdfSdkSpec.hpp +66 -0
- package/nitrogen/generated/shared/c++/PdfErrorCode.hpp +88 -0
- package/nitrogen/generated/shared/c++/PdfErrorEvent.hpp +89 -0
- package/nitrogen/generated/shared/c++/PdfFit.hpp +84 -0
- package/nitrogen/generated/shared/c++/PdfLoadEvent.hpp +91 -0
- package/nitrogen/generated/shared/c++/PdfPageEvent.hpp +87 -0
- package/nitrogen/generated/shared/c++/PdfPageSize.hpp +87 -0
- package/nitrogen/generated/shared/c++/PdfRenderRequest.hpp +112 -0
- package/nitrogen/generated/shared/c++/PdfTapEvent.hpp +91 -0
- package/nitrogen/generated/shared/c++/PdfViewSource.hpp +97 -0
- package/nitrogen/generated/shared/c++/PdfZoomEvent.hpp +87 -0
- package/nitrogen/generated/shared/c++/views/HybridNativePdfViewComponent.cpp +226 -0
- package/nitrogen/generated/shared/c++/views/HybridNativePdfViewComponent.hpp +130 -0
- package/nitrogen/generated/shared/json/NativePdfViewConfig.json +23 -0
- package/package.json +80 -4
- package/react-native.config.js +10 -0
- package/src/PdfView.tsx +221 -0
- package/src/document.ts +34 -0
- package/src/index.ts +16 -0
- package/src/specs/PdfSdk.nitro.ts +60 -0
- package/src/specs/PdfView.nitro.ts +149 -0
package/CHANGELOG.md
ADDED
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
# Changelog
|
|
2
|
+
|
|
3
|
+
## 0.1.1
|
|
4
|
+
|
|
5
|
+
The readme only. Nothing in the package's code changed.
|
|
6
|
+
|
|
7
|
+
- Screenshots of both documents on both platforms, three recordings, and a
|
|
8
|
+
loop of the sheet being zoomed into at the top of the page. The loop is an
|
|
9
|
+
image rather than a video so that it plays on npm as well as on GitHub.
|
|
10
|
+
|
|
11
|
+
## 0.1.0
|
|
12
|
+
|
|
13
|
+
First release.
|
|
14
|
+
|
|
15
|
+
- `PdfView`, a viewer that draws the part of the page in view, in tiles,
|
|
16
|
+
at the zoom it is being viewed at, on both platforms.
|
|
17
|
+
- Documents from a local path, a `file://` url, an `http(s)` url the view
|
|
18
|
+
fetches and keeps, or an Android `content://` url, with request headers and a
|
|
19
|
+
password.
|
|
20
|
+
- `fit`, `minZoom`, `maxZoom`, `doubleTapZoom`, `pageGap`, `backdropColor` and
|
|
21
|
+
`scrollIndicators` for how it opens and how far it goes.
|
|
22
|
+
- `onLoad`, `onPageChange`, `onZoom`, `onError` and `onTap`, and `goToPage` and
|
|
23
|
+
`setZoom` through a ref.
|
|
24
|
+
- `openPdfDocument`, for drawing a page into a PNG file without a viewer.
|
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Halil İbrahim Akyüz
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/PdfSdk.podspec
ADDED
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
require "json"
|
|
2
|
+
|
|
3
|
+
package = JSON.parse(File.read(File.join(__dir__, "package.json")))
|
|
4
|
+
|
|
5
|
+
Pod::Spec.new do |s|
|
|
6
|
+
# The pod's name is the iOS module name from nitro.json, and has to stay that
|
|
7
|
+
# way: the generated bridge imports `PdfSdk-Swift.h`, which is the header
|
|
8
|
+
# Xcode writes for a Swift module of this name.
|
|
9
|
+
s.name = "PdfSdk"
|
|
10
|
+
s.version = package["version"]
|
|
11
|
+
s.summary = package["description"]
|
|
12
|
+
s.description = package["description"]
|
|
13
|
+
s.homepage = "https://github.com/Halilibrahimakyz/react-native-pdf-sdk"
|
|
14
|
+
s.license = { :type => "MIT" }
|
|
15
|
+
s.authors = "Halil İbrahim Akyüz"
|
|
16
|
+
s.platforms = { :ios => min_ios_version_supported }
|
|
17
|
+
# Never fetched: the app carries this package inside it and CocoaPods is
|
|
18
|
+
# pointed at the folder. Here because a podspec must name a source.
|
|
19
|
+
s.source = { :git => "https://github.com/Halilibrahimakyz/react-native-pdf-sdk.git", :tag => "v#{s.version}" }
|
|
20
|
+
|
|
21
|
+
s.source_files = ["ios/**/*.{swift,h,m,mm}"]
|
|
22
|
+
# Run on a Mac rather than on a phone; see ios/tests/run.sh.
|
|
23
|
+
s.exclude_files = ["ios/tests/**/*"]
|
|
24
|
+
|
|
25
|
+
# Add all files generated by Nitrogen
|
|
26
|
+
load 'nitrogen/generated/ios/PdfSdk+autolinking.rb'
|
|
27
|
+
add_nitrogen_files(s)
|
|
28
|
+
|
|
29
|
+
s.dependency "React-jsi"
|
|
30
|
+
s.dependency "React-callinvoker"
|
|
31
|
+
install_modules_dependencies(s)
|
|
32
|
+
end
|
package/README.md
CHANGED
|
@@ -1,3 +1,271 @@
|
|
|
1
|
-
#
|
|
1
|
+
# react-native-pdf-sdk
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
A PDF viewer for React Native that draws the part of the page you are looking
|
|
4
|
+
at, at the zoom you are looking at it, using the renderer that ships with the
|
|
5
|
+
platform.
|
|
6
|
+
|
|
7
|
+
Viewers built on a whole-page bitmap fall over on architectural and engineering
|
|
8
|
+
drawings, where one sheet can be metres wide: fitted to a phone the sheet is a
|
|
9
|
+
line, and zoomed in the bitmap is too large to hold. This one never draws a
|
|
10
|
+
whole page. It draws the rectangle in view, in tiles, so a sheet of any size
|
|
11
|
+
costs about what a letter page costs.
|
|
12
|
+
|
|
13
|
+
```tsx
|
|
14
|
+
import { PdfView } from 'react-native-pdf-sdk';
|
|
15
|
+
|
|
16
|
+
<PdfView
|
|
17
|
+
source={{ uri: 'https://example.com/plan.pdf' }}
|
|
18
|
+
onPageChange={({ page, pageCount }) => setLabel(`${page + 1} / ${pageCount}`)}
|
|
19
|
+
style={StyleSheet.absoluteFill}
|
|
20
|
+
/>;
|
|
21
|
+
```
|
|
22
|
+
|
|
23
|
+
## What it looks like
|
|
24
|
+
|
|
25
|
+
Both documents below are drawn by `example/pdfs/make.swift` and ship with this
|
|
26
|
+
repository. Nothing pictured belongs to anyone: every line of it came out of
|
|
27
|
+
that script.
|
|
28
|
+
|
|
29
|
+
<img src="https://raw.githubusercontent.com/Halilibrahimakyz/react-native-pdf-sdk/main/docs/sheet-zoom-ios.gif" width="280" alt="A sheet zoomed into until the notes set at four points are readable" />
|
|
30
|
+
|
|
31
|
+
**One sheet, 26000 × 1500 points.** A drawing that wide is a line when it is
|
|
32
|
+
fitted to a phone's width, so it opens fitted to the height and is read by
|
|
33
|
+
panning. The second column is the same screen after pinching into the middle of
|
|
34
|
+
it: the casting note set at 4 points, a smudge in the first column, is the same
|
|
35
|
+
ink in the same document.
|
|
36
|
+
|
|
37
|
+
| | Opening | The same place, zoomed in |
|
|
38
|
+
| --- | --- | --- |
|
|
39
|
+
| iOS | <img src="https://raw.githubusercontent.com/Halilibrahimakyz/react-native-pdf-sdk/main/docs/sheet-fitted-ios.png" width="240" alt="The sheet fitted to the height on iOS" /> | <img src="https://raw.githubusercontent.com/Halilibrahimakyz/react-native-pdf-sdk/main/docs/sheet-detail-ios.png" width="240" alt="The same sheet zoomed into panel PA2 on iOS" /> |
|
|
40
|
+
| Android | <img src="https://raw.githubusercontent.com/Halilibrahimakyz/react-native-pdf-sdk/main/docs/sheet-fitted-android.png" width="240" alt="The sheet fitted to the height on Android" /> | <img src="https://raw.githubusercontent.com/Halilibrahimakyz/react-native-pdf-sdk/main/docs/sheet-detail-android.png" width="240" alt="The same sheet zoomed into panel PA2 on Android" /> |
|
|
41
|
+
|
|
42
|
+
**Ten ordinary pages.** The case every viewer is built for: fitted to the
|
|
43
|
+
width, scrolled, with a gap and a page number between one page and the next.
|
|
44
|
+
|
|
45
|
+
| iOS | Android |
|
|
46
|
+
| --- | --- |
|
|
47
|
+
| <img src="https://raw.githubusercontent.com/Halilibrahimakyz/react-native-pdf-sdk/main/docs/report-pages-ios.png" width="240" alt="Two pages of the report on iOS" /> | <img src="https://raw.githubusercontent.com/Halilibrahimakyz/react-native-pdf-sdk/main/docs/report-pages-android.png" width="240" alt="Two pages of the report on Android" /> |
|
|
48
|
+
|
|
49
|
+
### Recordings
|
|
50
|
+
|
|
51
|
+
Whole, and at the speed they happened. They play on the GitHub page; where
|
|
52
|
+
this file is rendered by something else they are links.
|
|
53
|
+
|
|
54
|
+
The sheet, pinched in and out on iOS:
|
|
55
|
+
|
|
56
|
+
https://github.com/user-attachments/assets/0eead98f-01e7-4dd4-8d49-aed94b51f8f2
|
|
57
|
+
|
|
58
|
+
The same on Android:
|
|
59
|
+
|
|
60
|
+
https://github.com/user-attachments/assets/d4c5a9db-51ec-417b-a550-8c844b5d489b
|
|
61
|
+
|
|
62
|
+
The report, scrolled:
|
|
63
|
+
|
|
64
|
+
https://github.com/user-attachments/assets/a93a3de5-4e77-46d2-8a2e-8ab7bc371ace
|
|
65
|
+
|
|
66
|
+
The counters in the corner are React Native's own performance monitor, left on
|
|
67
|
+
deliberately. Read those rather than the fluidity of the video itself: both
|
|
68
|
+
screen recorders top out well under sixty frames a second, and what they cost
|
|
69
|
+
is charged to the same threads they are measuring.
|
|
70
|
+
|
|
71
|
+
## Requirements
|
|
72
|
+
|
|
73
|
+
- React Native 0.78 or newer with the **New Architecture** enabled. There is no
|
|
74
|
+
old architecture fallback.
|
|
75
|
+
- [`react-native-nitro-modules`](https://nitro.margelo.com), which carries the
|
|
76
|
+
bridge this is built on.
|
|
77
|
+
- iOS 15, Android 7 (API 24).
|
|
78
|
+
|
|
79
|
+
```sh
|
|
80
|
+
npm install react-native-pdf-sdk react-native-nitro-modules
|
|
81
|
+
cd ios && pod install
|
|
82
|
+
```
|
|
83
|
+
|
|
84
|
+
Nothing to register and nothing to wrap: both platforms autolink.
|
|
85
|
+
|
|
86
|
+
## The document
|
|
87
|
+
|
|
88
|
+
`source` takes a string or an object.
|
|
89
|
+
|
|
90
|
+
| Field | What it does |
|
|
91
|
+
| --- | --- |
|
|
92
|
+
| `uri` | A local path, a `file://` url, an `http(s)` url the view fetches and keeps, or on Android a `content://` url |
|
|
93
|
+
| `headers` | Sent with the request when the document is fetched over http |
|
|
94
|
+
| `password` | For a document that needs one to open |
|
|
95
|
+
| `cache` | Keep a fetched document on disk for next time. Defaults to true |
|
|
96
|
+
|
|
97
|
+
A string is shorthand for `{ uri }`. A fetched document is kept under a name
|
|
98
|
+
made from its url, so opening it again reads it from disk.
|
|
99
|
+
|
|
100
|
+
Encrypted documents open on iOS with the right password. On Android a password
|
|
101
|
+
can only be handed to the platform from Android 15; before that the platform
|
|
102
|
+
refuses an encrypted document outright, and `onError` says so with the code
|
|
103
|
+
`password`.
|
|
104
|
+
|
|
105
|
+
## Props
|
|
106
|
+
|
|
107
|
+
| Prop | Type | Default | What it does |
|
|
108
|
+
| --- | --- | --- | --- |
|
|
109
|
+
| `source` | `string \| PdfViewSource` | | The document |
|
|
110
|
+
| `page` | `number` | `0` | Which page to show, zero based. Changing it scrolls there |
|
|
111
|
+
| `fit` | `'auto' \| 'width' \| 'height' \| 'page'` | `'auto'` | How the document is sized when it opens |
|
|
112
|
+
| `minZoom` | `number` | until the widest page fits | How far out it goes, as a multiple of the opening zoom |
|
|
113
|
+
| `maxZoom` | `number` | until a PDF point is 13 screen points | How far in it goes, likewise |
|
|
114
|
+
| `doubleTapZoom` | `number` | three times, at least 4 | Where a double tap lands |
|
|
115
|
+
| `pageGap` | `number` | `14` | The gap between pages, in PDF points |
|
|
116
|
+
| `backdropColor` | `ColorValue` | transparent | The ground behind and between the pages |
|
|
117
|
+
| `scrollIndicators` | `boolean` | `true` | iOS only, since Android draws none |
|
|
118
|
+
| `style` | `StyleProp<ViewStyle>` | `flex: 1` | |
|
|
119
|
+
|
|
120
|
+
`fit` is worth a sentence. `auto` fits the width, unless the first page is a
|
|
121
|
+
strip rather than a page, in which case it fills the height instead: a
|
|
122
|
+
building's formwork plan is one sheet thirteen metres wide and sixty
|
|
123
|
+
centimetres tall, and fitted to the width that is a line across the middle of
|
|
124
|
+
the screen.
|
|
125
|
+
|
|
126
|
+
Every zoom number in this API is a multiple of the zoom the document opened at,
|
|
127
|
+
whatever that turned out to be, so `maxZoom={3}` means the same thing to a
|
|
128
|
+
letter page and to a plan.
|
|
129
|
+
|
|
130
|
+
## Events
|
|
131
|
+
|
|
132
|
+
| Event | Gives you |
|
|
133
|
+
| --- | --- |
|
|
134
|
+
| `onLoad` | `{ pageCount, width, height }`, the first page's size in PDF points |
|
|
135
|
+
| `onPageChange` | `{ page, pageCount }`, the page under the middle of the view, zero based |
|
|
136
|
+
| `onZoom` | `{ zoom, scale }`, a multiple of the opening zoom and what it works out to in screen points per PDF point |
|
|
137
|
+
| `onError` | `{ code, message }`, where code is `notFound`, `password`, `unsupported`, `network` or `unknown` |
|
|
138
|
+
| `onTap` | `{ x, y, page }`, for a tap that was not part of a double tap |
|
|
139
|
+
|
|
140
|
+
`onError` is where a viewer earns its keep. Nothing is thrown, nothing is
|
|
141
|
+
logged for you: a document that will not open says so once, with a code you can
|
|
142
|
+
branch on, and the view stays empty until it is given another one.
|
|
143
|
+
|
|
144
|
+
## Controlling it
|
|
145
|
+
|
|
146
|
+
```tsx
|
|
147
|
+
const viewer = useRef<PdfViewHandle>(null);
|
|
148
|
+
|
|
149
|
+
viewer.current?.goToPage(4); // zero based
|
|
150
|
+
viewer.current?.setZoom(2, false); // twice the opening zoom, without animating
|
|
151
|
+
```
|
|
152
|
+
|
|
153
|
+
## Pictures of pages
|
|
154
|
+
|
|
155
|
+
Sometimes you want an image rather than a viewer: a thumbnail beside a
|
|
156
|
+
filename, a preview in a list, a page to share.
|
|
157
|
+
|
|
158
|
+
```ts
|
|
159
|
+
import { openPdfDocument } from 'react-native-pdf-sdk';
|
|
160
|
+
|
|
161
|
+
const document = openPdfDocument(path);
|
|
162
|
+
const size = document.getPageSize(0);
|
|
163
|
+
const png = await document.renderToFile({
|
|
164
|
+
page: 0,
|
|
165
|
+
outWidth: 200,
|
|
166
|
+
outHeight: Math.round((200 * size.height) / size.width),
|
|
167
|
+
});
|
|
168
|
+
document.close();
|
|
169
|
+
```
|
|
170
|
+
|
|
171
|
+
`renderToFile` answers the path it wrote. Give it `x`, `y`, `width` and
|
|
172
|
+
`height` in the page's own points to draw a region of a page rather than the
|
|
173
|
+
whole of it, and `path` to say where the PNG goes.
|
|
174
|
+
|
|
175
|
+
## What is native and why
|
|
176
|
+
|
|
177
|
+
Everything that happens while a finger is on the screen: the zoom, the
|
|
178
|
+
movement, the tile grid and the drawing are read from the same numbers in the
|
|
179
|
+
same pass. Drawn from JavaScript, the transform and the layout of what it moves
|
|
180
|
+
cannot be changed together, and every change of zoom shows a frame or two of the
|
|
181
|
+
document in the wrong place.
|
|
182
|
+
|
|
183
|
+
The two platforms draw the same way and open a document at the same size, and
|
|
184
|
+
the numbers that decide both are written twice, once in Kotlin and once in
|
|
185
|
+
Swift, with the same tests on each side. They differ where the platform does:
|
|
186
|
+
|
|
187
|
+
- **Android** counts the pinch, the double tap and the flick by hand, because
|
|
188
|
+
the platform's own detectors stop reporting halfway through a pinch and claim
|
|
189
|
+
the second tap of a double tap for themselves. `PdfRenderer` draws.
|
|
190
|
+
- **iOS** hands all of that to a `UIScrollView`, which is the scroll view every
|
|
191
|
+
iOS app is read through, and reads the zoom and the offset back out of it on
|
|
192
|
+
every frame. `CGPDFDocument` draws.
|
|
193
|
+
|
|
194
|
+
The drawing is where the two differ most, and all of it follows from one
|
|
195
|
+
measurement: `CGPDFDocument` keeps nothing between draws, so a page's content is
|
|
196
|
+
read again for every tile, and on a structural drawing that is a tenth of a
|
|
197
|
+
second whether the tile is a postage stamp or a screenful. Everything below is
|
|
198
|
+
there to make that number matter less, and each was measured before and after:
|
|
199
|
+
|
|
200
|
+
- Tiles are 1280 pixels rather than Android's 512, so a screenful is four to six
|
|
201
|
+
drawings rather than fifteen.
|
|
202
|
+
- They are drawn through three open copies of the file at once. The copies cost
|
|
203
|
+
almost nothing: the system maps the file once, and what is not shared is the
|
|
204
|
+
parsing, which is the part being done in parallel.
|
|
205
|
+
- A page small enough to be drawn in one go is drawn in one go, so an ordinary
|
|
206
|
+
page read at the zoom it opens at is one drawing instead of six.
|
|
207
|
+
- Tiles are drawn at zooms from a fixed ladder rather than at wherever the
|
|
208
|
+
fingers stopped, so a zoom that comes back finds what it drew last time
|
|
209
|
+
instead of asking for the same picture under another name.
|
|
210
|
+
- Every level held is drawn, coarsest first, so moving at a deep zoom lands on
|
|
211
|
+
something drawn on the way in rather than on the page's own small drawing.
|
|
212
|
+
- A double tap asks for the tiles of where it is going when it starts, since the
|
|
213
|
+
movement and a tile take about as long as each other.
|
|
214
|
+
- What is held is held least recently used, because every tile on the screen is
|
|
215
|
+
read every frame and so can never be the one thrown out.
|
|
216
|
+
|
|
217
|
+
## Trying it
|
|
218
|
+
|
|
219
|
+
`example/pdfs` holds two documents drawn by `example/pdfs/make.swift`, so that a
|
|
220
|
+
screenshot of this package is this package's own work and carries nobody's
|
|
221
|
+
data. `sheet.pdf` is one drawing nine metres wide with dimensions written small
|
|
222
|
+
enough to need the zoom; `report.pdf` is ten ordinary A4 pages. Run `npm run
|
|
223
|
+
pdfs` to draw them again.
|
|
224
|
+
|
|
225
|
+
The quickest way to open them on a device is to serve the folder and hand the
|
|
226
|
+
url to the view, which also exercises the fetching it does itself:
|
|
227
|
+
|
|
228
|
+
```sh
|
|
229
|
+
cd example/pdfs && python3 -m http.server 8000
|
|
230
|
+
```
|
|
231
|
+
|
|
232
|
+
An iOS simulator reaches that at `http://localhost:8000/sheet.pdf` and an
|
|
233
|
+
Android emulator at `http://10.0.2.2:8000/sheet.pdf`.
|
|
234
|
+
|
|
235
|
+
## Tests
|
|
236
|
+
|
|
237
|
+
The arithmetic that decides what the reader sees is free of Android and of UIKit
|
|
238
|
+
on purpose, so all of it runs on the machine you are writing on, in seconds,
|
|
239
|
+
with no emulator and no simulator.
|
|
240
|
+
|
|
241
|
+
```sh
|
|
242
|
+
npm run test:ios # swiftc, straight on the Mac
|
|
243
|
+
npm run test:android # the same questions, on the JVM
|
|
244
|
+
npm run verify # both, and the types
|
|
245
|
+
```
|
|
246
|
+
|
|
247
|
+
Both sides ask the same questions, which is how a difference between the two
|
|
248
|
+
platforms turns up as a failing test rather than as a complaint about one phone.
|
|
249
|
+
|
|
250
|
+
## Changing the spec
|
|
251
|
+
|
|
252
|
+
`src/specs/*.nitro.ts` are the source of truth for the bridge. After editing
|
|
253
|
+
them run `npm run specs`, then `pod install` and a gradle sync in the app.
|
|
254
|
+
|
|
255
|
+
## Releasing
|
|
256
|
+
|
|
257
|
+
```sh
|
|
258
|
+
npm install # inside this package, for the build tools
|
|
259
|
+
npm publish # verifies, builds, then publishes
|
|
260
|
+
```
|
|
261
|
+
|
|
262
|
+
## Contributing
|
|
263
|
+
|
|
264
|
+
Patches welcome. [CONTRIBUTING.md](CONTRIBUTING.md) has the commit format, what
|
|
265
|
+
`npm run verify` covers and the two rules that are easy to break without
|
|
266
|
+
noticing: the numbers behind the behaviour live twice, once per platform, and
|
|
267
|
+
the native view takes no optional props.
|
|
268
|
+
|
|
269
|
+
## Licence
|
|
270
|
+
|
|
271
|
+
MIT
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
project(PdfSdk)
|
|
2
|
+
cmake_minimum_required(VERSION 3.9.0)
|
|
3
|
+
|
|
4
|
+
set(PACKAGE_NAME PdfSdk)
|
|
5
|
+
set(CMAKE_VERBOSE_MAKEFILE ON)
|
|
6
|
+
set(CMAKE_CXX_STANDARD 20)
|
|
7
|
+
|
|
8
|
+
add_library(${PACKAGE_NAME} SHARED
|
|
9
|
+
src/main/cpp/cpp-adapter.cpp
|
|
10
|
+
)
|
|
11
|
+
|
|
12
|
+
include(${CMAKE_SOURCE_DIR}/../nitrogen/generated/android/PdfSdk+autolinking.cmake)
|
|
13
|
+
|
|
14
|
+
include_directories(
|
|
15
|
+
"src/main/cpp"
|
|
16
|
+
)
|
|
17
|
+
|
|
18
|
+
find_library(LOG_LIB log)
|
|
19
|
+
|
|
20
|
+
target_link_libraries(
|
|
21
|
+
${PACKAGE_NAME}
|
|
22
|
+
${LOG_LIB}
|
|
23
|
+
android
|
|
24
|
+
)
|
|
@@ -0,0 +1,132 @@
|
|
|
1
|
+
buildscript {
|
|
2
|
+
repositories {
|
|
3
|
+
google()
|
|
4
|
+
mavenCentral()
|
|
5
|
+
}
|
|
6
|
+
|
|
7
|
+
dependencies {
|
|
8
|
+
classpath "com.android.tools.build:gradle:8.10.1"
|
|
9
|
+
}
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
def reactNativeArchitectures() {
|
|
13
|
+
def value = rootProject.getProperties().get("reactNativeArchitectures")
|
|
14
|
+
return value ? value.split(",") : ["armeabi-v7a", "x86", "x86_64", "arm64-v8a"]
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
def isNewArchitectureEnabled() {
|
|
18
|
+
return rootProject.hasProperty("newArchEnabled") && rootProject.getProperty("newArchEnabled") == "true"
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
apply plugin: "com.android.library"
|
|
22
|
+
apply plugin: "org.jetbrains.kotlin.android"
|
|
23
|
+
apply from: "../nitrogen/generated/android/PdfSdk+autolinking.gradle"
|
|
24
|
+
|
|
25
|
+
if (isNewArchitectureEnabled()) {
|
|
26
|
+
apply plugin: "com.facebook.react"
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
def getExtOrDefault(name) {
|
|
30
|
+
return rootProject.ext.has(name) ? rootProject.ext.get(name) : project.properties["PdfSdk_" + name]
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
def getExtOrIntegerDefault(name) {
|
|
34
|
+
return rootProject.ext.has(name) ? rootProject.ext.get(name) : (project.properties["PdfSdk_" + name]).toInteger()
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
android {
|
|
38
|
+
namespace "com.margelo.nitro.pdfsdk"
|
|
39
|
+
|
|
40
|
+
ndkVersion getExtOrDefault("ndkVersion")
|
|
41
|
+
compileSdkVersion getExtOrIntegerDefault("compileSdkVersion")
|
|
42
|
+
|
|
43
|
+
defaultConfig {
|
|
44
|
+
minSdkVersion getExtOrIntegerDefault("minSdkVersion")
|
|
45
|
+
targetSdkVersion getExtOrIntegerDefault("targetSdkVersion")
|
|
46
|
+
buildConfigField "boolean", "IS_NEW_ARCHITECTURE_ENABLED", isNewArchitectureEnabled().toString()
|
|
47
|
+
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
|
|
48
|
+
|
|
49
|
+
externalNativeBuild {
|
|
50
|
+
cmake {
|
|
51
|
+
cppFlags "-frtti -fexceptions -Wall -Wextra -fstack-protector-all"
|
|
52
|
+
arguments "-DANDROID_STL=c++_shared", "-DANDROID_SUPPORT_FLEXIBLE_PAGE_SIZES=ON"
|
|
53
|
+
abiFilters (*reactNativeArchitectures())
|
|
54
|
+
|
|
55
|
+
// Never build this unoptimised, not even for debugging.
|
|
56
|
+
//
|
|
57
|
+
// react-native's ConcreteShadowNode::getConcreteSharedProps() returns a
|
|
58
|
+
// reference to the temporary that std::static_pointer_cast makes, which
|
|
59
|
+
// is a dangling reference the moment the function returns. Inlined, the
|
|
60
|
+
// temporary lives long enough and nothing is noticed. At -O0 it is
|
|
61
|
+
// destroyed on return, and the view crashes on its first layout, in the
|
|
62
|
+
// generated code that copies those props into the shadow node's state.
|
|
63
|
+
// Every other Nitro view package does the same thing for the same
|
|
64
|
+
// reason.
|
|
65
|
+
buildTypes {
|
|
66
|
+
debug {
|
|
67
|
+
cppFlags "-O1 -g"
|
|
68
|
+
}
|
|
69
|
+
release {
|
|
70
|
+
cppFlags "-O2"
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
externalNativeBuild {
|
|
78
|
+
cmake {
|
|
79
|
+
path "CMakeLists.txt"
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
packagingOptions {
|
|
84
|
+
excludes = [
|
|
85
|
+
"META-INF",
|
|
86
|
+
"META-INF/**",
|
|
87
|
+
"**/libc++_shared.so",
|
|
88
|
+
"**/libfbjni.so",
|
|
89
|
+
"**/libjsi.so",
|
|
90
|
+
"**/libfolly_json.so",
|
|
91
|
+
"**/libfolly_runtime.so",
|
|
92
|
+
"**/libglog.so",
|
|
93
|
+
"**/libhermes.so",
|
|
94
|
+
"**/libreactnative.so",
|
|
95
|
+
"**/libreactnativejni.so"
|
|
96
|
+
]
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
buildFeatures {
|
|
100
|
+
buildConfig true
|
|
101
|
+
prefab true
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
buildTypes {
|
|
105
|
+
release {
|
|
106
|
+
minifyEnabled false
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
lintOptions {
|
|
111
|
+
disable "GradleCompatible"
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
compileOptions {
|
|
115
|
+
sourceCompatibility JavaVersion.VERSION_1_8
|
|
116
|
+
targetCompatibility JavaVersion.VERSION_1_8
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
repositories {
|
|
121
|
+
google()
|
|
122
|
+
mavenCentral()
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
dependencies {
|
|
126
|
+
implementation "com.facebook.react:react-native:+"
|
|
127
|
+
implementation project(":react-native-nitro-modules")
|
|
128
|
+
|
|
129
|
+
// The arithmetic that decides what the reader sees runs on the JVM, with no
|
|
130
|
+
// device and no emulator. See src/test.
|
|
131
|
+
testImplementation "junit:junit:4.13.2"
|
|
132
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
<manifest xmlns:android="http://schemas.android.com/apk/res/android" />
|
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
package com.margelo.nitro.pdfsdk
|
|
2
|
+
|
|
3
|
+
import androidx.annotation.Keep
|
|
4
|
+
import com.facebook.proguard.annotations.DoNotStrip
|
|
5
|
+
import com.margelo.nitro.core.Promise
|
|
6
|
+
import java.io.File
|
|
7
|
+
import java.util.UUID
|
|
8
|
+
import java.util.concurrent.Executors
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* An open document, for taking pictures of pages.
|
|
12
|
+
*
|
|
13
|
+
* Drawing happens on a thread of its own: opening a page of a structural
|
|
14
|
+
* drawing is a third of a second, and that is not something to do on the thread
|
|
15
|
+
* JavaScript runs on.
|
|
16
|
+
*/
|
|
17
|
+
@DoNotStrip
|
|
18
|
+
@Keep
|
|
19
|
+
class HybridPdfDocument(
|
|
20
|
+
private val source: PdfPageSource,
|
|
21
|
+
private val cacheDirectory: String,
|
|
22
|
+
) : HybridPdfDocumentSpec() {
|
|
23
|
+
|
|
24
|
+
private val worker = Executors.newSingleThreadExecutor { runnable ->
|
|
25
|
+
Thread(runnable, "pdf-sdk-document")
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
override val pageCount: Double get() = source.pageCount.toDouble()
|
|
29
|
+
|
|
30
|
+
override fun getPageSize(page: Double): PdfPageSize {
|
|
31
|
+
require(page.isFinite() && page >= 0) { "There is no page $page" }
|
|
32
|
+
val (width, height) = source.pageSize(page.toInt())
|
|
33
|
+
return PdfPageSize(width.toDouble(), height.toDouble())
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
override fun renderToFile(request: PdfRenderRequest): Promise<String> {
|
|
37
|
+
require(request.page.isFinite() && request.page >= 0) { "There is no page ${request.page}" }
|
|
38
|
+
require(request.outWidth >= 1 && request.outHeight >= 1) {
|
|
39
|
+
"An image cannot be smaller than a pixel"
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
val page = request.page.toInt()
|
|
43
|
+
val (width, height) = source.pageSize(page)
|
|
44
|
+
val rect = PageRect(
|
|
45
|
+
x = (request.x ?: 0.0).toFloat(),
|
|
46
|
+
y = (request.y ?: 0.0).toFloat(),
|
|
47
|
+
width = (request.width ?: width.toDouble()).toFloat(),
|
|
48
|
+
height = (request.height ?: height.toDouble()).toFloat(),
|
|
49
|
+
)
|
|
50
|
+
val path = request.path ?: File(
|
|
51
|
+
File(cacheDirectory, "pdf-sdk"),
|
|
52
|
+
"${UUID.randomUUID()}.png",
|
|
53
|
+
).path
|
|
54
|
+
|
|
55
|
+
val promise = Promise<String>()
|
|
56
|
+
worker.execute {
|
|
57
|
+
try {
|
|
58
|
+
promise.resolve(
|
|
59
|
+
source.renderToFile(page, rect, request.outWidth.toInt(), request.outHeight.toInt(), path)
|
|
60
|
+
)
|
|
61
|
+
} catch (error: Throwable) {
|
|
62
|
+
promise.reject(error)
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
return promise
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
override fun close() {
|
|
69
|
+
source.close()
|
|
70
|
+
worker.shutdown()
|
|
71
|
+
}
|
|
72
|
+
}
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
package com.margelo.nitro.pdfsdk
|
|
2
|
+
|
|
3
|
+
import androidx.annotation.Keep
|
|
4
|
+
import com.facebook.proguard.annotations.DoNotStrip
|
|
5
|
+
import com.margelo.nitro.NitroModules
|
|
6
|
+
|
|
7
|
+
/** Opens documents for whoever wants pictures of pages rather than a viewer. */
|
|
8
|
+
@DoNotStrip
|
|
9
|
+
@Keep
|
|
10
|
+
class HybridPdfSdk : HybridPdfSdkSpec() {
|
|
11
|
+
override fun open(path: String, password: String?): HybridPdfDocumentSpec {
|
|
12
|
+
val context = NitroModules.applicationContext
|
|
13
|
+
?: throw PdfFailure.unknown("There is no application to open a document in")
|
|
14
|
+
val descriptor = PdfSourceLoader.open(context, path)
|
|
15
|
+
return HybridPdfDocument(PdfPageSource(descriptor, password), context.cacheDir.path)
|
|
16
|
+
}
|
|
17
|
+
}
|