Skip to content

Commit 8373a7a

Browse files
Update main interface - new pdf2htmlEX(ctx).setInputPDF("file").convert()
1 parent a7c7590 commit 8373a7a

2 files changed

Lines changed: 39 additions & 41 deletions

File tree

pdf2htmlEX/src/main/cpp/pdf2htmlEX.cc

Lines changed: 0 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -51,26 +51,6 @@ void vector_to_char_pp(const std::vector<const std::string> & input, int * argc,
5151
*argv = output;
5252
}
5353

54-
extern "C"
55-
JNIEXPORT void JNICALL
56-
Java_com_viliussutkus89_android_pdf2htmlex_pdf2htmlEX_set_1env_1values_1for_1fontforge(JNIEnv *env,
57-
jobject,
58-
jstring homedir_,
59-
jstring tmpdir_,
60-
jstring username_) {
61-
const char * homedir = env->GetStringUTFChars(homedir_, nullptr);
62-
const char * tmpdir = env->GetStringUTFChars(tmpdir_, nullptr);
63-
const char * username = env->GetStringUTFChars(username_, nullptr);
64-
65-
setenv("HOME", homedir, 0);
66-
setenv("TMPDIR", tmpdir, 0);
67-
setenv("USER", username, 0);
68-
69-
env->ReleaseStringUTFChars(homedir_, homedir);
70-
env->ReleaseStringUTFChars(tmpdir_, tmpdir);
71-
env->ReleaseStringUTFChars(username_, username);
72-
}
73-
7454
extern "C"
7555
JNIEXPORT void JNICALL
7656
Java_com_viliussutkus89_android_pdf2htmlex_pdf2htmlEX_set_1environment_1value(JNIEnv *env, jobject,

pdf2htmlEX/src/main/java/com/viliussutkus89/android/pdf2htmlex/pdf2htmlEX.java

Lines changed: 39 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -34,24 +34,20 @@ public final class pdf2htmlEX {
3434

3535
private static final String TAG = "pdf2htmlEX";
3636

37-
private void prepareEnvironmentForFontforge(Context ctx) {
38-
File homeDir = new File(ctx.getCacheDir(), "homeForFontforge");
39-
homeDir.mkdir();
40-
41-
File tmpDir = new File(ctx.getCacheDir(), "tmpdir");
42-
tmpDir.mkdir();
43-
44-
String model = Build.MODEL;
45-
46-
set_env_values_for_fontforge(homeDir.getAbsolutePath(), tmpDir.getAbsolutePath(), model);
47-
}
48-
4937
private File m_pdf2htmlEX_dataDir;
5038
private File m_poppler_dataDir;
5139
private File m_pdf2htmlEX_tmpDir;
5240
private File m_outputHtmlsDir;
5341

54-
public pdf2htmlEX(@NonNull Context ctx) {
42+
private File p_inputPDF;
43+
44+
public static class ConversionFailedException extends Exception {
45+
public ConversionFailedException(String errorMessage) {
46+
super(errorMessage);
47+
}
48+
}
49+
50+
private synchronized void init(@NonNull Context ctx) {
5551
File filesDir = ctx.getFilesDir();
5652
File cacheDir = ctx.getCacheDir();
5753
// @TODO: https://github.com/ViliusSutkus89/pdf2htmlEX-Android/issues/9
@@ -75,19 +71,43 @@ public pdf2htmlEX(@NonNull Context ctx) {
7571
m_outputHtmlsDir = new File(m_pdf2htmlEX_tmpDir, "output-htmls");
7672
m_outputHtmlsDir.mkdir();
7773

78-
prepareEnvironmentForFontforge(ctx);
74+
File homeDir = new File(cacheDir, "homeForFontforge");
75+
homeDir.mkdir();
76+
77+
File tmpDir = new File(cacheDir, "tmpdir");
78+
tmpDir.mkdir();
79+
80+
set_environment_value("HOME", homeDir.getAbsolutePath());
81+
set_environment_value("TMPDIR", tmpDir.getAbsolutePath());
82+
set_environment_value("USER", Build.MODEL);
7983

8084
FontconfigAndroid.init(ctx.getAssets(), cacheDir, filesDir);
8185
}
8286

83-
public class ConversionFailedException extends Exception {
84-
public ConversionFailedException(String errorMessage) {
85-
super(errorMessage);
86-
}
87+
public pdf2htmlEX(@NonNull Context ctx) {
88+
init(ctx);
89+
}
90+
91+
public pdf2htmlEX setInputPDF(@NonNull File inputPDF) {
92+
this.p_inputPDF = inputPDF;
93+
return this;
8794
}
8895

8996
public File convert(@NonNull File inputPDF) throws IOException, ConversionFailedException {
90-
String inputFilenameNoPDFExt = inputPDF.getName();
97+
setInputPDF(inputPDF);
98+
return convert();
99+
}
100+
101+
public File convert() throws IOException, ConversionFailedException {
102+
if (null == this.p_inputPDF) {
103+
throw new ConversionFailedException("No Input PDF given!");
104+
}
105+
106+
if (!this.p_inputPDF.exists()) {
107+
throw new ConversionFailedException("Input PDF does not exist!");
108+
}
109+
110+
String inputFilenameNoPDFExt = this.p_inputPDF.getName();
91111
if (inputFilenameNoPDFExt.endsWith(".pdf")) {
92112
inputFilenameNoPDFExt = inputFilenameNoPDFExt.substring(0, inputFilenameNoPDFExt.length() - 4);
93113
}
@@ -114,6 +134,4 @@ public File convert(@NonNull File inputPDF) throws IOException, ConversionFailed
114134
// Because Java cannot setenv for the current process
115135
static native void set_environment_value(String key, String value);
116136

117-
// Because Java can't set env vars for the current process...
118-
private native void set_env_values_for_fontforge(String homeDir, String tmpDir, String username);
119137
}

0 commit comments

Comments
 (0)