openharmony 鸿蒙 js-apis-print

2025-06-12 浏览 (1)

@ohos.print (Print)

The print module provides APIs for basic print operations.

NOTE 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.

Modules to Import

import { print } from '@kit.BasicServicesKit';

print.PrintTask

Implements event listeners for print tasks.

on

on(type: 'block', callback: Callback<void>): void

Registers a listener for the print task blocking event. This API uses a callback to return the result.

Required permissions: ohos.permission.PRINT

System capability: SystemCapability.Print.PrintFramework

Parameters

NameTypeMandatoryDescription
typestringYesListening type.
The value is fixed at 'block',
indicating blocking of the print task.
callbackCallback<void>YesCallback used to return the result.

Error codes

For details about the error codes, see Error Codes of the Print Service.

IDError Message
201the application does not have permission to call this function.
401Parameter error. Possible causes: 1.Mandatory parameters are left unspecified; 2.Incorrect parameter types.

Example

import { print } from '@kit.BasicServicesKit';
import { BusinessError } from '@ohos.base';

let file = ['file://data/print/a.png', 'file://data/print/b.png'];
print.print(file).then((printTask: print.PrintTask) => {
    printTask.on('block', () => {
        console.log('print state is block');
    })
    // ...
}).catch((error: BusinessError) => {
    console.log('print err ' + JSON.stringify(error));
})

on

on(type: 'succeed', callback: Callback<void>): void

Registers a listener for the print task blocking event. This API uses a callback to return the result.

Required permissions: ohos.permission.PRINT

System capability: SystemCapability.Print.PrintFramework

Parameters

NameTypeMandatoryDescription
typestringYesListening type.
The value is fixed at 'succeed',
indicating success of the print task.
callbackCallback<void>YesCallback used to return the result.

Error codes

For details about the error codes, see Error Codes of the Print Service.

IDError Message
201the application does not have permission to call this function.
401Parameter error. Possible causes: 1.Mandatory parameters are left unspecified; 2.Incorrect parameter types.

Example

import { print } from '@kit.BasicServicesKit';
import { BusinessError } from '@ohos.base';

let file = ['file://data/print/a.png', 'file://data/print/b.png'];
print.print(file).then((printTask: print.PrintTask) => {
    printTask.on('succeed', () => {
        console.log('print state is succeed');
    })
    // ...
}).catch((error: BusinessError) => {
    console.log('print err ' + JSON.stringify(error));
})

on

on(type: 'fail', callback: Callback<void>): void

Registers a listener for the print task blocking event. This API uses a callback to return the result.

Required permissions: ohos.permission.PRINT

System capability: SystemCapability.Print.PrintFramework

Parameters

NameTypeMandatoryDescription
typestringYesListening type.
The value is fixed at 'fail',
indicating failure of the print task.
callbackCallback<void>YesCallback used to return the result.

Error codes

For details about the error codes, see Error Codes of the Print Service.

IDError Message
201the application does not have permission to call this function.
401Parameter error. Possible causes: 1.Mandatory parameters are left unspecified; 2.Incorrect parameter types.

Example

import { print } from '@kit.BasicServicesKit';
import { BusinessError } from '@ohos.base';

let file = ['file://data/print/a.png', 'file://data/print/b.png'];
print.print(file).then((printTask: print.PrintTask) => {
    printTask.on('fail', () => {
        console.log('print state is fail');
    })
    // ...
}).catch((error: BusinessError) => {
    console.log('print err ' + JSON.stringify(error));
})

on

on(type: 'cancel', callback: Callback<void>): void

Registers a listener for the print task blocking event. This API uses a callback to return the result.

Required permissions: ohos.permission.PRINT

System capability: SystemCapability.Print.PrintFramework

Parameters

NameTypeMandatoryDescription
typestringYesListening type.
The value is fixed at 'cancel',
indicating canceling of the print task.
callbackCallback<void>YesCallback used to return the result.

Error codes

For details about the error codes, see Error Codes of the Print Service.

IDError Message
201the application does not have permission to call this function.
401Parameter error. Possible causes: 1.Mandatory parameters are left unspecified; 2.Incorrect parameter types.

Example

import { print } from '@kit.BasicServicesKit';
import { BusinessError } from '@ohos.base';

let file = ['file://data/print/a.png', 'file://data/print/b.png'];
print.print(file).then((printTask: print.PrintTask) => {
    printTask.on('cancel', () => {
        console.log('print state is cancel');
    })
    // ...
}).catch((error: BusinessError) => {
    console.log('print err ' + JSON.stringify(error));
})

off

off(type: 'block', callback?: Callback<void>): void

Unregisters the listener for the print task blocking event. This API uses a callback to return the result.

Required permissions: ohos.permission.PRINT

