diff --git a/android/src/main/java/android/print/PdfConverter.java b/android/src/main/java/android/print/PdfConverter.java index 36f7269..44d9a2f 100644 --- a/android/src/main/java/android/print/PdfConverter.java +++ b/android/src/main/java/android/print/PdfConverter.java @@ -45,22 +45,23 @@ public static synchronized PdfConverter getInstance() { @Override public void run() { - if (mContext == null || mHtmlString == null || mPdfFile == null) return; - mWebView = new WebView(mContext); mWebView.setWebViewClient(new WebViewClient() { @Override public void onPageFinished(WebView view, String url) { - if (Build.VERSION.SDK_INT < Build.VERSION_CODES.KITKAT) return; - - PrintDocumentAdapter documentAdapter = mWebView.createPrintDocumentAdapter(); - documentAdapter.onLayout(null, getPdfPrintAttrs(), null, new PrintDocumentAdapter.LayoutResultCallback() {}, null); - documentAdapter.onWrite(new PageRange[]{PageRange.ALL_PAGES}, getOutputFileDescriptor(), null, new PrintDocumentAdapter.WriteResultCallback() { - @Override - public void onWriteFinished(PageRange[] pages) { - destroy(); - } - }); + if (Build.VERSION.SDK_INT < Build.VERSION_CODES.KITKAT) + throw new RuntimeException("call requires API level 19"); + else { + PrintDocumentAdapter documentAdapter = mWebView.createPrintDocumentAdapter(); + documentAdapter.onLayout(null, getPdfPrintAttrs(), null, new PrintDocumentAdapter.LayoutResultCallback() { + }, null); + documentAdapter.onWrite(new PageRange[]{PageRange.ALL_PAGES}, getOutputFileDescriptor(), null, new PrintDocumentAdapter.WriteResultCallback() { + @Override + public void onWriteFinished(PageRange[] pages) { + destroy(); + } + }); + } } }); mWebView.loadData(mHtmlString, "text/HTML", "UTF-8"); @@ -75,6 +76,13 @@ public void setPdfPrintAttrs(PrintAttributes printAttrs) { } public void convert(Context context, String htmlString, File file) { + if (context == null) + throw new IllegalArgumentException("context can't be null"); + if (htmlString == null) + throw new IllegalArgumentException("htmlString can't be null"); + if (file == null) + throw new IllegalArgumentException("file can't be null"); + if (mIsCurrentlyConverting) return; diff --git a/android/src/main/java/com/christopherdro/htmltopdf/RNHTMLtoPDFModule.java b/android/src/main/java/com/christopherdro/htmltopdf/RNHTMLtoPDFModule.java index cde8847..3f9d877 100644 --- a/android/src/main/java/com/christopherdro/htmltopdf/RNHTMLtoPDFModule.java +++ b/android/src/main/java/com/christopherdro/htmltopdf/RNHTMLtoPDFModule.java @@ -11,6 +11,10 @@ import java.io.File; import java.util.UUID; +import android.util.Base64; +import java.io.IOException; +import java.io.RandomAccessFile; + import android.os.Environment; import android.print.PdfConverter; @@ -52,9 +56,16 @@ public void convert(final ReadableMap options, final Promise promise) { } String filePath = convertToPDF(htmlString, destinationFile); + String base64 = ""; + + if (options.hasKey("base64") && options.getBoolean("base64") == true) { + base64 = encodeFromFile(destinationFile); + } + WritableMap resultMap = Arguments.createMap(); resultMap.putString("filePath", filePath); + resultMap.putString("base64", base64); promise.resolve(resultMap); } catch (Exception e) { @@ -86,4 +97,10 @@ private File getTempFile(String fileName) throws Exception { } } + private String encodeFromFile(File file) throws IOException{ + RandomAccessFile randomAccessFile = new RandomAccessFile(file, "r"); + byte[] fileBytes = new byte[(int)randomAccessFile.length()]; + randomAccessFile.readFully(fileBytes); + return Base64.encodeToString(fileBytes, Base64.DEFAULT); + } }