Skip to content

Commit feae358

Browse files
committed
Allow Base64 encoding.
Throw exceptions rather than silent failures.
1 parent 313a3be commit feae358

File tree

2 files changed

+37
-12
lines changed

2 files changed

+37
-12
lines changed

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

Lines changed: 20 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -45,22 +45,23 @@ public static synchronized PdfConverter getInstance() {
4545

4646
@Override
4747
public void run() {
48-
if (mContext == null || mHtmlString == null || mPdfFile == null) return;
49-
5048
mWebView = new WebView(mContext);
5149
mWebView.setWebViewClient(new WebViewClient() {
5250
@Override
5351
public void onPageFinished(WebView view, String url) {
54-
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.KITKAT) return;
55-
56-
PrintDocumentAdapter documentAdapter = mWebView.createPrintDocumentAdapter();
57-
documentAdapter.onLayout(null, getPdfPrintAttrs(), null, new PrintDocumentAdapter.LayoutResultCallback() {}, null);
58-
documentAdapter.onWrite(new PageRange[]{PageRange.ALL_PAGES}, getOutputFileDescriptor(), null, new PrintDocumentAdapter.WriteResultCallback() {
59-
@Override
60-
public void onWriteFinished(PageRange[] pages) {
61-
destroy();
62-
}
63-
});
52+
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.KITKAT)
53+
throw new RuntimeException("call requires API level 19");
54+
else {
55+
PrintDocumentAdapter documentAdapter = mWebView.createPrintDocumentAdapter();
56+
documentAdapter.onLayout(null, getPdfPrintAttrs(), null, new PrintDocumentAdapter.LayoutResultCallback() {
57+
}, null);
58+
documentAdapter.onWrite(new PageRange[]{PageRange.ALL_PAGES}, getOutputFileDescriptor(), null, new PrintDocumentAdapter.WriteResultCallback() {
59+
@Override
60+
public void onWriteFinished(PageRange[] pages) {
61+
destroy();
62+
}
63+
});
64+
}
6465
}
6566
});
6667
mWebView.loadData(mHtmlString, "text/HTML", "UTF-8");
@@ -75,6 +76,13 @@ public void setPdfPrintAttrs(PrintAttributes printAttrs) {
7576
}
7677

7778
public void convert(Context context, String htmlString, File file) {
79+
if (context == null)
80+
throw new IllegalArgumentException("context can't be null");
81+
if (htmlString == null)
82+
throw new IllegalArgumentException("htmlString can't be null");
83+
if (file == null)
84+
throw new IllegalArgumentException("file can't be null");
85+
7886
if (mIsCurrentlyConverting)
7987
return;
8088

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

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,10 @@
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+
1418

1519
import android.os.Environment;
1620
import android.print.PdfConverter;
@@ -52,9 +56,16 @@ public void convert(final ReadableMap options, final Promise promise) {
5256
}
5357

5458
String filePath = convertToPDF(htmlString, destinationFile);
59+
String base64 = "";
60+
61+
if (options.hasKey("base64") && options.getBoolean("base64") == true) {
62+
base64 = encodeFromFile(destinationFile);
63+
}
64+
5565

5666
WritableMap resultMap = Arguments.createMap();
5767
resultMap.putString("filePath", filePath);
68+
resultMap.putString("base64", base64);
5869

5970
promise.resolve(resultMap);
6071
} catch (Exception e) {
@@ -86,4 +97,10 @@ private File getTempFile(String fileName) throws Exception {
8697
}
8798
}
8899

100+
private String encodeFromFile(File file) throws IOException{
101+
RandomAccessFile randomAccessFile = new RandomAccessFile(file, "r");
102+
byte[] fileBytes = new byte[(int)randomAccessFile.length()];
103+
randomAccessFile.readFully(fileBytes);
104+
return Base64.encodeToString(fileBytes, Base64.DEFAULT);
105+
}
89106
}

0 commit comments

Comments
 (0)