更新记录

1.0.2(2023-04-21)

  1. ios增加html打印

1.0.1(2023-03-09)

  1. 增加打印远程网络图片

1.0.0(2022-12-28)

  1. AirPrint隔空打印机
  2. 打印图片
  3. 打印网址
  4. 打印文件pdf、word、excel、ppt等
查看更多

平台兼容性

Android Android CPU类型 iOS
适用版本区间:4.4 - 12.0 armeabi-v7a:未测试,arm64-v8a:未测试,x86:未测试 适用版本区间:11 - 16

原生插件通用使用流程:

  1. 购买插件,选择该插件绑定的项目。
  2. 在HBuilderX里找到项目,在manifest的app原生插件配置中勾选模块,如需要填写参数则参考插件作者的文档添加。
  3. 根据插件作者的提供的文档开发代码,在代码中引用插件,调用插件功能。
  4. 打包自定义基座,选择插件,得到自定义基座,然后运行时选择自定义基座,进行log输出测试。
  5. 开发完毕后正式云打包

付费原生插件目前不支持离线打包。
Android 离线打包原生插件另见文档 https://nativesupport.dcloud.net.cn/NativePlugin/offline_package/android
iOS 离线打包原生插件另见文档 https://nativesupport.dcloud.net.cn/NativePlugin/offline_package/ios

注意事项:使用HBuilderX2.7.14以下版本,如果同一插件且同一appid下购买并绑定了多个包名,提交云打包界面提示包名绑定不一致时,需要在HBuilderX项目中manifest.json->“App原生插件配置”->”云端插件“列表中删除该插件重新选择


功能

  1. AirPrint隔空打印机
  2. 打印图片
  3. 打印网址
  4. 打印文件pdf、word、excel、ppt等

插件变量:


var print = uni.requireNativePlugin("wrs-print");
  • 打印图片

                var absPath = plus.io.convertLocalFileSystemURL('_www');
                // Android获取的absPath以/结尾,iOS获取的absPath不是/结尾
                if (absPath.endWith('/')) {
                    absPath = absPath.substring(0, absPath.length - 1);
                }
                var url = absPath + "/static/test.png"
                // url支持本地绝对路径和远程网络地址
                switch (uni.getSystemInfoSync().platform) {
                    case 'android': {
                        print.printImage({
                            url: url,
                            scaleMode: 1, // 1: SCALE_MODE_FIT 2: SCALE_MODE_FILL
                            jobName: "打印图片"
                        }, (resp) => {
                            this.showMsg(JSON.stringify(resp));
                        });
                    }
                    break;
                case "ios": {
                    var params = {};
                    params.url = url;
                    params.printInfo = {
                        // printerID:"xxxx", 打印机ID
                        jobName: "打印图片", // 默认应用程序名称
                        outputType: 0, // 0:UIPrintInfoOutputGeneral(B&W or color, normal quality output for mixed text, graphics, and images)  1:UIPrintInfoOutputPhoto 2:UIPrintInfoOutputGrayscale 3:UIPrintInfoOutputPhotoGrayscale
                        orientation: 0, // 0:UIPrintInfoOrientationPortrait  1:UIPrintInfoOrientationLandscape
                        duplex: 0 // 0: UIPrintInfoDuplexNone  1:UIPrintInfoDuplexLongEdge  2:UIPrintInfoDuplexShortEdge
                    };
                    print.printImage(params, (resp) => {
                        this.showMsg(JSON.stringify(resp));
                    });
                }
                break;
                default:
                    break;
                }
  • 打印网址

var url = "https://www.baidu.com";
                switch (uni.getSystemInfoSync().platform) {
                    case 'android': {
                        print.printUrl({
                            url: url,
                            jobName: "打印url"
                        });
                    }
                    break;
                case "ios": {

                    var params = {};
                    params.url = url;
                    params.printInfo = {
                        // printerID:"xxxx", 打印机ID
                        jobName: "打印URL", // 默认应用程序名称
                        outputType: 0, // 0:UIPrintInfoOutputGeneral(B&W or color, normal quality output for mixed text, graphics, and images)
                        orientation: 0, // 0:UIPrintInfoOrientationPortrait  1:UIPrintInfoOrientationLandscape
                        duplex: 0 // 0: UIPrintInfoDuplexNone  1:UIPrintInfoDuplexLongEdge  2:UIPrintInfoDuplexShortEdge
                    };
                    print.printUrl(params, (resp) => {
                        this.showMsg(JSON.stringify(resp));
                    });
                }
                break;
                default:
                    break;
                }
  • 打印文件pdf、word、excel、ppt等