System capability: SystemCapability.Print.PrintFramework

Parameters

NameTypeMandatoryDescription
typestringYesListening type.
The value is fixed at 'block',
indicating blocking of the print task.
callbackCallback<void>NoCallback used to return the result.

Error codes

For details about the error codes, see Error Codes of the Print Service.

IDError Message
201the application does not have permission to call this function.
401Parameter error. Possible causes: 1.Mandatory parameters are left unspecified; 2.Incorrect parameter types.

Example

import { print } from '@kit.BasicServicesKit';
import { BusinessError } from '@ohos.base';

let file = ['file://data/print/a.png', 'file://data/print/b.png'];
print.print(file).then((printTask: print.PrintTask) => {
    printTask.off('block', () => {
        console.log('unregister state block');
    })
    // ...
}).catch((error: BusinessError) => {
    console.log('print err ' + JSON.stringify(error));
})

off

off(type: 'succeed', callback?: Callback<void>): void

Unregisters the listener for the print task blocking event. This API uses a callback to return the result.

Required permissions: ohos.permission.PRINT

System capability: SystemCapability.Print.PrintFramework

Parameters

NameTypeMandatoryDescription
typestringYesListening type.
The value is fixed at 'succeed',
indicating success of the print task.
callbackCallback<void>NoCallback used to return the result.

Error codes

For details about the error codes, see Error Codes of the Print Service.

IDError Message
201the application does not have permission to call this function.
401Parameter error. Possible causes: 1.Mandatory parameters are left unspecified; 2.Incorrect parameter types.

Example

import { print } from '@kit.BasicServicesKit';
import { BusinessError } from '@ohos.base';

let file = ['file://data/print/a.png', 'file://data/print/b.png'];
print.print(file).then((printTask: print.PrintTask) => {
    printTask.off('succeed', () => {
        console.log('unregister state succeed');
    })
    // ...
}).catch((error: BusinessError) => {
    console.log('print err ' + JSON.stringify(error));
})

off

off(type: 'fail', callback?: Callback<void>): void

Unregisters the listener for the print task blocking event. This API uses a callback to return the result.

Required permissions: ohos.permission.PRINT

System capability: SystemCapability.Print.PrintFramework

Parameters

NameTypeMandatoryDescription
typestringYesListening type.
The value is fixed at 'fail',
indicating failure of the print task.
callbackCallback<void>NoCallback used to return the result.

Error codes

For details about the error codes, see Error Codes of the Print Service.

IDError Message
201the application does not have permission to call this function.
401Parameter error. Possible causes: 1.Mandatory parameters are left unspecified; 2.Incorrect parameter types.

Example

import { print } from '@kit.BasicServicesKit';
import { BusinessError } from '@ohos.base';

let file = ['file://data/print/a.png', 'file://data/print/b.png'];
print.print(file).then((printTask: print.PrintTask) => {
    printTask.off('fail', () => {
        console.log('unregister state fail');
    })
    // ...
}).catch((error: BusinessError) => {
    console.log('print err ' + JSON.stringify(error));
})

off

off(type: 'cancel', callback?: Callback<void>): void

Unregisters the listener for the print task blocking event. This API uses a callback to return the result.

Required permissions: ohos.permission.PRINT

System capability: SystemCapability.Print.PrintFramework

Parameters

NameTypeMandatoryDescription
typestringYesListening type.
The value is fixed at 'cancel',
indicating canceling of the print task.
callbackCallback<void>NoCallback used to return the result.

Error codes

For details about the error codes, see Error Codes of the Print Service.

IDError Message
201the application does not have permission to call this function.
401Parameter error. Possible causes: 1.Mandatory parameters are left unspecified; 2.Incorrect parameter types.

Example

import { print } from '@kit.BasicServicesKit';
import { BusinessError } from '@ohos.base';

let file = ['file://data/print/a.png', 'file://data/print/b.png'];
print.print(file).then((printTask: print.PrintTask) => {
    printTask.off('cancel', () => {
        console.log('unregister state cancel');
    })
    // ...
}).catch((error: BusinessError) => {
    console.log('print err ' + JSON.stringify(error));
})

print.PrintDocumentAdapter11+

Provides information about the document to print. This API must be implemented by a third-party application.

onStartLayoutWrite

onStartLayoutWrite(jobId: string, oldAttrs: PrintAttributes, newAttrs: PrintAttributes, fd: number, writeResultCallback: (jobId: string, writeResult: PrintFileCreationState) => void): void

Sends 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.

Required permissions: ohos.permission.PRINT

System capability: SystemCapability.Print.PrintFramework

Parameters

