Home > Software design >  cordova-plugin-printer printing raw html instead of rendered html
cordova-plugin-printer printing raw html instead of rendered html

Time:08-31

I am using cordova-plugin-printer for one of my Cordova app. I have installed the plugin, and also able to access android native printing service. The plugin's print function can be called in app using it's function cordova.plugins.printer.print(); as described in the plugin documents here and on it's git repository.

I actually need to print a selected <div> from the HTML, using JavaScript/jQuery.

But the problem I'm facing is that when I am passing html as a var this plugin is printing raw HTML, and not the rendered HTML as desired. Where as, if I directly pass HTML code, as a parameter, it gives the desired result.

My Example Code:

When I call

cordova.plugins.printer.print('<h1>How r u</h1>');//print as desired.

But when I write code like this:

<div id="printable">
<h1>How are you</h1>
</div>

var printable = $("#printable").html();
cordova.plugins.printer.print(printable);//prints raw html

Can anyone help? or correct me what am I doing wrong? seems to be something silly, which I am probably unable to figure out.

CodePudding user response:

You need to remove line breaks from your html. Try

 var printable = $("#printable").html();
 printable = printable.replace(/(\r\n|\n|\r)/gm, '');
 cordova.plugins.printer.print(printable);
  • Related