通过JavaScript inappbrowser进行Cordova/Phonegap打印 [英] Cordova/Phonegap print via JavaScript inappbrowser

查看:48
本文介绍了通过JavaScript inappbrowser进行Cordova/Phonegap打印的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从正在开发的iPad应用程序打印页面(或某些页面).应用程序启动时要做的第一件事是通过以下代码加载外部网站:

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://*****.**;

我现在想从这个外部网站上打印一些东西(在iPad上的Safari中效果很好).试试这个简单的代码

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();

但是,在Cordova/Phonegap包装的Web应用程序中,它不起作用.

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

我知道有用于此的插件>,但是那些插件要求js/html代码在本地才能工作,对吧?

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!

谢谢!

推荐答案

我知道这是一个老问题,但到目前为止尚未得到回答,我遇到了同样的问题.

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

将通过ajax访问的页面加载到本地javascript变量中,并将包含要打印的html的该本地变量发送到打印插件的打印功能.

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

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

您可以使用jQuery $ .ajax,XmlHttpRequest或任何您想要将html字符串转换为局部变量的东西.

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

示例用法(需要jQuery):

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
    );
};

这篇关于通过JavaScript inappbrowser进行Cordova/Phonegap打印的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