NameTypeMandatoryDescription
jobIdstringYesID of the print job.
oldAttrsPrintAttributesYesOld print attributes.
newAttrsPrintAttributesYesNew print attributes.
fdnumberYesPDF file descriptor sent to the API caller.
writeResultCallback(jobId: string, writeResult: PrintFileCreationState)YesCallback used to print the updated file.

Error codes

For details about the error codes, see Error Codes of the Print Service.

IDError Message
201the application does not have permission to call this function.
401Parameter error. Possible causes: 1.Mandatory parameters are left unspecified; 2.Incorrect parameter types.

Example

import { print } from '@kit.BasicServicesKit';
import { BusinessError } from '@ohos.base';

class MyPrintDocumentAdapter implements print.PrintDocumentAdapter {
    onStartLayoutWrite(jobId: string, oldAttrs: print.PrintAttributes, newAttrs: print.PrintAttributes, fd: number,
        writeResultCallback: (jobId: string, writeResult: print.PrintFileCreationState) => void) {
        writeResultCallback(jobId, print.PrintFileCreationState.PRINT_FILE_CREATED);
    };
    onJobStateChanged(jobId: string, state: print.PrintDocumentAdapterState) {
        if (state == print.PrintDocumentAdapterState.PREVIEW_DESTROY) {
            console.log('PREVIEW_DESTROY');
        } else if (state == print.PrintDocumentAdapterState.PRINT_TASK_SUCCEED) {
            console.log('PRINT_TASK_SUCCEED');
        } else if (state == print.PrintDocumentAdapterState.PRINT_TASK_FAIL) {
            console.log('PRINT_TASK_FAIL');
        } else if (state == print.PrintDocumentAdapterState.PRINT_TASK_CANCEL) {
            console.log('PRINT_TASK_CANCEL');
        } else if (state == print.PrintDocumentAdapterState.PRINT_TASK_BLOCK) {
            console.log('PRINT_TASK_BLOCK');
        }
    }
}

onJobStateChanged

onJobStateChanged(jobId: string, state: PrintDocumentAdapterState): void

Registers a listener for print job state changes.

Required permissions: ohos.permission.PRINT

System capability: SystemCapability.Print.PrintFramework

Parameters

NameTypeMandatoryDescription
jobIdstringYesID of the print job.
statePrintDocumentAdapterStateYesNew state of the print job.

Error codes

For details about the error codes, see Error Codes of the Print Service.

IDError Message
201the application does not have permission to call this function.
401Parameter error. Possible causes: 1.Mandatory parameters are left unspecified; 2.Incorrect parameter types.

Example

import { print } from '@kit.BasicServicesKit';
import { BusinessError } from '@ohos.base';

class MyPrintDocumentAdapter implements print.PrintDocumentAdapter {
    onStartLayoutWrite(jobId: string, oldAttrs: print.PrintAttributes, newAttrs: print.PrintAttributes, fd: number,
        writeResultCallback: (jobId: string, writeResult: print.PrintFileCreationState) => void) {
        writeResultCallback(jobId, print.PrintFileCreationState.PRINT_FILE_CREATED);
    };
    onJobStateChanged(jobId: string, state: print.PrintDocumentAdapterState) {
        if (state == print.PrintDocumentAdapterState.PREVIEW_DESTROY) {
            console.log('PREVIEW_DESTROY');
        } else if (state == print.PrintDocumentAdapterState.PRINT_TASK_SUCCEED) {
            console.log('PRINT_TASK_SUCCEED');
        } else if (state == print.PrintDocumentAdapterState.PRINT_TASK_FAIL) {
            console.log('PRINT_TASK_FAIL');
        } else if (state == print.PrintDocumentAdapterState.PRINT_TASK_CANCEL) {
            console.log('PRINT_TASK_CANCEL');
        } else if (state == print.PrintDocumentAdapterState.PRINT_TASK_BLOCK) {
            console.log('PRINT_TASK_BLOCK');
        }
    }
}

print.print

print(files: Array<string>, callback: AsyncCallback<PrintTask>): void

Prints files. This API uses an asynchronous callback to return the result.

Required permissions: ohos.permission.PRINT

System capability: SystemCapability.Print.PrintFramework

Parameters

NameTypeMandatoryDescription
filesArray<string>YesList 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 uriPermissionManager.grantUriPermission() to authorize the print application. This API is a system API. print is recommended for third-party application.
callbackAsyncCallback<PrintTask>YesCallback used to return the result.

Error codes

For details about the error codes, see Error Codes of the Print Service.

IDError Message
201the application does not have permission to call this function.
401Parameter error. Possible causes: 1.Mandatory parameters are left unspecified; 2.Incorrect parameter types.

Example

import { print } from '@kit.BasicServicesKit';
import { BusinessError } from '@ohos.base';

