9
9
import com .facebook .react .bridge .WritableMap ;
10
10
11
11
import java .io .File ;
12
+ import java .io .IOException ;
12
13
import java .util .UUID ;
13
14
14
15
import android .os .Environment ;
15
16
import android .print .PdfConverter ;
16
17
17
18
public class RNHTMLtoPDFModule extends ReactContextBaseJavaModule {
18
19
19
- private final ReactApplicationContext mReactContext ;
20
+ private static final String HTML = "html" ;
21
+ private static final String FILE_NAME = "fileName" ;
22
+ private static final String DIRECTORY = "directory" ;
23
+ private static final String BASE_64 = "base64" ;
24
+ private static final String BASE_URL = "baseURL" ;
25
+
26
+ private static final String PDF_EXTENSION = ".pdf" ;
27
+ private static final String PDF_PREFIX = "PDF_" ;
28
+
29
+ private final ReactApplicationContext mReactContext ;
20
30
21
31
public RNHTMLtoPDFModule (ReactApplicationContext reactContext ) {
22
32
super (reactContext );
@@ -32,57 +42,64 @@ public String getName() {
32
42
public void convert (final ReadableMap options , final Promise promise ) {
33
43
try {
34
44
File destinationFile ;
35
- String htmlString = options .hasKey ("html" ) ? options .getString ("html" ) : null ;
36
- if (htmlString == null ) return ;
45
+ String htmlString = options .hasKey (HTML ) ? options .getString (HTML ) : null ;
46
+ if (htmlString == null ) {
47
+ promise .reject (new Exception ("RNHTMLtoPDF error: Invalid htmlString parameter." ));
48
+ return ;
49
+ }
37
50
38
51
String fileName ;
39
- if (options .hasKey ("fileName" )) {
40
- fileName = options .getString ("fileName" );
52
+ if (options .hasKey (FILE_NAME )) {
53
+ fileName = options .getString (FILE_NAME );
54
+ if (!isFileNameValid (fileName )) {
55
+ promise .reject (new Exception ("RNHTMLtoPDF error: Invalid fileName parameter." ));
56
+ return ;
57
+ }
41
58
} else {
42
- fileName = UUID .randomUUID ().toString ();
59
+ fileName = PDF_PREFIX + UUID .randomUUID ().toString ();
43
60
}
44
61
45
- if (options .hasKey ("directory" ) && options . getString ( "directory" ). equals ( "docs" )) {
62
+ if (options .hasKey (DIRECTORY )) {
46
63
String state = Environment .getExternalStorageState ();
47
- File path = (Environment .MEDIA_MOUNTED .equals (state )) ?
48
- new File (Environment .getExternalStorageDirectory (), Environment .DIRECTORY_DOCUMENTS )
49
- : new File (mReactContext .getFilesDir (), Environment .DIRECTORY_DOCUMENTS );
50
-
51
- if (!path .exists ()) path .mkdir ();
52
- destinationFile = new File (path , fileName + ".pdf" );
64
+ File path = (Environment .MEDIA_MOUNTED .equals (state )) ?
65
+ new File (Environment .getExternalStorageDirectory (), options .getString (DIRECTORY )) :
66
+ new File (mReactContext .getFilesDir (), options .getString (DIRECTORY ));
67
+
68
+ if (!path .exists ()) {
69
+ if (!path .mkdirs ()) {
70
+ promise .reject (new Exception ("RNHTMLtoPDF error: Could not create folder structure." ));
71
+ return ;
72
+ }
73
+ }
74
+ destinationFile = new File (path , fileName + PDF_EXTENSION );
53
75
} else {
54
76
destinationFile = getTempFile (fileName );
55
77
}
56
78
57
79
convertToPDF (htmlString ,
58
80
destinationFile ,
59
- options .hasKey ("base64" ) && options .getBoolean ("base64" ) == true ,
81
+ options .hasKey (BASE_64 ) && options .getBoolean (BASE_64 ) ,
60
82
Arguments .createMap (),
61
83
promise ,
62
- options .hasKey ("baseURL" ) ? options .getString ("baseURL" ) : null );
84
+ options .hasKey (BASE_URL ) ? options .getString (BASE_URL ) : null );
63
85
} catch (Exception e ) {
64
- promise .reject (e . getMessage () );
86
+ promise .reject (e );
65
87
}
66
88
}
67
89
68
90
private void convertToPDF (String htmlString , File file , boolean shouldEncode , WritableMap resultMap , Promise promise ,
69
91
String baseURL ) throws Exception {
70
- try {
71
92
PdfConverter .getInstance ().convert (mReactContext , htmlString , file , shouldEncode , resultMap , promise , baseURL );
72
- } catch (Exception e ) {
73
- throw new Exception (e );
74
- }
75
93
}
76
94
77
- private File getTempFile (String fileName ) throws Exception {
78
- try {
95
+ private File getTempFile (String fileName ) throws IOException {
79
96
File outputDir = getReactApplicationContext ().getCacheDir ();
80
- File outputFile = File .createTempFile ("PDF_" + UUID . randomUUID (). toString (), ".pdf" , outputDir );
97
+ return File .createTempFile (fileName , PDF_EXTENSION , outputDir );
81
98
82
- return outputFile ;
99
+ }
83
100
84
- } catch (Exception e ) {
85
- throw new Exception (e );
86
- }
101
+ private boolean isFileNameValid (String fileName ) throws Exception {
102
+ return new File (fileName ).getCanonicalFile ().getName ().equals (fileName );
87
103
}
88
104
}
105
+
0 commit comments