1# @ohos.print (Print) 2 3<!--Kit: Basic Services Kit--> 4<!--Subsystem: Print--> 5<!--Owner: @guoshengbang--> 6<!--Designer: @gcw_4D6e0BBd--> 7<!--Tester: @guoshengbang--> 8<!--Adviser: @RayShih--> 9 10The **print** module provides APIs for basic print operations. 11 12> **NOTE** 13> The initial APIs of this module are supported since API version 10. Newly added APIs will be marked with a superscript to indicate their earliest API version. 14 15## Modules to Import 16 17```ts 18import { print } from '@kit.BasicServicesKit'; 19``` 20 21## PrintTask 22 23Implements event listeners for print tasks. 24 25### PrintTask.on 26 27on(type: 'block', callback: Callback<void>): void 28 29Registers a listener for the print task block event. This API uses a callback to return the result. 30 31**Required permissions**: ohos.permission.PRINT 32 33**System capability**: SystemCapability.Print.PrintFramework 34 35**Parameters** 36| **Name**| **Type**| **Mandatory**| **Description**| 37| -------- | -------- | -------- | -------- | 38| type | string | Yes| Listening type.<br>The value is fixed at **'block'**, indicating blocking of the print task. | 39| callback | Callback<void> | Yes| Callback used to return the result.| 40 41**Error codes** 42 43For details about the error codes, see [Error Codes of the Print Service](./errorcode-print.md). 44 45| ID| Error Message | 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**Example** 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<void>): void 71 72Registers a listener for the print task success event. This API uses a callback to return the result. 73 74**Required permissions**: ohos.permission.PRINT 75 76**System capability**: SystemCapability.Print.PrintFramework 77 78**Parameters** 79| **Name**| **Type**| **Mandatory**| **Description**| 80| -------- | -------- | -------- | -------- | 81| type | string | Yes| Listening type.<br>The value is fixed at **'succeed'**, indicating success of the print task. | 82| callback | Callback<void> | Yes| Callback used to return the result.| 83 84**Error codes** 85 86For details about the error codes, see [Error Codes of the Print Service](./errorcode-print.md). 87 88| ID| Error Message | 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**Example** 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<void>): void 114 115Registers a listener for the print task failure event. This API uses a callback to return the result. 116 117**Required permissions**: ohos.permission.PRINT 118 119**System capability**: SystemCapability.Print.PrintFramework 120 121**Parameters** 122| **Name**| **Type**| **Mandatory**| **Description**| 123| -------- | -------- | -------- | -------- | 124| type | string | Yes| Listening type.<br>The value is fixed at **'fail'**, indicating failure of the print task. | 125| callback | Callback<void> | Yes| Callback used to return the result.| 126 127**Error codes** 128 129For details about the error codes, see [Error Codes of the Print Service](./errorcode-print.md). 130 131| ID| Error Message | 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**Example** 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<void>): void 157 158Registers a listener for the print task cancellation event. This API uses a callback to return the result. 159 160**Required permissions**: ohos.permission.PRINT 161 162**System capability**: SystemCapability.Print.PrintFramework 163 164**Parameters** 165| **Name**| **Type**| **Mandatory**| **Description**| 166| -------- | -------- | -------- | -------- | 167| type | string | Yes| Listening type.<br>The value is fixed at **'cancel'**, indicating cancellation of the print task. | 168| callback | Callback<void> | Yes| Callback used to return the result.| 169 170**Error codes** 171 172For details about the error codes, see [Error Codes of the Print Service](./errorcode-print.md). 173 174| ID| Error Message | 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**Example** 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<void>): void 200 201Unregisters the listener for the print task block event. This API uses a callback to return the result. 202 203**Required permissions**: ohos.permission.PRINT 204 205**System capability**: SystemCapability.Print.PrintFramework 206 207**Parameters** 208| **Name**| **Type**| **Mandatory**| **Description**| 209| -------- | -------- | -------- | -------- | 210| type | string | Yes| Listening type.<br>The value is fixed at **'block'**, indicating blocking of the print task. | 211| callback | Callback<void> | No| Callback used to return the result.| 212 213**Error codes** 214 215For details about the error codes, see [Error Codes of the Print Service](./errorcode-print.md). 216 217| ID| Error Message | 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**Example** 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<void>): void 243 244Unregisters the listener for the print task success event. This API uses a callback to return the result. 245 246**Required permissions**: ohos.permission.PRINT 247 248**System capability**: SystemCapability.Print.PrintFramework 249 250**Parameters** 251| **Name**| **Type**| **Mandatory**| **Description**| 252| -------- | -------- | -------- | -------- | 253| type | string | Yes| Listening type.<br>The value is fixed at **'succeed'**, indicating success of the print task. | 254| callback | Callback<void> | No| Callback used to return the result.| 255 256**Error codes** 257 258For details about the error codes, see [Error Codes of the Print Service](./errorcode-print.md). 259 260| ID| Error Message | 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**Example** 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<void>): void 286 287Unregisters the listener for the print task failure event. This API uses a callback to return the result. 288 289**Required permissions**: ohos.permission.PRINT 290 291**System capability**: SystemCapability.Print.PrintFramework 292 293**Parameters** 294| **Name**| **Type**| **Mandatory**| **Description**| 295| -------- | -------- | -------- | -------- | 296| type | string | Yes| Listening type.<br>The value is fixed at **'fail'**, indicating failure of the print task. | 297| callback | Callback<void> | No| Callback used to return the result.| 298 299**Error codes** 300 301For details about the error codes, see [Error Codes of the Print Service](./errorcode-print.md). 302 303| ID| Error Message | 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**Example** 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<void>): void 329 330Unregisters the listener for the print task cancellation event. This API uses a callback to return the result. 331 332**Required permissions**: ohos.permission.PRINT 333 334**System capability**: SystemCapability.Print.PrintFramework 335 336**Parameters** 337| **Name**| **Type**| **Mandatory**| **Description**| 338| -------- | -------- | -------- | -------- | 339| type | string | Yes| Listening type.<br>The value is fixed at **'cancel'**, indicating cancellation of the print task. | 340| callback | Callback<void> | No| Callback used to return the result.| 341 342**Error codes** 343 344For details about the error codes, see [Error Codes of the Print Service](./errorcode-print.md). 345 346| ID| Error Message | 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**Example** 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 371Provides information about the document to print. This API must be implemented by a third-party application. 372 373### onStartLayoutWrite 374 375onStartLayoutWrite(jobId: string, oldAttrs: PrintAttributes, newAttrs: PrintAttributes, fd: number, writeResultCallback: (jobId: string, writeResult: PrintFileCreationState) => void): void 376 377Sends an empty PDF file descriptor to a third-party application. The third-party application updates the file with the new print attributes and then calls **writeResultCallback** to print the file. 378 379**Required permissions**: ohos.permission.PRINT 380 381**System capability**: SystemCapability.Print.PrintFramework 382 383**Parameters** 384| **Name**| **Type**| **Mandatory**| **Description**| 385| -------- | -------- | -------- | -------- | 386| jobId | string | Yes| ID of the print job.| 387| oldAttrs | [PrintAttributes](#printattributes11) | Yes| Old print attributes.| 388| newAttrs | [PrintAttributes](#printattributes11) | Yes| New print attributes.| 389| fd | number | Yes| PDF file descriptor sent to the API caller.| 390| writeResultCallback | (jobId: string, writeResult: [PrintFileCreationState](#printfilecreationstate11)) => void | Yes| Callback used to print the updated file.| 391 392**Error codes** 393 394For details about the error codes, see [Error Codes of the Print Service](./errorcode-print.md). 395 396| ID| Error Message | 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**Example** 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 429 430onJobStateChanged(jobId: string, state: PrintDocumentAdapterState): void 431 432Registers a listener for print job state changes. 433 434**Required permissions**: ohos.permission.PRINT 435 436**System capability**: SystemCapability.Print.PrintFramework 437 438**Parameters** 439| **Name**| **Type**| **Mandatory**| **Description**| 440| -------- | -------- | -------- | -------- | 441| jobId | string | Yes| ID of the print job.| 442| state | [PrintDocumentAdapterState](#printdocumentadapterstate11) | Yes| New state of the print job.| 443 444**Error codes** 445 446For details about the error codes, see [Error Codes of the Print Service](./errorcode-print.md). 447 448| ID| Error Message | 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**Example** 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<string>, callback: AsyncCallback<PrintTask>): void 483 484Prints files. This API uses an asynchronous callback to return the result. 485 486**Required permissions**: ohos.permission.PRINT 487 488**System capability**: SystemCapability.Print.PrintFramework 489 490**Parameters** 491| **Name**| **Type**| **Mandatory**| **Description**| 492| -------- | -------- | -------- | -------- | 493| files | Array<string> | Yes| List of files to print. Images (in .jpg, .png, .gif, .bmp, or .webp format) and PDF files are supported. You should save the files to the application sandbox, obtain the sandbox URI through **fileUri.getUriFromPath**, and then pass this URI as a parameter to this API.| 494| callback | AsyncCallback<[PrintTask](#printtask)> | Yes| Callback used to return the result.| 495 496**Error codes** 497 498For details about the error codes, see [Error Codes of the Print Service](./errorcode-print.md). 499 500| ID| Error Message | 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**Example** 506 507```ts 508import { print } from '@kit.BasicServicesKit'; 509import { BusinessError } from '@ohos.base'; 510import { fileUri } from '@kit.CoreFileKit'; 511 512// Pass in the URIs of the files. 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<string>): Promise<PrintTask> 529 530Prints files. This API uses a promise to return the result. 531 532**Required permissions**: ohos.permission.PRINT 533 534**System capability**: SystemCapability.Print.PrintFramework 535 536**Parameters** 537| **Name**| **Type**| **Mandatory**| **Description**| 538| -------- | -------- | -------- | -------- | 539| files | Array<string> | Yes| List of files to print. Images (in .jpg, .png, .gif, .bmp, or .webp format) and PDF files are supported. You should save the files to the application sandbox, obtain the sandbox URI through **fileUri.getUriFromPath**, and then pass this URI as a parameter to this API.| 540 541**Return value** 542| **Type**| **Description**| 543| -------- | -------- | 544| Promise<[PrintTask](#printtask)> | Print result.| 545 546**Error codes** 547 548For details about the error codes, see [Error Codes of the Print Service](./errorcode-print.md). 549 550| ID| Error Message | 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**Example** 556 557```ts 558import { print } from '@kit.BasicServicesKit'; 559import { BusinessError } from '@ohos.base'; 560import { fileUri } from '@kit.CoreFileKit'; 561 562// Pass in the URIs of the files. 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<string>, context: Context, callback: AsyncCallback<PrintTask>): void 577 578Prints files. This API uses an asynchronous callback to return the result. 579 580**Required permissions**: ohos.permission.PRINT 581 582**System capability**: SystemCapability.Print.PrintFramework 583 584**Parameters** 585| **Name**| **Type**| **Mandatory**| **Description**| 586| -------- | -------- | -------- | -------- | 587| files | Array<string> | Yes| List of files to print. Images (in .jpg, .png, .gif, .bmp, or .webp format) and PDF files are supported. You should save the files to the application sandbox, obtain the sandbox URI through **fileUri.getUriFromPath**, and then pass this URI as a parameter to this API.| 588| context | Context | Yes| UIAbilityContext used to start the system print UI.| 589| callback | AsyncCallback<[PrintTask](#printtask)> | Yes| Callback used to return the result.| 590 591**Error codes** 592 593For details about the error codes, see [Error Codes of the Print Service](./errorcode-print.md). 594 595| ID| Error Message | 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**Example** 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("Print").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<string>, context: Context): Promise<PrintTask> 640 641Prints files. This API uses a promise to return the result. 642 643**Required permissions**: ohos.permission.PRINT 644 645**System capability**: SystemCapability.Print.PrintFramework 646 647**Parameters** 648| **Name**| **Type**| **Mandatory**| **Description**| 649| -------- | -------- | -------- | -------- | 650| files | Array<string> | Yes| List of files to print. Images (in .jpg, .png, .gif, .bmp, or .webp format) and PDF files are supported. You should save the files to the application sandbox, obtain the sandbox URI through **fileUri.getUriFromPath**, and then pass this URI as a parameter to this API.| 651| context | Context | Yes| UIAbilityContext used to start the system print UI.| 652 653**Return value** 654| **Type**| **Description**| 655| -------- | -------- | 656| Promise<[PrintTask](#printtask)> | Print result.| 657 658**Error codes** 659 660For details about the error codes, see [Error Codes of the Print Service](./errorcode-print.md). 661 662| ID| Error Message | 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**Example** 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("Print").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<PrintTask> 705 706Prints a file. This API uses a promise to return the result. 707 708**Required permissions**: ohos.permission.PRINT 709 710**System capability**: SystemCapability.Print.PrintFramework 711 712**Parameters** 713| **Name**| **Type**| **Mandatory**| **Description**| 714| -------- | -------- | -------- | -------- | 715| jobName | string | Yes| Name of the file to print, for example, **test.pdf**. The printer uses the [onStartLayoutWrite](#onstartlayoutwrite) API to send the **fd** of the empty PDF file to the API caller. The API caller uses the new print attributes to update the file to print.| 716| printAdapter | [PrintDocumentAdapter](#printdocumentadapter11) | Yes| [PrintDocumentAdapter](#printdocumentadapter11) API instance implemented by a third-party application.| 717| printAttributes | [PrintAttributes](#printattributes11) | Yes| Print attributes.| 718| context | Context | Yes| UIAbilityContext used to start the system print UI.| 719 720**Return value** 721| **Type**| **Description**| 722| -------- | -------- | 723| Promise<[PrintTask](#printtask)> | Print result.| 724 725**Error codes** 726 727For details about the error codes, see [Error Codes of the Print Service](./errorcode-print.md). 728 729| ID| Error Message | 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**Example** 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("Print").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 784Defines the print attributes. 785 786**System capability**: SystemCapability.Print.PrintFramework 787 788**Attributes** 789| **Name**| **Type**| **Mandatory**| **Description**| 790| -------- | -------- | -------- | -------- | 791| copyNumber | number | No| Number of printed file copies. The default value is **1**.| 792| pageRange | [PrintPageRange](#printpagerange11) | No| Page range of the file to print.| 793| pageSize | [PrintPageSize](#printpagesize11) \| [PrintPageType](#printpagetype11) | No| Page size of the file to print.| 794| directionMode | [PrintDirectionMode](#printdirectionmode11) | No| Print direction mode.| 795| colorMode | [PrintColorMode](#printcolormode11) | No| Color mode of the files to print.| 796| duplexMode | [PrintDuplexMode](#printduplexmode11) | No| Duplex mode of the files to print.| 797 798## PrintPageRange<sup>11+</sup> 799 800Defines the print range. 801 802**System capability**: SystemCapability.Print.PrintFramework 803 804**Attributes** 805| **Name**| **Type**| **Mandatory**| **Description**| 806| -------- | -------- | -------- | -------- | 807| startPage | number | No| Start page. The default value is **1**.| 808| endPage | number | No| End page. The default value is the maximum number of pages of the file to be printed.| 809| pages | Array<number> | No| Page range set of the file to print. The default value is empty.| 810 811 812## PrintPageSize<sup>11+</sup> 813 814Defines the size of the printed page. 815 816**System capability**: SystemCapability.Print.PrintFramework 817 818**Attributes** 819| **Name**| **Type**| **Mandatory**| **Description**| 820| -------- | -------- | -------- | -------- | 821| id | string | Yes| Paper size ID.| 822| name | string | Yes| Paper size name.| 823| width | number | Yes| Page width, in millimeters.| 824| height | number | Yes| Page height, in millimeters.| 825 826 827 828## PrintDirectionMode<sup>11+</sup> 829 830Enumerates the print direction modes. 831 832**System capability**: SystemCapability.Print.PrintFramework 833 834| **Name**| **Value**| **Description**| 835| -------- | -------- | -------- | 836| DIRECTION_MODE_AUTO | 0 | Automatic.| 837| DIRECTION_MODE_PORTRAIT | 1 | Portrait mode.| 838| DIRECTION_MODE_LANDSCAPE | 2 | Landscape mode.| 839 840## PrintColorMode<sup>11+</sup> 841 842Enumerates the color modes. 843 844**System capability**: SystemCapability.Print.PrintFramework 845 846| **Name**| **Value**| **Description**| 847| -------- | -------- | -------- | 848| COLOR_MODE_MONOCHROME | 0 | Black and white.| 849| COLOR_MODE_COLOR | 1 | Color.| 850 851## PrintDuplexMode<sup>11+</sup> 852 853Enumerates the duplex modes. 854 855**System capability**: SystemCapability.Print.PrintFramework 856 857| **Name**| **Value**| **Description**| 858| -------- | -------- | -------- | 859| DUPLEX_MODE_NONE | 0 | Simplex (single-sided).| 860| DUPLEX_MODE_LONG_EDGE | 1 | Duplex (double-sided) with flipping on long edge.| 861| DUPLEX_MODE_SHORT_EDGE | 2 | Duplex (double-sided) with flipping on short edge.| 862 863## PrintPageType<sup>11+</sup> 864 865Enumerates the print page types. 866 867**System capability**: SystemCapability.Print.PrintFramework 868 869| **Name**| **Value**| **Description**| 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 | 4 x 6 photo paper.| 880| PAGE_PHOTO_5X7 | 9 | 5 x 7 photo paper.| 881| PAGE_INT_DL_ENVELOPE | 10 | International envelope DL.| 882| PAGE_B_TABLOID | 11 | B Tabloid.| 883 884## PrintDocumentAdapterState<sup>11+</sup> 885 886Enumerates the print job states. 887 888**System capability**: SystemCapability.Print.PrintFramework 889 890| **Name**| **Value**| **Description**| 891| -------- | -------- | -------- | 892| PREVIEW_DESTROY | 0 | The preview fails.| 893| PRINT_TASK_SUCCEED | 1 | The print job is successful.| 894| PRINT_TASK_FAIL | 2 | The print job is failed.| 895| PRINT_TASK_CANCEL | 3 | The print job is canceled.| 896| PRINT_TASK_BLOCK | 4 | The print job is blocked.| 897 898## PrintFileCreationState<sup>11+</sup> 899 900Enumerates the print file creation status. 901 902**System capability**: SystemCapability.Print.PrintFramework 903 904| **Name**| **Value**| **Description**| 905| -------- | -------- | -------- | 906| PRINT_FILE_CREATED | 0 | The print file is created successfully.| 907| PRINT_FILE_CREATION_FAILED | 1 | The print file fails to be created.| 908| PRINT_FILE_CREATED_UNRENDERED | 2 | The print file is successfully created but not rendered.| 909 910## PrinterState<sup>14+</sup> 911 912Enumerates the printer states. 913 914**System capability**: SystemCapability.Print.PrintFramework 915 916| **Name**| **Value**| **Description**| 917| -------- | -------- | -------- | 918| PRINTER_ADDED | 0 | A new printer is added.| 919| PRINTER_REMOVED | 1 | The printer is removed.| 920| PRINTER_CAPABILITY_UPDATED | 2 | The printer is updated.| 921| PRINTER_CONNECTED | 3 | The printer is connected.| 922| PRINTER_DISCONNECTED | 4 | The printer is disconnected.| 923| PRINTER_RUNNING | 5 | The printer is running.| 924 925## PrintJobState<sup>14+</sup> 926 927Enumerates the print job states. 928 929**System capability**: SystemCapability.Print.PrintFramework 930 931| **Name**| **Value**| **Description**| 932| -------- | -------- | -------- | 933| PRINT_JOB_PREPARE | 0 | The printer is prepared for the print job.| 934| PRINT_JOB_QUEUED | 1 | The print job is on the print queue of the printer.| 935| PRINT_JOB_RUNNING | 2 | The print job is being executed.| 936| PRINT_JOB_BLOCKED | 3 | The print job is blocked.| 937| PRINT_JOB_COMPLETED | 4 | The print job is complete.| 938 939## PrintJobSubState<sup>14+</sup> 940 941Enumerates the print job substates. 942 943**System capability**: SystemCapability.Print.PrintFramework 944 945| **Name**| **Value**| **Description**| 946| -------- | -------- | -------- | 947| PRINT_JOB_COMPLETED_SUCCESS | 0 | The print job is successful.| 948| PRINT_JOB_COMPLETED_FAILED | 1 | The print job is failed.| 949| PRINT_JOB_COMPLETED_CANCELLED | 2 | The print job is canceled by user.| 950| PRINT_JOB_COMPLETED_FILE_CORRUPTED | 3 | The print job is corrupted.| 951| PRINT_JOB_BLOCK_OFFLINE | 4 | The printer is offline.| 952| PRINT_JOB_BLOCK_BUSY | 5 | The printer is occupied by another process.| 953| PRINT_JOB_BLOCK_CANCELLED | 6 | The print job is canceled due to a block.| 954| PRINT_JOB_BLOCK_OUT_OF_PAPER | 7 | The printer is out of paper.| 955| PRINT_JOB_BLOCK_OUT_OF_INK | 8 | The printer is out of ink.| 956| PRINT_JOB_BLOCK_OUT_OF_TONER | 9 | The printer is out of toner.| 957| PRINT_JOB_BLOCK_JAMMED | 10 | The printer is in a paper jam.| 958| PRINT_JOB_BLOCK_DOOR_OPEN | 11 | The printer door is open.| 959| PRINT_JOB_BLOCK_SERVICE_REQUEST | 12 | Print service request.| 960| PRINT_JOB_BLOCK_LOW_ON_INK | 13 | The printer is low on ink.| 961| PRINT_JOB_BLOCK_LOW_ON_TONER | 14 | The printer is low on toner.| 962| PRINT_JOB_BLOCK_REALLY_LOW_ON_INK | 15 | The printer is extremely low on ink.| 963| PRINT_JOB_BLOCK_BAD_CERTIFICATE | 16 | The print certificate is incorrect.| 964| PRINT_JOB_BLOCK_DRIVER_EXCEPTION<sup>20+</sup> | 17 | The print driver is abnormal.| 965| PRINT_JOB_BLOCK_ACCOUNT_ERROR | 18 | There is an error with the printer account.| 966| PRINT_JOB_BLOCK_PRINT_PERMISSION_ERROR | 19 | There is an error with the printer permission.| 967| PRINT_JOB_BLOCK_PRINT_COLOR_PERMISSION_ERROR | 20 | There is an error with the color printing permission.| 968| PRINT_JOB_BLOCK_NETWORK_ERROR | 21 | The printer fails to connect to the network.| 969| PRINT_JOB_BLOCK_SERVER_CONNECTION_ERROR | 22 | The printer fails to connect to the server.| 970| PRINT_JOB_BLOCK_LARGE_FILE_ERROR | 23 | There is an error with a large file printing.| 971| PRINT_JOB_BLOCK_FILE_PARSING_ERROR | 24 | There is an error with file parsing.| 972| PRINT_JOB_BLOCK_SLOW_FILE_CONVERSION | 25 | The file conversion is slow.| 973| PRINT_JOB_RUNNING_UPLOADING_FILES | 26 | The file is uploading.| 974| PRINT_JOB_RUNNING_CONVERTING_FILES | 27 | The file is converting.| 975| PRINT_JOB_BLOCK_FILE_UPLOADING_ERROR<sup>18+</sup> | 30 | The file fails to be uploaded.| 976| PRINT_JOB_BLOCK_DRIVER_MISSING<sup>20+</sup> | 34 | The print driver is missing.| 977| PRINT_JOB_BLOCK_INTERRUPT<sup>20+</sup> | 35 | The print job is interrupted.| 978| PRINT_JOB_BLOCK_PRINTER_UNAVAILABLE<sup>20+</sup> | 98 | The printer is unavailable.| 979| PRINT_JOB_BLOCK_UNKNOWN | 99 | There is an unknown error with the printer.| 980 981## PrintErrorCode<sup>14+</sup> 982 983Enumerates the print error codes. 984 985**System capability**: SystemCapability.Print.PrintFramework 986 987| **Name**| **Value**| **Description**| 988| -------- | -------- | -------- | 989| E_PRINT_NONE | 0 | No error.| 990| E_PRINT_NO_PERMISSION | 201 | No permission.| 991| E_PRINT_INVALID_PARAMETER | 401 | Invalid parameters.| 992| E_PRINT_GENERIC_FAILURE | 13100001 | Printing failure.| 993| E_PRINT_RPC_FAILURE | 13100002 | RPC failure.| 994| E_PRINT_SERVER_FAILURE | 13100003 | Print service failure.| 995| E_PRINT_INVALID_EXTENSION | 13100004 | Invalid printer extension.| 996| E_PRINT_INVALID_PRINTER | 13100005 | Invalid printer.| 997| E_PRINT_INVALID_PRINT_JOB | 13100006 | Invalid print job.| 998| E_PRINT_FILE_IO | 13100007 | Incorrect file input/output.| 999| E_PRINT_TOO_MANY_FILES<sup>18+</sup> | 13100010 | Excessive files. Maximum number: 99.| 1000 1001## ApplicationEvent<sup>14+</sup> 1002 1003Enumerates print application events. 1004 1005**System capability**: SystemCapability.Print.PrintFramework 1006 1007| **Name**| **Value**| **Description**| 1008| -------- | -------- | -------- | 1009| APPLICATION_CREATED | 0 | Starts the print application.| 1010| APPLICATION_CLOSED_FOR_STARTED | 1 | Closes the print application by clicking **Start**.| 1011| APPLICATION_CLOSED_FOR_CANCELED | 2 | Closes the print application by clicking **Cancel**.| 1012 1013## print.addPrinterToDiscovery<sup>14+</sup> 1014 1015addPrinterToDiscovery(printerInformation: PrinterInformation): Promise<void> 1016 1017Adds a printer to the printer discovery list. This API uses a promise to return the result. 1018 1019**Required permissions**: ohos.permission.PRINT 1020 1021**System capability**: SystemCapability.Print.PrintFramework 1022 1023**Parameters** 1024| **Name**| **Type**| **Mandatory**| **Description**| 1025| -------- | -------- | -------- | -------- | 1026| printerInformation | [PrinterInformation](#printerinformation14) | Yes| The added printer.| 1027 1028**Return value** 1029| **Type**| **Description**| 1030| -------- | -------- | 1031| Promise<void> | Result of adding a printer to the printer discovery list.| 1032 1033**Error codes** 1034 1035For details about the error codes, see [Error Codes of the Print Service](./errorcode-print.md). 1036 1037| ID| Error Message | 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**Example** 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<void> 1067 1068Updates the printer capabilities to the printer discovery list. This API uses a promise to return the result. 1069 1070**Required permissions**: ohos.permission.PRINT 1071 1072**System capability**: SystemCapability.Print.PrintFramework 1073 1074**Parameters** 1075| **Name**| **Type**| **Mandatory**| **Description**| 1076| -------- | -------- | -------- | -------- | 1077| printerInformation | [PrinterInformation](#printerinformation14) | Yes| Printer whose capability is to be updated.| 1078 1079**Return value** 1080| **Type**| **Description**| 1081| -------- | -------- | 1082| Promise<void> | Result of updating the printer capabilities to the printer discovery list.| 1083 1084**Error codes** 1085 1086For details about the error codes, see [Error Codes of the Print Service](./errorcode-print.md). 1087 1088| ID| Error Message | 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**Example** 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<void> 1136 1137Removes a printer from the printer discovery list. This API uses a promise to return the result. 1138 1139**Required permissions**: ohos.permission.PRINT 1140 1141**System capability**: SystemCapability.Print.PrintFramework 1142 1143**Parameters** 1144| **Name**| **Type**| **Mandatory**| **Description**| 1145| -------- | -------- | -------- | -------- | 1146| printerId | string | Yes| Printer to remove.| 1147 1148**Return value** 1149| **Type**| **Description**| 1150| -------- | -------- | 1151| Promise<void> | Result of removing a printer from the printer discovery list.| 1152 1153**Error codes** 1154 1155For details about the error codes, see [Error Codes of the Print Service](./errorcode-print.md). 1156 1157| ID| Error Message | 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**Example** 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<PrinterInformation> 1179 1180Obtains printer information based on the printer ID. This API uses a promise to return the result. 1181 1182**Required permissions**: ohos.permission.PRINT 1183 1184**System capability**: SystemCapability.Print.PrintFramework 1185 1186**Parameters** 1187| **Name**| **Type**| **Mandatory**| **Description**| 1188| -------- | -------- | -------- | -------- | 1189| printerId | string | Yes| Printer ID used to obtain information.| 1190 1191**Return value** 1192| **Type**| **Description**| 1193| -------- | -------- | 1194| Promise<[PrinterInformation](#printerinformation14)> | Printer information obtained based on the printer ID.| 1195 1196**Error codes** 1197 1198For details about the error codes, see [Error Codes of the Print Service](./errorcode-print.md). 1199 1200| ID| Error Message | 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**Example** 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 1221Defines the printer information. 1222 1223**System capability**: SystemCapability.Print.PrintFramework 1224 1225**Attributes** 1226| **Name**| **Type**| **Mandatory**| **Description**| 1227| -------- | -------- | -------- | -------- | 1228| printerId | string | Yes| Printer ID.| 1229| printerName | string | Yes| Printer name.| 1230| printerStatus | [PrinterStatus](#printerstatus14) | Yes| Printer state.| 1231| description | string | No| Printer description.| 1232| capability | [PrinterCapabilities](#printercapabilities14) | No| Printer capabilities.| 1233| uri | string | No| Printer URI.| 1234| printerMake | string | No| Printer model.| 1235| preferences<sup>18+</sup> | [PrinterPreferences](#printerpreferences18) | No| Printer preferences.| 1236| alias<sup>18+</sup> | string | No| Printer alias.| 1237| options | string | No| Printer details.| 1238 1239## PrinterCapabilities<sup>14+</sup> 1240 1241Defines the printer capabilities. 1242 1243**System capability**: SystemCapability.Print.PrintFramework 1244 1245**Attributes** 1246| **Name**| **Type**| **Mandatory**| **Description**| 1247| -------- | -------- | -------- | -------- | 1248| supportedPageSizes | Array<[PrintPageSize](#printpagesize11)> | Yes| List of paper sizes supported by the printer.| 1249| supportedColorModes | Array<[PrintColorMode](#printcolormode11)> | Yes| List of color modes supported by the printer.| 1250| supportedDuplexModes | Array<[PrintDuplexMode](#printduplexmode11)> | Yes| List of single- and double-sided modes supported by the printer.| 1251| supportedMediaTypes | Array<string> | No| List of paper types supported by the printer.| 1252| supportedQualities | Array<[PrintQuality](#printquality14)> | No| List of print quality supported by the printer.| 1253| supportedOrientations | Array<[PrintOrientationMode](#printorientationmode14)> | No| List of print directions supported by the printer.| 1254| options | string | No| Printer capability details.| 1255 1256## PrintQuality<sup>14+</sup> 1257 1258Enumerates the print qualities. 1259 1260**System capability**: SystemCapability.Print.PrintFramework 1261 1262| **Name**| **Value**| **Description**| 1263| -------- | -------- | -------- | 1264| QUALITY_DRAFT | 3 | Draft| 1265| QUALITY_NORMAL | 4 | Standard| 1266| QUALITY_HIGH | 5 | High| 1267 1268## PrintOrientationMode<sup>14+</sup> 1269 1270Enumerates the print directions. 1271 1272**System capability**: SystemCapability.Print.PrintFramework 1273 1274| **Name**| **Value**| **Description**| 1275| -------- | -------- | -------- | 1276| ORIENTATION_MODE_PORTRAIT | 0 | Portrait mode.| 1277| ORIENTATION_MODE_LANDSCAPE | 1 | Landscape mode.| 1278| ORIENTATION_MODE_REVERSE_LANDSCAPE | 2 | Reverse landscape mode.| 1279| ORIENTATION_MODE_REVERSE_PORTRAIT | 3 | Reverse portrait mode.| 1280| ORIENTATION_MODE_NONE | 4 | Adaptive mode.| 1281 1282## PrinterStatus<sup>14+</sup> 1283 1284Enumerates the printer states. 1285 1286**System capability**: SystemCapability.Print.PrintFramework 1287 1288| **Name**| **Value**| **Description**| 1289| -------- | -------- | -------- | 1290| PRINTER_IDLE | 0 | The printer is idle.| 1291| PRINTER_BUSY | 1 | The printer is busy.| 1292| PRINTER_UNAVAILABLE | 2 | The printer is unavailable.| 1293 1294## PrinterPreferences<sup>18+</sup> 1295 1296Defines the printer preferences. 1297 1298**System capability**: SystemCapability.Print.PrintFramework 1299 1300**Attributes** 1301| **Name**| **Type**| **Mandatory**| **Description**| 1302| -------- | -------- | -------- | -------- | 1303| defaultDuplexMode | [PrintDuplexMode](#printduplexmode11) | No| Default duplex mode.| 1304| defaultPrintQuality | [PrintQuality](#printquality14) | No| Default print quality.| 1305| defaultMediaType | string | No| Default paper type.| 1306| defaultPageSizeId | string | No| ID of the default paper size. The value can be a standard paper size defined by the International Organization for Standardization (ISO), for example, ISO_A4, or a non-standard paper size defined in the system, for example, Custom.178 × 254 mm.| 1307| defaultOrientation | [PrintOrientationMode](#printorientationmode14) | No| Default print orientation.| 1308| borderless | boolean | No| Whether to print without margins. The value **true** means to print without margins, and **false** means the opposite. The default value is **false**.| 1309| options | string | No| Other fields in the printer preferences. The fields are queried from the printer or obtained from the printer driver and stored in the string in JSON format.| 1310 1311## PrinterEvent<sup>18+</sup> 1312 1313Enumerates printer-related events. 1314 1315**System capability**: SystemCapability.Print.PrintFramework 1316 1317| **Name**| **Value**| **Description**| 1318| -------- | -------- | -------- | 1319| PRINTER_EVENT_ADDED | 0 | Printer added.| 1320| PRINTER_EVENT_DELETED | 1 | Printer deleted.| 1321| PRINTER_EVENT_STATE_CHANGED | 2 | Printer state changed.| 1322| PRINTER_EVENT_INFO_CHANGED | 3 | Printer information changed.| 1323| PRINTER_EVENT_PREFERENCE_CHANGED | 4 | Printer preferences changed.| 1324| PRINTER_EVENT_LAST_USED_PRINTER_CHANGED | 5 | The last used printer changed.| 1325 1326## DefaultPrinterType<sup>18+</sup> 1327 1328Enumerates default printer types. 1329 1330**System capability**: SystemCapability.Print.PrintFramework 1331 1332| **Name**| **Value**| **Description**| 1333| -------- | -------- | -------- | 1334| DEFAULT_PRINTER_TYPE_SET_BY_USER | 0 | The printer set by the user serves as the default printer.| 1335| DEFAULT_PRINTER_TYPE_LAST_USED_PRINTER | 1 | The printer used last time serves as the default printer.| 1336 1337## print.getAddedPrinters<sup>18+</sup> 1338 1339getAddedPrinters(): Promise<Array<string>> 1340 1341Obtains the list of printers added to the system. This API uses a promise to return the result. 1342 1343**Required permissions**: ohos.permission.MANAGE_PRINT_JOB or ohos.permission.PRINT 1344 1345**System capability**: SystemCapability.Print.PrintFramework 1346 1347**Return value** 1348| **Type**| **Description**| 1349| -------- | -------- | 1350| Promise<Array<string>> | Promise used to return the result.| 1351 1352**Error codes** 1353 1354For details about the error codes, see [Error Codes of the Print Service](./errorcode-print.md). 1355 1356| ID| Error Message | 1357| -------- | ------------------------------------------- | 1358| 201 | the application does not have permission to call this function. | 1359 1360**Example** 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 1378Defines a callback that takes the printer event and printer information as parameters. 1379 1380**System capability**: SystemCapability.Print.PrintFramework 1381 1382**Parameters** 1383| **Name**| **Type**| **Mandatory**| **Description**| 1384| -------- | -------- | -------- | -------- | 1385| event | [PrinterEvent](#printerevent18) | Yes| Printer event.| 1386| printerInformation | PrinterInformation | Yes| Printer information.| 1387 1388## print.on<sup>18+</sup> 1389 1390on(type: 'printerChange', callback: PrinterChangeCallback): void 1391 1392Registers a listener for the printer change events. 1393 1394**Required permissions**: ohos.permission.PRINT 1395 1396**System capability**: SystemCapability.Print.PrintFramework 1397 1398**Parameters** 1399| **Name**| **Type**| **Mandatory**| **Description**| 1400| -------- | -------- | -------- | -------- | 1401| type | 'printerChange' | Yes| Printer change event.| 1402| callback | [PrinterChangeCallback](#printerchangecallback18) | Yes| Callback to be invoked when the printer changes.| 1403 1404**Error codes** 1405 1406For details about the error codes, see [Error Codes of the Print Service](./errorcode-print.md). 1407 1408| ID| Error Message | 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**Example** 1414 1415```ts 1416import { print } from '@kit.BasicServicesKit'; 1417 1418// Trigger this callback when an 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 1430Unregisters the listener for printer state change events. This API uses a callback to return the result. 1431 1432**Required permissions**: ohos.permission.PRINT 1433 1434**System capability**: SystemCapability.Print.PrintFramework 1435 1436**Parameters** 1437| **Name**| **Type**| **Mandatory**| **Description**| 1438| -------- | -------- | -------- | -------- | 1439| type | 'printerChange' | Yes| Printer change event.| 1440| callback | [PrinterChangeCallback](#printerchangecallback18) | No| Callback to unregister.| 1441 1442**Error codes** 1443 1444For details about the error codes, see [Error Codes of the Print Service](./errorcode-print.md). 1445 1446| ID| Error Message | 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**Example** 1452 1453```ts 1454import { print } from '@kit.BasicServicesKit'; 1455 1456// Trigger this callback when an 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<string>, callback: AsyncCallback<void>): void 1468 1469Discovers printers by specifying the extension list. The discovered printers contain the specified print extension abilities. If an empty extension list is specified, all extension abilities are loaded. This API uses an asynchronous callback to return the result. 1470 1471**Required permissions**: ohos.permission.MANAGE_PRINT_JOB or ohos.permission.PRINT 1472 1473**System capability**: SystemCapability.Print.PrintFramework 1474 1475**Parameters** 1476| **Name**| **Type**| **Mandatory**| **Description**| 1477| -------- | -------- | -------- | -------- | 1478| extensionList | Array<string> | Yes| List of [PrintExtensionAbilities](./js-apis-app-ability-PrintExtensionAbility.md) to be loaded. The list members are the bundle names of the applications with print extension abilities. An empty list indicates that all extension abilities are loaded.| 1479| callback | AsyncCallback<void> | Yes| Callback used to return the result.| 1480 1481**Error codes** 1482 1483For details about the error codes, see [Error Codes of the Print Service](./errorcode-print.md). 1484 1485| ID| Error Message | 1486| -------- | ------------------------------------------- | 1487| 201 | the application does not have permission to call this function. | 1488 1489**Example** 1490 1491```ts 1492import { print } from '@kit.BasicServicesKit'; 1493import { BusinessError } from '@ohos.base'; 1494 1495// Load all print extension abilities. 1496let extensionList: string[] = []; 1497// Specify the bundle name of your applications to load required print extension abilities during printer discovery. 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<string>): Promise<void> 1511 1512Discovers printers by specifying the extension list. The discovered printers contain the specified print extension abilities. If an empty extension list is specified, all extension abilities are loaded. This API uses a promise to return the result. 1513 1514**Required permissions**: ohos.permission.MANAGE_PRINT_JOB or ohos.permission.PRINT 1515 1516**System capability**: SystemCapability.Print.PrintFramework 1517 1518**Parameters** 1519| **Name**| **Type**| **Mandatory**| **Description**| 1520| -------- | -------- | -------- | -------- | 1521| extensionList | Array<string> | Yes| List of [PrintExtensionAbilities](./js-apis-app-ability-PrintExtensionAbility.md) to be loaded. The list members are the bundle names of the applications with print extension abilities. An empty list indicates that all extension abilities are loaded.| 1522 1523**Return value** 1524| **Type**| **Description**| 1525| -------- | -------- | 1526| Promise<void> | Promise used to return the result.| 1527 1528**Error codes** 1529 1530For details about the error codes, see [Error Codes of the Print Service](./errorcode-print.md). 1531 1532| ID| Error Message | 1533| -------- | ------------------------------------------- | 1534| 201 | the application does not have permission to call this function. | 1535 1536**Example** 1537 1538```ts 1539import { print } from '@kit.BasicServicesKit'; 1540import { BusinessError } from '@ohos.base'; 1541 1542// Load all print extension abilities. 1543let extensionList: string[] = []; 1544// Specify the bundle name of your applications to load required print extension abilities during printer discovery. 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<void>): void 1556 1557Stops discovering printers. This API uses an asynchronous callback to return the result. 1558 1559**Required permissions**: ohos.permission.MANAGE_PRINT_JOB or ohos.permission.PRINT 1560 1561**System capability**: SystemCapability.Print.PrintFramework 1562 1563**Parameters** 1564| **Name**| **Type**| **Mandatory**| **Description**| 1565| -------- | -------- | -------- | -------- | 1566| callback | AsyncCallback<void> | Yes| Callback used to return the result.| 1567 1568**Error codes** 1569 1570For details about the error codes, see [Error Codes of the Print Service](./errorcode-print.md). 1571 1572| ID| Error Message | 1573| -------- | ------------------------------------------- | 1574| 201 | the application does not have permission to call this function. | 1575 1576**Example** 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<void> 1594 1595Stops discovering printers. This API uses a promise to return the result. 1596 1597**Required permissions**: ohos.permission.MANAGE_PRINT_JOB or ohos.permission.PRINT 1598 1599**System capability**: SystemCapability.Print.PrintFramework 1600 1601**Return value** 1602| **Type**| **Description**| 1603| -------- | -------- | 1604| Promise<void> | Promise used to return the result.| 1605 1606**Error codes** 1607 1608For details about the error codes, see [Error Codes of the Print Service](./errorcode-print.md). 1609 1610| ID| Error Message | 1611| -------- | ------------------------------------------- | 1612| 201 | the application does not have permission to call this function. | 1613 1614**Example** 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<void>): void 1630 1631Connects to a printer by printer ID. This API uses an asynchronous callback to return the result. 1632 1633**Required permissions**: ohos.permission.MANAGE_PRINT_JOB or ohos.permission.PRINT 1634 1635**System capability**: SystemCapability.Print.PrintFramework 1636 1637**Parameters** 1638| **Name**| **Type**| **Mandatory**| **Description**| 1639| -------- | -------- | -------- | -------- | 1640| printerId | string | Yes| Printer ID.| 1641| callback | AsyncCallback<void> | Yes| Callback used to return the result.| 1642 1643**Error codes** 1644 1645For details about the error codes, see [Error Codes of the Print Service](./errorcode-print.md). 1646 1647| ID| Error Message | 1648| -------- | ------------------------------------------- | 1649| 201 | the application does not have permission to call this function. | 1650 1651**Example** 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<void> 1670 1671Connects to a printer by printer ID. This API uses a promise to return the result. 1672 1673**Required permissions**: ohos.permission.MANAGE_PRINT_JOB or ohos.permission.PRINT 1674 1675**System capability**: SystemCapability.Print.PrintFramework 1676 1677**Parameters** 1678| **Name**| **Type**| **Mandatory**| **Description**| 1679| -------- | -------- | -------- | -------- | 1680| printerId | string | Yes| Printer ID.| 1681 1682**Return value** 1683| **Type**| **Description**| 1684| -------- | -------- | 1685| Promise<void> |Promise used to return the result.| 1686 1687**Error codes** 1688 1689For details about the error codes, see [Error Codes of the Print Service](./errorcode-print.md). 1690 1691| ID| Error Message | 1692| -------- | ------------------------------------------- | 1693| 201 | the application does not have permission to call this function. | 1694 1695**Example** 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