// Pass in the URIs of the files.
let files = ['file://data/print/a.png', 'file://data/print/b.png'];
// Alternatively, pass in the fd.
//let files = ['fd://1', 'fd://2'];
print.print(files, (err: BusinessError, printTask: print.PrintTask) => {
    if (err) {
        console.log('print err ' + JSON.stringify(err));
    } else {
        printTask.on('succeed', () => {
            console.log('print state is succeed');
        })
        // ...
    }
})

print.print

print(files: Array<string>): Promise<PrintTask>

Prints files. This API uses a promise to return the result.

Required permissions: ohos.permission.PRINT

System capability: SystemCapability.Print.PrintFramework

Parameters

NameTypeMandatoryDescription
filesArray<string>YesList 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 uriPermissionManager.grantUriPermission() to authorize the print application. This API is a system API. print is recommended for third-party application.

Return value

TypeDescription
Promise<PrintTask>Print result.

Error codes

For details about the error codes, see Error Codes of the Print Service.

IDError Message
201the application does not have permission to call this function.
401Parameter error. Possible causes: 1.Mandatory parameters are left unspecified; 2.Incorrect parameter types.

Example

import { print } from '@kit.BasicServicesKit';
import { BusinessError } from '@ohos.base';

// Pass in the URIs of the files.
let files = ['file://data/print/a.png', 'file://data/print/b.png'];
// Alternatively, pass in the fd.
//let files = ['fd://1', 'fd://2'];
print.print(files).then((printTask: print.PrintTask) => {
    printTask.on('succeed', () => {
        console.log('print state is succeed');
    })
    // ...
}).catch((error: BusinessError) => {
    console.log('print err ' + JSON.stringify(error));
})

print.print11+

print(files: Array<string>, context: Context, callback: AsyncCallback<PrintTask>): void

Prints files. This API uses an asynchronous callback to return the result.

Required permissions: ohos.permission.PRINT

System capability: SystemCapability.Print.PrintFramework

Parameters

NameTypeMandatoryDescription
filesArray<string>YesList 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 uriPermissionManager.grantUriPermission() to authorize the print application. This API is a system API. print is recommended for third-party application.
contextContextYesUIAbilityContext used to start the system print UI.
callbackAsyncCallback<PrintTask>YesCallback used to return the result.

Error codes

For details about the error codes, see Error Codes of the Print Service.

IDError Message
201the application does not have permission to call this function.
401Parameter error. Possible causes: 1.Mandatory parameters are left unspecified; 2.Incorrect parameter types.

Example

import { print } from '@kit.BasicServicesKit';
import { BusinessError } from '@ohos.base';

// Pass in the URIs of the files.
let files = ['file://data/print/a.png', 'file://data/print/b.png'];
// Alternatively, pass in the fd.
//let files = ['fd://1', 'fd://2'];
let context = getContext(this);
print.print(files, context, (err: BusinessError, printTask: print.PrintTask) => {
    if (err) {
        console.log('print err ' + JSON.stringify(err));
    } else {
        printTask.on('succeed', () => {
            console.log('print state is succeed');
        })
        // ...
    }
})

print.print11+

print(files: Array<string>, context: Context): Promise<PrintTask>

Prints files. This API uses a promise to return the result.

Required permissions: ohos.permission.PRINT

System capability: SystemCapability.Print.PrintFramework

Parameters

NameTypeMandatoryDescription
filesArray<string>YesList 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 uriPermissionManager.grantUriPermission() to authorize the print application. This API is a system API. print is recommended for third-party application.
contextContextYesUIAbilityContext used to start the system print UI.

Return value

TypeDescription
Promise<PrintTask>Print result.

Error codes

For details about the error codes, see Error Codes of the Print Service.

IDError Message
201the application does not have permission to call this function.
401Parameter error. Possible causes: 1.Mandatory parameters are left unspecified; 2.Incorrect parameter types.

Example

import { print } from '@kit.BasicServicesKit';
import { BusinessError } from '@ohos.base';

// Pass in the URIs of the files.
let files = ['file://data/print/a.png', 'file://data/print/b.png'];
// Alternatively, pass in the fd.
//let files = ['fd://1', 'fd://2'];
let context = getContext(this);
print.print(files, context).then((printTask: print.PrintTask) => {
    printTask.on('succeed', () => {
        console.log('print state is succeed');
    })
    // ...
}).catch((error: BusinessError) => {
    console.log('print err ' + JSON.stringify(error));
})

print.print11+

print(jobName: string, printAdapter: PrintDocumentAdapter, printAttributes: PrintAttributes, context: Context): Promise<PrintTask>

Prints a file. This API uses a promise to return the result.

Required permissions: ohos.permission.PRINT

System capability: SystemCapability.Print.PrintFramework

