cesarvr / pdf-generator

Cordova plugin to generate pdf in the client-side
MIT License
107 stars 61 forks source link

crash at android #68

Open pyummm opened 6 years ago

pyummm commented 6 years ago

Notice APP crash at plugin at android platform (galaxy tab s2 (sm-t813) 7.0 ), note s4 n915 6.0.1 pdf has multipages, images inside, html size with base64 images 2534130b

Used interface: pdf.htmlToPDF({ data: this.htmlpdf, documentSize: "A4", landscape: "portrait", type: "base64" }, (pdf:string) => {...

Right same code successfully generate pdf with much bigger size (up to 50 images and more) at ios platform (Air 2, iphone 6)

Also notice, at android pdf generates and open with type "share"

cordova-pdf-generator 1.9.3 "PDFGenerator"

cesarvr commented 6 years ago

Can you try updating to the latest version 1.9.8.

Also in this example, I test printing a huge volume of 4K images against iOS and Android (>22), maybe you can see something useful there.

If still not work just post a snippet of that is crashing, Android version, Cordova Version.

pyummm commented 6 years ago

Hello, many thanks for responce, i installed lates plugin version (cordova-pdf-generator 2.0.0 "PDFGenerator"), android-minSdkVersion = 23, cordova android 6.4.0, device has android 7.0

And notice crashes with html size over 1.5mb inclubing base64 images inside this size. here piece of code. also below links to two html sources, which i pass to generator. One generate pdf, other bigger crashes. That are same, just 2 extra photos inside.

                        let options = {
                            documentSize: 'A4',
                            type: this.pdfType,
                            fileName: 'myFile.pdf'
                        };
                        pdf.fromData(this.htmlpdf, options)
                            .then((pdf:string)=> {

Examples of html https://drive.google.com/open?id=1J5H5eA15exg_CoivNX__QOaVrpUNRl8o

https://drive.google.com/open?id=1dmf0Q3l4JtXFehB6lsq3zWGj1MWL6FrB

------------------updated I get pdf base64 string, write to file and store it to app container.

macsupport commented 6 years ago

I am getting a crash in Android as well, printing pdf.fromData using cordova-pdf-generator 2.0.0 , cordova android 6.4.0, android 7.0. No images, just text. Works fine in iOS

Here's some debug info:

I/PDFGenerator: Mounting offscreen webview W/cr_Ime: updateState: type [0->0], flags [0], show [false], W/zygote: Attempt to remove non-JNI local reference, dumping thread E/PDFPrinterWebView: creating a new WebView adapter. E/PDFPrinterWebView: creating a new print job. W/zygote: Attempt to remove non-JNI local reference, dumping thread D/SystemWebChromeClient: file:///android_asset/www/js/app4.js: Line 531 : success-> I/chromium: [INFO:CONSOLE(531)] "success->", source: file:///android_asset/www/js/app4.js (531) D/CordovaActivity: Paused the activity. W/zygote: Attempt to remove non-JNI local reference, dumping thread E/PDFPrinterWebView: creating a new WebView adapter. E/PDFPrinterWebView: creating a new print job. W/CordovaPlugin: Attempted to send a second callback for ID: PDFService368540272 Result was: "sucess" W/zygote: Attempt to remove non-JNI local reference, dumping thread W/zygote: Attempt to remove non-JNI local reference, dumping thread W/zygote: Attempt to remove non-JNI local reference, dumping thread D/AndroidRuntime: Shutting down VM

E/AndroidRuntime: FATAL EXCEPTION: main Process: com.vetcalculators.android, PID: 9894 java.lang.IllegalStateException: printing is already pending at org.chromium.android_webview.AwPdfExporter.exportToPdf(AwPdfExporter.java:59) at org.chromium.android_webview.AwPrintDocumentAdapter.onWrite(AwPrintDocumentAdapter.java:70) at com.pdf.generator.PDFPrinter.onWrite(PDFPrinter.java:62) at android.print.PrintManager$PrintDocumentAdapterDelegate$MyHandler.handleMessage(PrintManager.java:1071) at android.os.Handler.dispatchMessage(Handler.java:105) at android.os.Looper.loop(Looper.java:164) at android.app.ActivityThread.main(ActivityThread.java:6541) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767) Disconnected from the target VM, address: 'localhost:8625', transport: 'socket'

cesarvr commented 6 years ago

Hi @pyummm

I just added your file as a part of this example, this is just an app that I create to showcase multiple use case , if you look at the example "Load With Internal Asset" it will load your sample page crash.html.

Basically I just load the page you send as a string, then:

  var opts = {
    documentSize: "A4",
    landscape: "portrait",
    type: "share"
  }

  pdf
  .fromData(yourPageAsString, opts)
  .then(function(pdf) {
     progressHide() . }).catch(this.failure) 
  })

I got:

screen shot 2018-02-22 at 06 34 04 screen shot 2018-02-22 at 06 34 22

I tested this with API>=21

Cheers.

macsupport commented 6 years ago

I thought I should post my code. Works well in iOS but crashes the Android App

$(document).on("click touchstart", ".pdfme", function() {
  $(".pdfme").each(function(index) {
     $("form").each(function(index) {
  var formStorage = $(this).attr("id");
   var formData = app.form.convertToData(formStorage);
   var formData1 = JSON.stringify(formData);
     });

  var pdfLink = $(this).data("pdf");
  var pdfTitle = $(this).data("pdftitle");
    var pdfElement = $("#" + pdfLink);
    var clonePrint = $(pdfElement).clone();
    var pdfContent = "";
    var pdfContent = $(pdfElement).clone().html();

    var pdfhtmlContent =
      "<!DOCTYPE html>" +
      "<html>" +
      "<head>" +
      '<meta charset="utf-8" />' +
      '<style>@page{size:A4}.hidden-print{display:none!important}.table-row{display:flex;display:-webkit-flex;flex-direction:row;-webkit-flex-direction:row;flex-wrap:no-wrap;-webkit-flex-wrap:no-wrap;width:100%;padding-left:10px;padding-right:10px}.wrapper{display:flex;display:-webkit-flex;flex-direction:row;-webkit-flex-direction:row}.column{flex-grow:0;-webkit-flex-grow:0;flex-shrink:0;-webkit-flex-shrink:0;vertical-align:top;padding:2px}.index{width:0px}.drugName{width:260px}.range{width:260px}.index{text-align:center}.drugName{font-weight:bold;color:#337ab5}.drugName,.concentration{flex-grow:1;-webkit-flex-grow:1;overflow:hidden;white-space:nowrap;padding-right:4px}.result{width:100px;}.drugName-concentration,.drugName-concentration-condition-range,.attributes{flex-grow:1;-webkit-flex-grow:1}li.item-content.item-input{display: none;} .patient-content{display: inline;}.div.patientCol {background: #fff;text-align: center;color: #000;border: 1px solid #ddd;padding: 5px;margin-bottom: 15px;font-size: 12px;}.row {display: flex;justify-content: space-between;flex-wrap: wrap;align-items: flex-start;}.row > [class*="col-"].row > .col {box-sizing: border-box;}.row .col {width: 100%;}.output{font-weight:bold;}.column {padding:2px;}.subtitle, .subtitle, .drugName p.subtitle {font-size: 13px !important;line-height: 13px !important;font-weight: normal;font-style: italic;margin:  0.1em 0;} .dosage, .vol{height: 16px;line-height: 16px;font-weight:bold;}input.drug, input.fluid,input.shock, .item-input-wrap input{display: block;width: 80px;height: 15px;padding: 3px 6px;margin: 0;font-size: 14px;border:none;}.fa-lg{font-size: 1.33333em;line-height: .75em;vertical-align: -.0667em}div.card-content{font-size: 14px;}.fa-lg{font-size: 1.33333em;line-height: .75em;vertical-align: -.0667em}.table-row{border-bottom: 1px solid #e0e0e0;}.fa-lg{font-size: 1.33333em;line-height: .75em;vertical-align: -.0667em}</style>  ' +
      "</head>" +
      "<body>" +
      pdfContent +
      "</body>" +
      "</html>";  

var printPDF = {

// Application Constructor
   initialize: function() {
    document.addEventListener('deviceready', this.onDeviceReady.bind(this), false);
  },
  // deviceready Event Handler
  // Bind any cordova events here. Common events are:
  // 'pause', 'resume', etc.
  onDeviceReady: function() {
    this.receivedEvent('deviceready');
  },
   // Update DOM on a Received Event
      receivedEvent: function() { 

    // Our code start here
    var opts = {
                    documentSize: "A4",
                    landscape: "portrait",
                    type: "share",
        fileName: pdfTitle + '.pdf' //it will use this filename as a place-holder
    };

    pdf.fromData(pdfhtmlContent, opts)
    .then((status) => console.log('success->', status))
    .catch((error) => console.log(error));
   // It end here
    console.log('Received Event: ' + pdfhtmlContent + pdfElement);
    }
    }
   printPDF.initialize();

   });

     }); //end pdfprint`
cesarvr commented 6 years ago

@macsupport

I think the way you initialise that class is not correct, here is a guide, just follow the instruction until you get the PDF, then you can try your example there I'm sure it will work.

Cheers.

pyummm commented 6 years ago

I confirm, at android type "share" works, "base64" - works just at "small sizes" html IOS works with all types.

Try change at your demo, share to base64

macsupport commented 6 years ago

Thanks, I have read the example page a number of times but the code in the article is for pdf.fromURL and I am attempting to get a specific id within each page, not the entire page, to convert to a PDF using pdf.fromData. I did try the example initially, in multiple ways, but unfortunately, it crashed on both platforms.

cesarvr commented 6 years ago

@pyummm I have try the crash.html you send me, you can try here:

screen shot 2018-02-23 at 12 05 16 screen shot 2018-02-23 at 12 05 28

If still failing, please send me the html so I can debug the plugin against it. Cheers.

cesarvr commented 6 years ago

@macsupport can you try the version 2.0.1, I just added some validations to trigger an error in case you send a non-string, if it continue failing just post the error message you are getting if any.

Cheers.

macsupport commented 6 years ago

I installed 2.0.1. Here's the debug console output. The first part of the output is the data (html) from the specific page div id I am saving in the generated pdf with css included.

W/zygote: Attempt to remove non-JNI local reference, dumping thread I/chatty: uid=10078(com.vetcalculators.android) identical 6 lines W/zygote: Attempt to remove non-JNI local reference, dumping thread I/PDFGenerator: Mounting offscreen webview I/zygote: Do full code cache collection, code=123KB, data=108KB I/zygote: After code cache collection, code=101KB, data=71KB

D/SystemWebChromeClient: file:///android_asset/www/js/app4.js: Line 534 : Received Event: <!DOCTYPE html> undefined[object Object] I/chromium: [INFO:CONSOLE(534)] "Received Event: <!DOCTYPE html> undefined[object Object]", source: file:///android_asset/www/js/app4.js (534)

/////////////////////////////////////////////////////////////////

W/zygote: Attempt to remove non-JNI local reference, dumping thread I/chatty: uid=10078(com.vetcalculators.android) identical 1 line W/zygote: Attempt to remove non-JNI local reference, dumping thread E/PDFPrinterWebView: creating a new WebView adapter. E/PDFPrinterWebView: creating a new print job. E/PDFPrinterWebView: creating a new WebView adapter. E/PDFPrinterWebView: creating a new print job. W/CordovaPlugin: Attempted to send a second callback for ID: PDFService795131683 Result was: "sucess" D/SystemWebChromeClient: file:///android_asset/www/js/app4.js: Line 531 : success-> I/chromium: [INFO:CONSOLE(531)] "success->", source: file:///android_asset/www/js/app4.js (531) D/CordovaActivity: Paused the activity. W/zygote: Attempt to remove non-JNI local reference, dumping thread D/AndroidRuntime: Shutting down VM E/AndroidRuntime: FATAL EXCEPTION: main Process: com.vetcalculators.android, PID: 6836 java.lang.IllegalStateException: printing is already pending at org.chromium.android_webview.AwPdfExporter.exportToPdf(AwPdfExporter.java:8) at org.chromium.android_webview.AwPrintDocumentAdapter.onWrite(AwPrintDocumentAdapter.java:29) at com.pdf.generator.PDFPrinter.onWrite(PDFPrinter.java:62) at android.print.PrintManager$PrintDocumentAdapterDelegate$MyHandler.handleMessage(PrintManager.java:1075) at android.os.Handler.dispatchMessage(Handler.java:106) at android.os.Looper.loop(Looper.java:164) at android.app.ActivityThread.main(ActivityThread.java:6494) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807) Disconnected from the target VM, address: 'localhost:8628', transport: 'socket'

pyummm commented 6 years ago

I installed sample, and confirm, it crashes at option crash.html from assets with XHR/URL, both, and "Get BASE64".

Also i notify, at IOS, Ipad air 2 and lots other ios devices, plugin works with huge sizes of html, generate documents with about 100 pages, where probably each page include image. Report same as this one crash example, but much huge.

pyummm commented 6 years ago

Around java sources. I not programming java, but found problems around method: getAsBase64() here twolines where runtime error with reason="memory violation" sig=6 appear.

encodedBase64 = Base64.encodeToString( bytes, Base64.DEFAULT ); cordovaCallback.success(encodedBase64);

I play around and not found solution to return long string with callback, probably android/java will have problem with it.

Probably better to store base64 to file right at plugin.

cesarvr commented 6 years ago

can you please post the API level you are using, Also Cordova version? , also the crash is only happening when you try to get the PDF in base64?

pyummm commented 6 years ago

Yes, just base64 and at java big strings operations. cordova (Cordova CLI) : 8.0.0 Cordova Platforms : android 6.4.0

cesarvr commented 6 years ago

Nice, just need now the Android SDK API Level , you can see that in Android Studio , In the emulator section.

pyummm commented 6 years ago

installed SDK API 22->27, compiles from 23 and up, each from 23 and higher generate same bug, emulators none installed

macsupport commented 6 years ago

Some additional info inn the debugger I am seeing when trying to print data (html) on Android.

W/PluginManager: THREAD WARNING: exec() call to PDFService.htmlToPDF blocked the main thread for 63ms. Plugin should use CordovaInterface.getThreadPool().

W/zygote: Attempt to remove non-JNI local reference, dumping thread I/PDFGenerator: Mounting offscreen webview W/zygote: Attempt to remove non-JNI local reference, dumping thread I/PDFGenerator: Mounting offscreen webview W/cr_Ime: updateState: type [0->0], flags [0], show [false], W/zygote: Attempt to remove non-JNI local reference, dumping thread E/PDFPrinterWebView: creating a new WebView adapter. E/PDFPrinterWebView: creating a new print job. I/chromium: [INFO:CONSOLE(0)] "Upcoming versions will block content-initiated top frame navigations to data: URLs. For more information, see https://goo.gl/BaZAea.", source: about:blank (0) W/zygote: Attempt to remove non-JNI local reference, dumping thread E/PDFPrinterWebView: creating a new WebView adapter. E/PDFPrinterWebView: creating a new print job.

W/CordovaPlugin: Attempted to send a second callback for ID: PDFService988530754 Result was: "sucess"

D/CordovaActivity: Paused the activity. W/zygote: Attempt to remove non-JNI local reference, dumping thread D/AndroidRuntime: Shutting down VM

_E/AndroidRuntime: FATAL EXCEPTION: main Process: com.vetcalculators.android, PID: 10395 java.lang.IllegalStateException: printing is already pending at org.chromium.android_webview.AwPdfExporter.exportToPdf(AwPdfExporter.java:59) at org.chromium.androidwebview.AwPrintDocumentAdapter.onWrite(AwPrintDocumentAdapter.java:70) at com.pdf.generator.PDFPrinter.onWrite(PDFPrinter.java:62) at android.print.PrintManager$PrintDocumentAdapterDelegate$MyHandler.handleMessage(PrintManager.java:1071) at android.os.Handler.dispatchMessage(Handler.java:105) at android.os.Looper.loop(Looper.java:164) at android.app.ActivityThread.main(ActivityThread.java:6541) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767)

macsupport commented 6 years ago

Any update on this issue?

f18nfz commented 1 year ago

I've got the same issue when trying to use pdf.fromURL with the same error messages, did you find a resolution?

  java.lang.IllegalStateException: printing is already pending
        at ww.onWrite(chromium-Monochrome.aab-stable-524911823:68)
        at com.pdf.generator.PDFPrinter.onWrite(PDFPrinter.java:62)
        at android.print.PrintManager$PrintDocumentAdapterDelegate$MyHandler.handleMessage(PrintManager.java:1078)
        at android.os.Handler.dispatchMessage(Handler.java:106)
        at android.os.Looper.loop(Looper.java:214)
        at android.app.ActivityThread.main(ActivityThread.java:7050)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:494)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:965)