Skip to content

Commit

Permalink
Allow Base64 encoding.
Browse files Browse the repository at this point in the history
Throw exceptions rather than silent failures.
  • Loading branch information
IslamSalah committed Nov 20, 2017
1 parent 313a3be commit feae358
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 12 deletions.
32 changes: 20 additions & 12 deletions android/src/main/java/android/print/PdfConverter.java
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand All @@ -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;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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);
}
}

0 comments on commit feae358

Please sign in to comment.