Cordova/Phonegap print via JavaScript inappbrowser

可紊 提交于 2021-02-19 06:25:23

问题


I want to print a page (or some pages) from an iPad app I'm developing. The first thing the app does when launching is to load an external web site by this code:

window.location = https://*****.**;

It is from this external web site I now want to print some stuff (works great in Safari on iPad). Trying this simple code

window.print();

But it doesn't work i the wrapped web app by Cordova/Phonegap.

I know there's plugins for this > but those require the js/html code to be local to work, right?

Any suggestions how I can make printings available in my app? Any suggestion is very welcome, hopefully there's an easy way!

Thanks!


回答1:


I know this is an old question, but it was not answered so far and I came accross the same problem.

Load the page you are visiting via ajax into a local javascript variable and send this local variable containing the html you want to get printed to the print function of your print plugin.

OR

If you want to print the current page, just get the contents into a local variable and send it your print plugin.

https://github.com/hazemhagrass/phonegap-print

You can use jQuery $.ajax, XmlHttpRequest or anything you want to get the html string into a local variable.

Example Usage (jQuery required):

$.get('https://*****.**',function(html){
    window.app.print(data,
        function(){
            console.log('success');
        },
        function(){
            console.log('error');
        }
    );
});

***OR***

var html = $('body').html();
window.app.print(html,
    function(){
         console.log('success');
    },
    function(){
         console.log('error');
    }
);

window.app = window.app || {};
window.app.print = function(html,successCallback,errorCallback) {
    successCallback = successCallback|| function(){};
    errorCallback = errorCallback|| function(){};
    var type = "text/html";
    var title = "test.html";
    var fileContent = html;
    window.plugins.PrintPlugin.print(
        fileContent,
        successCallback,
        errorCallback,
        "",
        type,
        title
    );
};


来源:https://stackoverflow.com/questions/25219472/cordova-phonegap-print-via-javascript-inappbrowser

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!