Parameters

NameTypeMandatoryDescription
jobNamestringYesName of the file to print, for example, test.pdf. The printer uses the 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.
printAdapterPrintDocumentAdapterYesPrintDocumentAdapter API instance implemented by a third-party application.
printAttributesPrintAttributesYesPrint attributes.
contextContextYesUIAbilityContext used to start the system print UI.

Return value

TypeDescription
Promise<PrintTask>Print result.

Error codes

For details about the error codes, see Error Codes of the Print Service.

IDError Message
201the application does not have permission to call this function.
401Parameter error. Possible causes: 1.Mandatory parameters are left unspecified; 2.Incorrect parameter types.

Example

import { print } from '@kit.BasicServicesKit';
import { BusinessError } from '@ohos.base';

let jobName : string = "jobName";
let printAdapter : print.PrintDocumentAdapter|null = null;
let printAttributes : print.PrintAttributes = {
    copyNumber: 1,
    pageRange: {
        startPage: 0,
        endPage: 5,
        pages: []
    },
    pageSize: print.PrintPageType.PAGE_ISO_A3,
    directionMode: print.PrintDirectionMode.DIRECTION_MODE_AUTO,
    colorMode: print.PrintColorMode.COLOR_MODE_MONOCHROME,
    duplexMode: print.PrintDuplexMode.DUPLEX_MODE_NONE
}
let context = getContext();

print.print(jobName, printAdapter, printAttributes, context).then((printTask: print.PrintTask) => {
    printTask.on('succeed', () => {
        console.log('print state is succeed');
    })
    // ...
}).catch((error: BusinessError) => {
    console.log('print err ' + JSON.stringify(error));
})

print.PrintAttributes11+

Defines the print attributes.

System capability: SystemCapability.Print.PrintFramework

Attributes

NameTypeMandatoryDescription
copyNumbernumberNoNumber of printed file copies.
pageRangePrintPageRangeNoPage range of the file to print.
pageSizePrintPageSize |PrintPageTypeNoPage size of the file to print.
directionModePrintDirectionModeNoPrint direction mode.
colorModePrintColorModeNoColor mode of the files to print.
duplexModePrintDuplexModeNoDuplex mode of the files to print.

print.PrintPageRange11+

Defines the print range.

System capability: SystemCapability.Print.PrintFramework

Attributes

NameTypeMandatoryDescription
startPagenumberNoStart page.
endPagenumberNoEnd page.
pagesArray<number>NoPage range set of the file to print.

print.PrintPageSize11+

Defines the size of the printed page.

System capability: SystemCapability.Print.PrintFramework

Attributes

NameTypeMandatoryDescription
idstringYesPaper size ID.
namestringYesPaper size name.
widthnumberYesPage width, in millimeters.
heightnumberYesPage height, in millimeters.

print.PrintDirectionMode11+

Enumerates the print direction modes.

System capability: SystemCapability.Print.PrintFramework

NameValueDescription
DIRECTION_MODE_AUTO0Automatic.
DIRECTION_MODE_PORTRAIT1Portrait mode.
DIRECTION_MODE_LANDSCAPE2Landscape mode.

print.PrintColorMode11+

Enumerates the color modes.

System capability: SystemCapability.Print.PrintFramework

NameValueDescription
COLOR_MODE_MONOCHROME0Black and white.
COLOR_MODE_COLOR1Color.

print.PrintDuplexMode11+

Enumerates the duplex modes.

System capability: SystemCapability.Print.PrintFramework

NameValueDescription
DUPLEX_MODE_NONE0Simplex (single-sided).
DUPLEX_MODE_LONG_EDGE1Duplex (double-sided) with flipping on long edge.
DUPLEX_MODE_SHORT_EDGE2Duplex (double-sided) with flipping on short edge.

print.PrintPageType11+

Enumerates the print page types.

System capability: SystemCapability.Print.PrintFramework

NameValueDescription
PAGE_ISO_A30A3.
PAGE_ISO_A41A4.
PAGE_ISO_A52A5.
PAGE_JIS_B53B5.
PAGE_ISO_C54C5.
PAGE_ISO_DL5DL.
PAGE_LETTER6Letter.
PAGE_LEGAL7Legal.
PAGE_PHOTO_4X684 x 6 photo paper.
PAGE_PHOTO_5X795 x 7 photo paper.
PAGE_INT_DL_ENVELOPE10International envelope DL.
PAGE_B_TABLOID11B Tabloid.

print.PrintDocumentAdapterState11+

Enumerates the print job states.

System capability: SystemCapability.Print.PrintFramework

