katzer / cordova-plugin-printer

Print HTML documents
Apache License 2.0
312 stars 292 forks source link

HTML regoularly printed in Android fails in iOs #241

Closed lbrutti closed 4 years ago

lbrutti commented 4 years ago

Hello, I need to print a webview both in iOs and Android. In Android everything works fine while in iOs, print dialog is opern but no content is displayed.

Debugging plugin native code, I found multiple calls to these lines in APPPrinter.m

dispatch_sync(dispatch_get_main_queue(), ^{
            item = [[UIMarkupTextPrintFormatter alloc]
                    initWithMarkupText:content];
        });

While the first time, content contains webview html, the latter is set to nil, is it normal? Here's the snippet used to invoke plugin.

this.http.get('../../assets/css/print.css', { responseType: 'text' }).subscribe(
      cssFile => {

        let PDF_HTML = `<style>${cssFile}</style> ${content}`;
        let html = '<html>';
        html += '<head>';
        html += '<style>';
        html += cssFile;
        html += '</style>';
        html += '</head>';
        html += '<body>';
        html += content;
        html += '</body>';
        html += '</html>';
        this.printer.print(html, options)
          .then(res => {
            console.log(res);
          },
            err => {
              console.error(err);
            });
      },
      err => {
        console.error(err);
      });

here's my ionic info output:

cli packages: (C:\Users\xxxxxxxxx\AppData\Roaming\nvm\v10.16.0\node_modules)

    @ionic/cli-utils  : 1.19.2
    ionic (Ionic CLI) : 3.20.0

global packages:

    cordova (Cordova CLI) : not installed
    Gulp CLI              : [10:40:17] CLI version 3.9.1 [10:40:17] Local version 3.9.1

local packages:

    @ionic/app-scripts : 3.2.4
    Cordova Platforms  : android 7.1.4
    Ionic Framework    : ionic-angular 3.9.8

System:

    Android SDK Tools : 26.1.1
    Node              : v10.16.0
    npm               : 6.9.0 
    OS                : Windows 10

Environment Variables:

    ANDROID_HOME : C:\Users\xxxxxxxxx\AppData\Local\Android\Sdk   

Misc:

    backend : pro

Package json :

"@ionic-native/printer": "4.20.0"
"cordova-plugin-printer": "0.8.0",

Thanks for your support!

lbrutti commented 4 years ago

For those who may encounter an analog issue: it seems like the problem lied in css rules in the form of: page-break-<xxx>

Replacing page-break with break-<xxx> solved the issue.

As in MDN WebDocs is now deprecated. Weirdly enough, break-<xxx> syntax is not recognized by Android, but that's outside the issue's scope.