|
4 | 4 | * pdf2htmlEX-Android (https://github.com/ViliusSutkus89/pdf2htmlEX-Android) |
5 | 5 | * Android port of pdf2htmlEX - Convert PDF to HTML without losing text or format. |
6 | 6 | * |
7 | | - * Copyright (c) 2019 Vilius Sutkus <ViliusSutkus89@gmail.com> |
| 7 | + * Copyright (c) 2019, 2022 ViliusSutkus89.com |
8 | 8 | * |
9 | | - * This program is free software: you can redistribute it and/or modify |
| 9 | + * pdf2htmlEX-Android is free software: you can redistribute it and/or modify |
10 | 10 | * it under the terms of the GNU General Public License version 3 as |
11 | 11 | * published by the Free Software Foundation. |
12 | 12 | * |
|
19 | 19 | * along with this program. If not, see <https://www.gnu.org/licenses/>. |
20 | 20 | */ |
21 | 21 |
|
22 | | -#include <cstdlib> |
23 | | -#include <string> |
24 | 22 | #include <jni.h> |
25 | 23 | #include <android/log.h> |
| 24 | +#include "CCharGC.h" |
26 | 25 | #include "pdf2htmlEX.h" |
27 | 26 |
|
28 | 27 | #define retValOK 0 |
29 | 28 | #define retValError 1 |
30 | 29 | #define retValEncryptionError 2 |
31 | 30 | #define retValCopyProtected 3 |
32 | 31 |
|
33 | | -class CCharGC { |
34 | | -private: |
35 | | - JNIEnv *env; |
36 | | - jstring input; |
37 | | - const char * cstr; |
| 32 | +extern "C" |
| 33 | +JNIEXPORT jlong JNICALL |
| 34 | +Java_com_viliussutkus89_android_pdf2htmlex_NativeConverter_createNewConverterObject(JNIEnv *env, jclass, |
| 35 | + jstring tmp_dir, |
| 36 | + jstring data_dir, |
| 37 | + jstring poppler_dir) { |
| 38 | + auto * pdf2htmlEX = new pdf2htmlEX::pdf2htmlEX(); |
38 | 39 |
|
39 | | -public: |
40 | | - CCharGC(JNIEnv *env, jstring input) : env(env), input(input) { |
41 | | - this->cstr = env->GetStringUTFChars(input, nullptr); |
42 | | - } |
| 40 | + pdf2htmlEX->setTMPDir(CCharGC(env, tmp_dir).c_str()); |
| 41 | + pdf2htmlEX->setDataDir(CCharGC(env, data_dir).c_str()); |
| 42 | + pdf2htmlEX->setPopplerDataDir(CCharGC(env, poppler_dir).c_str()); |
43 | 43 |
|
44 | | - const char * c_str() const { |
45 | | - return this->cstr; |
46 | | - } |
| 44 | + return (jlong) pdf2htmlEX; |
| 45 | +} |
47 | 46 |
|
48 | | - bool isEmpty() const { return this->cstr[0] == '\0'; } |
| 47 | +extern "C" |
| 48 | +JNIEXPORT void JNICALL |
| 49 | +Java_com_viliussutkus89_android_pdf2htmlex_NativeConverter_dealloc(JNIEnv *, jclass, jlong converter) { |
| 50 | + auto * pdf2htmlEX = (pdf2htmlEX::pdf2htmlEX *) converter; |
| 51 | + delete pdf2htmlEX; |
| 52 | +} |
49 | 53 |
|
50 | | - ~CCharGC() { |
51 | | - env->ReleaseStringUTFChars(this->input, this->cstr); |
| 54 | +extern "C" |
| 55 | +JNIEXPORT jint JNICALL |
| 56 | +Java_com_viliussutkus89_android_pdf2htmlex_NativeConverter_convert(JNIEnv *, jclass, jlong converter) { |
| 57 | + auto * pdf2htmlEX = (pdf2htmlEX::pdf2htmlEX *) converter; |
| 58 | + try { |
| 59 | + pdf2htmlEX->convert(); |
| 60 | + } catch (const pdf2htmlEX::EncryptionPasswordException & e) { |
| 61 | + return retValEncryptionError; |
| 62 | + } catch (const pdf2htmlEX::DocumentCopyProtectedException & e) { |
| 63 | + return retValCopyProtected; |
| 64 | + } catch (const pdf2htmlEX::ConversionFailedException & e) { |
| 65 | + __android_log_print(ANDROID_LOG_ERROR, "pdf2htmlEX-Android" , "%s", e.what()); |
| 66 | + return retValError; |
52 | 67 | } |
53 | | -}; |
| 68 | + return retValOK; |
| 69 | +} |
54 | 70 |
|
55 | 71 | extern "C" |
56 | 72 | JNIEXPORT void JNICALL |
57 | | -Java_com_viliussutkus89_android_pdf2htmlex_pdf2htmlEX_set_1environment_1value(JNIEnv *env, jclass, |
58 | | - jstring key_, |
59 | | - jstring value_) { |
60 | | - CCharGC key(env, key_); |
61 | | - CCharGC value(env, value_); |
62 | | - setenv(key.c_str(), value.c_str(), 1); |
| 73 | +Java_com_viliussutkus89_android_pdf2htmlex_NativeConverter_setInputFile(JNIEnv *env, jclass, jlong converter, |
| 74 | + jstring input_file) { |
| 75 | + auto * pdf2htmlEX = (pdf2htmlEX::pdf2htmlEX *) converter; |
| 76 | + pdf2htmlEX->setInputFilename(CCharGC(env, input_file).c_str()); |
63 | 77 | } |
64 | 78 |
|
65 | 79 | extern "C" |
66 | | -JNIEXPORT jint JNICALL |
67 | | -Java_com_viliussutkus89_android_pdf2htmlex_pdf2htmlEX_call_1pdf2htmlEX(JNIEnv *env, jobject, |
68 | | - jstring dataDir_, |
69 | | - jstring popplerDir_, jstring tmpDir_, |
70 | | - jstring inputFile_, |
71 | | - jstring outputFile_, |
72 | | - jstring ownerPassword_, |
73 | | - jstring userPassword_, |
74 | | - jboolean enableOutline, |
75 | | - jboolean enableDrm, |
76 | | - jstring backgroundFormat_, |
77 | | - jboolean enableEmbedFont, |
78 | | - jboolean enableEmbedExternalFont, |
79 | | - jboolean processAnnotation |
80 | | - ) { |
81 | | - CCharGC dataDir(env, dataDir_); |
82 | | - CCharGC popplerDir(env, popplerDir_); |
83 | | - CCharGC tmpDir(env, tmpDir_); |
84 | | - CCharGC inputFile(env, inputFile_); |
85 | | - CCharGC outputFile(env, outputFile_); |
86 | | - CCharGC ownerPassword(env, ownerPassword_); |
87 | | - CCharGC userPassword(env, userPassword_); |
88 | | - CCharGC backgroundFormat(env, backgroundFormat_); |
89 | | - |
90 | | - pdf2htmlEX::pdf2htmlEX converter; |
91 | | - converter.setProcessOutline(enableOutline == JNI_TRUE); |
92 | | - converter.setDRM(enableDrm == JNI_TRUE); |
93 | | - converter.setDataDir(dataDir.c_str()); |
94 | | - converter.setPopplerDataDir(popplerDir.c_str()); |
95 | | - converter.setTMPDir(tmpDir.c_str()); |
96 | | - converter.setInputFilename(inputFile.c_str()); |
97 | | - converter.setOutputFilename(outputFile.c_str()); |
98 | | - converter.setEmbedFont(enableEmbedFont == JNI_TRUE); |
99 | | - converter.setEmbedExternalFont(enableEmbedExternalFont == JNI_TRUE); |
100 | | - converter.setProcessAnnotation(processAnnotation == JNI_TRUE); |
101 | | - |
102 | | - if (!backgroundFormat.isEmpty()) { |
103 | | - converter.setBackgroundImageFormat(backgroundFormat.c_str()); |
104 | | - } |
105 | | - |
106 | | - if (!ownerPassword.isEmpty()) { |
107 | | - converter.setOwnerPassword(ownerPassword.c_str()); |
108 | | - } |
109 | | - |
110 | | - if (!userPassword.isEmpty()) { |
111 | | - converter.setUserPassword(userPassword.c_str()); |
112 | | - } |
113 | | - |
114 | | - try { |
115 | | - converter.convert(); |
116 | | - } catch (const pdf2htmlEX::EncryptionPasswordException & e) { |
117 | | - return retValEncryptionError; |
118 | | - } catch (const pdf2htmlEX::DocumentCopyProtectedException & e) { |
119 | | - return retValCopyProtected; |
120 | | - } catch (const pdf2htmlEX::ConversionFailedException & e) { |
121 | | - __android_log_print(ANDROID_LOG_ERROR, "pdf2htmlEX-Android" , "%s", e.what()); |
122 | | - return retValError; |
123 | | - } |
124 | | - return retValOK; |
| 80 | +JNIEXPORT void JNICALL |
| 81 | +Java_com_viliussutkus89_android_pdf2htmlex_NativeConverter_setOutputFile(JNIEnv *env, jclass, jlong converter, |
| 82 | + jstring output_file) { |
| 83 | + auto * pdf2htmlEX = (pdf2htmlEX::pdf2htmlEX *) converter; |
| 84 | + pdf2htmlEX->setOutputFilename(CCharGC(env, output_file).c_str()); |
| 85 | +} |
| 86 | + |
| 87 | +extern "C" |
| 88 | +JNIEXPORT void JNICALL |
| 89 | +Java_com_viliussutkus89_android_pdf2htmlex_NativeConverter_setOwnerPassword(JNIEnv *env, jclass, jlong converter, |
| 90 | + jstring owner_password) { |
| 91 | + auto * pdf2htmlEX = (pdf2htmlEX::pdf2htmlEX *) converter; |
| 92 | + pdf2htmlEX->setOwnerPassword(CCharGC(env, owner_password).c_str()); |
| 93 | +} |
| 94 | + |
| 95 | +extern "C" |
| 96 | +JNIEXPORT void JNICALL |
| 97 | +Java_com_viliussutkus89_android_pdf2htmlex_NativeConverter_setUserPassword(JNIEnv *env, jclass, jlong converter, |
| 98 | + jstring user_password) { |
| 99 | + auto * pdf2htmlEX = (pdf2htmlEX::pdf2htmlEX *) converter; |
| 100 | + pdf2htmlEX->setUserPassword(CCharGC(env, user_password).c_str()); |
| 101 | +} |
| 102 | + |
| 103 | +extern "C" |
| 104 | +JNIEXPORT void JNICALL |
| 105 | +Java_com_viliussutkus89_android_pdf2htmlex_NativeConverter_setOutline(JNIEnv *env, jclass, jlong converter, |
| 106 | + jobject enable) { |
| 107 | + auto * pdf2htmlEX = (pdf2htmlEX::pdf2htmlEX *) converter; |
| 108 | + pdf2htmlEX->setProcessOutline(enable); |
| 109 | +} |
| 110 | + |
| 111 | +extern "C" |
| 112 | +JNIEXPORT void JNICALL |
| 113 | +Java_com_viliussutkus89_android_pdf2htmlex_NativeConverter_setDrm(JNIEnv *env, jclass, jlong converter, |
| 114 | + jobject enable) { |
| 115 | + auto * pdf2htmlEX = (pdf2htmlEX::pdf2htmlEX *) converter; |
| 116 | + pdf2htmlEX->setDRM(enable); |
| 117 | +} |
| 118 | + |
| 119 | +extern "C" |
| 120 | +JNIEXPORT void JNICALL |
| 121 | +Java_com_viliussutkus89_android_pdf2htmlex_NativeConverter_setEmbedFont(JNIEnv *, jclass, jlong converter, |
| 122 | + jobject embed) { |
| 123 | + auto * pdf2htmlEX = (pdf2htmlEX::pdf2htmlEX *) converter; |
| 124 | + pdf2htmlEX->setEmbedFont(embed); |
| 125 | +} |
| 126 | + |
| 127 | +extern "C" |
| 128 | +JNIEXPORT void JNICALL |
| 129 | +Java_com_viliussutkus89_android_pdf2htmlex_NativeConverter_setEmbedExternalFont(JNIEnv *, jclass, jlong converter, |
| 130 | + jobject embed) { |
| 131 | + auto * pdf2htmlEX = (pdf2htmlEX::pdf2htmlEX *) converter; |
| 132 | + pdf2htmlEX->setEmbedExternalFont(embed); |
| 133 | +} |
| 134 | + |
| 135 | +extern "C" |
| 136 | +JNIEXPORT void JNICALL |
| 137 | +Java_com_viliussutkus89_android_pdf2htmlex_NativeConverter_setProcessAnnotation(JNIEnv *, jclass, jlong converter, |
| 138 | + jobject process) { |
| 139 | + auto * pdf2htmlEX = (pdf2htmlEX::pdf2htmlEX *) converter; |
| 140 | + pdf2htmlEX->setProcessAnnotation(process); |
| 141 | +} |
| 142 | + |
| 143 | +extern "C" |
| 144 | +JNIEXPORT void JNICALL |
| 145 | +Java_com_viliussutkus89_android_pdf2htmlex_NativeConverter_setBackgroundFormat(JNIEnv *env, jclass, jlong converter, |
| 146 | + jstring background_format) { |
| 147 | + auto * pdf2htmlEX = (pdf2htmlEX::pdf2htmlEX *) converter; |
| 148 | + pdf2htmlEX->setBackgroundImageFormat(CCharGC(env, background_format).c_str()); |
125 | 149 | } |
0 commit comments