# 打印网页 printPage

● 接口函数

zwexplorer.__printer.printPage

● 接口说明

打印网页,支持在线网址。
不传入时为打印当前页(此种情况仅适用于在政务浏览器中打开的页面,不支持中间件模式)

● Typescript 类型参考

type Margins = {
    marginType?: ('default' | 'none'  | 'custom'); //只有在值为custom的情况下才需要以下四个方位的外边距参数
    top?: number;
    bottom?: number;
    left?: number;
    right?: number;
}

type PageRanges = {
    from: number;
    to: number;
}

type printPageOpt = {
    silent?: boolean;
    printBackground?: boolean;
    deviceName?: string;
    color?: boolean;
    margins?: Margins;
    landscape?: boolean;
    scaleFactor?: number;
    pagesPerSheet?: number;
    copies?: number;
    pageRanges?: PageRanges[];
    duplexMode?: ('simplex' | 'shortEdge' | 'longEdge');
    dpi?: Record<string, number>;
    pageSize?: (string) | (Size);
}


type ReqBody = {
    data?: string,
    options?: printPageOpt
}
type ResBody = {
    resultCode: number,//0表示成功,-1或其他表示失败
    resultMessage: string, //具体错误信息
    data?: any //保留备用
}


printPage(req: ReqBody, callback?: (res: ResBody)=>void): Promise<ResBody>

● 请求参数

参数 类型 是否必填 说明
req.data String 需要打印的网页url地址
req.options Object 打印网页时的打印参数
req.options.deviceName String 打印机的设备名
req.options.color Boolean 是否彩色打印,true为彩色,false为黑白,默认为true
req.options.copies Number 打印份数,默认为1份
req.options.dpi Number 打印质量,默认为300
req.options.pageRanges PageRanges[] 打印页数
req.options.pageRanges[index].from Number 起始页,index为数组序数
req.options.pageRanges[index].to Number 结束页,index为数组序数
req.options.margins Margins 边距
req.options.margins.marginType String 默认、无、自定义:('default' | 'none' | 'custom')
req.options.margins.top Number 上边距
req.options.margins.right Number 右边距
req.options.margins.bottom Number 下边距
req.options.margins.left Number 左边距
req.options.slient Boolean 是否静默打印,true为静默打印
req.options.landscape Boolean 是否竖向打印,false为竖向,true为横向
req.options.printBackground Boolean 是否带背景颜色打印
req.options.headerFooter Boolean 是否带页头和页尾打印
req.options.scaleFactor Boolean 缩放大小,取值为1~100,默认为100
req.options.pageSize Boolean 打印纸张尺寸,取值为'A3' | 'A4' | 'A5'

● 返回数据

参数 类型 必然存在 说明
res.resultCode Object 打印成功与否的状态码,-1 为打印失败,0 为打印成功
res.resultMessage Boolean 返回是否打印成功的消息提示

● 示例

let req = { data: "www.baidu.com", options: { pageSize: "A4", copies: 2 } };
zwexplorer.__printer.printPage(req, (res) => {});
// 或
window.print(req, (res) => {});

# 打印pdf文件 printPdf

注意事项

在Linux系统(信创环境)中,color属性参数无法使用,
需要预生成对应的类别(彩色或黑白)的PDF

● 接口函数

zwexplorer.__printer.printPdf

● 接口说明

打印 pdf 文件

● Typescript 类型参考



 type PageRanges= {
    from: number;
    to: number;
  }

type printObj={
    deviceName?: string;//打印设备的名称
    color?: boolean;//是否彩色打印,默认为true
    landscape?: boolean;//打印方向,false为纵向,true为横向。默认为false
    copies?: number;//打印副本数量
    pageRanges?: PageRanges[];//打印的具体页数,从某页到某页
    pageSize?: "A3" | "A4" | "A5" ; //打印纸张格式,值为`A3`, `A4`, `A5`
    duplexMode?:('duplex'| 'duplexshort'| 'duplexlong' 'simplex')//'duplex'| 'duplexshort'| 'duplexlong' 'simplex'
  }

type ReqBody = {
    data?: string, //url所指向的pdf文件地址或者本地pdf文件地址
    options?: printObj
}
type ResBody = {
    resultCode: number,//0表示成功,-1或其他表示失败
    resultMessage: string, //具体错误信息
    data?: any //保留备用
}

printPdf(data: ReqBody, callback?: (res: ResBody)=>void): Promise<ResBody>

● 请求参数

参数 类型 是否必填 说明
req.data String 需要打印的网页 url 地址
req.options Object 打印网页时的打印参数
req.options.deviceName String 打印机的设备名
req.options.color Boolean 是否彩色打印,true 为彩色,false 为黑白,默认为 true
req.options.copies Number 打印份数,默认为 1 份
req.options.pageRanges PageRanges[] 打印页数
req.options.pageRanges[index].from Number 起始页,index 为数组序数
req.options.pageRanges[index].to Number 结束页,index 为数组序数
req.options.landscape Boolean 是否竖向打印,false 为竖向,true 为横向
req.options.pageSize Boolean 打印纸张尺寸,取值为'A3'

● 返回数据

参数 类型 必然存在 说明
res.resultCode Object 返回调用弹窗接口的入参
res.resultMessage Boolean 返回点击弹窗的按钮结果

● 示例

let req = {
  data: "https://demo.zwexplorer.com/native-api/resources/5%E5%8F%B7%E6%96%87.pdf",
  options: { pageSize: "A4", color: false },
};
zwexplorer.__printer.printPdf(req, (res) => {});

# 获取打印机列表 getPrinters

● 接口函数

zwexplorer.__printer.getPrinters

● 接口说明

获取打印机列表

● Typescript 类型参考


type ResBody = {
    resultCode: number,//0表示成功,-1或其他表示失败
    resultMessage: string, //具体错误信息
    data?: any //保留备用
}

getPrinters( callback?: (res: ResBody)=>void): Promise<ResBody>

● 返回数据

参数 类型 必然存在 说明
res.resultCode Object 返回调用弹窗接口的入参
res.resultMessage Boolean 返回点击弹窗的按钮结果

● 示例

zwexplorer.__printer.getPrinters(ReqBody, (res) => {});