1# @ohos.print (Print) 2 3The **print** module provides APIs for basic print operations. 4 5> **NOTE** 6> 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. 7 8## Modules to Import 9 10```ts 11import { print } from '@kit.BasicServicesKit'; 12``` 13 14## PrintTask 15 16Implements event listeners for print tasks. 17 18### on 19 20on(type: 'block', callback: Callback<void>): void 21 22Registers a listener for the print task blocking event. This API uses a callback to return the result. 23 24**Required permissions**: ohos.permission.PRINT 25 26**System capability**: SystemCapability.Print.PrintFramework 27 28**Parameters** 29| **Name**| **Type**| **Mandatory**| **Description**| 30| -------- | -------- | -------- | -------- | 31| type | string | Yes| Listening type.<br>The value is fixed at **'block'**,<br>indicating blocking of the print task.| 32| callback | Callback<void> | Yes| Callback used to return the result.| 33 34**Error codes** 35 36For details about the error codes, see [Error Codes of the Print Service](./errorcode-print.md). 37 38| ID| Error Message | 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**Example** 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 64Registers a listener for the print task blocking event. This API uses a callback to return the result. 65 66**Required permissions**: ohos.permission.PRINT 67 68**System capability**: SystemCapability.Print.PrintFramework 69 70**Parameters** 71| **Name**| **Type**| **Mandatory**| **Description**| 72| -------- | -------- | -------- | -------- | 73| type | string | Yes| Listening type.<br>The value is fixed at **'succeed'**,<br>indicating success of the print task.| 74| callback | Callback<void> | Yes| Callback used to return the result.| 75 76**Error codes** 77 78For details about the error codes, see [Error Codes of the Print Service](./errorcode-print.md). 79 80| ID| Error Message | 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**Example** 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 106Registers a listener for the print task blocking event. This API uses a callback to return the result. 107 108**Required permissions**: ohos.permission.PRINT 109 110**System capability**: SystemCapability.Print.PrintFramework 111 112**Parameters** 113| **Name**| **Type**| **Mandatory**| **Description**| 114| -------- | -------- | -------- | -------- | 115| type | string | Yes| Listening type.<br>The value is fixed at **'fail'**,<br>indicating failure of the print task.| 116| callback | Callback<void> | Yes| Callback used to return the result.| 117 118**Error codes** 119 120For details about the error codes, see [Error Codes of the Print Service](./errorcode-print.md). 121 122| ID| Error Message | 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**Example** 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 148Registers a listener for the print task blocking event. This API uses a callback to return the result. 149 150**Required permissions**: ohos.permission.PRINT 151 152**System capability**: SystemCapability.Print.PrintFramework 153 154**Parameters** 155| **Name**| **Type**| **Mandatory**| **Description**| 156| -------- | -------- | -------- | -------- | 157| type | string | Yes| Listening type.<br>The value is fixed at **'cancel'**,<br>indicating canceling of the print task.| 158| callback | Callback<void> | Yes| Callback used to return the result.| 159 160**Error codes** 161 162For details about the error codes, see [Error Codes of the Print Service](./errorcode-print.md). 163 164| ID| Error Message | 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**Example** 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 190Unregisters the listener for the print task blocking event. This API uses a callback to return the result. 191 192**Required permissions**: ohos.permission.PRINT 193 194**System capability**: SystemCapability.Print.PrintFramework 195 196**Parameters** 197| **Name**| **Type**| **Mandatory**| **Description**| 198| -------- | -------- | -------- | -------- | 199| type | string | Yes| Listening type.<br>The value is fixed at **'block'**,<br>indicating blocking of the print task.| 200| callback | Callback<void> | No| Callback used to return the result.| 201 202**Error codes** 203 204For details about the error codes, see [Error Codes of the Print Service](./errorcode-print.md). 205 206| ID| Error Message | 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**Example** 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 232Unregisters the listener for the print task blocking event. This API uses a callback to return the result. 233 234**Required permissions**: ohos.permission.PRINT 235 236**System capability**: SystemCapability.Print.PrintFramework 237 238**Parameters** 239| **Name**| **Type**| **Mandatory**| **Description**| 240| -------- | -------- | -------- | -------- | 241| type | string | Yes| Listening type.<br>The value is fixed at **'succeed'**,<br>indicating success of the print task.| 242| callback | Callback<void> | No| Callback used to return the result.| 243 244**Error codes** 245 246For details about the error codes, see [Error Codes of the Print Service](./errorcode-print.md). 247 248| ID| Error Message | 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**Example** 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 274Unregisters the listener for the print task blocking event. This API uses a callback to return the result. 275 276**Required permissions**: ohos.permission.PRINT 277 278**System capability**: SystemCapability.Print.PrintFramework 279 280**Parameters** 281| **Name**| **Type**| **Mandatory**| **Description**| 282| -------- | -------- | -------- | -------- | 283| type | string | Yes| Listening type.<br>The value is fixed at **'fail'**,<br>indicating failure of the print task.| 284| callback | Callback<void> | No| Callback used to return the result.| 285 286**Error codes** 287 288For details about the error codes, see [Error Codes of the Print Service](./errorcode-print.md). 289 290| ID| Error Message | 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**Example** 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 316Unregisters the listener for the print task blocking event. This API uses a callback to return the result. 317 318**Required permissions**: ohos.permission.PRINT 319 320**System capability**: SystemCapability.Print.PrintFramework 321 322**Parameters** 323| **Name**| **Type**| **Mandatory**| **Description**| 324| -------- | -------- | -------- | -------- | 325| type | string | Yes| Listening type.<br>The value is fixed at **'cancel'**,<br>indicating canceling of the print task.| 326| callback | Callback<void> | No| Callback used to return the result.| 327 328**Error codes** 329 330For details about the error codes, see [Error Codes of the Print Service](./errorcode-print.md). 331 332| ID| Error Message | 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**Example** 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 356Provides information about the document to print. This API must be implemented by a third-party application. 357 358### onStartLayoutWrite 359 360onStartLayoutWrite(jobId: string, oldAttrs: PrintAttributes, newAttrs: PrintAttributes, fd: number, writeResultCallback: (jobId: string, writeResult: PrintFileCreationState) => void): void 361 362Updates the file to be printed. This API uses **writeResultCallback** as the callback. 363 364**Required permissions**: ohos.permission.PRINT 365 366**System capability**: SystemCapability.Print.PrintFramework 367 368**Parameters** 369| **Name**| **Type**| **Mandatory**| **Description**| 370| -------- | -------- | -------- | -------- | 371| jobId | string | Yes| ID of the print job.| 372| oldAttrs | PrintAttributes | Yes| Old print attributes.| 373| newAttrs | PrintAttributes | Yes| New print attributes.| 374| fd | number | Yes| File descriptor.| 375| writeResultCallback | (jobId: string, writeResult: PrintFileCreationState) | Yes| Callback used to return the result.| 376 377**Error codes** 378 379For details about the error codes, see [Error Codes of the Print Service](./errorcode-print.md). 380 381| ID| Error Message | 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**Example** 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 417Registers a listener for print job state changes. 418 419**Required permissions**: ohos.permission.PRINT 420 421**System capability**: SystemCapability.Print.PrintFramework 422 423**Parameters** 424| **Name**| **Type**| **Mandatory**| **Description**| 425| -------- | -------- | -------- | -------- | 426| jobId | string | Yes| ID of the print job.| 427| state | PrintDocumentAdapterState | Yes| New state of the print job.| 428 429**Error codes** 430 431For details about the error codes, see [Error Codes of the Print Service](./errorcode-print.md). 432 433| ID| Error Message | 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**Example** 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 469Prints files. This API uses an asynchronous callback to return the result. 470 471**Required permissions**: ohos.permission.PRINT 472 473**System capability**: SystemCapability.Print.PrintFramework 474 475**Parameters** 476| **Name**| **Type**| **Mandatory**| **Description**| 477| -------- | -------- | -------- | -------- | 478| files | Array<string> | Yes| List of files to print. Images (in .jpg, .png, .gif, .bmp, or .webp format) and PDF files are supported. Before a system application passes in the URI, it needs to call the **uriPermissionManager.grantUriPermission()** API to authorize the print application. This API is a system API. [print](#print11-2) is recommended for third-party application.| 479| callback | AsyncCallback<PrintTask> | Yes| Callback used to return the result.| 480 481**Error codes** 482 483For details about the error codes, see [Error Codes of the Print Service](./errorcode-print.md). 484 485| ID| Error Message | 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**Example** 491 492```ts 493import { print } from '@kit.BasicServicesKit'; 494import { BusinessError } from '@ohos.base'; 495 496// Pass in the URIs of the files. 497let files = ['file://data/print/a.png', 'file://data/print/b.png']; 498// Alternatively, pass in the 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 516Prints files. This API uses a promise to return the result. 517 518**Required permissions**: ohos.permission.PRINT 519 520**System capability**: SystemCapability.Print.PrintFramework 521 522**Parameters** 523| **Name**| **Type**| **Mandatory**| **Description**| 524| -------- | -------- | -------- | -------- | 525| files | Array<string> | Yes| List of files to print. Images (in .jpg, .png, .gif, .bmp, or .webp format) and PDF files are supported. Before a system application passes in the URI, it needs to call the **uriPermissionManager.grantUriPermission()** API to authorize the print application. This API is a system API. [print](#print11-2) is recommended for third-party application.| 526 527**Return value** 528| **Type**| **Description**| 529| -------- | -------- | 530| Promise<PrintTask> | Print result.| 531 532**Error codes** 533 534For details about the error codes, see [Error Codes of the Print Service](./errorcode-print.md). 535 536| ID| Error Message | 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**Example** 542 543```ts 544import { print } from '@kit.BasicServicesKit'; 545import { BusinessError } from '@ohos.base'; 546 547// Pass in the URIs of the files. 548let files = ['file://data/print/a.png', 'file://data/print/b.png']; 549// Alternatively, pass in the 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 565Prints files. This API uses an asynchronous callback to return the result. 566 567**Required permissions**: ohos.permission.PRINT 568 569**System capability**: SystemCapability.Print.PrintFramework 570 571**Parameters** 572| **Name**| **Type**| **Mandatory**| **Description**| 573| -------- | -------- | -------- | -------- | 574| files | Array<string> | Yes| List of files to print. Images (in .jpg, .png, .gif, .bmp, or .webp format) and PDF files are supported. Before a system application passes in the URI, it needs to call the **uriPermissionManager.grantUriPermission()** API to authorize the print application. This API is a system API. [print](#print11-2) is recommended for third-party application.| 575| context | Context | Yes| UIAbility context used to start printing.| 576| callback | AsyncCallback<PrintTask> | Yes| Callback used to return the result.| 577 578**Error codes** 579 580For details about the error codes, see [Error Codes of the Print Service](./errorcode-print.md). 581 582| ID| Error Message | 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**Example** 588 589```ts 590import { print } from '@kit.BasicServicesKit'; 591import { BusinessError } from '@ohos.base'; 592 593// Pass in the URIs of the files. 594let files = ['file://data/print/a.png', 'file://data/print/b.png']; 595// Alternatively, pass in the 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 614Prints files. This API uses a promise to return the result. 615 616**Required permissions**: ohos.permission.PRINT 617 618**System capability**: SystemCapability.Print.PrintFramework 619 620**Parameters** 621| **Name**| **Type**| **Mandatory**| **Description**| 622| -------- | -------- | -------- | -------- | 623| files | Array<string> | Yes| List of files to print. Images (in .jpg, .png, .gif, .bmp, or .webp format) and PDF files are supported. Before a system application passes in the URI, it needs to call the **uriPermissionManager.grantUriPermission()** API to authorize the print application. This API is a system API. [print](#print11-2) is recommended for third-party application.| 624| context | Context | Yes| UIAbility context used to start printing.| 625 626**Return value** 627| **Type**| **Description**| 628| -------- | -------- | 629| Promise<PrintTask> | Print result.| 630 631**Error codes** 632 633For details about the error codes, see [Error Codes of the Print Service](./errorcode-print.md). 634 635| ID| Error Message | 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**Example** 641 642```ts 643import { print } from '@kit.BasicServicesKit'; 644import { BusinessError } from '@ohos.base'; 645 646// Pass in the URIs of the files. 647let files = ['file://data/print/a.png', 'file://data/print/b.png']; 648// Alternatively, pass in the 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 665Prints a file. This API uses a promise to return the result. 666 667**Required permissions**: ohos.permission.PRINT 668 669**System capability**: SystemCapability.Print.PrintFramework 670 671**Parameters** 672| **Name**| **Type**| **Mandatory**| **Description**| 673| -------- | -------- | -------- | -------- | 674| jobName | string | Yes| Name of the file to print.| 675| printAdapter | PrintDocumentAdapter | Yes| Feature implemented by a third-party application.| 676| printAttributes | PrintAttributes | Yes| Print attributes.| 677| context | Context | Yes| UIAbility context used to start printing.| 678 679**Return value** 680| **Type**| **Description**| 681| -------- | -------- | 682| Promise<PrintTask> | Print result.| 683 684**Error codes** 685 686For details about the error codes, see [Error Codes of the Print Service](./errorcode-print.md). 687 688| ID| Error Message | 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**Example** 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 727Defines the print attributes. 728 729**System capability**: SystemCapability.Print.PrintFramework 730 731**Attributes** 732| **Name**| **Type**| **Mandatory**| **Description**| 733| -------- | -------- | -------- | -------- | 734| copyNumber | number | No| Copy of the file list.| 735| pageRange | PrintPageRange | No| Range of pages to print.| 736| pageSize | PrintPageSize \| PrintPageType | No| Page size of the files to print.| 737| directionMode | PrintDirectionMode | No| Print direction mode.| 738| colorMode | PrintColorMode | No| Color mode of the files to print.| 739| duplexMode | PrintDuplexMode | No| Duplex mode of the files to print.| 740 741## PrintPageRange<sup>11+</sup> 742 743Defines the print range. 744 745**System capability**: SystemCapability.Print.PrintFramework 746 747**Attributes** 748| **Name**| **Type**| **Mandatory**| **Description**| 749| -------- | -------- | -------- | -------- | 750| startPage | number | No| Start page.| 751| endPage | number | No| End page.| 752| pages | Array<number> | No| Discrete pages.| 753 754 755## PrintPageSize<sup>11+</sup> 756 757Defines the size of the printed page. 758 759**System capability**: SystemCapability.Print.PrintFramework 760 761**Attributes** 762| **Name**| **Type**| **Mandatory**| **Description**| 763| -------- | -------- | -------- | -------- | 764| id | string | Yes| Page size ID.| 765| name | string | Yes| Page size name.| 766| width | number | Yes| Page width, in millimeters.| 767| height | number | Yes| Page height, in millimeters.| 768 769 770 771## PrintDirectionMode<sup>11+</sup> 772 773Enumerates the print direction modes. 774 775**System capability**: SystemCapability.Print.PrintFramework 776 777| **Name**| **Value**| **Description**| 778| -------- | -------- | -------- | 779| DIRECTION_MODE_AUTO | 0 | Automatic.| 780| DIRECTION_MODE_PORTRAIT | 1 | Portrait mode.| 781| DIRECTION_MODE_LANDSCAPE | 2 | Landscape mode.| 782 783## PrintColorMode<sup>11+</sup> 784 785Enumerates the color modes. 786 787**System capability**: SystemCapability.Print.PrintFramework 788 789| **Name**| **Value**| **Description**| 790| -------- | -------- | -------- | 791| COLOR_MODE_MONOCHROME | 0 | Black and white.| 792| COLOR_MODE_COLOR | 1 | Color.| 793 794## PrintDuplexMode<sup>11+</sup> 795 796Enumerates the duplex modes. 797 798**System capability**: SystemCapability.Print.PrintFramework 799 800| **Name**| **Value**| **Description**| 801| -------- | -------- | -------- | 802| DUPLEX_MODE_NONE | 0 | Simplex (single-sided).| 803| DUPLEX_MODE_LONG_EDGE | 1 | Duplex (double-sided) with flipping on long edge.| 804| DUPLEX_MODE_SHORT_EDGE | 2 | Duplex (double-sided) with flipping on short edge.| 805 806## PrintPageType<sup>11+</sup> 807 808Enumerates the print page types. 809 810**System capability**: SystemCapability.Print.PrintFramework 811 812| **Name**| **Value**| **Description**| 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 | 4 x 6 photo paper.| 823| PAGE_PHOTO_5X7 | 9 | 5 x 7 photo paper.| 824| PAGE_INT_DL_ENVELOPE | 10 | International envelope DL.| 825| PAGE_B_TABLOID | 11 | B Tabloid.| 826 827## PrintDocumentAdapterState<sup>11+</sup> 828 829Enumerates the print job states. 830 831**System capability**: SystemCapability.Print.PrintFramework 832 833| **Name**| **Value**| **Description**| 834| -------- | -------- | -------- | 835| PREVIEW_DESTROY | 0 | The preview fails.| 836| PRINT_TASK_SUCCEED | 1 | The print job is successful.| 837| PRINT_TASK_FAIL | 2 | The print job failed.| 838| PRINT_TASK_CANCEL | 3 | The print job is canceled.| 839| PRINT_TASK_BLOCK | 4 | The print job is blocked.| 840 841## PrintFileCreationState<sup>11+</sup> 842 843Enumerates the print file creation status. 844 845**System capability**: SystemCapability.Print.PrintFramework 846 847| **Name**| **Value**| **Description**| 848| -------- | -------- | -------- | 849| PRINT_FILE_CREATED | 0 | The print file is created successfully.| 850| PRINT_FILE_CREATION_FAILED | 1 | The print file fails to be created.| 851| PRINT_FILE_CREATED_UNRENDERED | 2 | The print file is successfully created but not rendered.| 852