You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Flutter InAppWebView 6.0.0中PrintJobSettings设置不生效问题

问题解决:InAppWebView 6.0.0 PrintJobSettings不生效及A4尺寸不显示

问题根源

你的代码存在两个关键问题:

  1. MediaSize参数错误:A4纸张的尺寸换算错误,且使用了随机id,系统无法识别为标准A4规格
  2. 打印流程处理错误:你dispose了系统提供的printJobController并自行调用printCurrentPage,导致自定义设置未注入到系统打印流程中

修正后的代码

onPrintRequest: (controller, url, printJobController) async {
  if (printJobController == null) return false;

  final jobName = "${await controller.getTitle() ?? ''} - PDF Document example";
  final printSettings = PrintJobSettings(
    jobName: jobName,
    colorMode: PrintJobColorMode.MONOCHROME,
    outputType: PrintJobOutputType.GRAYSCALE,
    orientation: PrintJobOrientation.PORTRAIT,
    margins: EdgeInsets.all(10),
    // 标准A4纸张参数:210mm×297mm,转换为mils(1mm=39.37mils)
    mediaSize: PrintJobMediaSize(
      id: "iso_a4",
      name: PrintJobMediaSizeName.isoA4,
      widthMils: 8267,
      heightMils: 11693,
    ),
    numberOfPages: 1,
  );

  // 将配置注入系统打印控制器并启动打印
  await printJobController.setSettings(printSettings);
  await printJobController.start();
  return true;
},

关键修正点说明

  • MediaSize修正:使用标准A4的id"iso_a4"和预定义名称PrintJobMediaSizeName.isoA4,同时填入正确的尺寸换算值(宽8267mils、高11693mils)
  • 打印流程修正:保留系统提供的printJobController,通过它的setSettings和start方法完成打印,确保自定义设置被系统打印流程识别
  • 移除了不必要的printJobController?.dispose()调用,避免破坏系统打印生命周期

内容的提问来源于stack exchange,提问作者Md Hemal

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.18 00:00:04