1# @ohos.print (打印) 2 3该模块为基本打印的操作API,提供调用基础打印功能的接口。 4 5> **说明:** 6> 本模块首批接口从API version 10开始支持。后续版本的新增接口,采用上角标单独标记接口的起始版本。 7 8## 导入模块 9 10```ts 11import { print } from '@kit.BasicServicesKit'; 12``` 13 14## PrintTask 15 16打印任务完成后的事件监听回调接口类。 17 18### on 19 20on(type: 'block', callback: Callback<void>): void 21 22注册打印完成后的监听,使用callback回调。 23 24**需要权限:** ohos.permission.PRINT 25 26**系统能力:** SystemCapability.Print.PrintFramework 27 28**参数:** 29| **参数名** | **类型** | **必填** | **说明** | 30| -------- | -------- | -------- | -------- | 31| type | string | 是 | 注册监听,<br/>监听字段:block,<br/>表示打印阻塞 | 32| callback | Callback<void> | 是 | 打印完成后处于响应状态的回调 | 33 34**错误码:** 35 36以下错误码的详细介绍请参见[打印服务错误码](./errorcode-print.md)。 37 38| 错误码ID | 错误信息 | 39| -------- | ------------------------------------------- | 40| 201 | the application does not have permission to call this function. | 41| 401 | Parameter error. Possible causes: 1.Mandatory parameters are left unspecified; 2.Incorrect parameter types. | 42 43**示例:** 44 45```ts 46import { print } from '@kit.BasicServicesKit'; 47import { BusinessError } from '@ohos.base'; 48 49let file = ['file://data/print/a.png', 'file://data/print/b.png']; 50print.print(file).then((printTask: print.PrintTask) => { 51 printTask.on('block', () => { 52 console.log('print state is block'); 53 }) 54 // ... 55}).catch((error: BusinessError) => { 56 console.log('print err ' + JSON.stringify(error)); 57}) 58``` 59 60### on 61 62on(type: 'succeed', callback: Callback<void>): void 63 64注册打印完成后的监听,使用callback回调。 65 66**需要权限:** ohos.permission.PRINT 67 68**系统能力:** SystemCapability.Print.PrintFramework 69 70**参数:** 71| **参数名** | **类型** | **必填** | **说明** | 72| -------- | -------- | -------- | -------- | 73| type | string | 是 | 注册监听,<br/>监听字段:succeed,<br/>表示打印成功 | 74| callback | Callback<void> | 是 | 打印完成后处于响应状态的回调 | 75 76**错误码:** 77 78以下错误码的详细介绍请参见[打印服务错误码](./errorcode-print.md)。 79 80| 错误码ID | 错误信息 | 81| -------- | ------------------------------------------- | 82| 201 | the application does not have permission to call this function. | 83| 401 | Parameter error. Possible causes: 1.Mandatory parameters are left unspecified; 2.Incorrect parameter types. | 84 85**示例:** 86 87```ts 88import { print } from '@kit.BasicServicesKit'; 89import { BusinessError } from '@ohos.base'; 90 91let file = ['file://data/print/a.png', 'file://data/print/b.png']; 92print.print(file).then((printTask: print.PrintTask) => { 93 printTask.on('succeed', () => { 94 console.log('print state is succeed'); 95 }) 96 // ... 97}).catch((error: BusinessError) => { 98 console.log('print err ' + JSON.stringify(error)); 99}) 100``` 101 102### on 103 104on(type: 'fail', callback: Callback<void>): void 105 106注册打印完成后的监听,使用callback回调。 107 108**需要权限:** ohos.permission.PRINT 109 110**系统能力:** SystemCapability.Print.PrintFramework 111 112**参数:** 113| **参数名** | **类型** | **必填** | **说明** | 114| -------- | -------- | -------- | -------- | 115| type | string | 是 | 注册监听,<br/>监听字段:fail,<br/>表示打印失败 | 116| callback | Callback<void> | 是 | 打印完成后处于响应状态的回调 | 117 118**错误码:** 119 120以下错误码的详细介绍请参见[打印服务错误码](./errorcode-print.md)。 121 122| 错误码ID | 错误信息 | 123| -------- | ------------------------------------------- | 124| 201 | the application does not have permission to call this function. | 125| 401 | Parameter error. Possible causes: 1.Mandatory parameters are left unspecified; 2.Incorrect parameter types. | 126 127**示例:** 128 129```ts 130import { print } from '@kit.BasicServicesKit'; 131import { BusinessError } from '@ohos.base'; 132 133let file = ['file://data/print/a.png', 'file://data/print/b.png']; 134print.print(file).then((printTask: print.PrintTask) => { 135 printTask.on('fail', () => { 136 console.log('print state is fail'); 137 }) 138 // ... 139}).catch((error: BusinessError) => { 140 console.log('print err ' + JSON.stringify(error)); 141}) 142``` 143 144### on 145 146on(type: 'cancel', callback: Callback<void>): void 147 148注册打印完成后的监听,使用callback回调。 149 150**需要权限:** ohos.permission.PRINT 151 152**系统能力:** SystemCapability.Print.PrintFramework 153 154**参数:** 155| **参数名** | **类型** | **必填** | **说明** | 156| -------- | -------- | -------- | -------- | 157| type | string | 是 | 注册监听,<br/>监听字段:cancel,<br/>表示打印取消 | 158| callback | Callback<void> | 是 | 打印完成后处于响应状态的回调 | 159 160**错误码:** 161 162以下错误码的详细介绍请参见[打印服务错误码](./errorcode-print.md)。 163 164| 错误码ID | 错误信息 | 165| -------- | ------------------------------------------- | 166| 201 | the application does not have permission to call this function. | 167| 401 | Parameter error. Possible causes: 1.Mandatory parameters are left unspecified; 2.Incorrect parameter types. | 168 169**示例:** 170 171```ts 172import { print } from '@kit.BasicServicesKit'; 173import { BusinessError } from '@ohos.base'; 174 175let file = ['file://data/print/a.png', 'file://data/print/b.png']; 176print.print(file).then((printTask: print.PrintTask) => { 177 printTask.on('cancel', () => { 178 console.log('print state is cancel'); 179 }) 180 // ... 181}).catch((error: BusinessError) => { 182 console.log('print err ' + JSON.stringify(error)); 183}) 184``` 185 186### off 187 188off(type: 'block', callback?: Callback<void>): void 189 190取消打印完成后的监听,使用callback回调。 191 192**需要权限:** ohos.permission.PRINT 193 194**系统能力:** SystemCapability.Print.PrintFramework 195 196**参数:** 197| **参数名** | **类型** | **必填** | **说明** | 198| -------- | -------- | -------- | -------- | 199| type | string | 是 | 取消监听,<br/>监听字段:block,<br/>表示打印阻塞 | 200| callback | Callback<void> | 否 | 取消相应状态监听成功后的回调 | 201 202**错误码:** 203 204以下错误码的详细介绍请参见[打印服务错误码](./errorcode-print.md)。 205 206| 错误码ID | 错误信息 | 207| -------- | ------------------------------------------- | 208| 201 | the application does not have permission to call this function. | 209| 401 | Parameter error. Possible causes: 1.Mandatory parameters are left unspecified; 2.Incorrect parameter types. | 210 211**示例:** 212 213```ts 214import { print } from '@kit.BasicServicesKit'; 215import { BusinessError } from '@ohos.base'; 216 217let file = ['file://data/print/a.png', 'file://data/print/b.png']; 218print.print(file).then((printTask: print.PrintTask) => { 219 printTask.off('block', () => { 220 console.log('unregister state block'); 221 }) 222 // ... 223}).catch((error: BusinessError) => { 224 console.log('print err ' + JSON.stringify(error)); 225}) 226``` 227 228### off 229 230off(type: 'succeed', callback?: Callback<void>): void 231 232取消打印完成后的监听,使用callback回调。 233 234**需要权限:** ohos.permission.PRINT 235 236**系统能力:** SystemCapability.Print.PrintFramework 237 238**参数:** 239| **参数名** | **类型** | **必填** | **说明** | 240| -------- | -------- | -------- | -------- | 241| type | string | 是 | 取消监听,<br/>监听字段:succeed,<br/>表示打印成功 | 242| callback | Callback<void> | 否 | 取消相应状态监听成功后的回调 | 243 244**错误码:** 245 246以下错误码的详细介绍请参见[打印服务错误码](./errorcode-print.md)。 247 248| 错误码ID | 错误信息 | 249| -------- | ------------------------------------------- | 250| 201 | the application does not have permission to call this function. | 251| 401 | Parameter error. Possible causes: 1.Mandatory parameters are left unspecified; 2.Incorrect parameter types. | 252 253**示例:** 254 255```ts 256import { print } from '@kit.BasicServicesKit'; 257import { BusinessError } from '@ohos.base'; 258 259let file = ['file://data/print/a.png', 'file://data/print/b.png']; 260print.print(file).then((printTask: print.PrintTask) => { 261 printTask.off('succeed', () => { 262 console.log('unregister state succeed'); 263 }) 264 // ... 265}).catch((error: BusinessError) => { 266 console.log('print err ' + JSON.stringify(error)); 267}) 268``` 269 270### off 271 272off(type: 'fail', callback?: Callback<void>): void 273 274取消打印完成后的监听,使用callback回调。 275 276**需要权限:** ohos.permission.PRINT 277 278**系统能力:** SystemCapability.Print.PrintFramework 279 280**参数:** 281| **参数名** | **类型** | **必填** | **说明** | 282| -------- | -------- | -------- | -------- | 283| type | string | 是 | 取消监听,<br/>监听字段:fail,<br/>表示打印失败 | 284| callback | Callback<void> | 否 | 取消相应状态监听成功后的回调 | 285 286**错误码:** 287 288以下错误码的详细介绍请参见[打印服务错误码](./errorcode-print.md)。 289 290| 错误码ID | 错误信息 | 291| -------- | ------------------------------------------- | 292| 201 | the application does not have permission to call this function. | 293| 401 | Parameter error. Possible causes: 1.Mandatory parameters are left unspecified; 2.Incorrect parameter types. | 294 295**示例:** 296 297```ts 298import { print } from '@kit.BasicServicesKit'; 299import { BusinessError } from '@ohos.base'; 300 301let file = ['file://data/print/a.png', 'file://data/print/b.png']; 302print.print(file).then((printTask: print.PrintTask) => { 303 printTask.off('fail', () => { 304 console.log('unregister state fail'); 305 }) 306 // ... 307}).catch((error: BusinessError) => { 308 console.log('print err ' + JSON.stringify(error)); 309}) 310``` 311 312### off 313 314off(type: 'cancel', callback?: Callback<void>): void 315 316取消打印完成后的监听,使用callback回调。 317 318**需要权限:** ohos.permission.PRINT 319 320**系统能力:** SystemCapability.Print.PrintFramework 321 322**参数:** 323| **参数名** | **类型** | **必填** | **说明** | 324| -------- | -------- | -------- | -------- | 325| type | string | 是 | 取消监听,<br/>监听字段:cancel,<br/>表示打印取消 | 326| callback | Callback<void> | 否 | 取消相应状态监听成功后的回调 | 327 328**错误码:** 329 330以下错误码的详细介绍请参见[打印服务错误码](./errorcode-print.md)。 331 332| 错误码ID | 错误信息 | 333| -------- | ------------------------------------------- | 334| 201 | the application does not have permission to call this function. | 335| 401 | Parameter error. Possible causes: 1.Mandatory parameters are left unspecified; 2.Incorrect parameter types. | 336 337**示例:** 338 339```ts 340import { print } from '@kit.BasicServicesKit'; 341import { BusinessError } from '@ohos.base'; 342 343let file = ['file://data/print/a.png', 'file://data/print/b.png']; 344print.print(file).then((printTask: print.PrintTask) => { 345 printTask.off('cancel', () => { 346 console.log('unregister state cancel'); 347 }) 348 // ... 349}).catch((error: BusinessError) => { 350 console.log('print err ' + JSON.stringify(error)); 351}) 352``` 353 354## PrintDocumentAdapter<sup>11+</sup> 355 356第三方应用程序实现此接口来渲染要打印的文件。 357 358### onStartLayoutWrite 359 360onStartLayoutWrite(jobId: string, oldAttrs: PrintAttributes, newAttrs: PrintAttributes, fd: number, writeResultCallback: (jobId: string, writeResult: PrintFileCreationState) => void): void 361 362打印服务会通过本接口将一个空的pdf文件的文件描述符传给三方应用,由三方应用使用新的打印参数更新待打印文件,更新文件完成后通过本接口的回调方法writeResultCallback通知打印服务。 363 364**需要权限:** ohos.permission.PRINT 365 366**系统能力:** SystemCapability.Print.PrintFramework 367 368**参数:** 369| **参数名** | **类型** | **必填** | **说明** | 370| -------- | -------- | -------- | -------- | 371| jobId | string | 是 | 表示打印任务ID | 372| oldAttrs | PrintAttributes | 是 | 表示旧打印参数 | 373| newAttrs | PrintAttributes | 是 | 表示新打印参数 | 374| fd | number | 是 | 表示打印文件传给接口调用方的pdf文件的文件描述符。 | 375| writeResultCallback | (jobId: string, writeResult: PrintFileCreationState) | 是 | 表示三方应用使用新的打印参数更新待打印文件完成后的回调 | 376 377**错误码:** 378 379以下错误码的详细介绍请参见[打印服务错误码](./errorcode-print.md)。 380 381| 错误码ID | 错误信息 | 382| -------- | ------------------------------------------- | 383| 201 | the application does not have permission to call this function. | 384| 401 | Parameter error. Possible causes: 1.Mandatory parameters are left unspecified; 2.Incorrect parameter types. | 385 386**示例:** 387 388```ts 389import { print } from '@kit.BasicServicesKit'; 390import { BusinessError } from '@ohos.base'; 391 392class MyPrintDocumentAdapter implements print.PrintDocumentAdapter { 393 onStartLayoutWrite(jobId: string, oldAttrs: print.PrintAttributes, newAttrs: print.PrintAttributes, fd: number, 394 writeResultCallback: (jobId: string, writeResult: print.PrintFileCreationState) => void) { 395 writeResultCallback(jobId, print.PrintFileCreationState.PRINT_FILE_CREATED); 396 }; 397 onJobStateChanged(jobId: string, state: print.PrintDocumentAdapterState) { 398 if (state == print.PrintDocumentAdapterState.PREVIEW_DESTROY) { 399 console.log('PREVIEW_DESTROY'); 400 } else if (state == print.PrintDocumentAdapterState.PRINT_TASK_SUCCEED) { 401 console.log('PRINT_TASK_SUCCEED'); 402 } else if (state == print.PrintDocumentAdapterState.PRINT_TASK_FAIL) { 403 console.log('PRINT_TASK_FAIL'); 404 } else if (state == print.PrintDocumentAdapterState.PRINT_TASK_CANCEL) { 405 console.log('PRINT_TASK_CANCEL'); 406 } else if (state == print.PrintDocumentAdapterState.PRINT_TASK_BLOCK) { 407 console.log('PRINT_TASK_BLOCK'); 408 } 409 } 410} 411``` 412 413### onJobStateChanged 414 415onJobStateChanged(jobId: string, state: PrintDocumentAdapterState): void 416 417实现这个接口来监听打印任务状态的改变。 418 419**需要权限:** ohos.permission.PRINT 420 421**系统能力:** SystemCapability.Print.PrintFramework 422 423**参数:** 424| **参数名** | **类型** | **必填** | **说明** | 425| -------- | -------- | -------- | -------- | 426| jobId | string | 是 | 表示打印任务ID | 427| state | PrintDocumentAdapterState | 是 | 表示打印任务更改为该状态 | 428 429**错误码:** 430 431以下错误码的详细介绍请参见[打印服务错误码](./errorcode-print.md)。 432 433| 错误码ID | 错误信息 | 434| -------- | ------------------------------------------- | 435| 201 | the application does not have permission to call this function. | 436| 401 | Parameter error. Possible causes: 1.Mandatory parameters are left unspecified; 2.Incorrect parameter types. | 437 438**示例:** 439 440```ts 441import { print } from '@kit.BasicServicesKit'; 442import { BusinessError } from '@ohos.base'; 443 444class MyPrintDocumentAdapter implements print.PrintDocumentAdapter { 445 onStartLayoutWrite(jobId: string, oldAttrs: print.PrintAttributes, newAttrs: print.PrintAttributes, fd: number, 446 writeResultCallback: (jobId: string, writeResult: print.PrintFileCreationState) => void) { 447 writeResultCallback(jobId, print.PrintFileCreationState.PRINT_FILE_CREATED); 448 }; 449 onJobStateChanged(jobId: string, state: print.PrintDocumentAdapterState) { 450 if (state == print.PrintDocumentAdapterState.PREVIEW_DESTROY) { 451 console.log('PREVIEW_DESTROY'); 452 } else if (state == print.PrintDocumentAdapterState.PRINT_TASK_SUCCEED) { 453 console.log('PRINT_TASK_SUCCEED'); 454 } else if (state == print.PrintDocumentAdapterState.PRINT_TASK_FAIL) { 455 console.log('PRINT_TASK_FAIL'); 456 } else if (state == print.PrintDocumentAdapterState.PRINT_TASK_CANCEL) { 457 console.log('PRINT_TASK_CANCEL'); 458 } else if (state == print.PrintDocumentAdapterState.PRINT_TASK_BLOCK) { 459 console.log('PRINT_TASK_BLOCK'); 460 } 461 } 462} 463``` 464 465## print 466 467print(files: Array<string>, callback: AsyncCallback<PrintTask>): void 468 469打印接口,传入文件进行打印,使用callback异步回调。 470 471**需要权限:** ohos.permission.PRINT 472 473**系统能力:** SystemCapability.Print.PrintFramework 474 475**参数:** 476| **参数名** | **类型** | **必填** | **说明** | 477| -------- | -------- | -------- | -------- | 478| files | Array<string> | 是 | 待打印文件列表,支持图片(.jpg .png .gif .bmp .webp)和pdf。系统应用传入uri时,需先调用uriPermissionManager.grantUriPermission()接口给打印应用授权,此接口为系统接口。三方应用建议使用[print](#print11-2)。 | 479| callback | AsyncCallback<PrintTask> | 是 | 异步获取打印完成之后的回调 | 480 481**错误码:** 482 483以下错误码的详细介绍请参见[打印服务错误码](./errorcode-print.md)。 484 485| 错误码ID | 错误信息 | 486| -------- | ------------------------------------------- | 487| 201 | the application does not have permission to call this function. | 488| 401 | Parameter error. Possible causes: 1.Mandatory parameters are left unspecified; 2.Incorrect parameter types. | 489 490**示例:** 491 492```ts 493import { print } from '@kit.BasicServicesKit'; 494import { BusinessError } from '@ohos.base'; 495 496//传入文件的uri 497let files = ['file://data/print/a.png', 'file://data/print/b.png']; 498//或者传入fd 499//let files = ['fd://1', 'fd://2']; 500print.print(files, (err: BusinessError, printTask: print.PrintTask) => { 501 if (err) { 502 console.log('print err ' + JSON.stringify(err)); 503 } else { 504 printTask.on('succeed', () => { 505 console.log('print state is succeed'); 506 }) 507 // ... 508 } 509}) 510``` 511 512## print 513 514print(files: Array<string>): Promise<PrintTask> 515 516打印接口,传入文件进行打印,使用Promise异步回调。 517 518**需要权限:** ohos.permission.PRINT 519 520**系统能力:** SystemCapability.Print.PrintFramework 521 522**参数:** 523| **参数名** | **类型** | **必填** | **说明** | 524| -------- | -------- | -------- | -------- | 525| files | Array<string> | 是 | 待打印文件列表,支持图片(.jpg .png .gif .bmp .webp)和pdf。系统应用传入uri时,需先调用uriPermissionManager.grantUriPermission()接口给打印应用授权,此接口为系统接口。三方应用建议使用[print](#print11-2)。 | 526 527**返回值:** 528| **类型** | **说明** | 529| -------- | -------- | 530| Promise<PrintTask> | 打印完成结果 | 531 532**错误码:** 533 534以下错误码的详细介绍请参见[打印服务错误码](./errorcode-print.md)。 535 536| 错误码ID | 错误信息 | 537| -------- | ------------------------------------------- | 538| 201 | the application does not have permission to call this function. | 539| 401 | Parameter error. Possible causes: 1.Mandatory parameters are left unspecified; 2.Incorrect parameter types. | 540 541**示例:** 542 543```ts 544import { print } from '@kit.BasicServicesKit'; 545import { BusinessError } from '@ohos.base'; 546 547//传入文件的uri 548let files = ['file://data/print/a.png', 'file://data/print/b.png']; 549//或者传入fd 550//let files = ['fd://1', 'fd://2']; 551print.print(files).then((printTask: print.PrintTask) => { 552 printTask.on('succeed', () => { 553 console.log('print state is succeed'); 554 }) 555 // ... 556}).catch((error: BusinessError) => { 557 console.log('print err ' + JSON.stringify(error)); 558}) 559``` 560 561## print<sup>11+</sup> 562 563print(files: Array<string>, context: Context, callback: AsyncCallback<PrintTask>): void 564 565打印接口,传入文件进行打印,使用callback异步回调。 566 567**需要权限:** ohos.permission.PRINT 568 569**系统能力:** SystemCapability.Print.PrintFramework 570 571**参数:** 572| **参数名** | **类型** | **必填** | **说明** | 573| -------- | -------- | -------- | -------- | 574| files | Array<string> | 是 | 待打印文件列表,支持图片(.jpg .png .gif .bmp .webp)和pdf。系统应用传入uri时,需先调用uriPermissionManager.grantUriPermission()接口给打印应用授权,此接口为系统接口。三方应用建议使用[print](#print11-2)。 | 575| context | Context | 是 | 用于拉起系统打印界面的UIAbilityContext | 576| callback | AsyncCallback<PrintTask> | 是 | 异步获取打印完成之后的回调 | 577 578**错误码:** 579 580以下错误码的详细介绍请参见[打印服务错误码](./errorcode-print.md)。 581 582| 错误码ID | 错误信息 | 583| -------- | ------------------------------------------- | 584| 201 | the application does not have permission to call this function. | 585| 401 | Parameter error. Possible causes: 1.Mandatory parameters are left unspecified; 2.Incorrect parameter types. | 586 587**示例:** 588 589```ts 590import { print } from '@kit.BasicServicesKit'; 591import { BusinessError } from '@ohos.base'; 592 593//传入文件的uri 594let files = ['file://data/print/a.png', 'file://data/print/b.png']; 595//或者传入fd 596//let files = ['fd://1', 'fd://2']; 597let context = getContext(this); 598print.print(files, context, (err: BusinessError, printTask: print.PrintTask) => { 599 if (err) { 600 console.log('print err ' + JSON.stringify(err)); 601 } else { 602 printTask.on('succeed', () => { 603 console.log('print state is succeed'); 604 }) 605 // ... 606 } 607}) 608``` 609 610## print<sup>11+</sup> 611 612print(files: Array<string>, context: Context): Promise<PrintTask> 613 614打印接口,传入文件进行打印,使用Promise异步回调。 615 616**需要权限:** ohos.permission.PRINT 617 618**系统能力:** SystemCapability.Print.PrintFramework 619 620**参数:** 621| **参数名** | **类型** | **必填** | **说明** | 622| -------- | -------- | -------- | -------- | 623| files | Array<string> | 是 | 待打印文件列表,支持图片(.jpg .png .gif .bmp .webp)和pdf。系统应用传入uri时,需先调用uriPermissionManager.grantUriPermission()接口给打印应用授权,此接口为系统接口。三方应用建议使用[print](#print11-2)。 | 624| context | Context | 是 | 用于拉起系统打印界面的UIAbilityContext | 625 626**返回值:** 627| **类型** | **说明** | 628| -------- | -------- | 629| Promise<PrintTask> | 打印完成结果 | 630 631**错误码:** 632 633以下错误码的详细介绍请参见[打印服务错误码](./errorcode-print.md)。 634 635| 错误码ID | 错误信息 | 636| -------- | ------------------------------------------- | 637| 201 | the application does not have permission to call this function. | 638| 401 | Parameter error. Possible causes: 1.Mandatory parameters are left unspecified; 2.Incorrect parameter types. | 639 640**示例:** 641 642```ts 643import { print } from '@kit.BasicServicesKit'; 644import { BusinessError } from '@ohos.base'; 645 646//传入文件的uri 647let files = ['file://data/print/a.png', 'file://data/print/b.png']; 648//或者传入fd 649//let files = ['fd://1', 'fd://2']; 650let context = getContext(this); 651print.print(files, context).then((printTask: print.PrintTask) => { 652 printTask.on('succeed', () => { 653 console.log('print state is succeed'); 654 }) 655 // ... 656}).catch((error: BusinessError) => { 657 console.log('print err ' + JSON.stringify(error)); 658}) 659``` 660 661## print<sup>11+</sup> 662 663print(jobName: string, printAdapter: PrintDocumentAdapter, printAttributes: PrintAttributes, context: Context): Promise<PrintTask> 664 665打印接口,传入文件进行打印,三方应用需要更新打印文件,使用Promise异步回调。 666 667**需要权限:** ohos.permission.PRINT 668 669**系统能力:** SystemCapability.Print.PrintFramework 670 671**参数:** 672| **参数名** | **类型** | **必填** | **说明** | 673| -------- | -------- | -------- | -------- | 674| jobName | string | 是 | 表示待打印文件名称,例如:test.pdf。打印侧会通过[onStartLayoutWrite](#onstartlayoutwrite)接口将空的pdf文件的fd传给接口调用方,由调用方使用新的打印参数更新待打印文件。 | 675| printAdapter | PrintDocumentAdapter | 是 | 表示三方应用实现的[PrintDocumentAdapter](#printdocumentadapter11)接口实例 | 676| printAttributes | PrintAttributes | 是 | 表示打印参数 | 677| context | Context | 是 | 用于拉起系统打印界面的UIAbilityContext | 678 679**返回值:** 680| **类型** | **说明** | 681| -------- | -------- | 682| Promise<PrintTask> | 打印完成结果 | 683 684**错误码:** 685 686以下错误码的详细介绍请参见[打印服务错误码](./errorcode-print.md)。 687 688| 错误码ID | 错误信息 | 689| -------- | ------------------------------------------- | 690| 201 | the application does not have permission to call this function. | 691| 401 | Parameter error. Possible causes: 1.Mandatory parameters are left unspecified; 2.Incorrect parameter types. | 692 693**示例:** 694 695```ts 696import { print } from '@kit.BasicServicesKit'; 697import { BusinessError } from '@ohos.base'; 698 699let jobName : string = "jobName"; 700let printAdapter : print.PrintDocumentAdapter | null = null; 701let printAttributes : print.PrintAttributes = { 702 copyNumber: 1, 703 pageRange: { 704 startPage: 0, 705 endPage: 5, 706 pages: [] 707 }, 708 pageSize: print.PrintPageType.PAGE_ISO_A3, 709 directionMode: print.PrintDirectionMode.DIRECTION_MODE_AUTO, 710 colorMode: print.PrintColorMode.COLOR_MODE_MONOCHROME, 711 duplexMode: print.PrintDuplexMode.DUPLEX_MODE_NONE 712} 713let context = getContext(); 714 715print.print(jobName, printAdapter, printAttributes, context).then((printTask: print.PrintTask) => { 716 printTask.on('succeed', () => { 717 console.log('print state is succeed'); 718 }) 719 // ... 720}).catch((error: BusinessError) => { 721 console.log('print err ' + JSON.stringify(error)); 722}) 723``` 724 725## PrintAttributes<sup>11+</sup> 726 727定义打印参数的接口。 728 729**系统能力:** SystemCapability.Print.PrintFramework 730 731**属性:** 732| **名称** | **类型** | **必填** | **说明** | 733| -------- | -------- | -------- | -------- | 734| copyNumber | number | 否 | 表示文件打印份数 | 735| pageRange | PrintPageRange | 否 | 表示待打印文件的页面范围 | 736| pageSize | PrintPageSize \| PrintPageType | 否 | 表示代打印文件的纸张类型 | 737| directionMode | PrintDirectionMode | 否 | 表示待打印文件的方向 | 738| colorMode | PrintColorMode | 否 | 表示待打印文件的色彩模式 | 739| duplexMode | PrintDuplexMode | 否 | 表示待打印文件的单双面模式 | 740 741## PrintPageRange<sup>11+</sup> 742 743定义打印范围的接口。 744 745**系统能力:** SystemCapability.Print.PrintFramework 746 747**属性:** 748| **名称** | **类型** | **必填** | **说明** | 749| -------- | -------- | -------- | -------- | 750| startPage | number | 否 | 表示起始页 | 751| endPage | number | 否 | 表示结束页 | 752| pages | Array<number> | 否 | 表示待打印的页面范围的集合| 753 754 755## PrintPageSize<sup>11+</sup> 756 757定义打印页面尺寸的接口。 758 759**系统能力:** SystemCapability.Print.PrintFramework 760 761**属性:** 762| **名称** | **类型** | **必填** | **说明** | 763| -------- | -------- | -------- | -------- | 764| id | string | 是 | 表示纸张类型ID | 765| name | string | 是 | 表示纸张类型名称 | 766| width | number | 是 | 表示页面宽度,单位:毫米 | 767| height | number | 是 | 表示页面高度,单位:毫米 | 768 769 770 771## PrintDirectionMode<sup>11+</sup> 772 773打印纸张方向的枚举。 774 775**系统能力:** SystemCapability.Print.PrintFramework 776 777| **名称** | **值** | **说明** | 778| -------- | -------- | -------- | 779| DIRECTION_MODE_AUTO | 0 | 表示自动选择纸张方向 | 780| DIRECTION_MODE_PORTRAIT | 1 | 表示纵向打印 | 781| DIRECTION_MODE_LANDSCAPE | 2 | 表示横向打印 | 782 783## PrintColorMode<sup>11+</sup> 784 785打印色彩模式的枚举。 786 787**系统能力:** SystemCapability.Print.PrintFramework 788 789| **名称** | **值** | **说明** | 790| -------- | -------- | -------- | 791| COLOR_MODE_MONOCHROME | 0 | 表示黑白打印 | 792| COLOR_MODE_COLOR | 1 | 表示彩色打印 | 793 794## PrintDuplexMode<sup>11+</sup> 795 796打印单双面模式的枚举。 797 798**系统能力:** SystemCapability.Print.PrintFramework 799 800| **名称** | **值** | **说明** | 801| -------- | -------- | -------- | 802| DUPLEX_MODE_NONE | 0 | 表示单面打印 | 803| DUPLEX_MODE_LONG_EDGE | 1 | 表示双面打印沿长边翻转 | 804| DUPLEX_MODE_SHORT_EDGE | 2 | 表示双面打印沿短边翻转 | 805 806## PrintPageType<sup>11+</sup> 807 808打印纸张类型的枚举。 809 810**系统能力:** SystemCapability.Print.PrintFramework 811 812| **名称** | **值** | **说明** | 813| -------- | -------- | -------- | 814| PAGE_ISO_A3 | 0 | 表示A3 | 815| PAGE_ISO_A4 | 1 | 表示A4 | 816| PAGE_ISO_A5 | 2 | 表示A5 | 817| PAGE_JIS_B5 | 3 | 表示B5 | 818| PAGE_ISO_C5 | 4 | 表示C5 | 819| PAGE_ISO_DL | 5 | 表示DL | 820| PAGE_LETTER | 6 | 表示Letter | 821| PAGE_LEGAL | 7 | 表示Legal | 822| PAGE_PHOTO_4X6 | 8 | 表示4x6相纸 | 823| PAGE_PHOTO_5X7 | 9 | 表示5x7相纸 | 824| PAGE_INT_DL_ENVELOPE | 10 | 表示INT DL ENVELOPE | 825| PAGE_B_TABLOID | 11 | 表示B Tabloid | 826 827## PrintDocumentAdapterState<sup>11+</sup> 828 829打印任务状态的枚举。 830 831**系统能力:** SystemCapability.Print.PrintFramework 832 833| **名称** | **值** | **说明** | 834| -------- | -------- | -------- | 835| PREVIEW_DESTROY | 0 | 表示预览失败 | 836| PRINT_TASK_SUCCEED | 1 | 表示打印任务成功 | 837| PRINT_TASK_FAIL | 2 | 表示打印任务失败 | 838| PRINT_TASK_CANCEL | 3 | 表示打印任务取消 | 839| PRINT_TASK_BLOCK | 4 | 表示打印任务阻塞 | 840 841## PrintFileCreationState<sup>11+</sup> 842 843打印文件创建状态的枚举。 844 845**系统能力:** SystemCapability.Print.PrintFramework 846 847| **名称** | **值** | **说明** | 848| -------- | -------- | -------- | 849| PRINT_FILE_CREATED | 0 | 表示打印文件创建成功 | 850| PRINT_FILE_CREATION_FAILED | 1 | 表示打印文件创建失败| 851| PRINT_FILE_CREATED_UNRENDERED | 2 | 表示打印文件创建成功但未渲染 | 852 853