@compdfkit_pdf_sdk/react_native
Version:
ComPDFKit for React Native is a comprehensive SDK that allows you to quickly add PDF functionality to Android, iOS, and React Native applications.
91 lines • 3.35 kB
JavaScript
/**
* Copyright © 2014-2026 PDF Technologies, Inc. All Rights Reserved.
*
* THIS SOURCE CODE AND ANY ACCOMPANYING DOCUMENTATION ARE PROTECTED BY INTERNATIONAL COPYRIGHT LAW
* AND MAY NOT BE RESOLD OR REDISTRIBUTED. USAGE IS BOUND TO THE ComPDFKit LICENSE AGREEMENT.
* UNAUTHORIZED REPRODUCTION OR DISTRIBUTION IS SUBJECT TO CIVIL AND CRIMINAL PENALTIES.
* This notice may not be removed from this file.
*/
import { NativeModules } from "react-native";
import CPDFFontName from "../document/CPDFFontName";
import { getDefaultConfig } from "./DefaultConfig";
const nativeMethodNames = [
"getVersionCode",
"getSDKBuildTag",
"init_",
"initialize",
"initWithPath",
"openDocument",
"removeSignFileList",
"pickFile",
"setImportFontDir",
"updateImportFontDir",
"getFonts",
];
const nativeComPDFKit = NativeModules.ComPDFKit;
function getAvailableNativeMethodNames() {
if (!nativeComPDFKit) {
return [];
}
return Object.keys(nativeComPDFKit)
.filter((key) => typeof nativeComPDFKit[key] === "function")
.sort();
}
function createMissingNativeMethodError(methodName) {
const availableMethods = getAvailableNativeMethodNames();
const moduleState = nativeComPDFKit ? "loaded" : "missing";
const availableMethodsText = availableMethods.length
? availableMethods.join(", ")
: "none";
return new Error([
`ComPDFKit native method '${methodName}' is unavailable.`,
`NativeModules.ComPDFKit is ${moduleState}.`,
`Available native methods: ${availableMethodsText}.`,
"This usually means the JavaScript sources are newer than the installed native app.",
"Rebuild and reinstall the host app after changing Android/iOS native module APIs.",
].join(" "));
}
function getRequiredNativeMethod(methodName) {
const method = nativeComPDFKit?.[methodName];
if (typeof method !== "function") {
throw createMissingNativeMethodError(methodName);
}
return method;
}
const comPDFKitBase = {
...(nativeComPDFKit ?? {}),
getDefaultConfig,
async createUri(fileName, childDirectoryName, mimeType) {
const createUri = nativeComPDFKit?.createUri;
if (typeof createUri !== "function") {
return Promise.reject(new Error("createUri() is only supported on Android."));
}
return createUri(fileName, childDirectoryName, mimeType);
},
async getFonts() {
const getFonts = nativeComPDFKit?.getFonts;
if (typeof getFonts !== "function") {
throw createMissingNativeMethodError("getFonts");
}
const fonts = await getFonts();
return Array.isArray(fonts)
? fonts.map((font) => CPDFFontName.fromJson(font))
: [];
},
};
const ComPDFKit = new Proxy(comPDFKitBase, {
get(target, property, receiver) {
const value = Reflect.get(target, property, receiver);
if (typeof property === "string" &&
nativeMethodNames.includes(property) &&
typeof value === "undefined") {
return (...args) => {
const method = getRequiredNativeMethod(property);
return method(...args);
};
}
return value;
},
});
export { ComPDFKit };
//# sourceMappingURL=ComPDFKitModule.js.map