11/*
22 * pdf2htmlEX.java
33 *
4- * Copyright (C) 2019, 2020, 2022 ViliusSutkus89.com
4+ * Copyright (C) 2019, 2020, 2022, 2023 ViliusSutkus89.com
55 *
66 * pdf2htmlEX-Android is free software: you can redistribute it and/or modify
77 * it under the terms of the GNU General Public License as published by
3535import java .io .IOException ;
3636
3737
38+ @ SuppressWarnings ("unused" )
3839public class pdf2htmlEX implements Closeable {
3940 public static class ConversionFailedException extends Exception {
4041 public ConversionFailedException (String errorMessage ) {
@@ -64,10 +65,16 @@ public CopyProtectionException(String errorMessage) {
6465
6566 static private final Object s_initSynchronizer = new Object ();
6667
67- private final File m_outputHtmlsDir ;
68+ // subdir of cache dir
69+ private final File mOutputDir ;
70+
71+ // subdir of mOutputDir by default, unless requested otherwise by setDestinationDir() / setOutputHtml()
72+ private File mDestinationDir ;
73+
74+ private String mOutputFilename ;
6875
6976 private File mInputPdf ;
70- private File mOutputHtml ;
77+
7178 private boolean mUserPasswordEntered ;
7279 private boolean mOwnerPasswordEntered ;
7380
@@ -88,7 +95,7 @@ public pdf2htmlEX(@NonNull Context ctx) {
8895
8996 // tmpDir is where pdf2htmlEX (not pdf2htmlEX-Android wrapper) does it's work
9097 File pdf2htmlEX_tmpDir = new File (cacheDir , "pdf2htmlEX-tmp" );
91- m_outputHtmlsDir = new File (cacheDir , "output-htmls" );
98+ mOutputDir = new File (cacheDir , "output-htmls" );
9299
93100 File fontforgeHome = new File (cacheDir , "FontforgeHome" );
94101 File envTMPDIR = new File (cacheDir , "envTMPDIR" );
@@ -106,8 +113,10 @@ public pdf2htmlEX(@NonNull Context ctx) {
106113 // Poppler requires encoding data
107114 File poppler_dataDir = ae .extract (new File (filesDir , "share" ), "poppler" );
108115
116+ // @TODO: fontforge data
117+
109118 pdf2htmlEX_tmpDir .mkdir ();
110- m_outputHtmlsDir .mkdir ();
119+ mOutputDir .mkdir ();
111120
112121 fontforgeHome .mkdir ();
113122 EnvVar .set ("HOME" , fontforgeHome .getAbsolutePath ());
@@ -130,24 +139,26 @@ public void close() {
130139 nc .close ();
131140 }
132141
133- public static String generateOutputFilename (String inputFilename ) {
134- String inputFilenameNoPDFExt = inputFilename ;
135- if (inputFilenameNoPDFExt .endsWith (".pdf" )) {
136- inputFilenameNoPDFExt = inputFilenameNoPDFExt .substring (0 , inputFilenameNoPDFExt .length () - 4 );
137- }
138- return inputFilenameNoPDFExt + ".html" ;
142+ private static String generateOutputFilenameWithoutExt (String inputFilename ) {
143+ if (inputFilename .endsWith (".pdf" ))
144+ return inputFilename .substring (0 , inputFilename .length () - 4 );
145+ else
146+ return inputFilename ;
139147 }
140148
141- private static File generateOutputFile (File outputDir , String inputFilename ) throws IOException {
142- String outputFilenameNoExt = generateOutputFilename (inputFilename );
143- outputFilenameNoExt = outputFilenameNoExt .substring (0 , outputFilenameNoExt .length () - 5 );
149+ private static String generateOutputFilename (String inputFilename ) {
150+ return generateOutputFilenameWithoutExt (inputFilename ) + ".html" ;
151+ }
152+
153+ private static File generateOutputSubDirectory (File outputDir , String inputFilename ) {
154+ String outputFilenameNoExt = generateOutputFilenameWithoutExt (inputFilename );
144155
145- File outputFile = new File (outputDir , outputFilenameNoExt + ".html" );
146- for (int i = 0 ; !outputFile . createNewFile (); i ++) {
147- outputFile = new File (outputDir , outputFilenameNoExt + "-" + i + ".html" );
156+ File outputSubDir = new File (outputDir , outputFilenameNoExt );
157+ for (int i = 2 ; !outputSubDir . mkdir (); i ++) {
158+ outputSubDir = new File (outputDir , outputFilenameNoExt + "-" + i );
148159 }
149160
150- return outputFile ;
161+ return outputSubDir ;
151162 }
152163
153164 public File convert () throws IOException , ConversionFailedException {
@@ -159,18 +170,37 @@ public File convert() throws IOException, ConversionFailedException {
159170 }
160171 NativeConverter .setInputFile (nc .mConverter , inputPdf .getAbsolutePath ());
161172
162- File outputHtml = mOutputHtml ;
163- if (null == outputHtml ) {
164- outputHtml = generateOutputFile (m_outputHtmlsDir , inputPdf .getName ());
165- }
166- NativeConverter .setOutputFile (nc .mConverter , outputHtml .getAbsolutePath ());
173+ String outputFilename = mOutputFilename ;
174+ File destinationDir = mDestinationDir ;
175+
176+ if (null == outputFilename )
177+ outputFilename = generateOutputFilename (inputPdf .getName ());
178+
179+ if (null == destinationDir )
180+ destinationDir = generateOutputSubDirectory (mOutputDir , inputPdf .getName ());
181+
182+ File outputFile = new File (destinationDir , outputFilename );
183+
184+ NativeConverter .setOutputFilename (nc .mConverter , outputFilename );
185+ NativeConverter .setDestinationDir (nc .mConverter , destinationDir .getAbsolutePath ());
167186
168187 int retVal = NativeConverter .convert (nc .mConverter );
169188 if (0 == retVal ) {
170- return outputHtml ;
189+ return outputFile ;
171190 }
172191
173- outputHtml .delete ();
192+ outputFile .delete ();
193+ if (null == mDestinationDir ) {
194+ if (destinationDir .exists () && destinationDir .isDirectory ()) {
195+ File [] outputFiles = destinationDir .listFiles ();
196+ if (outputFiles != null ) {
197+ for (File i : outputFiles ) {
198+ i .delete ();
199+ }
200+ }
201+ destinationDir .delete ();
202+ }
203+ }
174204
175205 // retVal values defined in pdf2htmlEX.cc
176206 switch (retVal ) {
@@ -190,8 +220,8 @@ public File convert() throws IOException, ConversionFailedException {
190220 }
191221
192222 public File convert (@ NonNull File inputPDF ) throws IOException , ConversionFailedException {
193- setInputPDF (inputPDF );
194- return convert ();
223+ this . setInputPDF (inputPDF );
224+ return this . convert ();
195225 }
196226
197227 public pdf2htmlEX setInputPDF (@ Nullable File inputPDF ) {
@@ -200,7 +230,17 @@ public pdf2htmlEX setInputPDF(@Nullable File inputPDF) {
200230 }
201231
202232 public pdf2htmlEX setOutputHtml (@ Nullable File outputHtml ) {
203- mOutputHtml = outputHtml ;
233+ File destinationDir = null ;
234+ String filename = null ;
235+ if (null != outputHtml ) {
236+ destinationDir = outputHtml .getAbsoluteFile ().getParentFile ();
237+ filename = outputHtml .getName ();
238+ }
239+ return this .setDestinationDir (destinationDir ).setOutputFilename (filename );
240+ }
241+
242+ public pdf2htmlEX setOutputFilename (@ Nullable String outputFilename ) {
243+ mOutputFilename = outputFilename ;
204244 return this ;
205245 }
206246
@@ -269,11 +309,25 @@ public pdf2htmlEX setSplitPages(boolean splitPages) {
269309 return this ;
270310 }
271311
272- // skipped:
273- // setDestinationDir(const std::string &destinationDir);
274- // setCSSFilename(const std::string &cssFilename);
275- // setPageFilename(const std::string &pageFilename);
276- // setOutlineFilename(const std::string &outlineFilename);
312+ public pdf2htmlEX setDestinationDir (@ Nullable File destinationDir ) {
313+ mDestinationDir = destinationDir ;
314+ return this ;
315+ }
316+
317+ public pdf2htmlEX setCSSFilename (@ Nullable String cssFilename ) {
318+ NativeConverter .setCSSFilename (nc .mConverter , cssFilename );
319+ return this ;
320+ }
321+
322+ public pdf2htmlEX setPageFilename (@ Nullable String pageFilename ) {
323+ NativeConverter .setPageFilename (nc .mConverter , pageFilename );
324+ return this ;
325+ }
326+
327+ public pdf2htmlEX setOutlineFilename (@ Nullable String outlineFilename ) {
328+ NativeConverter .setOutlineFilename (nc .mConverter , outlineFilename );
329+ return this ;
330+ }
277331
278332 public pdf2htmlEX setProcessNonText (boolean processNonText ) {
279333 NativeConverter .setProcessNonText (nc .mConverter , processNonText );
@@ -284,10 +338,6 @@ public pdf2htmlEX setProcessOutline(boolean processOutline) {
284338 NativeConverter .setProcessOutline (nc .mConverter , processOutline );
285339 return this ;
286340 }
287- @ Deprecated // setOutline is already exposed in a released version.
288- public pdf2htmlEX setOutline (boolean processOutline ) {
289- return setProcessOutline (processOutline );
290- }
291341
292342 public pdf2htmlEX setProcessAnnotation (boolean processAnnotation ) {
293343 NativeConverter .setProcessAnnotation (nc .mConverter , processAnnotation );
@@ -426,15 +476,6 @@ public pdf2htmlEX setCoveredTextDPI(double coveredTextDPI) {
426476 return this ;
427477 }
428478
429- /**
430- * @param backgroundFormat: png (default), jpg or svg
431- */
432- @ Deprecated
433- public pdf2htmlEX setBackgroundFormat (@ NonNull String backgroundFormat ) {
434- NativeConverter .setBackgroundImageFormat (nc .mConverter , backgroundFormat );
435- return this ;
436- }
437-
438479 public enum BackgroundImageFormat {
439480 PNG ("png" ),
440481 JPG ("jpg" ),
0 commit comments