@@ -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