• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# @ohos.print (打印)
2
3<!--Kit: Basic Services Kit-->
4<!--Subsystem: Print-->
5<!--Owner: @guoshengbang-->
6<!--Designer: @gcw_4D6e0BBd-->
7<!--Tester: @guoshengbang-->
8<!--Adviser: @RayShih-->
9
10该模块为基本打印的操作API,提供调用基础打印功能的接口。
11
12> **说明:**
13> 本模块首批接口从API version 10开始支持。后续版本的新增接口,采用上角标单独标记接口的起始版本。
14
15## 导入模块
16
17```ts
18import { print } from '@kit.BasicServicesKit';
19```
20
21## PrintTask
22
23打印任务完成后的事件监听回调接口类。
24
25### PrintTask.on
26
27on(type: 'block', callback: Callback&lt;void&gt;): void
28
29注册打印完成后的监听,使用callback回调。
30
31**需要权限:** ohos.permission.PRINT
32
33**系统能力:** SystemCapability.Print.PrintFramework
34
35**参数:**
36| **参数名** | **类型** | **必填** | **说明** |
37| -------- | -------- | -------- | -------- |
38| type | string | 是 | 注册监听,<br/>监听字段:block,<br/>表示打印阻塞。 |
39| callback | Callback&lt;void&gt; | 是 | 打印完成后处于响应状态的回调。 |
40
41**错误码:**
42
43以下错误码的详细介绍请参见[打印服务错误码](./errorcode-print.md)。
44
45| 错误码ID | 错误信息                                    |
46| -------- | ------------------------------------------- |
47| 201 | the application does not have permission to call this function. |
48| 401 | Parameter error. Possible causes: 1.Mandatory parameters are left unspecified; 2.Incorrect parameter types. |
49
50**示例:**
51
52```ts
53import { print } from '@kit.BasicServicesKit';
54import { BusinessError } from '@ohos.base';
55import { fileUri } from '@kit.CoreFileKit';
56
57let filePath = '/data/storage/el2/base/haps/entry/files/test.pdf';
58print.print([fileUri.getUriFromPath(filePath)]).then((printTask: print.PrintTask) => {
59    printTask.on('block', () => {
60        console.log('print state is block');
61    })
62    // ...
63}).catch((error: BusinessError) => {
64    console.error('print err ' + JSON.stringify(error));
65})
66```
67
68### PrintTask.on
69
70on(type: 'succeed', callback: Callback&lt;void&gt;): void
71
72注册打印完成后的监听,使用callback回调。
73
74**需要权限:** ohos.permission.PRINT
75
76**系统能力:** SystemCapability.Print.PrintFramework
77
78**参数:**
79| **参数名** | **类型** | **必填** | **说明** |
80| -------- | -------- | -------- | -------- |
81| type | string | 是 | 注册监听,<br/>监听字段:succeed,<br/>表示打印成功。 |
82| callback | Callback&lt;void&gt; | 是 | 打印完成后处于响应状态的回调。 |
83
84**错误码:**
85
86以下错误码的详细介绍请参见[打印服务错误码](./errorcode-print.md)。
87
88| 错误码ID | 错误信息                                    |
89| -------- | ------------------------------------------- |
90| 201 | the application does not have permission to call this function. |
91| 401 | Parameter error. Possible causes: 1.Mandatory parameters are left unspecified; 2.Incorrect parameter types. |
92
93**示例:**
94
95```ts
96import { print } from '@kit.BasicServicesKit';
97import { BusinessError } from '@ohos.base';
98import { fileUri } from '@kit.CoreFileKit';
99
100let filePath = '/data/storage/el2/base/haps/entry/files/test.pdf';
101print.print([fileUri.getUriFromPath(filePath)]).then((printTask: print.PrintTask) => {
102    printTask.on('succeed', () => {
103        console.log('print state is succeed');
104    })
105    // ...
106}).catch((error: BusinessError) => {
107    console.error('print err ' + JSON.stringify(error));
108})
109```
110
111### PrintTask.on
112
113on(type: 'fail', callback: Callback&lt;void&gt;): void
114
115注册打印完成后的监听,使用callback回调。
116
117**需要权限:** ohos.permission.PRINT
118
119**系统能力:** SystemCapability.Print.PrintFramework
120
121**参数:**
122| **参数名** | **类型** | **必填** | **说明** |
123| -------- | -------- | -------- | -------- |
124| type | string | 是 | 注册监听,<br/>监听字段:fail,<br/>表示打印失败。 |
125| callback | Callback&lt;void&gt; | 是 | 打印完成后处于响应状态的回调。 |
126
127**错误码:**
128
129以下错误码的详细介绍请参见[打印服务错误码](./errorcode-print.md)。
130
131| 错误码ID | 错误信息                                    |
132| -------- | ------------------------------------------- |
133| 201 | the application does not have permission to call this function. |
134| 401 | Parameter error. Possible causes: 1.Mandatory parameters are left unspecified; 2.Incorrect parameter types. |
135
136**示例:**
137
138```ts
139import { print } from '@kit.BasicServicesKit';
140import { BusinessError } from '@ohos.base';
141import { fileUri } from '@kit.CoreFileKit';
142
143let filePath = '/data/storage/el2/base/haps/entry/files/test.pdf';
144print.print([fileUri.getUriFromPath(filePath)]).then((printTask: print.PrintTask) => {
145    printTask.on('fail', () => {
146        console.log('print state is fail');
147    })
148    // ...
149}).catch((error: BusinessError) => {
150    console.error('print err ' + JSON.stringify(error));
151})
152```
153
154### PrintTask.on
155
156on(type: 'cancel', callback: Callback&lt;void&gt;): void
157
158注册打印完成后的监听,使用callback回调。
159
160**需要权限:** ohos.permission.PRINT
161
162**系统能力:** SystemCapability.Print.PrintFramework
163
164**参数:**
165| **参数名** | **类型** | **必填** | **说明** |
166| -------- | -------- | -------- | -------- |
167| type | string | 是 | 注册监听,<br/>监听字段:cancel,<br/>表示打印取消。 |
168| callback | Callback&lt;void&gt; | 是 | 打印完成后处于响应状态的回调。 |
169
170**错误码:**
171
172以下错误码的详细介绍请参见[打印服务错误码](./errorcode-print.md)。
173
174| 错误码ID | 错误信息                                    |
175| -------- | ------------------------------------------- |
176| 201 | the application does not have permission to call this function. |
177| 401 | Parameter error. Possible causes: 1.Mandatory parameters are left unspecified; 2.Incorrect parameter types. |
178
179**示例:**
180
181```ts
182import { print } from '@kit.BasicServicesKit';
183import { BusinessError } from '@ohos.base';
184import { fileUri } from '@kit.CoreFileKit';
185
186let filePath = '/data/storage/el2/base/haps/entry/files/test.pdf';
187print.print([fileUri.getUriFromPath(filePath)]).then((printTask: print.PrintTask) => {
188    printTask.on('cancel', () => {
189        console.log('print state is cancel');
190    })
191    // ...
192}).catch((error: BusinessError) => {
193    console.error('print err ' + JSON.stringify(error));
194})
195```
196
197### PrintTask.off
198
199off(type: 'block', callback?: Callback&lt;void&gt;): void
200
201取消打印完成后的监听,使用callback回调。
202
203**需要权限:** ohos.permission.PRINT
204
205**系统能力:** SystemCapability.Print.PrintFramework
206
207**参数:**
208| **参数名** | **类型** | **必填** | **说明** |
209| -------- | -------- | -------- | -------- |
210| type | string | 是 | 取消监听,<br/>监听字段:block,<br/>表示打印阻塞。 |
211| callback | Callback&lt;void&gt; | 否 | 取消相应状态监听成功后的回调。 |
212
213**错误码:**
214
215以下错误码的详细介绍请参见[打印服务错误码](./errorcode-print.md)。
216
217| 错误码ID | 错误信息                                    |
218| -------- | ------------------------------------------- |
219| 201 | the application does not have permission to call this function. |
220| 401 | Parameter error. Possible causes: 1.Mandatory parameters are left unspecified; 2.Incorrect parameter types. |
221
222**示例:**
223
224```ts
225import { print } from '@kit.BasicServicesKit';
226import { BusinessError } from '@ohos.base';
227import { fileUri } from '@kit.CoreFileKit';
228
229let filePath = '/data/storage/el2/base/haps/entry/files/test.pdf';
230print.print([fileUri.getUriFromPath(filePath)]).then((printTask: print.PrintTask) => {
231    printTask.off('block', () => {
232        console.log('unregister state block');
233    })
234    // ...
235}).catch((error: BusinessError) => {
236    console.error('print err ' + JSON.stringify(error));
237})
238```
239
240### PrintTask.off
241
242off(type: 'succeed', callback?: Callback&lt;void&gt;): void
243
244取消打印完成后的监听,使用callback回调。
245
246**需要权限:** ohos.permission.PRINT
247
248**系统能力:** SystemCapability.Print.PrintFramework
249
250**参数:**
251| **参数名** | **类型** | **必填** | **说明** |
252| -------- | -------- | -------- | -------- |
253| type | string | 是 | 取消监听,<br/>监听字段:succeed,<br/>表示打印成功。 |
254| callback | Callback&lt;void&gt; | 否 | 取消相应状态监听成功后的回调。 |
255
256**错误码:**
257
258以下错误码的详细介绍请参见[打印服务错误码](./errorcode-print.md)。
259
260| 错误码ID | 错误信息                                    |
261| -------- | ------------------------------------------- |
262| 201 | the application does not have permission to call this function. |
263| 401 | Parameter error. Possible causes: 1.Mandatory parameters are left unspecified; 2.Incorrect parameter types. |
264
265**示例:**
266
267```ts
268import { print } from '@kit.BasicServicesKit';
269import { BusinessError } from '@ohos.base';
270import { fileUri } from '@kit.CoreFileKit';
271
272let filePath = '/data/storage/el2/base/haps/entry/files/test.pdf';
273print.print([fileUri.getUriFromPath(filePath)]).then((printTask: print.PrintTask) => {
274    printTask.off('succeed', () => {
275        console.log('unregister state succeed');
276    })
277    // ...
278}).catch((error: BusinessError) => {
279    console.error('print err ' + JSON.stringify(error));
280})
281```
282
283### PrintTask.off
284
285off(type: 'fail', callback?: Callback&lt;void&gt;): void
286
287取消打印完成后的监听,使用callback回调。
288
289**需要权限:** ohos.permission.PRINT
290
291**系统能力:** SystemCapability.Print.PrintFramework
292
293**参数:**
294| **参数名** | **类型** | **必填** | **说明** |
295| -------- | -------- | -------- | -------- |
296| type | string | 是 | 取消监听,<br/>监听字段:fail,<br/>表示打印失败。 |
297| callback | Callback&lt;void&gt; | 否 | 取消相应状态监听成功后的回调。 |
298
299**错误码:**
300
301以下错误码的详细介绍请参见[打印服务错误码](./errorcode-print.md)。
302
303| 错误码ID | 错误信息                                    |
304| -------- | ------------------------------------------- |
305| 201 | the application does not have permission to call this function. |
306| 401 | Parameter error. Possible causes: 1.Mandatory parameters are left unspecified; 2.Incorrect parameter types. |
307
308**示例:**
309
310```ts
311import { print } from '@kit.BasicServicesKit';
312import { BusinessError } from '@ohos.base';
313import { fileUri } from '@kit.CoreFileKit';
314
315let filePath = '/data/storage/el2/base/haps/entry/files/test.pdf';
316print.print([fileUri.getUriFromPath(filePath)]).then((printTask: print.PrintTask) => {
317    printTask.off('fail', () => {
318        console.log('unregister state fail');
319    })
320    // ...
321}).catch((error: BusinessError) => {
322    console.error('print err ' + JSON.stringify(error));
323})
324```
325
326### PrintTask.off
327
328off(type: 'cancel', callback?: Callback&lt;void&gt;): void
329
330取消打印完成后的监听,使用callback回调。
331
332**需要权限:** ohos.permission.PRINT
333
334**系统能力:** SystemCapability.Print.PrintFramework
335
336**参数:**
337| **参数名** | **类型** | **必填** | **说明** |
338| -------- | -------- | -------- | -------- |
339| type | string | 是 | 取消监听,<br/>监听字段:cancel,<br/>表示打印取消。 |
340| callback | Callback&lt;void&gt; | 否 | 取消相应状态监听成功后的回调。 |
341
342**错误码:**
343
344以下错误码的详细介绍请参见[打印服务错误码](./errorcode-print.md)。
345
346| 错误码ID | 错误信息                                    |
347| -------- | ------------------------------------------- |
348| 201 | the application does not have permission to call this function. |
349| 401 | Parameter error. Possible causes: 1.Mandatory parameters are left unspecified; 2.Incorrect parameter types. |
350
351**示例:**
352
353```ts
354import { print } from '@kit.BasicServicesKit';
355import { BusinessError } from '@ohos.base';
356import { fileUri } from '@kit.CoreFileKit';
357
358let filePath = '/data/storage/el2/base/haps/entry/files/test.pdf';
359print.print([fileUri.getUriFromPath(filePath)]).then((printTask: print.PrintTask) => {
360    printTask.off('cancel', () => {
361        console.log('unregister state cancel');
362    })
363    // ...
364}).catch((error: BusinessError) => {
365    console.error('print err ' + JSON.stringify(error));
366})
367```
368
369## PrintDocumentAdapter<sup>11+</sup>
370
371第三方应用程序实现此接口来渲染要打印的文件。
372
373### onStartLayoutWrite<sup>11+</sup>
374
375onStartLayoutWrite(jobId: string, oldAttrs: PrintAttributes, newAttrs: PrintAttributes, fd: number, writeResultCallback: (jobId: string, writeResult: PrintFileCreationState) => void): void
376
377打印服务会通过本接口将一个空的pdf文件的文件描述符传给三方应用,由三方应用使用新的打印参数更新待打印文件,更新文件完成后通过本接口的回调方法writeResultCallback通知打印服务。
378
379**需要权限:** ohos.permission.PRINT
380
381**系统能力:** SystemCapability.Print.PrintFramework
382
383**参数:**
384| **参数名** | **类型** | **必填** | **说明** |
385| -------- | -------- | -------- | -------- |
386| jobId | string | 是 | 表示打印任务ID。 |
387| oldAttrs | [PrintAttributes](#printattributes11) | 是 | 表示旧打印参数。 |
388| newAttrs | [PrintAttributes](#printattributes11) | 是 | 表示新打印参数。 |
389| fd | number | 是 | 表示打印文件传给接口调用方的pdf文件的文件描述符。 |
390| writeResultCallback | (jobId: string, writeResult: [PrintFileCreationState](#printfilecreationstate11)) => void | 是 | 表示三方应用使用新的打印参数更新待打印文件完成后的回调。 |
391
392**错误码:**
393
394以下错误码的详细介绍请参见[打印服务错误码](./errorcode-print.md)。
395
396| 错误码ID | 错误信息                                    |
397| -------- | ------------------------------------------- |
398| 201 | the application does not have permission to call this function. |
399| 401 | Parameter error. Possible causes: 1.Mandatory parameters are left unspecified; 2.Incorrect parameter types. |
400
401**示例:**
402
403```ts
404import { print } from '@kit.BasicServicesKit';
405import { BusinessError } from '@ohos.base';
406
407class MyPrintDocumentAdapter implements print.PrintDocumentAdapter {
408    onStartLayoutWrite(jobId: string, oldAttrs: print.PrintAttributes, newAttrs: print.PrintAttributes, fd: number,
409        writeResultCallback: (jobId: string, writeResult: print.PrintFileCreationState) => void) {
410        writeResultCallback(jobId, print.PrintFileCreationState.PRINT_FILE_CREATED);
411    };
412    onJobStateChanged(jobId: string, state: print.PrintDocumentAdapterState) {
413        if (state == print.PrintDocumentAdapterState.PREVIEW_DESTROY) {
414            console.log('PREVIEW_DESTROY');
415        } else if (state == print.PrintDocumentAdapterState.PRINT_TASK_SUCCEED) {
416            console.log('PRINT_TASK_SUCCEED');
417        } else if (state == print.PrintDocumentAdapterState.PRINT_TASK_FAIL) {
418            console.log('PRINT_TASK_FAIL');
419        } else if (state == print.PrintDocumentAdapterState.PRINT_TASK_CANCEL) {
420            console.log('PRINT_TASK_CANCEL');
421        } else if (state == print.PrintDocumentAdapterState.PRINT_TASK_BLOCK) {
422            console.log('PRINT_TASK_BLOCK');
423        }
424    }
425}
426```
427
428### onJobStateChanged<sup>11+</sup>
429
430onJobStateChanged(jobId: string, state: PrintDocumentAdapterState): void
431
432实现这个接口来监听打印任务状态的改变。
433
434**需要权限:** ohos.permission.PRINT
435
436**系统能力:** SystemCapability.Print.PrintFramework
437
438**参数:**
439| **参数名** | **类型** | **必填** | **说明** |
440| -------- | -------- | -------- | -------- |
441| jobId | string | 是 | 表示打印任务ID。 |
442| state | [PrintDocumentAdapterState](#printdocumentadapterstate11) | 是 | 表示打印任务更改为该状态。 |
443
444**错误码:**
445
446以下错误码的详细介绍请参见[打印服务错误码](./errorcode-print.md)。
447
448| 错误码ID | 错误信息                                    |
449| -------- | ------------------------------------------- |
450| 201 | the application does not have permission to call this function. |
451| 401 | Parameter error. Possible causes: 1.Mandatory parameters are left unspecified; 2.Incorrect parameter types. |
452
453**示例:**
454
455```ts
456import { print } from '@kit.BasicServicesKit';
457import { BusinessError } from '@ohos.base';
458
459class MyPrintDocumentAdapter implements print.PrintDocumentAdapter {
460    onStartLayoutWrite(jobId: string, oldAttrs: print.PrintAttributes, newAttrs: print.PrintAttributes, fd: number,
461        writeResultCallback: (jobId: string, writeResult: print.PrintFileCreationState) => void) {
462        writeResultCallback(jobId, print.PrintFileCreationState.PRINT_FILE_CREATED);
463    };
464    onJobStateChanged(jobId: string, state: print.PrintDocumentAdapterState) {
465        if (state == print.PrintDocumentAdapterState.PREVIEW_DESTROY) {
466            console.log('PREVIEW_DESTROY');
467        } else if (state == print.PrintDocumentAdapterState.PRINT_TASK_SUCCEED) {
468            console.log('PRINT_TASK_SUCCEED');
469        } else if (state == print.PrintDocumentAdapterState.PRINT_TASK_FAIL) {
470            console.log('PRINT_TASK_FAIL');
471        } else if (state == print.PrintDocumentAdapterState.PRINT_TASK_CANCEL) {
472            console.log('PRINT_TASK_CANCEL');
473        } else if (state == print.PrintDocumentAdapterState.PRINT_TASK_BLOCK) {
474            console.log('PRINT_TASK_BLOCK');
475        }
476    }
477}
478```
479
480## print.print
481
482print(files: Array&lt;string&gt;, callback: AsyncCallback&lt;PrintTask&gt;): void
483
484打印接口,传入文件进行打印,使用callback异步回调。
485
486**需要权限:** ohos.permission.PRINT
487
488**系统能力:** SystemCapability.Print.PrintFramework
489
490**参数:**
491| **参数名** | **类型** | **必填** | **说明** |
492| -------- | -------- | -------- | -------- |
493| files | Array&lt;string&gt; | 是 | 待打印文件列表,支持图片(.jpg .png .gif .bmp .webp)和pdf。文件需先保存到应用沙箱,通过fileUri.getUriFromPath获取到沙箱uri,再作为参数传入到本接口。 |
494| callback | AsyncCallback&lt;[PrintTask](#printtask)&gt; | 是 | 异步获取打印完成之后的回调。 |
495
496**错误码:**
497
498以下错误码的详细介绍请参见[打印服务错误码](./errorcode-print.md)。
499
500| 错误码ID | 错误信息                                    |
501| -------- | ------------------------------------------- |
502| 201 | the application does not have permission to call this function. |
503| 401 | Parameter error. Possible causes: 1.Mandatory parameters are left unspecified; 2.Incorrect parameter types. |
504
505**示例:**
506
507```ts
508import { print } from '@kit.BasicServicesKit';
509import { BusinessError } from '@ohos.base';
510import { fileUri } from '@kit.CoreFileKit';
511
512//传入文件的uri
513let filePath = '/data/storage/el2/base/haps/entry/files/test.pdf';
514print.print([fileUri.getUriFromPath(filePath)], (err: BusinessError, printTask: print.PrintTask) => {
515    if (err) {
516        console.error('print err ' + JSON.stringify(err));
517    } else {
518        printTask.on('succeed', () => {
519            console.log('print state is succeed');
520        })
521        // ...
522    }
523})
524```
525
526## print.print
527
528print(files: Array&lt;string&gt;): Promise&lt;PrintTask&gt;
529
530打印接口,传入文件进行打印,使用Promise异步回调。
531
532**需要权限:** ohos.permission.PRINT
533
534**系统能力:** SystemCapability.Print.PrintFramework
535
536**参数:**
537| **参数名** | **类型** | **必填** | **说明** |
538| -------- | -------- | -------- | -------- |
539| files | Array&lt;string&gt; | 是 | 待打印文件列表,支持图片(.jpg .png .gif .bmp .webp)和pdf。文件需先保存到应用沙箱,通过fileUri.getUriFromPath获取到沙箱uri,再作为参数传入到本接口。 |
540
541**返回值:**
542| **类型** | **说明** |
543| -------- | -------- |
544| Promise&lt;[PrintTask](#printtask)&gt; | 打印完成结果。 |
545
546**错误码:**
547
548以下错误码的详细介绍请参见[打印服务错误码](./errorcode-print.md)。
549
550| 错误码ID | 错误信息                                    |
551| -------- | ------------------------------------------- |
552| 201 | the application does not have permission to call this function. |
553| 401 | Parameter error. Possible causes: 1.Mandatory parameters are left unspecified; 2.Incorrect parameter types. |
554
555**示例:**
556
557```ts
558import { print } from '@kit.BasicServicesKit';
559import { BusinessError } from '@ohos.base';
560import { fileUri } from '@kit.CoreFileKit';
561
562//传入文件的uri
563let filePath = '/data/storage/el2/base/haps/entry/files/test.pdf';
564print.print([fileUri.getUriFromPath(filePath)]).then((printTask: print.PrintTask) => {
565    printTask.on('succeed', () => {
566        console.log('print state is succeed');
567    })
568    // ...
569}).catch((error: BusinessError) => {
570    console.error('print err ' + JSON.stringify(error));
571})
572```
573
574## print.print<sup>11+</sup>
575
576print(files: Array&lt;string&gt;, context: Context, callback: AsyncCallback&lt;PrintTask&gt;): void
577
578打印接口,传入文件进行打印,使用callback异步回调。
579
580**需要权限:** ohos.permission.PRINT
581
582**系统能力:** SystemCapability.Print.PrintFramework
583
584**参数:**
585| **参数名** | **类型** | **必填** | **说明** |
586| -------- | -------- | -------- | -------- |
587| files | Array&lt;string&gt; | 是 | 待打印文件列表,支持图片(.jpg .png .gif .bmp .webp)和pdf。文件需先保存到应用沙箱,通过fileUri.getUriFromPath获取到沙箱uri,再作为参数传入到本接口。 |
588| context | Context | 是 | 用于拉起系统打印界面的UIAbilityContext。 |
589| callback | AsyncCallback&lt;[PrintTask](#printtask)&gt; | 是 | 异步获取打印完成之后的回调。 |
590
591**错误码:**
592
593以下错误码的详细介绍请参见[打印服务错误码](./errorcode-print.md)。
594
595| 错误码ID | 错误信息                                    |
596| -------- | ------------------------------------------- |
597| 201 | the application does not have permission to call this function. |
598| 401 | Parameter error. Possible causes: 1.Mandatory parameters are left unspecified; 2.Incorrect parameter types. |
599
600**示例:**
601
602```ts
603import { print } from '@kit.BasicServicesKit';
604import { BusinessError } from '@ohos.base';
605import { fileUri } from '@kit.CoreFileKit';
606
607@Entry
608@Component
609struct Index {
610    build() {
611        Scroll() {
612            Column({ space: 10 }) {
613                Button("打印").width('90%').height(50).onClick(() => {
614                    let filePath = '/data/storage/el2/base/haps/entry/files/test.pdf';
615                    let context = this.getUIContext().getHostContext();
616                    print.print([fileUri.getUriFromPath(filePath)], context, (err: BusinessError, printTask: print.PrintTask) => {
617                        if (err) {
618                            console.error('print err ' + JSON.stringify(err));
619                        } else {
620                            printTask.on('succeed', () => {
621                                console.log('print state is succeed');
622                            })
623                            // ...
624                        }
625                    })
626                })
627            }
628            .justifyContent(FlexAlign.Center)
629            .constraintSize({ minHeight: '100%' })
630            .width('100%')
631        }
632        .height('100%')
633    }
634}
635```
636
637## print.print<sup>11+</sup>
638
639print(files: Array&lt;string&gt;, context: Context): Promise&lt;PrintTask&gt;
640
641打印接口,传入文件进行打印,使用Promise异步回调。
642
643**需要权限:** ohos.permission.PRINT
644
645**系统能力:** SystemCapability.Print.PrintFramework
646
647**参数:**
648| **参数名** | **类型** | **必填** | **说明** |
649| -------- | -------- | -------- | -------- |
650| files | Array&lt;string&gt; | 是 | 待打印文件列表,支持图片(.jpg .png .gif .bmp .webp)和pdf。文件需先保存到应用沙箱,通过fileUri.getUriFromPath获取到沙箱uri,再作为参数传入到本接口。 |
651| context | Context | 是 | 用于拉起系统打印界面的UIAbilityContext。 |
652
653**返回值:**
654| **类型** | **说明** |
655| -------- | -------- |
656| Promise&lt;[PrintTask](#printtask)&gt; | 打印完成结果。 |
657
658**错误码:**
659
660以下错误码的详细介绍请参见[打印服务错误码](./errorcode-print.md)。
661
662| 错误码ID | 错误信息                                    |
663| -------- | ------------------------------------------- |
664| 201 | the application does not have permission to call this function. |
665| 401 | Parameter error. Possible causes: 1.Mandatory parameters are left unspecified; 2.Incorrect parameter types. |
666
667**示例:**
668
669```ts
670import { print } from '@kit.BasicServicesKit';
671import { BusinessError } from '@ohos.base';
672import { fileUri } from '@kit.CoreFileKit';
673
674@Entry
675@Component
676struct Index {
677    build() {
678        Scroll() {
679            Column({ space: 10 }) {
680                Button("打印").width('90%').height(50).onClick(() => {
681                    let filePath = '/data/storage/el2/base/haps/entry/files/test.pdf';
682                    let context = this.getUIContext().getHostContext();
683                    print.print([fileUri.getUriFromPath(filePath)], context).then((printTask: print.PrintTask) => {
684                        printTask.on('succeed', () => {
685                            console.log('print state is succeed');
686                        })
687                        // ...
688                    }).catch((error: BusinessError) => {
689                        console.error('print err ' + JSON.stringify(error));
690                    })
691                })
692            }
693            .justifyContent(FlexAlign.Center)
694            .constraintSize({ minHeight: '100%' })
695            .width('100%')
696        }
697        .height('100%')
698    }
699}
700```
701
702## print.print<sup>11+</sup>
703
704print(jobName: string, printAdapter: PrintDocumentAdapter, printAttributes: PrintAttributes, context: Context): Promise&lt;PrintTask&gt;
705
706打印接口,传入文件进行打印,三方应用需要更新打印文件,使用Promise异步回调。
707
708**需要权限:** ohos.permission.PRINT
709
710**系统能力:** SystemCapability.Print.PrintFramework
711
712**参数:**
713| **参数名** | **类型** | **必填** | **说明** |
714| -------- | -------- | -------- | -------- |
715| jobName | string | 是 | 表示待打印文件名称,例如:test.pdf。打印侧会通过[onStartLayoutWrite](#onstartlayoutwrite11)接口将空的pdf文件的fd传给接口调用方,由调用方使用新的打印参数更新待打印文件。 |
716| printAdapter | [PrintDocumentAdapter](#printdocumentadapter11) | 是 | 表示三方应用实现的[PrintDocumentAdapter](#printdocumentadapter11)接口实例。 |
717| printAttributes | [PrintAttributes](#printattributes11) | 是 | 表示打印参数。 |
718| context | Context | 是 | 用于拉起系统打印界面的UIAbilityContext。 |
719
720**返回值:**
721| **类型** | **说明** |
722| -------- | -------- |
723| Promise&lt;[PrintTask](#printtask)&gt; | 打印完成结果。 |
724
725**错误码:**
726
727以下错误码的详细介绍请参见[打印服务错误码](./errorcode-print.md)。
728
729| 错误码ID | 错误信息                                    |
730| -------- | ------------------------------------------- |
731| 201 | the application does not have permission to call this function. |
732| 401 | Parameter error. Possible causes: 1.Mandatory parameters are left unspecified; 2.Incorrect parameter types. |
733
734**示例:**
735
736```ts
737import { print } from '@kit.BasicServicesKit';
738import { BusinessError } from '@ohos.base';
739
740@Entry
741@Component
742struct Index {
743    build() {
744        Scroll() {
745            Column({ space: 10 }) {
746                Button("打印").width('90%').height(50).onClick(() => {
747                    let jobName : string = "jobName";
748                    let printAdapter : print.PrintDocumentAdapter | null = null;
749                    let printAttributes : print.PrintAttributes = {
750                        copyNumber: 1,
751                        pageRange: {
752                            startPage: 0,
753                            endPage: 5,
754                            pages: []
755                        },
756                        pageSize: print.PrintPageType.PAGE_ISO_A3,
757                        directionMode: print.PrintDirectionMode.DIRECTION_MODE_AUTO,
758                        colorMode: print.PrintColorMode.COLOR_MODE_MONOCHROME,
759                        duplexMode: print.PrintDuplexMode.DUPLEX_MODE_NONE
760                    }
761                    let context = this.getUIContext().getHostContext();
762
763                    print.print(jobName, printAdapter, printAttributes, context).then((printTask: print.PrintTask) => {
764                        printTask.on('succeed', () => {
765                            console.log('print state is succeed');
766                        })
767                        // ...
768                    }).catch((error: BusinessError) => {
769                        console.error('print err ' + JSON.stringify(error));
770                    })
771                })
772            }
773            .justifyContent(FlexAlign.Center)
774            .constraintSize({ minHeight: '100%' })
775            .width('100%')
776        }
777        .height('100%')
778    }
779}
780```
781
782## PrintAttributes<sup>11+</sup>
783
784定义打印参数的接口。
785
786**系统能力:** SystemCapability.Print.PrintFramework
787
788**属性:**
789| **名称** | **类型** | **必填** | **说明** |
790| -------- | -------- | -------- | -------- |
791| copyNumber | number | 否 | 表示文件打印份数。默认值为1。 |
792| pageRange | [PrintPageRange](#printpagerange11) | 否 | 表示待打印文件的页面范围。 |
793| pageSize | [PrintPageSize](#printpagesize11) \| [PrintPageType](#printpagetype11) | 否 | 表示待打印文件的纸张类型。 |
794| directionMode | [PrintDirectionMode](#printdirectionmode11) | 否 | 表示待打印文件的方向。 |
795| colorMode | [PrintColorMode](#printcolormode11) | 否 | 表示待打印文件的色彩模式。 |
796| duplexMode | [PrintDuplexMode](#printduplexmode11) | 否 | 表示待打印文件的单双面模式。 |
797
798## PrintPageRange<sup>11+</sup>
799
800定义打印范围的接口。
801
802**系统能力:** SystemCapability.Print.PrintFramework
803
804**属性:**
805| **名称** | **类型** | **必填** | **说明** |
806| -------- | -------- | -------- | -------- |
807| startPage | number | 否 | 表示起始页。默认值为1。 |
808| endPage | number | 否 | 表示结束页。默认值为待打印文件的最大页数。 |
809| pages | Array&lt;number&gt; | 否 | 表示待打印的页面范围的集合。默认值为空。|
810
811
812## PrintPageSize<sup>11+</sup>
813
814定义打印页面尺寸的接口。
815
816**系统能力:** SystemCapability.Print.PrintFramework
817
818**属性:**
819| **名称** | **类型** | **必填** | **说明** |
820| -------- | -------- | -------- | -------- |
821| id | string | 是 | 表示纸张类型ID。 |
822| name | string | 是 | 表示纸张类型名称。 |
823| width | number | 是 | 表示页面宽度,单位:毫米。 |
824| height | number | 是 | 表示页面高度,单位:毫米。 |
825
826
827
828## PrintDirectionMode<sup>11+</sup>
829
830打印纸张方向的枚举。
831
832**系统能力:** SystemCapability.Print.PrintFramework
833
834| **名称** | **值** | **说明** |
835| -------- | -------- | -------- |
836| DIRECTION_MODE_AUTO | 0 | 表示自动选择纸张方向。 |
837| DIRECTION_MODE_PORTRAIT | 1 | 表示纵向打印。 |
838| DIRECTION_MODE_LANDSCAPE | 2 | 表示横向打印。 |
839
840## PrintColorMode<sup>11+</sup>
841
842打印色彩模式的枚举。
843
844**系统能力:** SystemCapability.Print.PrintFramework
845
846| **名称** | **值** | **说明** |
847| -------- | -------- | -------- |
848| COLOR_MODE_MONOCHROME | 0 | 表示黑白打印。 |
849| COLOR_MODE_COLOR | 1 | 表示彩色打印。 |
850
851## PrintDuplexMode<sup>11+</sup>
852
853打印单双面模式的枚举。
854
855**系统能力:** SystemCapability.Print.PrintFramework
856
857| **名称** | **值** | **说明** |
858| -------- | -------- | -------- |
859| DUPLEX_MODE_NONE | 0 | 表示单面打印。 |
860| DUPLEX_MODE_LONG_EDGE | 1 | 表示双面打印沿长边翻转。 |
861| DUPLEX_MODE_SHORT_EDGE | 2 | 表示双面打印沿短边翻转。 |
862
863## PrintPageType<sup>11+</sup>
864
865打印纸张类型的枚举。
866
867**系统能力:** SystemCapability.Print.PrintFramework
868
869| **名称** | **值** | **说明** |
870| -------- | -------- | -------- |
871| PAGE_ISO_A3 | 0 | 表示A3。 |
872| PAGE_ISO_A4 | 1 | 表示A4。 |
873| PAGE_ISO_A5 | 2 | 表示A5。 |
874| PAGE_JIS_B5 | 3 | 表示B5。 |
875| PAGE_ISO_C5 | 4 | 表示C5。 |
876| PAGE_ISO_DL | 5 | 表示DL。 |
877| PAGE_LETTER | 6 | 表示Letter。 |
878| PAGE_LEGAL | 7 | 表示Legal。 |
879| PAGE_PHOTO_4X6 | 8 | 表示4x6相纸。 |
880| PAGE_PHOTO_5X7 | 9 | 表示5x7相纸。 |
881| PAGE_INT_DL_ENVELOPE | 10 | 表示INT DL ENVELOPE。 |
882| PAGE_B_TABLOID | 11 | 表示B Tabloid。 |
883
884## PrintDocumentAdapterState<sup>11+</sup>
885
886打印任务状态的枚举。
887
888**系统能力:** SystemCapability.Print.PrintFramework
889
890| **名称** | **值** | **说明** |
891| -------- | -------- | -------- |
892| PREVIEW_DESTROY | 0 | 表示预览失败。 |
893| PRINT_TASK_SUCCEED | 1 | 表示打印任务成功。 |
894| PRINT_TASK_FAIL | 2 | 表示打印任务失败。 |
895| PRINT_TASK_CANCEL | 3 | 表示打印任务取消。 |
896| PRINT_TASK_BLOCK | 4 | 表示打印任务阻塞。 |
897
898## PrintFileCreationState<sup>11+</sup>
899
900打印文件创建状态的枚举。
901
902**系统能力:** SystemCapability.Print.PrintFramework
903
904| **名称** | **值** | **说明** |
905| -------- | -------- | -------- |
906| PRINT_FILE_CREATED | 0 | 表示打印文件创建成功。 |
907| PRINT_FILE_CREATION_FAILED | 1 | 表示打印文件创建失败。|
908| PRINT_FILE_CREATED_UNRENDERED | 2 | 表示打印文件创建成功但未渲染。 |
909
910## PrinterState<sup>14+</sup>
911
912打印机状态的枚举。
913
914**系统能力:** SystemCapability.Print.PrintFramework
915
916| **名称** | **值** | **说明** |
917| -------- | -------- | -------- |
918| PRINTER_ADDED | 0 | 表示新打印机到达。 |
919| PRINTER_REMOVED | 1 | 表示打印机丢失。 |
920| PRINTER_CAPABILITY_UPDATED | 2 | 表示打印机更新。 |
921| PRINTER_CONNECTED | 3 | 表示打印机已连接。 |
922| PRINTER_DISCONNECTED | 4 | 表示打印机已断开连接。 |
923| PRINTER_RUNNING | 5 | 表示打印机正在运行。 |
924
925## PrintJobState<sup>14+</sup>
926
927打印任务状态的枚举。
928
929**系统能力:** SystemCapability.Print.PrintFramework
930
931| **名称** | **值** | **说明** |
932| -------- | -------- | -------- |
933| PRINT_JOB_PREPARE | 0 | 表示打印任务的初始状态。 |
934| PRINT_JOB_QUEUED | 1 | 表示打印任务传送到打印机。 |
935| PRINT_JOB_RUNNING | 2 | 表示执行打印任务。|
936| PRINT_JOB_BLOCKED | 3 | 表示打印任务已被阻止。 |
937| PRINT_JOB_COMPLETED | 4 | 表示打印任务完成。 |
938
939## PrintJobSubState<sup>14+</sup>
940
941打印任务子状态的枚举。
942
943**系统能力:** SystemCapability.Print.PrintFramework
944
945| **名称** | **值** | **说明** |
946| -------- | -------- | -------- |
947| PRINT_JOB_COMPLETED_SUCCESS | 0 | 表示打印任务成功。 |
948| PRINT_JOB_COMPLETED_FAILED | 1 | 表示打印任务失败。 |
949| PRINT_JOB_COMPLETED_CANCELLED | 2 | 表示打印任务已取消。|
950| PRINT_JOB_COMPLETED_FILE_CORRUPTED | 3 | 表示打印任务已损坏。 |
951| PRINT_JOB_BLOCK_OFFLINE | 4 | 表示打印处于离线状态。 |
952| PRINT_JOB_BLOCK_BUSY | 5 | 表示打印被其他进程占用。 |
953| PRINT_JOB_BLOCK_CANCELLED | 6 | 表示打印任务已取消。 |
954| PRINT_JOB_BLOCK_OUT_OF_PAPER | 7 | 表示打印纸张用完。 |
955| PRINT_JOB_BLOCK_OUT_OF_INK | 8 | 表示打印墨水用完。 |
956| PRINT_JOB_BLOCK_OUT_OF_TONER | 9 | 表示打印墨粉用完。 |
957| PRINT_JOB_BLOCK_JAMMED | 10 | 表示打印卡纸。 |
958| PRINT_JOB_BLOCK_DOOR_OPEN | 11 | 表示打印盖开启。 |
959| PRINT_JOB_BLOCK_SERVICE_REQUEST | 12 | 表示打印服务请求。 |
960| PRINT_JOB_BLOCK_LOW_ON_INK | 13 | 表示打印墨水不足。 |
961| PRINT_JOB_BLOCK_LOW_ON_TONER | 14 | 表示打印墨粉不足。 |
962| PRINT_JOB_BLOCK_REALLY_LOW_ON_INK | 15 | 表示打印墨水量非常低。 |
963| PRINT_JOB_BLOCK_BAD_CERTIFICATE | 16 | 表示打印证书有误。 |
964| PRINT_JOB_BLOCK_DRIVER_EXCEPTION<sup>20+</sup> | 17 | 表示打印驱动异常。 |
965| PRINT_JOB_BLOCK_ACCOUNT_ERROR | 18 | 表示打印账户时出错。 |
966| PRINT_JOB_BLOCK_PRINT_PERMISSION_ERROR | 19 | 表示打印许可异常。 |
967| PRINT_JOB_BLOCK_PRINT_COLOR_PERMISSION_ERROR | 20 | 表示彩色打印权限异常。 |
968| PRINT_JOB_BLOCK_NETWORK_ERROR | 21 | 表示设备未连接到网络。 |
969| PRINT_JOB_BLOCK_SERVER_CONNECTION_ERROR | 22 | 表示无法连接服务器。 |
970| PRINT_JOB_BLOCK_LARGE_FILE_ERROR | 23 | 表示打印大文件异常。 |
971| PRINT_JOB_BLOCK_FILE_PARSING_ERROR | 24 | 表示文件分析异常。 |
972| PRINT_JOB_BLOCK_SLOW_FILE_CONVERSION | 25 | 表示文件转换太慢。 |
973| PRINT_JOB_RUNNING_UPLOADING_FILES | 26 | 表示正在上传文件。 |
974| PRINT_JOB_RUNNING_CONVERTING_FILES | 27 | 表示正在转换文件。 |
975| PRINT_JOB_BLOCK_FILE_UPLOADING_ERROR<sup>18+</sup> | 30 | 表示文件上传失败。 |
976| PRINT_JOB_BLOCK_DRIVER_MISSING<sup>20+</sup> | 34 | 表示打印驱动缺失。 |
977| PRINT_JOB_BLOCK_INTERRUPT<sup>20+</sup> | 35 | 表示打印任务中断。 |
978| PRINT_JOB_BLOCK_PRINTER_UNAVAILABLE<sup>20+</sup> | 98 | 表示打印机不可用。 |
979| PRINT_JOB_BLOCK_UNKNOWN | 99 | 表示打印未知问题。 |
980
981## PrintErrorCode<sup>14+</sup>
982
983打印错误代码的枚举。
984
985**系统能力:** SystemCapability.Print.PrintFramework
986
987| **名称** | **值** | **说明** |
988| -------- | -------- | -------- |
989| E_PRINT_NONE | 0 | 表示没有错误。 |
990| E_PRINT_NO_PERMISSION | 201 | 表示没有许可。 |
991| E_PRINT_INVALID_PARAMETER | 401 | 表示无效的参数。 |
992| E_PRINT_GENERIC_FAILURE | 13100001 | 表示一般打印失败。 |
993| E_PRINT_RPC_FAILURE | 13100002 | 表示RPC失败。 |
994| E_PRINT_SERVER_FAILURE | 13100003 | 表示打印服务失败。 |
995| E_PRINT_INVALID_EXTENSION | 13100004 | 表示打印扩展无效。 |
996| E_PRINT_INVALID_PRINTER | 13100005 | 表示打印机无效。 |
997| E_PRINT_INVALID_PRINT_JOB | 13100006 | 表示打印任务无效。 |
998| E_PRINT_FILE_IO | 13100007 | 表示文件输入/输出错误。 |
999| E_PRINT_TOO_MANY_FILES<sup>18+</sup> | 13100010 | 表示文件数量超过上限,当前上限99个。 |
1000
1001## ApplicationEvent<sup>14+</sup>
1002
1003打印应用事件的枚举。
1004
1005**系统能力:** SystemCapability.Print.PrintFramework
1006
1007| **名称** | **值** | **说明** |
1008| -------- | -------- | -------- |
1009| APPLICATION_CREATED | 0 | 表示打印应用被拉起的事件。 |
1010| APPLICATION_CLOSED_FOR_STARTED | 1 | 表示由于点击打印而关闭打印应用的事件。 |
1011| APPLICATION_CLOSED_FOR_CANCELED | 2 | 表示由于点击取消而关闭打印应用的事件。 |
1012
1013## print.addPrinterToDiscovery<sup>14+</sup>
1014
1015addPrinterToDiscovery(printerInformation: PrinterInformation): Promise&lt;void&gt;
1016
1017添加打印机到系统打印机发现列表,使用Promise异步回调。
1018
1019**需要权限:** ohos.permission.PRINT
1020
1021**系统能力:** SystemCapability.Print.PrintFramework
1022
1023**参数:**
1024| **参数名** | **类型** | **必填** | **说明** |
1025| -------- | -------- | -------- | -------- |
1026| printerInformation | [PrinterInformation](#printerinformation14) | 是 | 表示新发现的打印机。 |
1027
1028**返回值:**
1029| **类型** | **说明** |
1030| -------- | -------- |
1031| Promise&lt;void&gt; | 添加打印机到系统打印机发现列表完成结果。 |
1032
1033**错误码:**
1034
1035以下错误码的详细介绍请参见[打印服务错误码](./errorcode-print.md)。
1036
1037| 错误码ID | 错误信息                                    |
1038| -------- | ------------------------------------------- |
1039| 201 | the application does not have permission to call this function. |
1040| 401 | Parameter error. Possible causes: 1.Mandatory parameters are left unspecified; 2.Incorrect parameter types. |
1041
1042**示例:**
1043
1044```ts
1045import { print } from '@kit.BasicServicesKit';
1046import { BusinessError } from '@ohos.base';
1047
1048let printerInformation : print.PrinterInformation = {
1049    printerId : 'testPrinterId',
1050    printerName : 'testPrinterName',
1051    printerStatus : 0,
1052    description : 'testDesc',
1053    uri : 'testUri',
1054    printerMake : 'testPrinterMake',
1055    options : 'testOps'
1056};
1057print.addPrinterToDiscovery(printerInformation).then(() => {
1058    console.log('addPrinterToDiscovery success');
1059}).catch((error: BusinessError) => {
1060    console.error('addPrinterToDiscovery error : ' + JSON.stringify(error));
1061})
1062```
1063
1064## print.updatePrinterInDiscovery<sup>14+</sup>
1065
1066updatePrinterInDiscovery(printerInformation: PrinterInformation): Promise&lt;void&gt;
1067
1068更新打印机能力到系统打印机发现列表,使用Promise异步回调。
1069
1070**需要权限:** ohos.permission.PRINT
1071
1072**系统能力:** SystemCapability.Print.PrintFramework
1073
1074**参数:**
1075| **参数名** | **类型** | **必填** | **说明** |
1076| -------- | -------- | -------- | -------- |
1077| printerInformation | [PrinterInformation](#printerinformation14) | 是 | 表示待更新能力的打印机。 |
1078
1079**返回值:**
1080| **类型** | **说明** |
1081| -------- | -------- |
1082| Promise&lt;void&gt; | 更新打印机能力到系统打印机发现列表完成结果。 |
1083
1084**错误码:**
1085
1086以下错误码的详细介绍请参见[打印服务错误码](./errorcode-print.md)。
1087
1088| 错误码ID | 错误信息                                    |
1089| -------- | ------------------------------------------- |
1090| 201 | the application does not have permission to call this function. |
1091| 401 | Parameter error. Possible causes: 1.Mandatory parameters are left unspecified; 2.Incorrect parameter types. |
1092
1093**示例:**
1094
1095```ts
1096import { print } from '@kit.BasicServicesKit';
1097import { BusinessError } from '@ohos.base';
1098
1099let testPageSize : print.PrintPageSize = {
1100    id : 'ISO_A4',
1101    name : 'iso_a4_210x297mm',
1102    width : 8268,
1103    height : 11692
1104};
1105
1106let testCapability : print.PrinterCapabilities = {
1107    supportedPageSizes : [testPageSize],
1108    supportedColorModes : [print.PrintColorMode.COLOR_MODE_MONOCHROME],
1109    supportedDuplexModes : [print.PrintDuplexMode.DUPLEX_MODE_NONE],
1110    supportedMediaTypes : ['stationery'],
1111    supportedQualities : [print.PrintQuality.QUALITY_NORMAL],
1112    supportedOrientations : [print.PrintOrientationMode.ORIENTATION_MODE_PORTRAIT],
1113    options : 'testOptions'
1114};
1115
1116let printerInformation : print.PrinterInformation = {
1117    printerId : 'testPrinterId',
1118    printerName : 'testPrinterName',
1119    printerStatus : 0,
1120    description : 'testDesc',
1121    capability : testCapability,
1122    uri : 'testUri',
1123    printerMake : 'testPrinterMake',
1124    options : 'testOptions'
1125};
1126print.updatePrinterInDiscovery(printerInformation).then(() => {
1127    console.log('updatePrinterInDiscovery success');
1128}).catch((error: BusinessError) => {
1129    console.error('updatePrinterInDiscovery error : ' + JSON.stringify(error));
1130})
1131```
1132
1133## print.removePrinterFromDiscovery<sup>14+</sup>
1134
1135removePrinterFromDiscovery(printerId: string): Promise&lt;void&gt;
1136
1137从系统打印机发现列表里移除打印机,使用Promise异步回调。
1138
1139**需要权限:** ohos.permission.PRINT
1140
1141**系统能力:** SystemCapability.Print.PrintFramework
1142
1143**参数:**
1144| **参数名** | **类型** | **必填** | **说明** |
1145| -------- | -------- | -------- | -------- |
1146| printerId | string | 是 | 表示待移除的打印机。 |
1147
1148**返回值:**
1149| **类型** | **说明** |
1150| -------- | -------- |
1151| Promise&lt;void&gt; | 从系统打印机发现列表里移除打印机完成结果。 |
1152
1153**错误码:**
1154
1155以下错误码的详细介绍请参见[打印服务错误码](./errorcode-print.md)。
1156
1157| 错误码ID | 错误信息                                    |
1158| -------- | ------------------------------------------- |
1159| 201 | the application does not have permission to call this function. |
1160| 401 | Parameter error. Possible causes: 1.Mandatory parameters are left unspecified; 2.Incorrect parameter types. |
1161
1162**示例:**
1163
1164```ts
1165import { print } from '@kit.BasicServicesKit';
1166import { BusinessError } from '@ohos.base';
1167
1168let printerId : string = 'testPrinterId';
1169print.removePrinterFromDiscovery(printerId).then(() => {
1170    console.log('removePrinterFromDiscovery success');
1171}).catch((error: BusinessError) => {
1172    console.error('removePrinterFromDiscovery error : ' + JSON.stringify(error));
1173})
1174```
1175
1176## print.getPrinterInformationById<sup>14+</sup>
1177
1178getPrinterInformationById(printerId: string): Promise&lt;PrinterInformation&gt;
1179
1180根据打印机id获取打印机信息,使用Promise异步回调。
1181
1182**需要权限:** ohos.permission.PRINT
1183
1184**系统能力:** SystemCapability.Print.PrintFramework
1185
1186**参数:**
1187| **参数名** | **类型** | **必填** | **说明** |
1188| -------- | -------- | -------- | -------- |
1189| printerId | string | 是 | 表示待获取信息的打印机id。 |
1190
1191**返回值:**
1192| **类型** | **说明** |
1193| -------- | -------- |
1194| Promise&lt;[PrinterInformation](#printerinformation14)&gt; | 根据打印机id获取的对应打印机信息。 |
1195
1196**错误码:**
1197
1198以下错误码的详细介绍请参见[打印服务错误码](./errorcode-print.md)。
1199
1200| 错误码ID | 错误信息                                    |
1201| -------- | ------------------------------------------- |
1202| 201 | the application does not have permission to call this function. |
1203| 401 | Parameter error. Possible causes: 1.Mandatory parameters are left unspecified; 2.Incorrect parameter types. |
1204
1205**示例:**
1206
1207```ts
1208import { print } from '@kit.BasicServicesKit';
1209import { BusinessError } from '@ohos.base';
1210
1211let printerId : string = 'testPrinterId';
1212print.getPrinterInformationById(printerId).then((printerInformation : print.PrinterInformation) => {
1213    console.log('getPrinterInformationById data : ' + JSON.stringify(printerInformation));
1214}).catch((error: BusinessError) => {
1215    console.error('getPrinterInformationById error : ' + JSON.stringify(error));
1216})
1217```
1218
1219## PrinterInformation<sup>14+</sup>
1220
1221定义打印机信息的接口。
1222
1223**系统能力:** SystemCapability.Print.PrintFramework
1224
1225**属性:**
1226| **名称** | **类型** | **必填** | **说明** |
1227| -------- | -------- | -------- | -------- |
1228| printerId | string | 是 | 表示打印机ID。 |
1229| printerName | string | 是 | 表示打印机名称。 |
1230| printerStatus | [PrinterStatus](#printerstatus14) | 是 | 表示当前打印机状态。 |
1231| description | string | 否 | 表示打印机说明。 |
1232| capability | [PrinterCapabilities](#printercapabilities14) | 否 | 表示打印机能力。 |
1233| uri | string | 否 | 表示打印机uri。 |
1234| printerMake | string | 否 | 表示打印机型号。 |
1235| preferences<sup>18+</sup> | [PrinterPreferences](#printerpreferences18) | 否 | 表示打印机首选项。 |
1236| alias<sup>18+</sup> | string | 否 | 表示打印机别名。 |
1237| options | string | 否 | 表示打印机详细信息。 |
1238
1239## PrinterCapabilities<sup>14+</sup>
1240
1241定义打印机能力的接口。
1242
1243**系统能力:** SystemCapability.Print.PrintFramework
1244
1245**属性:**
1246| **名称** | **类型** | **必填** | **说明** |
1247| -------- | -------- | -------- | -------- |
1248| supportedPageSizes | Array&lt;[PrintPageSize](#printpagesize11)&gt; | 是 | 表示打印机支持的纸张尺寸列表。 |
1249| supportedColorModes | Array&lt;[PrintColorMode](#printcolormode11)&gt; | 是 | 表示打印机支持的色彩模式列表。 |
1250| supportedDuplexModes | Array&lt;[PrintDuplexMode](#printduplexmode11)&gt; | 是 | 表示打印机支持的单双面模式列表。 |
1251| supportedMediaTypes | Array&lt;string&gt; | 否 | 表示打印机支持的纸张类型列表。 |
1252| supportedQualities | Array&lt;[PrintQuality](#printquality14)&gt; | 否 | 表示打印机支持的打印质量列表。 |
1253| supportedOrientations | Array&lt;[PrintOrientationMode](#printorientationmode14)&gt; | 否 | 表示打印机支持的打印方向列表。 |
1254| options | string | 否 | 表示打印机能力详细信息。 |
1255
1256## PrintQuality<sup>14+</sup>
1257
1258打印质量的枚举。
1259
1260**系统能力:** SystemCapability.Print.PrintFramework
1261
1262| **名称** | **值** | **说明** |
1263| -------- | -------- | -------- |
1264| QUALITY_DRAFT | 3 | 表示经济的打印质量。 |
1265| QUALITY_NORMAL | 4 | 表示标准的打印质量。 |
1266| QUALITY_HIGH | 5 | 表示最佳的打印质量。 |
1267
1268## PrintOrientationMode<sup>14+</sup>
1269
1270打印方向的枚举。
1271
1272**系统能力:** SystemCapability.Print.PrintFramework
1273
1274| **名称** | **值** | **说明** |
1275| -------- | -------- | -------- |
1276| ORIENTATION_MODE_PORTRAIT | 0 | 表示纵向打印。 |
1277| ORIENTATION_MODE_LANDSCAPE | 1 | 表示横向打印。 |
1278| ORIENTATION_MODE_REVERSE_LANDSCAPE | 2 | 表示横向翻转打印。 |
1279| ORIENTATION_MODE_REVERSE_PORTRAIT | 3 | 表示纵向翻转打印。 |
1280| ORIENTATION_MODE_NONE | 4 | 表示自适应方向打印。 |
1281
1282## PrinterStatus<sup>14+</sup>
1283
1284打印机状态的枚举。
1285
1286**系统能力:** SystemCapability.Print.PrintFramework
1287
1288| **名称** | **值** | **说明** |
1289| -------- | -------- | -------- |
1290| PRINTER_IDLE | 0 | 表示打印机空闲状态。 |
1291| PRINTER_BUSY | 1 | 表示打印机忙碌状态。 |
1292| PRINTER_UNAVAILABLE | 2 | 表示打印机脱机状态。 |
1293
1294## PrinterPreferences<sup>18+</sup>
1295
1296定义打印机首选项的接口。
1297
1298**系统能力:** SystemCapability.Print.PrintFramework
1299
1300**属性:**
1301| **名称** | **类型** | **必填** | **说明** |
1302| -------- | -------- | -------- | -------- |
1303| defaultDuplexMode | [PrintDuplexMode](#printduplexmode11) | 否 | 表示默认单双面模式。 |
1304| defaultPrintQuality | [PrintQuality](#printquality14) | 否 | 表示默认打印质量。 |
1305| defaultMediaType | string | 否 | 表示默认纸张类型。 |
1306| defaultPageSizeId | string | 否 | 表示默认纸张尺寸的ID,其范围包含国际标准化组织定义的标准纸张尺寸,如ISO_A4,和系统中定义的非标准的纸张尺寸,如Custom.178x254mm,表示这种纸张尺寸为178毫米 x 254毫米。 |
1307| defaultOrientation | [PrintOrientationMode](#printorientationmode14) | 否 | 表示默认打印方向。 |
1308| borderless | boolean | 否 | 表示是否无边距打印,true表示无边距,false表示有边距。默认值为false。 |
1309| options | string | 否 | 表示打印机首选项中不在以上字段中的其他字段,查询打印机或者从打印机驱动获取,以json格式存储在string中。 |
1310
1311## PrinterEvent<sup>18+</sup>
1312
1313打印机相关事件的枚举。
1314
1315**系统能力:** SystemCapability.Print.PrintFramework
1316
1317| **名称** | **值** | **说明** |
1318| -------- | -------- | -------- |
1319| PRINTER_EVENT_ADDED | 0 | 表示打印机添加事件。 |
1320| PRINTER_EVENT_DELETED | 1 | 表示打印机删除事件。 |
1321| PRINTER_EVENT_STATE_CHANGED | 2 | 表示打印机状态变化事件。 |
1322| PRINTER_EVENT_INFO_CHANGED | 3 | 表示打印机信息变化事件。 |
1323| PRINTER_EVENT_PREFERENCE_CHANGED | 4 | 表示打印机首选项变化事件。 |
1324| PRINTER_EVENT_LAST_USED_PRINTER_CHANGED | 5 | 表示上次使用的打印机的变化事件。 |
1325
1326## DefaultPrinterType<sup>18+</sup>
1327
1328默认打印类型的枚举。
1329
1330**系统能力:** SystemCapability.Print.PrintFramework
1331
1332| **名称** | **值** | **说明** |
1333| -------- | -------- | -------- |
1334| DEFAULT_PRINTER_TYPE_SET_BY_USER | 0 | 表示将用户手动设置的默认打印机作为当前默认打印机。 |
1335| DEFAULT_PRINTER_TYPE_LAST_USED_PRINTER | 1 | 表示自动将上次使用的打印机作为当前默认打印机。 |
1336
1337## print.getAddedPrinters<sup>18+</sup>
1338
1339getAddedPrinters(): Promise&lt;Array&lt;string&gt;&gt;
1340
1341获取系统中已添加的打印机列表,使用Promise异步回调。
1342
1343**需要权限:** ohos.permission.MANAGE_PRINT_JOB or ohos.permission.PRINT
1344
1345**系统能力:** SystemCapability.Print.PrintFramework
1346
1347**返回值:**
1348| **类型** | **说明** |
1349| -------- | -------- |
1350| Promise&lt;Array&lt;string&gt;&gt; | 获取系统中已添加的打印机列表的完成结果回调。 |
1351
1352**错误码:**
1353
1354以下错误码的详细介绍请参见[打印服务错误码](./errorcode-print.md)。
1355
1356| 错误码ID | 错误信息                                    |
1357| -------- | ------------------------------------------- |
1358| 201 | the application does not have permission to call this function. |
1359
1360**示例:**
1361
1362```ts
1363import { print } from '@kit.BasicServicesKit';
1364import { BusinessError } from '@ohos.base';
1365
1366print.getAddedPrinters().then((printers: string[]) => {
1367    console.log('getAddedPrinters success ' + JSON.stringify(printers));
1368    // ...
1369}).catch((error: BusinessError) => {
1370    console.error('failed to getAddedPrinters because ' + JSON.stringify(error));
1371})
1372```
1373
1374## PrinterChangeCallback<sup>18+</sup>
1375
1376type PrinterChangeCallback = (event: PrinterEvent, printerInformation: PrinterInformation) => void
1377
1378将打印机事件和打印机信息作为参数的回调方法。
1379
1380**系统能力:** SystemCapability.Print.PrintFramework
1381
1382**参数:**
1383| **参数名** | **类型** | **必填** | **说明** |
1384| -------- | -------- | -------- | -------- |
1385| event | [PrinterEvent](#printerevent18) | 是 | 表示打印机事件。 |
1386| printerInformation | PrinterInformation | 是 | 表示打印机信息。 |
1387
1388## print.on<sup>18+</sup>
1389
1390on(type: 'printerChange', callback: PrinterChangeCallback): void
1391
1392注册打印机变动事件回调,使用callback回调。
1393
1394**需要权限:** ohos.permission.PRINT
1395
1396**系统能力:** SystemCapability.Print.PrintFramework
1397
1398**参数:**
1399| **参数名** | **类型** | **必填** | **说明** |
1400| -------- | -------- | -------- | -------- |
1401| type | 'printerChange' | 是 | 表示打印机变动事件。 |
1402| callback | [PrinterChangeCallback](#printerchangecallback18) | 是 | 打印机变动之后的回调。 |
1403
1404**错误码:**
1405
1406以下错误码的详细介绍请参见[打印服务错误码](./errorcode-print.md)。
1407
1408| 错误码ID | 错误信息                                    |
1409| -------- | ------------------------------------------- |
1410| 201 | the application does not have permission to call this function. |
1411| 401 | Parameter error. Possible causes: 1.Mandatory parameters are left unspecified; 2.Incorrect parameter types. |
1412
1413**示例:**
1414
1415```ts
1416import { print } from '@kit.BasicServicesKit';
1417
1418// Trigger this callback when a added printer is changed.
1419let onPrinterChange =
1420    (event: print.PrinterEvent, printerInformation: print.PrinterInformation) => {
1421        console.log('printerChange, event: ' + event + ', printerInformation: ' + JSON.stringify(printerInformation));
1422    };
1423print.on('printerChange', onPrinterChange);
1424```
1425
1426## print.off<sup>18+</sup>
1427
1428off(type: 'printerChange', callback?: PrinterChangeCallback): void
1429
1430取消注册打印机变动事件回调,使用callback回调。
1431
1432**需要权限:** ohos.permission.PRINT
1433
1434**系统能力:** SystemCapability.Print.PrintFramework
1435
1436**参数:**
1437| **参数名** | **类型** | **必填** | **说明** |
1438| -------- | -------- | -------- | -------- |
1439| type | 'printerChange' | 是 | 表示打印机变动事件。 |
1440| callback | [PrinterChangeCallback](#printerchangecallback18) | 否 | 表示取消注册打印机变动事件后的回调。 |
1441
1442**错误码:**
1443
1444以下错误码的详细介绍请参见[打印服务错误码](./errorcode-print.md)。
1445
1446| 错误码ID | 错误信息                                    |
1447| -------- | ------------------------------------------- |
1448| 201 | the application does not have permission to call this function. |
1449| 401 | Parameter error. Possible causes: 1.Mandatory parameters are left unspecified; 2.Incorrect parameter types. |
1450
1451**示例:**
1452
1453```ts
1454import { print } from '@kit.BasicServicesKit';
1455
1456// Trigger this callback when a added printer is changed.
1457let onPrinterChange =
1458    (event: print.PrinterEvent, printerInformation: print.PrinterInformation) => {
1459        console.log('printerChange, event: ' + event + ', printerInformation: ' + JSON.stringify(printerInformation));
1460    };
1461print.on('printerChange', onPrinterChange);
1462print.off('printerChange');
1463```
1464
1465## print.startDiscoverPrinter<sup>20+</sup>
1466
1467startDiscoverPrinter(extensionList: Array&lt;string&gt;, callback: AsyncCallback&lt;void&gt;): void
1468
1469通过指定“打印扩展能力列表”来发现打印机,发现的打印机具备包含指定的打印扩展能力。如果指定空的打印扩展能力列表,则表示加载所有扩展能力。使用callback异步回调。
1470
1471**需要权限:** ohos.permission.MANAGE_PRINT_JOBohos.permission.PRINT
1472
1473**系统能力:** SystemCapability.Print.PrintFramework
1474
1475**参数:**
1476| **参数名** | **类型** | **必填** | **说明** |
1477| -------- | -------- | -------- | -------- |
1478| extensionList | Array&lt;string&gt; | 是 | 要加载的[打印扩展能力](./js-apis-app-ability-PrintExtensionAbility.md)列表,列表成员为打印扩展能力的包名,空列表表示加载所有扩展能力。 |
1479| callback | AsyncCallback&lt;void&gt; | 是 | 异步开始发现打印机之后的回调。 |
1480
1481**错误码:**
1482
1483以下错误码的详细介绍请参见[打印服务错误码](./errorcode-print.md)。
1484
1485| 错误码ID | 错误信息                                    |
1486| -------- | ------------------------------------------- |
1487| 201 | the application does not have permission to call this function. |
1488
1489**示例:**
1490
1491```ts
1492import { print } from '@kit.BasicServicesKit';
1493import { BusinessError } from '@ohos.base';
1494
1495// 加载所有打印扩展能力
1496let extensionList: string[] = [];
1497// 通过指定自己应用的包名,在发现时加载自己的打印扩展能力
1498// let extensionList: string[] = ['com.myapplication.test'];
1499print.startDiscoverPrinter(extensionList, (err: BusinessError) => {
1500    if (err) {
1501        console.error('failed to start Discover Printer because : ' + JSON.stringify(err));
1502    } else {
1503        console.log('start Discover Printer success');
1504    }
1505})
1506```
1507
1508## print.startDiscoverPrinter<sup>20+</sup>
1509
1510startDiscoverPrinter(extensionList: Array&lt;string&gt;): Promise&lt;void&gt;
1511
1512通过指定“打印扩展能力列表”来发现打印机,发现的打印机具备包含指定的打印扩展能力。如果指定空的打印扩展能力列表,则表示加载所有扩展能力,使用Promise异步回调。
1513
1514**需要权限:** ohos.permission.MANAGE_PRINT_JOBohos.permission.PRINT
1515
1516**系统能力:** SystemCapability.Print.PrintFramework
1517
1518**参数:**
1519| **参数名** | **类型** | **必填** | **说明** |
1520| -------- | -------- | -------- | -------- |
1521| extensionList | Array&lt;string&gt; | 是 | 要加载的[打印扩展能力](./js-apis-app-ability-PrintExtensionAbility.md)列表,列表成员为打印扩展能力的包名,空列表表示加载所有扩展能力。 |
1522
1523**返回值:**
1524| **类型** | **说明** |
1525| -------- | -------- |
1526| Promise&lt;void&gt; | 开始发现打印机的完成结果。 |
1527
1528**错误码:**
1529
1530以下错误码的详细介绍请参见[打印服务错误码](./errorcode-print.md)。
1531
1532| 错误码ID | 错误信息                                    |
1533| -------- | ------------------------------------------- |
1534| 201 | the application does not have permission to call this function. |
1535
1536**示例:**
1537
1538```ts
1539import { print } from '@kit.BasicServicesKit';
1540import { BusinessError } from '@ohos.base';
1541
1542// 加载所有打印扩展能力
1543let extensionList: string[] = [];
1544// 通过指定自己应用的包名,在发现时加载自己的打印扩展能力
1545// let extensionList: string[] = ['com.myapplication.test'];
1546print.startDiscoverPrinter(extensionList).then(() => {
1547    console.log('start Discovery success');
1548}).catch((error: BusinessError) => {
1549    console.error('failed to start Discovery because : ' + JSON.stringify(error));
1550})
1551```
1552
1553## print.stopDiscoverPrinter<sup>20+</sup>
1554
1555stopDiscoverPrinter(callback: AsyncCallback&lt;void&gt;): void
1556
1557停止发现打印机,使用callback异步回调。
1558
1559**需要权限:** ohos.permission.MANAGE_PRINT_JOBohos.permission.PRINT
1560
1561**系统能力:** SystemCapability.Print.PrintFramework
1562
1563**参数:**
1564| **参数名** | **类型** | **必填** | **说明** |
1565| -------- | -------- | -------- | -------- |
1566| callback | AsyncCallback&lt;void&gt; | 是 | 停止发现打印机的异步回调。 |
1567
1568**错误码:**
1569
1570以下错误码的详细介绍请参见[打印服务错误码](./errorcode-print.md)。
1571
1572| 错误码ID | 错误信息                                    |
1573| -------- | ------------------------------------------- |
1574| 201 | the application does not have permission to call this function. |
1575
1576**示例:**
1577
1578```ts
1579import { print } from '@kit.BasicServicesKit';
1580import { BusinessError } from '@ohos.base';
1581
1582print.stopDiscoverPrinter((err: BusinessError) => {
1583    if (err) {
1584        console.error('failed to stop Discover Printer because : ' + JSON.stringify(err));
1585    } else {
1586        console.log('stop Discover Printer success');
1587    }
1588})
1589```
1590
1591## print.stopDiscoverPrinter<sup>20+</sup>
1592
1593stopDiscoverPrinter(): Promise&lt;void&gt;
1594
1595停止发现打印机,使用Promise异步回调。
1596
1597**需要权限:** ohos.permission.MANAGE_PRINT_JOBohos.permission.PRINT
1598
1599**系统能力:** SystemCapability.Print.PrintFramework
1600
1601**返回值:**
1602| **类型** | **说明** |
1603| -------- | -------- |
1604| Promise&lt;void&gt; | 停止发现打印机的完成结果。 |
1605
1606**错误码:**
1607
1608以下错误码的详细介绍请参见[打印服务错误码](./errorcode-print.md)。
1609
1610| 错误码ID | 错误信息                                    |
1611| -------- | ------------------------------------------- |
1612| 201 | the application does not have permission to call this function. |
1613
1614**示例:**
1615
1616```ts
1617import { print } from '@kit.BasicServicesKit';
1618import { BusinessError } from '@ohos.base';
1619
1620print.stopDiscoverPrinter().then(() => {
1621    console.log('stop Discovery success');
1622}).catch((error: BusinessError) => {
1623    console.error('failed to stop Discovery because : ' + JSON.stringify(error));
1624})
1625```
1626
1627## print.connectPrinter<sup>20+</sup>
1628
1629connectPrinter(printerId: string, callback: AsyncCallback&lt;void&gt;): void
1630
1631通过打印机ID连接打印机,使用callback异步回调。
1632
1633**需要权限:** ohos.permission.MANAGE_PRINT_JOBohos.permission.PRINT
1634
1635**系统能力:** SystemCapability.Print.PrintFramework
1636
1637**参数:**
1638| **参数名** | **类型** | **必填** | **说明** |
1639| -------- | -------- | -------- | -------- |
1640| printerId | string | 是 | 打印机ID。 |
1641| callback | AsyncCallback&lt;void&gt; | 是 | 通过打印机ID异步连接打印机的回调。 |
1642
1643**错误码:**
1644
1645以下错误码的详细介绍请参见[打印服务错误码](./errorcode-print.md)。
1646
1647| 错误码ID | 错误信息                                    |
1648| -------- | ------------------------------------------- |
1649| 201 | the application does not have permission to call this function. |
1650
1651**示例:**
1652
1653```ts
1654import { print } from '@kit.BasicServicesKit';
1655import { BusinessError } from '@ohos.base';
1656
1657let printerId: string = 'printerId_32';
1658print.connectPrinter(printerId, (err: BusinessError) => {
1659    if (err) {
1660        console.error('failed to connect Printer because : ' + JSON.stringify(err));
1661    } else {
1662        console.log('start connect Printer success');
1663    }
1664})
1665```
1666
1667## print.connectPrinter<sup>20+</sup>
1668
1669connectPrinter(printerId: string): Promise&lt;void&gt;
1670
1671通过打印机ID连接打印机,使用Promise异步回调。
1672
1673**需要权限:** ohos.permission.MANAGE_PRINT_JOBohos.permission.PRINT
1674
1675**系统能力:** SystemCapability.Print.PrintFramework
1676
1677**参数:**
1678| **参数名** | **类型** | **必填** | **说明** |
1679| -------- | -------- | -------- | -------- |
1680| printerId | string | 是 | 打印机ID |
1681
1682**返回值:**
1683| **类型** | **说明** |
1684| -------- | -------- |
1685| Promise&lt;void&gt; |通过打印机ID连接打印机完成结果。 |
1686
1687**错误码:**
1688
1689以下错误码的详细介绍请参见[打印服务错误码](./errorcode-print.md)。
1690
1691| 错误码ID | 错误信息                                    |
1692| -------- | ------------------------------------------- |
1693| 201 | the application does not have permission to call this function. |
1694
1695**示例:**
1696
1697```ts
1698import { print } from '@kit.BasicServicesKit';
1699import { BusinessError } from '@ohos.base';
1700
1701let printerId: string = 'printerId_32';
1702print.connectPrinter(printerId).then(() => {
1703    console.log('start connect Printer success');
1704}).catch((error: BusinessError) => {
1705    console.error('failed to connect Printer because : ' + JSON.stringify(error));
1706})
1707```
1708