Cordova Printer插件无法打印求助:调用print方法无响应
解决Cordova/Ionic中printer插件调用无响应的问题
我来帮你一步步排查这个打印没反应的问题,先从几个最常见的原因入手:
1. 确保插件正确初始化且在设备就绪后调用
Cordova插件必须在设备就绪之后才能正常工作,而且要确保模块配置正确:
- 先检查
app.module.ts里是否把Printer添加到了providers数组中:import { Printer } from '@ionic-native/printer/ngx'; @NgModule({ declarations: [AppComponent], imports: [...], providers: [ ... Printer, // 必须添加这里 { provide: RouteReuseStrategy, useClass: IonicRouteStrategy } ], bootstrap: [AppComponent] }) export class AppModule { } - 修改你的
Print方法,用Platform.ready()包裹打印逻辑,同时添加Promise的then/catch捕获错误:import { Platform } from '@ionic/angular'; // 构造函数注入Platform constructor(private printer: Printer, private platform: Platform) {} Print(){ console.log("Print Pressed"); this.platform.ready().then(() => { // 先检查打印功能是否可用 this.printer.isAvailable().then(available => { if (!available) { console.log("当前设备不支持打印功能"); return; } var options = { font: { size: 22, italic: true, align: 'center' }, header: { height: '6cm', label: { text: "\n\nDie Freuden", font: { bold: true, size: 37, align: 'center' } } }, footer: { height: '4cm', label: { text: 'Johann Wolfgang von Goethe, 1749-1832, deutscher Dichter, Naturforscher', font: { align: 'center' } } } }; this.printer.print("hello", options) .then(() => console.log("打印请求已发送")) .catch(err => console.error("打印出错:", err)); }).catch(err => console.error("检查打印可用性失败:", err)); console.log("Print Pressed>>>"); }); }
2. 简化打印选项,排查格式问题
有些版本的cordova-plugin-printer对选项格式要求比较严格,比如单位、属性名称可能有差异。你可以先简化选项,测试基础打印是否正常:
// 先用最简单的选项测试 const simpleOptions = { font: { size: 22, align: 'center' } }; this.printer.print("测试打印内容", simpleOptions) .then(...) .catch(...);
如果简化后能正常打印,再逐步添加header、footer等选项,排查哪个配置出了问题。
3. 确认测试环境和设备支持
- 不要在模拟器上测试打印功能,模拟器通常没有打印模块,一定要用真机测试
- 确保测试设备已经连接了可用的打印机,并且系统打印功能正常(可以先试试系统自带的打印功能是否能工作)
4. 检查插件安装状态
运行以下命令确认插件是否正确安装:
cordova plugin list
如果没有看到cordova-plugin-printer,重新安装插件:
cordova plugin add cordova-plugin-printer
内容的提问来源于stack exchange,提问作者puja
相关产品推荐
相关产品推荐