原理:

  1. ios支持打印url、图片、文件
  2. Android支持打印url、图片,不支持打印本地文件
  3. Android如果需要打印pdf、word等文件,需要把这些文件放到服务器上,通过office提供等接口打开浏览,再通过打印此url来达到打印文件等目的,本质上还是通过url来打印,如:

// 服务器的文件地址
var fileUrl = "http://www.xxxx/test.pdf";
// office提供的在线浏览文件接口
var printUrl = "https://view.officeapps.live.com/op/embed.aspx?src=" + fileUrl;

switch (uni.getSystemInfoSync().platform) {
                    case 'android': {
                    var fileUrl = "http://www.xxxx/test.pdf";
                    var printUrl = "https://view.officeapps.live.com/op/embed.aspx?src=" + fileUrl;
                        print.printFile({
                            url: printUrl,
                            jobName: "打印文件"
                        });
                    }
                    break;
                case "ios": {
                    var absPath = plus.io.convertLocalFileSystemURL('_www');
                    // Android获取的absPath以/结尾,iOS获取的absPath不是/结尾
                    if (absPath.endWith('/')) {
                        absPath = absPath.substring(0, absPath.length - 1);
                    }
                    var url = absPath + "/static/test.pdf";
                    var params = {};
                    params.url = url;
                    params.printInfo = {
                        // printerID:"xxxx", 打印机ID
                        jobName: "打印文件", // 默认应用程序名称
                        outputType: 0, // 0:UIPrintInfoOutputGeneral(B&W or color, normal quality output for mixed text, graphics, and images)
                        orientation: 0, // 0:UIPrintInfoOrientationPortrait  1:UIPrintInfoOrientationLandscape
                        duplex: 0 // 0: UIPrintInfoDuplexNone  1:UIPrintInfoDuplexLongEdge  2:UIPrintInfoDuplexShortEdge
                    };
                    print.printFile(params, (resp) => {
                        this.showMsg(JSON.stringify(resp));
                    });
                }
                break;
                default:
                    break;
                }
  • 打印HTML

                var html = "<html><body>

Test Content

Testing, testing, testing...

</body></html>"; var params = {}; params.html = html switch (uni.getSystemInfoSync().platform) { case 'android': { params.jobName = "打印HTML"; print.printHtml(params); } break; case 'ios':{ // params.contentInsets = { // top: 0, // bottom: 0, // left: 0, // right: 0 // }; // params.headerHeight = 100; // params.footerHeight = 100; // params.printInfo = { // // printerID:"xxxx", 打印机ID // jobName: "打印图片", // 默认应用程序名称 // outputType: 0, // 0:UIPrintInfoOutputGeneral(B&W or color, normal quality output for mixed text, graphics, and images) 1:UIPrintInfoOutputPhoto 2:UIPrintInfoOutputGrayscale 3:UIPrintInfoOutputPhotoGrayscale // orientation: 0, // 0:UIPrintInfoOrientationPortrait 1:UIPrintInfoOrientationLandscape // duplex: 0 // 0: UIPrintInfoDuplexNone 1:UIPrintInfoDuplexLongEdge 2:UIPrintInfoDuplexShortEdge // }; print.printHtml(params, (resp)=>{ console.log(JSON.stringify(resp)); }); }, break; default: break; }

支持定制,联系方式 QQ:252797991

如果觉得可以就点个👍吧,欢迎粉丝收藏,土豪打赏,您的关注就是我们创作的动力!

隐私、权限声明

1. 本插件需要申请的系统权限列表:

android: <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" /> ios: 无

2. 本插件采集的数据、发送的服务器地址、以及数据用途说明:

插件不采集任何数据

3. 本插件是否包含广告,如包含需详细说明广告表达方式、展示频率:

使用中有什么不明白的地方,就向插件作者提问吧~ 我要提问