NameValueDescription
PREVIEW_DESTROY0The preview fails.
PRINT_TASK_SUCCEED1The print job is successful.
PRINT_TASK_FAIL2The print job is failed.
PRINT_TASK_CANCEL3The print job is canceled.
PRINT_TASK_BLOCK4The print job is blocked.

print.PrintFileCreationState11+

Enumerates the print file creation status.

System capability: SystemCapability.Print.PrintFramework

NameValueDescription
PRINT_FILE_CREATED0The print file is created successfully.
PRINT_FILE_CREATION_FAILED1The print file fails to be created.
PRINT_FILE_CREATED_UNRENDERED2The print file is successfully created but not rendered.

print.PrinterState14+

Enumerates the printer states.

System capability: SystemCapability.Print.PrintFramework

NameValueDescription
PRINTER_ADDED0A new printer is added.
PRINTER_REMOVED1The printer is removed.
PRINTER_CAPABILITY_UPDATED2The printer is updated.
PRINTER_CONNECTED3The printer is connected.
PRINTER_DISCONNECTED4The printer is disconnected.
PRINTER_RUNNING5The printer is running.

print.PrintJobState14+

Enumerates the print job states.

System capability: SystemCapability.Print.PrintFramework

NameValueDescription
PRINT_JOB_PREPARE0The printer is prepared for the print job.
PRINT_JOB_QUEUED1The print job is on the print queue of the printer.
PRINT_JOB_RUNNING2The print job is being executed.
PRINT_JOB_BLOCKED3The print job is blocked.
PRINT_JOB_COMPLETED4The print job is complete.

print.PrintJobSubState14+

Enumerates the print job substates.

System capability: SystemCapability.Print.PrintFramework

NameValueDescription
PRINT_JOB_COMPLETED_SUCCESS0The print job is successful.
PRINT_JOB_COMPLETED_FAILED1The print job is failed.
PRINT_JOB_COMPLETED_CANCELLED2The print job is canceled by user.
PRINT_JOB_COMPLETED_FILE_CORRUPTED3The print job is corrupted.
PRINT_JOB_BLOCK_OFFLINE4The printer is offline.
PRINT_JOB_BLOCK_BUSY5The printer is occupied by another process.
PRINT_JOB_BLOCK_CANCELLED6The print job is canceled due to a block.
PRINT_JOB_BLOCK_OUT_OF_PAPER7The printer is out of paper.
PRINT_JOB_BLOCK_OUT_OF_INK8The printer is out of ink.
PRINT_JOB_BLOCK_OUT_OF_TONER9The printer is out of toner.
PRINT_JOB_BLOCK_JAMMED10The printer is in a paper jam.
PRINT_JOB_BLOCK_DOOR_OPEN11The printer door is open.
PRINT_JOB_BLOCK_SERVICE_REQUEST12Print service request.
PRINT_JOB_BLOCK_LOW_ON_INK13The printer is low on ink.
PRINT_JOB_BLOCK_LOW_ON_TONER14The printer is low on toner.
PRINT_JOB_BLOCK_REALLY_LOW_ON_INK15The printer is extremely low on ink.
PRINT_JOB_BLOCK_BAD_CERTIFICATE16The print certificate is incorrect.
PRINT_JOB_BLOCK_ACCOUNT_ERROR18There is an error with the printer account.
PRINT_JOB_BLOCK_PRINT_PERMISSION_ERROR19There is an error with the printer permission.
PRINT_JOB_BLOCK_PRINT_COLOR_PERMISSION_ERROR20There is an error with the color printing permission.
PRINT_JOB_BLOCK_NETWORK_ERROR21The printer fails to connect to the network.
PRINT_JOB_BLOCK_SERVER_CONNECTION_ERROR22The printer fails to connect to the server.
PRINT_JOB_BLOCK_LARGE_FILE_ERROR23There is an error with a large file printing.
PRINT_JOB_BLOCK_FILE_PARSING_ERROR24There is an error with file parsing.
PRINT_JOB_BLOCK_SLOW_FILE_CONVERSION25The file conversion is slow.
PRINT_JOB_RUNNING_UPLOADING_FILES26The file is uploading.
PRINT_JOB_RUNNING_CONVERTING_FILES27The file is converting.
PRINT_JOB_BLOCK_UNKNOWN99There is an unknown error with the printer.

print.PrintErrorCode14+

Enumerates the print error codes.

System capability: SystemCapability.Print.PrintFramework

NameValueDescription
E_PRINT_NONE0No error.
E_PRINT_NO_PERMISSION201No permission.
E_PRINT_INVALID_PARAMETER401Invalid parameters.
E_PRINT_GENERIC_FAILURE13100001Printing failure.
E_PRINT_RPC_FAILURE13100002RPC failure.
E_PRINT_SERVER_FAILURE13100003Print service failure.
E_PRINT_INVALID_EXTENSION13100004Invalid printer extension.
E_PRINT_INVALID_PRINTER13100005Invalid printer.
E_PRINT_INVALID_PRINT_JOB13100006Invalid print job.
E_PRINT_FILE_IO13100007Incorrect file input/output.

