Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ pdf.fromData( '<html><h1>Hello World</h1></html>', options)



##### filename
##### fileName

- You can specify the name of the PDF file.

Expand Down
1 change: 0 additions & 1 deletion plugin.xml
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@
<source-file src="src/android/PDFPrinter.java" target-dir="src/com/pdf/generator/"/>
<source-file src="src/android/PDFPrinterWebView.java" target-dir="src/com/pdf/generator/"/>
<source-file src="src/android/PDFtoBase64.java" target-dir="src/com/pdf/generator/"/>
<source-file src="src/android/PDFConfig.java" target-dir="src/com/pdf/generator/"/>

</platform>

Expand Down
29 changes: 0 additions & 29 deletions src/android/PDFConfig.java

This file was deleted.

108 changes: 38 additions & 70 deletions src/android/PDFGenerator.java
Original file line number Diff line number Diff line change
@@ -1,59 +1,34 @@
package com.pdf.generator;

import android.content.ActivityNotFoundException;
import android.content.Context;
import android.content.Intent;
import android.graphics.Canvas;
import android.graphics.Rect;
import android.graphics.pdf.PdfDocument;
import android.net.Uri;
import android.os.ParcelFileDescriptor;
import android.print.PrintManager;
import android.util.Log;
import android.view.View;
import android.webkit.WebView;

import org.apache.cordova.CordovaInterface;
import org.apache.cordova.CordovaPlugin;
import org.apache.cordova.CallbackContext;

import org.apache.cordova.CordovaPlugin;
import org.apache.cordova.LOG;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;

