Skip to content

Commit fba5079

Browse files
MatthieuLemoinechristopherdro
authored andcommitted
Fix Android async issue (#64)
1 parent 5d4092b commit fba5079

File tree

2 files changed

+52
-32
lines changed

2 files changed

+52
-32
lines changed

android/src/main/java/android/print/PdfConverter.java

Lines changed: 36 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,12 @@
1414
import android.webkit.WebViewClient;
1515

1616
import java.io.File;
17+
import android.util.Base64;
18+
import java.io.IOException;
19+
import java.io.RandomAccessFile;
20+
21+
import com.facebook.react.bridge.Promise;
22+
import com.facebook.react.bridge.WritableMap;
1723

1824
/**
1925
* Converts HTML to PDF.
@@ -32,6 +38,9 @@ public class PdfConverter implements Runnable {
3238
private PrintAttributes mPdfPrintAttrs;
3339
private boolean mIsCurrentlyConverting;
3440
private WebView mWebView;
41+
private boolean mShouldEncode;
42+
private WritableMap mResultMap;
43+
private Promise mPromise;
3544

3645
private PdfConverter() {
3746
}
@@ -58,7 +67,19 @@ public void onPageFinished(WebView view, String url) {
5867
documentAdapter.onWrite(new PageRange[]{PageRange.ALL_PAGES}, getOutputFileDescriptor(), null, new PrintDocumentAdapter.WriteResultCallback() {
5968
@Override
6069
public void onWriteFinished(PageRange[] pages) {
61-
destroy();
70+
try {
71+
String base64 = "";
72+
if (mShouldEncode) {
73+
base64 = encodeFromFile(mPdfFile);
74+
}
75+
mResultMap.putString("filePath", mPdfFile.getAbsolutePath());
76+
mResultMap.putString("base64", base64);
77+
mPromise.resolve(mResultMap);
78+
} catch (IOException e) {
79+
mPromise.reject(e.getMessage());
80+
} finally {
81+
destroy();
82+
}
6283
}
6384
});
6485
}
@@ -75,7 +96,7 @@ public void setPdfPrintAttrs(PrintAttributes printAttrs) {
7596
this.mPdfPrintAttrs = printAttrs;
7697
}
7798

78-
public void convert(Context context, String htmlString, File file) {
99+
public void convert(Context context, String htmlString, File file, boolean shouldEncode, WritableMap resultMap, Promise promise) {
79100
if (context == null)
80101
throw new IllegalArgumentException("context can't be null");
81102
if (htmlString == null)
@@ -90,6 +111,9 @@ public void convert(Context context, String htmlString, File file) {
90111
mHtmlString = htmlString;
91112
mPdfFile = file;
92113
mIsCurrentlyConverting = true;
114+
mShouldEncode = shouldEncode;
115+
mResultMap = resultMap;
116+
mPromise = promise;
93117
runOnUiThread(this);
94118
}
95119

@@ -126,5 +150,15 @@ private void destroy() {
126150
mPdfPrintAttrs = null;
127151
mIsCurrentlyConverting = false;
128152
mWebView = null;
153+
mShouldEncode = false;
154+
mResultMap = null;
155+
mPromise = null;
156+
}
157+
158+
private String encodeFromFile(File file) throws IOException{
159+
RandomAccessFile randomAccessFile = new RandomAccessFile(file, "r");
160+
byte[] fileBytes = new byte[(int)randomAccessFile.length()];
161+
randomAccessFile.readFully(fileBytes);
162+
return Base64.encodeToString(fileBytes, Base64.DEFAULT);
129163
}
130164
}

android/src/main/java/com/christopherdro/htmltopdf/RNHTMLtoPDFModule.java

Lines changed: 16 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,6 @@
1111
import java.io.File;
1212
import java.util.UUID;
1313

14-
import android.util.Base64;
15-
import java.io.IOException;
16-
import java.io.RandomAccessFile;
17-
18-
1914
import android.os.Environment;
2015
import android.print.PdfConverter;
2116

@@ -59,31 +54,29 @@ public void convert(final ReadableMap options, final Promise promise) {
5954
destinationFile = getTempFile(fileName);
6055
}
6156

62-
String filePath = convertToPDF(htmlString, destinationFile);
63-
String base64 = "";
64-
65-
if (options.hasKey("base64") && options.getBoolean("base64") == true) {
66-
base64 = encodeFromFile(destinationFile);
67-
}
68-
69-
70-
WritableMap resultMap = Arguments.createMap();
71-
resultMap.putString("filePath", filePath);
72-
resultMap.putString("base64", base64);
73-
74-
promise.resolve(resultMap);
57+
convertToPDF(
58+
htmlString,
59+
destinationFile,
60+
options.hasKey("base64") && options.getBoolean("base64") == true,
61+
Arguments.createMap(),
62+
promise
63+
);
7564
} catch (Exception e) {
7665
promise.reject(e.getMessage());
7766
}
7867
}
7968

80-
private String convertToPDF(String htmlString, File file) throws Exception {
69+
private void convertToPDF(String htmlString, File file, boolean shouldEncode, WritableMap resultMap, Promise promise) throws Exception {
8170
try {
8271
PdfConverter.getInstance()
83-
.convert(mReactContext, htmlString, file);
84-
String absolutePath = file.getAbsolutePath();
85-
86-
return absolutePath;
72+
.convert(
73+
mReactContext,
74+
htmlString,
75+
file,
76+
shouldEncode,
77+
resultMap,
78+
promise
79+
);
8780
} catch (Exception e) {
8881
throw new Exception(e);
8982
}
@@ -100,11 +93,4 @@ private File getTempFile(String fileName) throws Exception {
10093
throw new Exception(e);
10194
}
10295
}
103-
104-
private String encodeFromFile(File file) throws IOException{
105-
RandomAccessFile randomAccessFile = new RandomAccessFile(file, "r");
106-
byte[] fileBytes = new byte[(int)randomAccessFile.length()];
107-
randomAccessFile.readFully(fileBytes);
108-
return Base64.encodeToString(fileBytes, Base64.DEFAULT);
109-
}
11096
}

0 commit comments

Comments
 (0)