print.ApplicationEvent14+

Enumerates print application events.

System capability: SystemCapability.Print.PrintFramework

NameValueDescription
APPLICATION_CREATED0Starts the print application.
APPLICATION_CLOSED_FOR_STARTED1Closes the print application by clicking Start.
APPLICATION_CLOSED_FOR_CANCELED2Closes the print application by clicking Cancel.

print.addPrinterToDiscovery14+

addPrinterToDiscovery(printerInformation: PrinterInformation): Promise<void>

Adds a printer to the printer discovery list. This API uses a promise to return the result.

Required permissions: ohos.permission.PRINT

System capability: SystemCapability.Print.PrintFramework

Parameters

NameTypeMandatoryDescription
printerInformationPrinterInformationYesThe added printer.

Return value

TypeDescription
Promise<void>Result of adding a printer to the printer discovery list.

Error codes

For details about the error codes, see Error Codes of the Print Service.

IDError Message
201the application does not have permission to call this function.
401Parameter error. Possible causes: 1.Mandatory parameters are left unspecified; 2.Incorrect parameter types.

Example

import { print } from '@kit.BasicServicesKit';
import { BusinessError } from '@ohos.base';

let printerInformation : print.PrinterInformation = {
    printerId : 'testPrinterId',
    printerName : 'testPrinterName',
    printerStatus : 0,
    description : 'testDesc',
    uri : 'testUri',
    printerMake : 'testPrinterMake',
    options : 'testOps'
};
print.addPrinterToDiscovery(printerInformation).then((data : void) => {
    console.log('addPrinterToDiscovery data : ' + JSON.stringify(data));
}).catch((error: BusinessError) => {
    console.log('addPrinterToDiscovery error : ' + JSON.stringify(error));
})

print.updatePrinterInDiscovery14+

updatePrinterInDiscovery(printerInformation: PrinterInformation): Promise<void>

Updates the printer capabilities to the printer discovery list. This API uses a promise to return the result.

Required permissions: ohos.permission.PRINT

System capability: SystemCapability.Print.PrintFramework

Parameters

NameTypeMandatoryDescription
printerInformationPrinterInformationYesPrinter whose capability is to be updated.

Return value

TypeDescription
Promise<void>Result of updating the printer capabilities to the printer discovery list.

Error codes

For details about the error codes, see Error Codes of the Print Service.

IDError Message
201the application does not have permission to call this function.
401Parameter error. Possible causes: 1.Mandatory parameters are left unspecified; 2.Incorrect parameter types.

Example

import { print } from '@kit.BasicServicesKit';
import { BusinessError } from '@ohos.base';

let testPageSize : print.PrintPageSize = {
    id : 'ISO_A4',
    name : 'iso_a4_210x297mm',
    width : 8268,
    height : 11692
};

let testCapability : print.PrinterCapabilities = {
    supportedPageSizes : [testPageSize],
    supportedColorModes : [print.PrintColorMode.COLOR_MODE_MONOCHROME],
    supportedDuplexModes : [print.PrintDuplexMode.DUPLEX_MODE_NONE],
    supportedMediaTypes : ['stationery'],
    supportedQualities : [print.PrintQuality.QUALITY_NORMAL],
    supportedOrientations : [print.PrintOrientationMode.ORIENTATION_MODE_PORTRAIT],
    options : 'testOptions'
};

let printerInformation : print.PrinterInformation = {
    printerId : 'testPrinterId',
    printerName : 'testPrinterName',
    printerStatus : 0,
    description : 'testDesc',
    capability : testCapability,
    uri : 'testUri',
    printerMake : 'testPrinterMake',
    options : 'testOptions'
};
print.updatePrinterInDiscovery(printerInformation).then((data : void) => {
    console.log('updatePrinterInDiscovery data : ' + JSON.stringify(data));
}).catch((error: BusinessError) => {
    console.log('updatePrinterInDiscovery error : ' + JSON.stringify(error));
})

print.removePrinterFromDiscovery14+

removePrinterFromDiscovery(printerId: string): Promise<void>

Removes a printer from the printer discovery list. This API uses a promise to return the result.

Required permissions: ohos.permission.PRINT

System capability: SystemCapability.Print.PrintFramework

Parameters

NameTypeMandatoryDescription
printerIdstringYesPrinter to remove.

Return value

TypeDescription
Promise<void>Result of removing a printer from the printer discovery list.

Error codes

For details about the error codes, see Error Codes of the Print Service.