/**
* This class echoes a string called from JavaScript.
*/
* This plugin creates a PDF from a given HTML website or string
*/
public class PDFGenerator extends CordovaPlugin {

private final static String APPNAME = "PDFGenerator";
private WebView offscreenWebview = null;
private final static String LOG_TAG = "PDFGenerator";

public WebView getOffscreenWebkitInstance(Context ctx) {
LOG.i(APPNAME, "Mounting offscreen webview");
if (this.offscreenWebview == null){
WebView view = new WebView(ctx);
view.getSettings().setDatabaseEnabled(true);
view.getSettings().setJavaScriptEnabled(true);

return this.offscreenWebview = view;
}else{
return this.offscreenWebview;
}
LOG.d(LOG_TAG, "Mounting offscreen WebView");
WebView view = new WebView(ctx);
view.getSettings().setDatabaseEnabled(true);
view.getSettings().setJavaScriptEnabled(true);
return view;
}

@Override
public boolean execute(String action, JSONArray args, CallbackContext callbackContext) throws JSONException {
if (action.equals("htmlToPDF")) {
public boolean execute(String action, JSONArray args, CallbackContext callbackContext) {
if ("htmlToPDF".equals(action)) {
this.pdfPrinter(args, callbackContext);

return true;
}
return false;
Expand All @@ -64,52 +39,45 @@ public void onResume(boolean multitasking) {
super.onResume(multitasking);
}

private void pdfPrinter(final JSONArray args, final CallbackContext callbackContext) throws JSONException {
private void pdfPrinter(final JSONArray args, final CallbackContext callbackContext) {
final Context ctx = this.cordova.getActivity().getApplicationContext();
final CordovaInterface _cordova = this.cordova;
final CallbackContext cordovaCallback = callbackContext;

_cordova.getActivity().runOnUiThread(new Runnable() {
@Override
public void run() {
try {
WebView webview = getOffscreenWebkitInstance(ctx);
cordova.getActivity().runOnUiThread(() -> {
try {
WebView webview = getOffscreenWebkitInstance(ctx);

PrintManager printManager = (PrintManager) cordova.getActivity()
.getSystemService(Context.PRINT_SERVICE);
PrintManager printManager = (PrintManager) cordova.getActivity()
.getSystemService(Context.PRINT_SERVICE);

boolean outputBase64 = args.getString(4) != null && args.getString(4).equals("base64");
PDFPrinterWebView printerWebView = new PDFPrinterWebView(printManager, ctx, outputBase64);
boolean outputBase64 = args.getString(4) != null && args.getString(4).equals("base64");
PDFPrinterWebView printerWebView = new PDFPrinterWebView(printManager, ctx, outputBase64);

String fileNameArg = args.getString(5);
if (fileNameArg != null) {
printerWebView.setFileName(fileNameArg);
}
String fileNameArg = args.getString(5);
if (fileNameArg != null) {
printerWebView.setFileName(fileNameArg);
}

String pageType = args.getString(2);
printerWebView.setPageType(pageType);
String pageType = args.getString(2);
printerWebView.setPageType(pageType);

String orientation = args.getString(3);
if (orientation != null) {
printerWebView.setOrientation(orientation);
}
String orientation = args.getString(3);
if (orientation != null) {
printerWebView.setOrientation(orientation);
}

printerWebView.setCordovaCallback(cordovaCallback);
webview.setWebViewClient(printerWebView);
printerWebView.setCordovaCallback(callbackContext);
webview.setWebViewClient(printerWebView);

if (args.getString(0) != null && !args.getString(0).equals("null"))
webview.loadUrl(args.getString(0));
if (args.getString(0) != null && !args.getString(0).equals("null"))
webview.loadUrl(args.getString(0));

if (args.getString(1) != null && !args.getString(1).equals("null"))
webview.loadDataWithBaseURL(null,args.getString(1), "text/HTML","UTF-8", null);
if (args.getString(1) != null && !args.getString(1).equals("null"))
webview.loadDataWithBaseURL(null, args.getString(1), "text/HTML", "UTF-8", null);

} catch (JSONException e) {
e.printStackTrace();
Log.e(APPNAME, e.getMessage());
cordovaCallback.error("Native pasing arguments: " + e.getMessage());
}
} catch (JSONException e) {
Log.e(LOG_TAG, "Unable to parse JSON", e);
callbackContext.error("Native parsing arguments: " + e.getMessage());
}
});
}

}
36 changes: 14 additions & 22 deletions src/android/PDFPrinter.java
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
package com.pdf.generator;

import android.print.PDFConfig;
import android.os.Build;
import android.os.Bundle;
import android.os.CancellationSignal;
import android.os.ParcelFileDescriptor;
import android.print.PageRange;
import android.print.PrintAttributes;
import android.print.PrintDocumentAdapter;
import android.print.PrintDocumentInfo;
import android.webkit.WebView;

import org.apache.cordova.LOG;
Expand All @@ -18,53 +16,47 @@
*/

public class PDFPrinter extends PrintDocumentAdapter {

static final String APPNAME = "PDFPrinter";

private PrintDocumentAdapter mWrappedInstance = null;
private WebView webView = null;
private PrintAttributes attributes = null;
private PDFConfig config = null;
private static final String LOG_TAG = "PDFPrinter";
private final WebView webView;
private PrintDocumentAdapter mWrappedInstance;

public PDFPrinter(WebView webView, String fileName) {
if (Build.VERSION.SDK_INT >= 21) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
this.mWrappedInstance = webView.createPrintDocumentAdapter(fileName);
} else {
this.mWrappedInstance = webView.createPrintDocumentAdapter();
}
config = new PDFConfig();
this.webView = webView;
}



@Override
public void onStart() {
mWrappedInstance.onStart();
}

@Override
public void onLayout(PrintAttributes oldAttributes, PrintAttributes newAttributes,
CancellationSignal cancellationSignal, LayoutResultCallback callback, Bundle extras) {
CancellationSignal cancellationSignal, LayoutResultCallback callback, Bundle extras) {

mWrappedInstance.onLayout(
oldAttributes,
newAttributes,
cancellationSignal,
callback,
extras
oldAttributes,
newAttributes,
cancellationSignal,
callback,
extras
);
}

@Override
public void onWrite(PageRange[] pages, ParcelFileDescriptor destination, CancellationSignal cancellationSignal,
WriteResultCallback callback) {
WriteResultCallback callback) {
mWrappedInstance.onWrite(pages, destination, cancellationSignal, callback);
}

@Override
public void onFinish() {
LOG.i(APPNAME, "Cleaning pdfwriter & webView objects.");
LOG.d(LOG_TAG, "Cleaning pdfwriter & webView objects.");
mWrappedInstance.onFinish();
webView.destroy();
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This ensures to destroy the webView instance after the user either printed or cancelled the share PDF intent.

}
}
Loading