@paperlet/react-native 0.0.0-stage → 0.1.2
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/LICENSE +13 -0
- package/Paperlet.podspec +26 -0
- package/README.md +73 -2
- package/android/CMakeLists.txt +21 -0
- package/android/build.gradle +43 -0
- package/android/jniLibs/arm64-v8a/libpdfium.so +0 -0
- package/android/jniLibs/armeabi-v7a/libpdfium.so +0 -0
- package/android/jniLibs/x86_64/libpdfium.so +0 -0
- package/android/libs/arm64-v8a/libpaperlet_core.so +0 -0
- package/android/libs/armeabi-v7a/libpaperlet_core.so +0 -0
- package/android/libs/x86_64/libpaperlet_core.so +0 -0
- package/android/src/main/AndroidManifest.xml +1 -0
- package/android/src/main/cpp/cpp-adapter.cpp +8 -0
- package/android/src/main/java/com/paperlet/PaperletPackage.kt +20 -0
- package/cpp/Core.hpp +44 -0
- package/cpp/HybridPaperlet.cpp +63 -0
- package/cpp/HybridPaperlet.hpp +20 -0
- package/cpp/HybridPaperletDocument.cpp +98 -0
- package/cpp/HybridPaperletDocument.hpp +30 -0
- package/cpp/paperlet_core.h +29 -0
- package/ios/PaperletCore.xcframework/Info.plist +44 -0
- package/ios/PaperletCore.xcframework/ios-arm64/PaperletCore.framework/Info.plist +20 -0
- package/ios/PaperletCore.xcframework/ios-arm64/PaperletCore.framework/PaperletCore +0 -0
- package/ios/PaperletCore.xcframework/ios-arm64_x86_64-simulator/PaperletCore.framework/Info.plist +20 -0
- package/ios/PaperletCore.xcframework/ios-arm64_x86_64-simulator/PaperletCore.framework/PaperletCore +0 -0
- package/ios/pdfium.xcframework/Info.plist +44 -0
- package/ios/pdfium.xcframework/ios-arm64/pdfium.framework/Info.plist +28 -0
- package/ios/pdfium.xcframework/ios-arm64/pdfium.framework/pdfium +0 -0
- package/ios/pdfium.xcframework/ios-arm64_x86_64-simulator/pdfium.framework/Info.plist +28 -0
- package/ios/pdfium.xcframework/ios-arm64_x86_64-simulator/pdfium.framework/pdfium +0 -0
- package/nitro.json +8 -0
- package/nitrogen/generated/.gitattributes +1 -0
- package/nitrogen/generated/android/Paperlet+autolinking.cmake +82 -0
- package/nitrogen/generated/android/Paperlet+autolinking.gradle +27 -0
- package/nitrogen/generated/android/PaperletOnLoad.cpp +49 -0
- package/nitrogen/generated/android/PaperletOnLoad.hpp +34 -0
- package/nitrogen/generated/android/kotlin/com/margelo/nitro/paperlet/PaperletOnLoad.kt +35 -0
- package/nitrogen/generated/ios/Paperlet+autolinking.rb +62 -0
- package/nitrogen/generated/ios/Paperlet-Swift-Cxx-Bridge.cpp +17 -0
- package/nitrogen/generated/ios/Paperlet-Swift-Cxx-Bridge.hpp +27 -0
- package/nitrogen/generated/ios/Paperlet-Swift-Cxx-Umbrella.hpp +38 -0
- package/nitrogen/generated/ios/PaperletAutolinking.mm +35 -0
- package/nitrogen/generated/ios/PaperletAutolinking.swift +16 -0
- package/nitrogen/generated/shared/c++/HybridPaperletDocumentSpec.cpp +28 -0
- package/nitrogen/generated/shared/c++/HybridPaperletDocumentSpec.hpp +71 -0
- package/nitrogen/generated/shared/c++/HybridPaperletSpec.cpp +25 -0
- package/nitrogen/generated/shared/c++/HybridPaperletSpec.hpp +72 -0
- package/package.json +51 -4
- package/react-native.config.js +10 -0
- package/src/PaperletView.tsx +43 -0
- package/src/index.ts +85 -0
- package/src/specs/Paperlet.nitro.ts +31 -0
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
///
|
|
2
|
+
/// HybridPaperletSpec.hpp
|
|
3
|
+
/// This file was generated by nitrogen. DO NOT MODIFY THIS FILE.
|
|
4
|
+
/// https://github.com/margelo/nitro
|
|
5
|
+
/// Copyright © Marc Rousavy @ Margelo
|
|
6
|
+
///
|
|
7
|
+
|
|
8
|
+
#pragma once
|
|
9
|
+
|
|
10
|
+
#if __has_include(<NitroModules/HybridObject.hpp>)
|
|
11
|
+
#include <NitroModules/HybridObject.hpp>
|
|
12
|
+
#else
|
|
13
|
+
#error NitroModules cannot be found! Are you sure you installed NitroModules properly?
|
|
14
|
+
#endif
|
|
15
|
+
|
|
16
|
+
// Forward declaration of `HybridPaperletDocumentSpec` to properly resolve imports.
|
|
17
|
+
namespace margelo::nitro::paperlet { class HybridPaperletDocumentSpec; }
|
|
18
|
+
|
|
19
|
+
#include <string>
|
|
20
|
+
#include <memory>
|
|
21
|
+
#include "HybridPaperletDocumentSpec.hpp"
|
|
22
|
+
#include <NitroModules/ArrayBuffer.hpp>
|
|
23
|
+
#include <optional>
|
|
24
|
+
#include <vector>
|
|
25
|
+
|
|
26
|
+
namespace margelo::nitro::paperlet {
|
|
27
|
+
|
|
28
|
+
using namespace margelo::nitro;
|
|
29
|
+
|
|
30
|
+
/**
|
|
31
|
+
* An abstract base class for `Paperlet`
|
|
32
|
+
* Inherit this class to create instances of `HybridPaperletSpec` in C++.
|
|
33
|
+
* You must explicitly call `HybridObject`'s constructor yourself, because it is virtual.
|
|
34
|
+
* @example
|
|
35
|
+
* ```cpp
|
|
36
|
+
* class HybridPaperlet: public HybridPaperletSpec {
|
|
37
|
+
* public:
|
|
38
|
+
* HybridPaperlet(...): HybridObject(TAG) { ... }
|
|
39
|
+
* // ...
|
|
40
|
+
* };
|
|
41
|
+
* ```
|
|
42
|
+
*/
|
|
43
|
+
class HybridPaperletSpec: public virtual HybridObject {
|
|
44
|
+
public:
|
|
45
|
+
// Constructor
|
|
46
|
+
explicit HybridPaperletSpec(): HybridObject(TAG) { }
|
|
47
|
+
|
|
48
|
+
// Destructor
|
|
49
|
+
~HybridPaperletSpec() override = default;
|
|
50
|
+
|
|
51
|
+
public:
|
|
52
|
+
// Properties
|
|
53
|
+
|
|
54
|
+
|
|
55
|
+
public:
|
|
56
|
+
// Methods
|
|
57
|
+
virtual std::string initLicense(const std::string& key, const std::string& appId) = 0;
|
|
58
|
+
virtual std::shared_ptr<HybridPaperletDocumentSpec> open(const std::shared_ptr<ArrayBuffer>& data, const std::optional<std::string>& password) = 0;
|
|
59
|
+
virtual std::shared_ptr<ArrayBuffer> encrypt(const std::shared_ptr<ArrayBuffer>& pdf, const std::string& userPassword, const std::optional<std::string>& ownerPassword, double permissions) = 0;
|
|
60
|
+
virtual std::shared_ptr<ArrayBuffer> signPem(const std::shared_ptr<ArrayBuffer>& pdf, const std::string& keyPem, const std::string& certificatesPem, double page, const std::vector<double>& rect, const std::optional<std::string>& reason, const std::string& time) = 0;
|
|
61
|
+
virtual std::optional<std::string> parseMrz(const std::vector<std::string>& lines, double year) = 0;
|
|
62
|
+
|
|
63
|
+
protected:
|
|
64
|
+
// Hybrid Setup
|
|
65
|
+
void loadHybridMethods() override;
|
|
66
|
+
|
|
67
|
+
protected:
|
|
68
|
+
// Tag for logging
|
|
69
|
+
static constexpr auto TAG = "Paperlet";
|
|
70
|
+
};
|
|
71
|
+
|
|
72
|
+
} // namespace margelo::nitro::paperlet
|
package/package.json
CHANGED
|
@@ -1,6 +1,53 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@paperlet/react-native",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
|
|
3
|
+
"version": "0.1.2",
|
|
4
|
+
"description": "Paperlet PDF SDK for React Native: viewer, redaction, stamps, compression, encryption, signing, MRZ.",
|
|
5
|
+
"main": "src/index.ts",
|
|
6
|
+
"react-native": "src/index.ts",
|
|
7
|
+
"source": "src/index.ts",
|
|
8
|
+
"types": "src/index.ts",
|
|
9
|
+
"license": "SEE LICENSE IN LICENSE",
|
|
10
|
+
"homepage": "https://paperlet.ikolvi.com",
|
|
11
|
+
"author": "Ikolvi",
|
|
12
|
+
"files": [
|
|
13
|
+
"src",
|
|
14
|
+
"cpp",
|
|
15
|
+
"nitrogen",
|
|
16
|
+
"nitro.json",
|
|
17
|
+
"react-native.config.js",
|
|
18
|
+
"android/build.gradle",
|
|
19
|
+
"android/CMakeLists.txt",
|
|
20
|
+
"android/src",
|
|
21
|
+
"android/libs",
|
|
22
|
+
"android/jniLibs",
|
|
23
|
+
"ios",
|
|
24
|
+
"*.podspec",
|
|
25
|
+
"README.md"
|
|
26
|
+
],
|
|
27
|
+
"scripts": {
|
|
28
|
+
"nitrogen": "nitrogen",
|
|
29
|
+
"typecheck": "tsc --noEmit",
|
|
30
|
+
"binaries": "bash tool/binaries.sh"
|
|
31
|
+
},
|
|
32
|
+
"peerDependencies": {
|
|
33
|
+
"react": "*",
|
|
34
|
+
"react-native": "*",
|
|
35
|
+
"react-native-nitro-modules": "*"
|
|
36
|
+
},
|
|
37
|
+
"devDependencies": {
|
|
38
|
+
"@types/react": "^19.2.0",
|
|
39
|
+
"nitrogen": "0.37.2",
|
|
40
|
+
"react": "19.3.0",
|
|
41
|
+
"react-native": "0.88.1",
|
|
42
|
+
"react-native-nitro-modules": "0.37.2",
|
|
43
|
+
"typescript": "^6.0.3"
|
|
44
|
+
},
|
|
45
|
+
"repository": {
|
|
46
|
+
"type": "git",
|
|
47
|
+
"url": "git+https://github.com/Ikolvi/paperlet-sdk.git"
|
|
48
|
+
},
|
|
49
|
+
"bugs": "https://github.com/Ikolvi/paperlet-sdk/issues",
|
|
50
|
+
"publishConfig": {
|
|
51
|
+
"access": "public"
|
|
52
|
+
}
|
|
53
|
+
}
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
import { useEffect, useState } from 'react'
|
|
2
|
+
import { FlatList, Image, PixelRatio, View, type StyleProp, type ViewStyle } from 'react-native'
|
|
3
|
+
import type { PaperletDocument } from './specs/Paperlet.nitro'
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Scrollable pages of a document, rendered by PDFium. After changing the document (redact,
|
|
7
|
+
* stamp, ...) change `revision` to re-render.
|
|
8
|
+
*/
|
|
9
|
+
export function PaperletView({ document, revision = 0, style }: { document: PaperletDocument; revision?: number; style?: StyleProp<ViewStyle> }) {
|
|
10
|
+
const [width, setWidth] = useState(0)
|
|
11
|
+
const pages = Array.from({ length: document.pageCount }, (_, i) => i + 1)
|
|
12
|
+
return (
|
|
13
|
+
<FlatList
|
|
14
|
+
style={style}
|
|
15
|
+
data={pages}
|
|
16
|
+
keyExtractor={String}
|
|
17
|
+
extraData={`${width}:${revision}`}
|
|
18
|
+
onLayout={(e) => setWidth(e.nativeEvent.layout.width)}
|
|
19
|
+
renderItem={({ item }) => <Page document={document} page={item} width={width} revision={revision} />}
|
|
20
|
+
/>
|
|
21
|
+
)
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
function Page({ document, page, width, revision }: { document: PaperletDocument; page: number; width: number; revision: number }) {
|
|
25
|
+
const [uri, setUri] = useState<string>()
|
|
26
|
+
const [w, h] = document.pageSize(page)
|
|
27
|
+
useEffect(() => {
|
|
28
|
+
if (!width) return
|
|
29
|
+
let live = true
|
|
30
|
+
document.renderPage(page, Math.round(width * PixelRatio.get())).then(
|
|
31
|
+
(u) => live && setUri(u),
|
|
32
|
+
() => {} // closed while rendering
|
|
33
|
+
)
|
|
34
|
+
return () => {
|
|
35
|
+
live = false
|
|
36
|
+
}
|
|
37
|
+
}, [document, page, width, revision])
|
|
38
|
+
return (
|
|
39
|
+
<View style={{ width, height: (width * h) / w, backgroundColor: 'white', marginBottom: 8 }}>
|
|
40
|
+
{uri && <Image accessibilityLabel={`Page ${page}`} source={{ uri }} style={{ flex: 1 }} />}
|
|
41
|
+
</View>
|
|
42
|
+
)
|
|
43
|
+
}
|
package/src/index.ts
ADDED
|
@@ -0,0 +1,85 @@
|
|
|
1
|
+
import { NitroModules } from 'react-native-nitro-modules'
|
|
2
|
+
import type { Paperlet, PaperletDocument } from './specs/Paperlet.nitro'
|
|
3
|
+
|
|
4
|
+
export type { PaperletDocument } from './specs/Paperlet.nitro'
|
|
5
|
+
export { PaperletView } from './PaperletView'
|
|
6
|
+
|
|
7
|
+
const core = NitroModules.createHybridObject<Paperlet>('Paperlet')
|
|
8
|
+
|
|
9
|
+
export type PaperletTier = 'free' | 'indie' | 'business' | 'enterprise'
|
|
10
|
+
const tiers: PaperletTier[] = ['free', 'indie', 'business', 'enterprise']
|
|
11
|
+
|
|
12
|
+
export interface LicenseCheck {
|
|
13
|
+
tier: PaperletTier
|
|
14
|
+
/** Why the key was not accepted (trial mode); undefined when valid. */
|
|
15
|
+
reason?: string
|
|
16
|
+
licenseId?: string
|
|
17
|
+
coveredUntil?: string
|
|
18
|
+
releaseDate?: string
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
/**
|
|
22
|
+
* Verifies the licence key for this app (its bundle id / application id). Without a valid key
|
|
23
|
+
* paid features still work and add a "Paperlet Pro trial" watermark.
|
|
24
|
+
*/
|
|
25
|
+
export function initializePaperlet(licenseKey: string, appId: string): LicenseCheck {
|
|
26
|
+
const r = JSON.parse(core.initLicense(licenseKey, appId))
|
|
27
|
+
return { tier: tiers[r.tier] ?? 'free', reason: r.reason ?? undefined, licenseId: r.id ?? undefined, coveredUntil: r.until ?? undefined, releaseDate: r.release ?? undefined }
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
/** Opens a PDF; close it with `document.close()` when done. */
|
|
31
|
+
export function openDocument(pdf: ArrayBuffer, password?: string): PaperletDocument {
|
|
32
|
+
return core.open(pdf, password)
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
/** PDF permission bits (/P) for [encryptPdf]. */
|
|
36
|
+
export const Permissions = { print: 1 << 2, modify: 1 << 3, copy: 1 << 4, annotate: 1 << 5, fillForms: 1 << 8, assemble: 1 << 10, printHighQuality: 1 << 11 }
|
|
37
|
+
|
|
38
|
+
/** AES-256 encrypts a saved PDF (from `document.save()`). Default: every permission granted. */
|
|
39
|
+
export function encryptPdf(pdf: ArrayBuffer, userPassword: string, options: { ownerPassword?: string; permissions?: number } = {}): ArrayBuffer {
|
|
40
|
+
const all = Object.values(Permissions).reduce((a, b) => a | b, 0)
|
|
41
|
+
// Bits 7-8 and 13-32 must be 1 (ISO 32000-2, table 22).
|
|
42
|
+
const p = (options.permissions ?? all) | 0xfffff0c0
|
|
43
|
+
return core.encrypt(pdf, userPassword, options.ownerPassword, p | 0)
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
export interface SignOptions {
|
|
47
|
+
/** Unencrypted private key (PKCS#1 or PKCS#8 PEM). RSA. */
|
|
48
|
+
keyPem: string
|
|
49
|
+
/** PEM certificates, signer first, then the chain. */
|
|
50
|
+
certificatesPem: string
|
|
51
|
+
page?: number
|
|
52
|
+
/** [left, bottom, right, top] in points; omitted: an invisible signature. */
|
|
53
|
+
rect?: number[]
|
|
54
|
+
reason?: string
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
/** PAdES (CMS, SHA-256) signature of a saved PDF. */
|
|
58
|
+
export function signPdf(pdf: ArrayBuffer, o: SignOptions): ArrayBuffer {
|
|
59
|
+
const t = new Date()
|
|
60
|
+
const two = (n: number) => String(n).padStart(2, '0')
|
|
61
|
+
const time = `D:${t.getUTCFullYear()}${two(t.getUTCMonth() + 1)}${two(t.getUTCDate())}${two(t.getUTCHours())}${two(t.getUTCMinutes())}${two(t.getUTCSeconds())}Z`
|
|
62
|
+
return core.signPem(pdf, o.keyPem, o.certificatesPem, o.page ?? 1, o.rect ?? [0, 0, 0, 0], o.reason, time)
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
export interface Mrz {
|
|
66
|
+
format: 'td1' | 'td2' | 'td3'
|
|
67
|
+
documentCode: string
|
|
68
|
+
issuer: string
|
|
69
|
+
documentNumber: string
|
|
70
|
+
nationality: string
|
|
71
|
+
/** [year, month, day], or null when unreadable. */
|
|
72
|
+
birthDate: [number, number, number] | null
|
|
73
|
+
sex: string
|
|
74
|
+
expiryDate: [number, number, number] | null
|
|
75
|
+
surname: string
|
|
76
|
+
givenNames: string
|
|
77
|
+
optional: string
|
|
78
|
+
lines: string[]
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
/** Finds a machine-readable zone (passport, ID card, visa) in OCR text lines; every check digit must match. */
|
|
82
|
+
export function parseMrz(lines: string[]): Mrz | undefined {
|
|
83
|
+
const r = core.parseMrz(lines, new Date().getFullYear())
|
|
84
|
+
return r === undefined ? undefined : JSON.parse(r)
|
|
85
|
+
}
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
import type { HybridObject } from 'react-native-nitro-modules'
|
|
2
|
+
|
|
3
|
+
/** An open PDF. Page numbers start at 1; rectangles are [left, bottom, right, top] in points. */
|
|
4
|
+
export interface PaperletDocument extends HybridObject<{ ios: 'c++'; android: 'c++' }> {
|
|
5
|
+
readonly pageCount: number
|
|
6
|
+
/** [width, height] in points, as displayed. */
|
|
7
|
+
pageSize(page: number): number[]
|
|
8
|
+
/** The page `width` pixels wide, as a JPEG data URI for <Image>. */
|
|
9
|
+
renderPage(page: number, width: number): Promise<string>
|
|
10
|
+
/** Permanently removes everything inside `rects` (4 numbers each), filled with RGB `color`. Business tier. */
|
|
11
|
+
redact(page: number, rects: number[], color: number): void
|
|
12
|
+
/** Text stamp in `rect`, ARGB `color`. Indie tier. */
|
|
13
|
+
stampText(page: number, rect: number[], text: string, color: number): void
|
|
14
|
+
/** Recompresses large images as JPEG; returns how many changed. Indie tier. */
|
|
15
|
+
compressImages(page: number, maxDpi: number, quality: number): number
|
|
16
|
+
/** The document as a new PDF file. */
|
|
17
|
+
save(): ArrayBuffer
|
|
18
|
+
close(): void
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
export interface Paperlet extends HybridObject<{ ios: 'c++'; android: 'c++' }> {
|
|
22
|
+
/** Verifies the licence key for this app id; returns JSON (see LicenseCheck). */
|
|
23
|
+
initLicense(key: string, appId: string): string
|
|
24
|
+
open(data: ArrayBuffer, password?: string): PaperletDocument
|
|
25
|
+
/** AES-256 encryption of a saved PDF. Indie tier (unlicensed: trial watermark is added first by save). */
|
|
26
|
+
encrypt(pdf: ArrayBuffer, userPassword: string, ownerPassword: string | undefined, permissions: number): ArrayBuffer
|
|
27
|
+
/** PAdES signature with an unencrypted PEM key and PEM certificates (signer first). Business tier. */
|
|
28
|
+
signPem(pdf: ArrayBuffer, keyPem: string, certificatesPem: string, page: number, rect: number[], reason: string | undefined, time: string): ArrayBuffer
|
|
29
|
+
/** MRZ found in OCR lines, as JSON, or undefined. */
|
|
30
|
+
parseMrz(lines: string[], year: number): string | undefined
|
|
31
|
+
}
|