IDError Message
201the application does not have permission to call this function.
401Parameter error. Possible causes: 1.Mandatory parameters are left unspecified; 2.Incorrect parameter types.

Example

import { print } from '@kit.BasicServicesKit';
import { BusinessError } from '@ohos.base';

let printerId : string = 'testPrinterId';
print.removePrinterFromDiscovery(printerId).then((data : void) => {
    console.log('removePrinterFromDiscovery data : ' + JSON.stringify(data));
}).catch((error: BusinessError) => {
    console.log('removePrinterFromDiscovery error : ' + JSON.stringify(error));
})

print.getPrinterInformationById14+

getPrinterInformationById(printerId: string): Promise<PrinterInformation>

Obtains printer information based on the printer ID. This API uses a promise to return the result.

Required permissions: ohos.permission.PRINT

System capability: SystemCapability.Print.PrintFramework

Parameters

NameTypeMandatoryDescription
printerIdstringYesPrinter ID used to obtain information.

Return value

TypeDescription
Promise<PrinterInformation>Printer information obtained based on the printer ID.

Error codes

For details about the error codes, see Error Codes of the Print Service.

IDError Message
201the application does not have permission to call this function.
401Parameter error. Possible causes: 1.Mandatory parameters are left unspecified; 2.Incorrect parameter types.

Example

import { print } from '@kit.BasicServicesKit';
import { BusinessError } from '@ohos.base';

let printerId : string = 'testPrinterId';
print.getPrinterInformationById(printerId).then((printerInformation : print.PrinterInformation) => {
    console.log('getPrinterInformationById data : ' + JSON.stringify(printerInformation));
}).catch((error: BusinessError) => {
    console.log('getPrinterInformationById error : ' + JSON.stringify(error));
})

print.PrinterInformation14+

Defines the printer information.

System capability: SystemCapability.Print.PrintFramework

Attributes

NameTypeMandatoryDescription
printerIdstringYesPrinter ID.
printerNamestringYesPrinter name.
printerStatusPrinterStatusYesPrinter state.
descriptionstringNoPrinter description.
capabilityPrinterCapabilitiesNoPrinter capabilities.
uristringNoPrinter URI.
printerMakestringNoPrinter model.
optionsstringNoPrinter details.

print.PrinterCapabilities14+

Defines the printer capabilities.

System capability: SystemCapability.Print.PrintFramework

Attributes

NameTypeMandatoryDescription
supportedPageSizesArray<PrintPageSize>YesList of paper sizes supported by the printer.
supportedColorModesArray<PrintColorMode>YesList of color modes supported by the printer.
supportedDuplexModesArray<PrintDuplexMode>YesList of single- and double-sided modes supported by the printer.
supportedMediaTypesArray<string>NoList of paper types supported by the printer.
supportedQualitiesArray<PrintQuality>NoList of print quality supported by the printer.
supportedOrientationsArray<PrintOrientationMode>NoList of print directions supported by the printer.
optionsstringNoPrinter capability details.

print.PrintQuality14+

Enumerates the print qualities.

System capability: SystemCapability.Print.PrintFramework

NameValueDescription
QUALITY_DRAFT3Draft
QUALITY_NORMAL4Standard
QUALITY_HIGH5High

print.PrintOrientationMode14+

Enumerates the print directions.

System capability: SystemCapability.Print.PrintFramework

NameValueDescription
ORIENTATION_MODE_PORTRAIT0Portrait mode.
ORIENTATION_MODE_LANDSCAPE1Landscape mode.
ORIENTATION_MODE_REVERSE_LANDSCAPE2Reverse landscape mode.
ORIENTATION_MODE_REVERSE_PORTRAIT3Reverse portrait mode.
ORIENTATION_MODE_NONE4Adaptive mode.

print.PrinterStatus14+

Enumerates the printer states.

System capability: SystemCapability.Print.PrintFramework

NameValueDescription
PRINTER_IDLE0The printer is idle.
PRINTER_BUSY1The printer is busy.
PRINTER_UNAVAILABLE2The printer is unavailable.

你可能感兴趣的鸿蒙文章

harmony 鸿蒙Basic Services Kit

harmony 鸿蒙DeviceInfo

harmony 鸿蒙InitSync

harmony 鸿蒙OH_Print

harmony 鸿蒙OsAccount

harmony 鸿蒙Pasteboard

harmony 鸿蒙Print_Margin

harmony 鸿蒙Print_PageSize

harmony 鸿蒙Print_PrintAttributes

harmony 鸿蒙Print_PrintDocCallback

  • 所属分类: 后端技术
  • 本文标签: 软件 鸿蒙
  • 版权声明: 本文链接 https://seaxiang.com/blog/rVw2fd