openharmony 鸿蒙 js-apis-print-sys

2025-06-12 浏览 (1)

@ohos.print (Print) (System API)

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. This topic describes only system APIs provided by the module. For details about its public APIs, see @ohos.print (Print).

Modules to Import

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

print.PrintMargin

Defines the page margins for printing.

System API: This is a system API.

System capability: SystemCapability.Print.PrintFramework

Attributes

NameTypeMandatoryDescription
topnumberNoTop margin of the page.
bottomnumberNoBottom margin of the page.
leftnumberNoLeft margin of the page.
rightnumberNoRight margin of the page.

print.PrinterRange

Defines the print range.

System API: This is a system API.

System capability: SystemCapability.Print.PrintFramework

Attributes

NameTypeMandatoryDescription
startPagenumberNoStart page.
endPagenumberNoEnd page.
pagesArray<number>NoDiscrete pages.

print.PreviewAttribute

Defines the print preview attributes.

System API: This is a system API.

System capability: SystemCapability.Print.PrintFramework

Attributes

NameTypeMandatoryDescription
previewRangePrinterRangeYesPreview page range.
resultnumberNoPrint preview result.

print.PrintResolution

Defines the resolution for printing.

System API: This is a system API.

System capability: SystemCapability.Print.PrintFramework

Attributes

NameTypeMandatoryDescription
idstringYesResolution ID.
horizontalDpinumberYesHorizontal DPI.
verticalDpinumberYesVertical DPI.

print.PrinterCapability

Defines the printer capabilities.

System API: This is a system API.

System capability: SystemCapability.Print.PrintFramework

Attributes

NameTypeMandatoryDescription
colorModenumberYesColor mode.
duplexModenumberYesSingle-sided or double-sided printing mode.
pageSizeArray<PrintPageSize>YesList of page sizes supported by the printer.
resolutionArray<PrintResolution>NoList of resolutions supported by the printer.
minMarginPrintMarginNoMinimum margin of the printer.
options11+ObjectNoPrinter options. The value is a JSON object string.

print.PrinterInfo

Provides the printer information.

System API: This is a system API.

System capability: SystemCapability.Print.PrintFramework

Attributes

NameTypeMandatoryDescription
printerIdstringYesPrinter ID.
printerNamestringYesPrinter name.
printerStatePrinterStateYesPrinter state.
printerIconnumberNoResource ID of the printer icon.
descriptionstringNoPrinter description.
capabilityPrinterCapabilityNoPrinter capability.
optionsObjectNoPrinter options. The value is a JSON object string.

print.PrintJob

Defines a print job.

System API: This is a system API.

System capability: SystemCapability.Print.PrintFramework

Attributes

NameTypeMandatoryDescription
fdListArray<number>YesFD list of files to print.
jobIdstringYesID of the print job.
printerIdstringYesID of the printer used for printing.
jobStatePrintJobStateYesState of the print job.
jobSubstate11+PrintJobSubStateYesSubstate of the print job.
copyNumbernumberYesCopy of the file list.
pageRangePrinterRangeYesPrint range.
isSequentialbooleanYesWhether the printing is sequential. The value true means that the printing is sequential; the value false means the opposite. The default value is false.
pageSizePrintPageSizeYesSelected page size.
isLandscapebooleanYesWhether the printing is in landscape mode. The value true means that the printing is in landscape mode; the value false means the printing is in portrait mode The default value is false.
colorModenumberYesColor mode.
duplexModenumberYesSingle-sided or double-sided printing mode.
marginPrintMarginNoCurrent page margin.
previewPreviewAttributeNoPreview settings.
optionsObjectNoPrinter options. The value is a JSON object string.

print.PrinterExtensionInfo

Provides the printer extension information.

System API: This is a system API.

System capability: SystemCapability.Print.PrintFramework

Attributes

NameTypeMandatoryDescription
extensionIdstringYesID of the printer extension.
vendorIdstringYesVendor ID of the printer extension.
vendorNamestringYesVendor name of the printer extension.
vendorIconnumberYesVendor icon of the printer extension.
versionstringYesVersion of the printer extension.

print.queryAllPrinterExtensionInfos

queryAllPrinterExtensionInfos(callback: AsyncCallback<Array<PrinterExtensionInfo>>): void

Obtains the information of all installed printer extensions. This API uses an asynchronous callback to return the result.

Required permissions: ohos.permission.MANAGE_PRINT_JOB

System API: This is a system API.

System capability: SystemCapability.Print.PrintFramework

Parameters

NameTypeMandatoryDescription
callbackAsyncCallback<Array<PrinterExtensionInfo>>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.
202not system application

Example

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

print.queryAllPrinterExtensionInfos((err: BusinessError, extensionInfos: print.PrinterExtensionInfo[]) => {
    if (err) {
        console.log('queryAllPrinterExtensionInfos err ' + JSON.stringify(err));
    } else {
        console.log('queryAllPrinterExtensionInfos success ' + JSON.stringify(extensionInfos));
    }
})

print.queryAllPrinterExtensionInfos

queryAllPrinterExtensionInfos(): Promise<Array<PrinterExtensionInfo>>

Obtains the information of all installed printer extensions. This API uses a promise to return the result.

Required permissions: ohos.permission.MANAGE_PRINT_JOB

System API: This is a system API.

System capability: SystemCapability.Print.PrintFramework

Return value

TypeDescription
Promise<Array<PrinterExtensionInfo>>Promise used to return the result.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.
202not system application

Example

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

print.queryAllPrinterExtensionInfos().then((extensionInfos: print.PrinterExtensionInfo[]) => {
    console.log('queryAllPrinterExtensionInfos success ' + JSON.stringify(extensionInfos));
    // ...
}).catch((error: BusinessError) => {
    console.log('failed to get AllPrinterExtension bacause ' + JSON.stringify(error));
})

print.startDiscoverPrinter

startDiscoverPrinter(extensionList: Array<string>, callback: AsyncCallback<void>): void

Starts discovering printers with the specified printer extensions. This API uses an asynchronous callback to return the result.

Required permissions: ohos.permission.MANAGE_PRINT_JOB

System API: This is a system API.

System capability: SystemCapability.Print.PrintFramework

Parameters

NameTypeMandatoryDescription
extensionListArray<string>YesList of printer extensions to load.
callbackAsyncCallback<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.
202not system application
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 extensionList: string[] = [];
// If there is no information in extensionList, all extensions are used for printer discovery.
print.startDiscoverPrinter(extensionList, (err: BusinessError, data : void) => {
    if (err) {
        console.log('failed to start Discover Printer because : ' + JSON.stringify(err));
    } else {
        console.log('start Discover Printer success data : ' + JSON.stringify(data));
    }
})

print.startDiscoverPrinter

startDiscoverPrinter(extensionList: Array<string>): Promise<void>

Starts discovering printers with the specified printer extensions. This API uses a promise to return the result.

Required permissions: ohos.permission.MANAGE_PRINT_JOB

System API: This is a system API.

System capability: SystemCapability.Print.PrintFramework

Parameters

NameTypeMandatoryDescription
extensionListArray<string>YesList of printer extensions to load.

Return value

TypeDescription
Promise<void>Promise 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.
202not system application
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 extensionList: string[] = [];
// If there is no information in extensionList, all extensions are used for printer discovery.
print.startDiscoverPrinter(extensionList).then((data : void) => {
    console.log('start Discovery success data : ' + JSON.stringify(data));
}).catch((error: BusinessError) => {
    console.log('failed to start Discovery because : ' + JSON.stringify(error));
})

print.stopDiscoverPrinter

stopDiscoverPrinter(callback: AsyncCallback<void>): void

Stops discovering printers with the specified printer extensions. This API uses an asynchronous callback to return the result.

Required permissions: ohos.permission.MANAGE_PRINT_JOB

System API: This is a system API.

System capability: SystemCapability.Print.PrintFramework

Parameters

NameTypeMandatoryDescription
callbackAsyncCallback<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.
202not system application

Example

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

print.stopDiscoverPrinter((err: BusinessError, data : void) => {
    if (err) {
        console.log('failed to stop Discover Printer because : ' + JSON.stringify(err));
    } else {
        console.log('stop Discover Printer success data : ' + JSON.stringify(data));
    }
})

print.stopDiscoverPrinter

stopDiscoverPrinter(): Promise<void>

Stops discovering printers with the specified printer extensions. This API uses a promise to return the result.

Required permissions: ohos.permission.MANAGE_PRINT_JOB

System API: This is a system API.

System capability: SystemCapability.Print.PrintFramework

Return value

TypeDescription
Promise<void>Promise 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.
202not system application

Example

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

print.stopDiscoverPrinter().then((data : void) => {
    console.log('stop Discovery success data : ' + JSON.stringify(data));
}).catch((error: BusinessError) => {
    console.log('failed to stop Discovery because : ' + JSON.stringify(error));
})

print.connectPrinter

connectPrinter(printerId: string, callback: AsyncCallback<void>): void

Connects to the specified printer. This API uses an asynchronous callback to return the result.

Required permissions: ohos.permission.MANAGE_PRINT_JOB

System API: This is a system API.

System capability: SystemCapability.Print.PrintFramework

Parameters

NameTypeMandatoryDescription
printerIdstringYesPrinter ID.
callbackAsyncCallback<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.
202not system application
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 = 'printerId_32';
print.connectPrinter(printerId, (err: BusinessError, data : void) => {
    if (err) {
        console.log('failed to connect Printer because : ' + JSON.stringify(err));
    } else {
        console.log('start connect Printer success data : ' + JSON.stringify(data));
    }
})

print.connectPrinter

connectPrinter(printerId: string): Promise<void>

Connects to the specified printer. This API uses a promise to return the result.

Required permissions: ohos.permission.MANAGE_PRINT_JOB

System API: This is a system API.

System capability: SystemCapability.Print.PrintFramework

Parameters

NameTypeMandatoryDescription
printerIdstringYesPrinter ID.

Return value

TypeDescription
Promise<void>Promise 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.
202not system application
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 = 'printerId_32';
print.connectPrinter(printerId).then((data : void) => {
    console.log('start connect Printer success data : ' + JSON.stringify(data));
}).catch((error: BusinessError) => {
    console.log('failed to connect Printer because : ' + JSON.stringify(error));
})

print.disconnectPrinter

disconnectPrinter(printerId: string, callback: AsyncCallback<void>): void

Disconnects from the specified printer. This API uses an asynchronous callback to return the result.

Required permissions: ohos.permission.MANAGE_PRINT_JOB

System API: This is a system API.

System capability: SystemCapability.Print.PrintFramework

Parameters

NameTypeMandatoryDescription
printerIdstringYesPrinter ID.
callbackAsyncCallback<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.
202not system application
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 = 'printerId_32';
print.disconnectPrinter(printerId, (err: BusinessError, data : void) => {
    if (err) {
        console.log('failed to disconnect Printer because : ' + JSON.stringify(err));
    } else {
        console.log('start disconnect Printer success data : ' + JSON.stringify(data));
    }
})

print.disconnectPrinter

disconnectPrinter(printerId: string): Promise<void>

Disconnects from the specified printer. This API uses a promise to return the result.

Required permissions: ohos.permission.MANAGE_PRINT_JOB

System API: This is a system API.

System capability: SystemCapability.Print.PrintFramework

Parameters

NameTypeMandatoryDescription
printerIdstringYesPrinter ID.

Return value

TypeDescription
Promise<void>Promise 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.
202not system application
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 = 'printerId_32';
print.disconnectPrinter(printerId).then((data : void) => {
    console.log('start disconnect Printer success data : ' + JSON.stringify(data));
}).catch((error: BusinessError) => {
    console.log('failed to disconnect Printer because : ' + JSON.stringify(error));
})

print.queryPrinterCapability

queryPrinterCapability(printerId: string, callback: AsyncCallback<void>): void

Queries the printer capability. This API uses an asynchronous callback to return the result.

Required permissions: ohos.permission.MANAGE_PRINT_JOB

System API: This is a system API.

System capability: SystemCapability.Print.PrintFramework

Parameters

NameTypeMandatoryDescription
printerIdstringYesPrinter ID.
callbackAsyncCallback<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.
202not system application
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 = 'printerId_32';
print.queryPrinterCapability(printerId, (err: BusinessError, data : void) => {
    if (err) {
        console.log('failed to query Printer Capability because : ' + JSON.stringify(err));
    } else {
        console.log('start query Printer Capability success data : ' + JSON.stringify(data));
    }
})

print.queryPrinterCapability

queryPrinterCapability(printerId: string): Promise<void>

Queries the printer capability. This API uses a promise to return the result.

Required permissions: ohos.permission.MANAGE_PRINT_JOB

System API: This is a system API.

System capability: SystemCapability.Print.PrintFramework

Parameters

NameTypeMandatoryDescription
printerIdstringYesPrinter ID.

Return value

TypeDescription
Promise<void>Promise 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.
202not system application
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 = 'printerId_32';
print.queryPrinterCapability(printerId).then((data : void) => {
    console.log('start query Printer success data : ' + JSON.stringify(data));
}).catch((error: BusinessError) => {
    console.log('failed to query Printer Capability because : ' + JSON.stringify(error));
})

print.startPrintJob

startPrintJob(jobInfo: PrintJob, callback: AsyncCallback<void>): void

Starts the specified print job. This API uses an asynchronous callback to return the result.

Required permissions: ohos.permission.MANAGE_PRINT_JOB

System API: This is a system API.

System capability: SystemCapability.Print.PrintFramework

Parameters

NameTypeMandatoryDescription
jobInfoPrintJobYesInformation about the print job.
callbackAsyncCallback<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.
202not system application
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 jobInfo : print.PrintJob = {
    fdList : [0,1],
    jobId : 'jobId_12',
    printerId : 'printerId_32',
    jobState : 3,
    jobSubstate : print.PrintJobSubState.PRINT_JOB_COMPLETED_SUCCESS,
    copyNumber : 1,
    pageRange : {},
    isSequential : false,
    pageSize : {id : '', name : '', width : 10, height : 20},
    isLandscape : false,
    colorMode : 6,
    duplexMode : 6,
    margin : undefined,
    preview : undefined,
    options : undefined
};
print.startPrintJob(jobInfo, (err: BusinessError, data : void) => {
    if (err) {
        console.log('failed to start Print Job because : ' + JSON.stringify(err));
    } else {
        console.log('start Print Job success data : ' + JSON.stringify(data));
    }
})

print.startPrintJob

startPrintJob(jobInfo: PrintJob): Promise<void>

Starts the specified print job. This API uses a promise to return the result.

Required permissions: ohos.permission.MANAGE_PRINT_JOB

System API: This is a system API.

System capability: SystemCapability.Print.PrintFramework

Parameters

NameTypeMandatoryDescription
jobInfoPrintJobYesInformation about the print job.

Return value

TypeDescription
Promise<void>Promise 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.
202not system application
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 jobInfo : print.PrintJob = {
    fdList : [0,1],
    jobId : 'jobId_12',
    printerId : 'printerId_32',
    jobState : 3,
    jobSubstate : print.PrintJobSubState.PRINT_JOB_COMPLETED_SUCCESS,
    copyNumber : 1,
    pageRange : {},
    isSequential : false,
    pageSize : {id : '', name : '', width : 10, height : 20},
    isLandscape : false,
    colorMode : 6,
    duplexMode : 6,
    margin : undefined,
    preview : undefined,
    options : undefined
};
print.startPrintJob(jobInfo).then((data : void) => {
    console.log('start Print success data : ' + JSON.stringify(data));
}).catch((error: BusinessError) => {
    console.log('failed to start Print because : ' + JSON.stringify(error));
})

print.cancelPrintJob

cancelPrintJob(jobId: string, callback: AsyncCallback<void>): void

Cancels the specified print job, which is on the print queue of the printer. This API uses an asynchronous callback to return the result.

Required permissions: ohos.permission.MANAGE_PRINT_JOB

System API: This is a system API.

System capability: SystemCapability.Print.PrintFramework

Parameters

NameTypeMandatoryDescription
jobIdstringYesPrint job ID.
callbackAsyncCallback<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.
202not system application
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 jobId : string = '121212';
print.cancelPrintJob(jobId, (err: BusinessError, data : void) => {
    if (err) {
        console.log('cancelPrintJob failed, because : ' + JSON.stringify(err));
    } else {
        console.log('cancelPrintJob success, data: ' + JSON.stringify(data));
    }
})

print.cancelPrintJob

cancelPrintJob(jobId: string): Promise<void>

Cancels the specified print job, which is on the print queue of the printer. This API uses a promise to return the result.

Required permissions: ohos.permission.MANAGE_PRINT_JOB

System API: This is a system API.

System capability: SystemCapability.Print.PrintFramework

Parameters

NameTypeMandatoryDescription
jobIdstringYesPrint job ID.

Return value

TypeDescription
Promise<void>Promise 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.
202not system application
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 jobId : string = '121212';
print.cancelPrintJob(jobId).then((data : void) => {
    console.log('cancelPrintJob success, data : ' + JSON.stringify(data));
}).catch((error: BusinessError) => {
    console.log('cancelPrintJob failed, because : ' + JSON.stringify(error));
})

print.requestPrintPreview

requestPrintPreview(jobInfo: PrintJob, callback: Callback<number>): void

Requests print preview data. This API uses a callback to return the result.

Required permissions: ohos.permission.MANAGE_PRINT_JOB

System API: This is a system API.

System capability: SystemCapability.Print.PrintFramework

Parameters

NameTypeMandatoryDescription
jobInfoPrintJobYesInformation about the print job.
callbackCallback<number>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.
202not system application
401Parameter error. Possible causes: 1.Mandatory parameters are left unspecified; 2.Incorrect parameter types.

Example

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

let jobInfo : print.PrintJob = {
    fdList : [0,1],
    jobId : 'jobId_12',
    printerId : 'printerId_32',
    jobState : 3,
    jobSubstate : print.PrintJobSubState.PRINT_JOB_COMPLETED_SUCCESS,
    copyNumber : 1,
    pageRange : {},
    isSequential : false,
    pageSize : {id : '', name : '', width : 10, height : 20},
    isLandscape : false,
    colorMode : 6,
    duplexMode : 6,
    margin : undefined,
    preview : undefined,
    options : undefined
};
print.requestPrintPreview(jobInfo, (num : number) => {
    console.log('requestPrintPreview success, num : ' + JSON.stringify(num));

})

print.requestPrintPreview

requestPrintPreview(jobInfo: PrintJob): Promise<number>

Requests print preview data. This API uses a promise to return the result.

Required permissions: ohos.permission.MANAGE_PRINT_JOB

System API: This is a system API.

System capability: SystemCapability.Print.PrintFramework

Parameters

NameTypeMandatoryDescription
jobInfoPrintJobYesInformation about the print job.

Return value

TypeDescription
Promise<number>Promise 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.
202not system application
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 jobInfo : print.PrintJob = {
    fdList : [0,1],
    jobId : 'jobId_12',
    printerId : 'printerId_32',
    jobState : 3,
    jobSubstate : print.PrintJobSubState.PRINT_JOB_COMPLETED_SUCCESS,
    copyNumber : 1,
    pageRange : {},
    isSequential : false,
    pageSize : {id : '', name : '', width : 10, height : 20},
    isLandscape : false,
    colorMode : 6,
    duplexMode : 6,
    margin : undefined,
    preview : undefined,
    options : undefined
};
print.requestPrintPreview(jobInfo).then((num: number) => {
    console.log('requestPrintPreview success, num : ' + JSON.stringify(num));
}).catch((error: BusinessError) => {
    console.log('requestPrintPreview failed, because : ' + JSON.stringify(error));
})

print.on

on(type: 'printerStateChange', callback: (state: PrinterState, info: PrinterInfo) => void): void

Registers a listener for printer state change events. This API uses a callback to return the result.

Required permissions: ohos.permission.MANAGE_PRINT_JOB

System API: This is a system API.

System capability: SystemCapability.Print.PrintFramework

Parameters

NameTypeMandatoryDescription
type'printerStateChange'YesListening type. The value is fixed at 'printerStateChange'.
callback(state: PrinterState, info: PrinterInfo) => voidYesCallback 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.
202not system application
401Parameter error. Possible causes: 1.Mandatory parameters are left unspecified; 2.Incorrect parameter types.

Example

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

print.on('printerStateChange', (state: print.PrinterState, info: print.PrinterInfo) => {
    if (state === null||info === null) {
        console.log('printer state changed state is null or info is null');
        return;
    } else {
        console.log('on printer state changed, state : ' + JSON.stringify(state));
        console.log('on printer state changed, info : ' + JSON.stringify(info));
    }
})

print.off

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

Unregisters the listener for printer state change events. This API uses a callback to return the result.

Required permissions: ohos.permission.MANAGE_PRINT_JOB

System API: This is a system API.

System capability: SystemCapability.Print.PrintFramework

Parameters

NameTypeMandatoryDescription
type'printerStateChange'YesListening type. The value is fixed at 'printerStateChange'.
callbackCallback<boolean>NoCallback used to return the result. The value true means that the listener for printer state change events is successfully unregistered, and false means the opposite.

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.
202not system application
401Parameter error. Possible causes: 1.Mandatory parameters are left unspecified; 2.Incorrect parameter types.

Example

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

print.off('printerStateChange', (data: boolean) => {
    console.log('off printerStateChange data : ' + JSON.stringify(data));
})

print.on

on(type: 'jobStateChange', callback: (state: PrintJobState, job: PrintJob) => void): void

Registers a listener for print job state change events. This API uses a callback to return the result.

Required permissions: ohos.permission.MANAGE_PRINT_JOB

System API: This is a system API.

System capability: SystemCapability.Print.PrintFramework

Parameters

NameTypeMandatoryDescription
type'jobStateChange'YesListening type. The value is fixed at 'jobStateChange'.
callback(state: PrintJobState, job: PrintJob) => voidYesCallback 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.
202not system application
401Parameter error. Possible causes: 1.Mandatory parameters are left unspecified; 2.Incorrect parameter types.

Example

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

print.on('jobStateChange', (state: print.PrintJobState, job: print.PrintJob) => {
    console.log('onJobStateChange, state : ' + JSON.stringify(state) + ', job : ' + JSON.stringify(job));
})

print.off

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

Unregisters the listener for print job state change events. This API uses a callback to return the result.

Required permissions: ohos.permission.MANAGE_PRINT_JOB

System API: This is a system API.

System capability: SystemCapability.Print.PrintFramework

Parameters

NameTypeMandatoryDescription
type'jobStateChange'YesListening type. The value is fixed at 'jobStateChange'.
callbackCallback<boolean>NoCallback used to return the result. The value true means that the listener for print job state change events is successfully unregistered, and false means the opposite.

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.
202not system application
401Parameter error. Possible causes: 1.Mandatory parameters are left unspecified; 2.Incorrect parameter types.

Example

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

print.off('jobStateChange', (data: boolean) => {
    console.log('offJobStateChanged data : ' + JSON.stringify(data));
})

print.on

on(type: 'extInfoChange', callback: (extensionId: string, info: string) => void): void

Registers a listener for printer extension information change events. This API uses a callback to return the result.

Required permissions: ohos.permission.MANAGE_PRINT_JOB

System API: This is a system API.

System capability: SystemCapability.Print.PrintFramework

Parameters

NameTypeMandatoryDescription
type'extInfoChange'YesListening type. The value is fixed at 'extInfoChange'.
callback(extensionId: string, info: string) => voidYesCallback 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.
202not system application
401Parameter error. Possible causes: 1.Mandatory parameters are left unspecified; 2.Incorrect parameter types.

Example

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

print.on('extInfoChange', (extensionId: string, info: string) => {
    console.log('onExtInfoChange, entensionId : ' + JSON.stringify(extensionId) + ', info : ' + JSON.stringify(info));
})

print.off

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

Unregisters the listener for printer extension information change events. This API uses a callback to return the result.

Required permissions: ohos.permission.MANAGE_PRINT_JOB

System API: This is a system API.

System capability: SystemCapability.Print.PrintFramework

Parameters

NameTypeMandatoryDescription
type'extInfoChange'YesListening type. The value is fixed at 'extInfoChange'.
callbackCallback<boolean>NoCallback used to return the result. The value true means that the listener for printer extension information change events is successfully unregistered, and false means the opposite.

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.
202not system application
401Parameter error. Possible causes: 1.Mandatory parameters are left unspecified; 2.Incorrect parameter types.

Example

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

print.off('extInfoChange', (data: boolean) => {
    console.log('offExtInfoChange data : ' + JSON.stringify(data));
})

print.addPrinters

addPrinters(printers: Array<PrinterInfo>, callback: AsyncCallback<void>): void

Adds printers. This API uses an asynchronous callback to return the result.

Required permissions: ohos.permission.MANAGE_PRINT_JOB

System API: This is a system API.

System capability: SystemCapability.Print.PrintFramework

Parameters

NameTypeMandatoryDescription
printersArray<PrinterInfo>YesList of printers to add.
callbackAsyncCallback<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.
202not system application
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 printerInfo : print.PrinterInfo = {
    printerId : '3232',
    printerName : 'hhhhh',
    printerState : 0,
    printerIcon : 12,
    description : 'str',
    capability : undefined,
    options : 'opt'
};
print.addPrinters([printerInfo], (err: BusinessError, data : void) => {
    if (err) {
        console.log('addPrinters failed, because : ' + JSON.stringify(err));
    } else {
        console.log('addPrinters success, data : ' + JSON.stringify(data));
    }
})

print.addPrinters

addPrinters(printers: Array<PrinterInfo>): Promise<void>

Adds printers. This API uses a promise to return the result.

Required permissions: ohos.permission.MANAGE_PRINT_JOB

System API: This is a system API.

System capability: SystemCapability.Print.PrintFramework

Parameters

NameTypeMandatoryDescription
printersArray<PrinterInfo>YesList of printers to add.

Return value

TypeDescription
Promise<void>Promise 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.
202not system application
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 printerInfo : print.PrinterInfo = {
    printerId : '3232',
    printerName : 'hhhhh',
    printerState : 0,
    printerIcon : 12,
    description : 'str',
    capability : undefined,
    options : 'opt'
};
print.addPrinters([printerInfo]).then((data : void) => {
    console.log('add printers data : ' + JSON.stringify(data));
}).catch((error: BusinessError) => {
    console.log('add printers error : ' + JSON.stringify(error));
})

print.removePrinters

removePrinters(printerIds: Array<string>, callback: AsyncCallback<void>): void

Removes printers. This API uses an asynchronous callback to return the result.

Required permissions: ohos.permission.MANAGE_PRINT_JOB

System API: This is a system API.

System capability: SystemCapability.Print.PrintFramework

Parameters

NameTypeMandatoryDescription
printerIdsArray<string>YesList of printers to remove.
callbackAsyncCallback<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.
202not system application
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 = '1212';
print.removePrinters([printerId], (err: BusinessError, data : void) => {
    if (err) {
        console.log('removePrinters failed, because : ' + JSON.stringify(err));
    } else {
        console.log('removePrinters success, data : ' + JSON.stringify(data));
    }
})

print.removePrinters

removePrinters(printerIds: Array<string>): Promise<void>

Removes printers. This API uses a promise to return the result.

Required permissions: ohos.permission.MANAGE_PRINT_JOB

System API: This is a system API.

System capability: SystemCapability.Print.PrintFramework

Parameters

NameTypeMandatoryDescription
printerIdsArray<string>YesList of printers to remove.

Return value

TypeDescription
Promise<void>Promise 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.
202not system application
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 = '1212';
print.removePrinters([printerId]).then((data : void) => {
    console.log('remove printers data : ' + JSON.stringify(data));
}).catch((error: BusinessError) => {
    console.log('remove printers error : ' + JSON.stringify(error));
})

print.updatePrinters

updatePrinters(printers: Array<PrinterInfo>, callback: AsyncCallback<void>): void

Updates information about the specified printers. This API uses an asynchronous callback to return the result.

Required permissions: ohos.permission.MANAGE_PRINT_JOB

System API: This is a system API.

System capability: SystemCapability.Print.PrintFramework

Parameters

NameTypeMandatoryDescription
printersArray<PrinterInfo>YesList of printers whose information is to be updated.
callbackAsyncCallback<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.
202not system application
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 printerInfo : print.PrinterInfo = {
    printerId : '3232',
    printerName : 'hhhhh',
    printerState : 0,
    printerIcon : 12,
    description : 'str',
    capability : undefined,
    options : 'opt'
};
print.updatePrinters([printerInfo], (err: BusinessError, data : void) => {
    if (err) {
        console.log('updataPrinters failed, because : ' + JSON.stringify(err));
    } else {
        console.log('updataPrinters success, data : ' + JSON.stringify(data));
    }
})

print.updatePrinters

updatePrinters(printers: Array<PrinterInfo>): Promise<void>

Updates information about the specified printers. This API uses a promise to return the result.

Required permissions: ohos.permission.MANAGE_PRINT_JOB

System API: This is a system API.

System capability: SystemCapability.Print.PrintFramework

Parameters

NameTypeMandatoryDescription
printersArray<PrinterInfo>YesList of printers whose information is to be updated.

Return value

TypeDescription
Promise<void>Promise 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.
202not system application
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 printerInfo : print.PrinterInfo = {
    printerId : '3232',
    printerName : 'hhhhh',
    printerState : 0,
    printerIcon : 12,
    description : 'str',
    capability : undefined,
    options : 'opt'
};
print.updatePrinters([printerInfo]).then((data : void) => {
    console.log('update printers data : ' + JSON.stringify(data));
}).catch((error: BusinessError) => {
    console.log('update printers error : ' + JSON.stringify(error));
})

print.updatePrinterState

updatePrinterState(printerId: string, state: PrinterState, callback: AsyncCallback<void>): void

Updates the printer state. This API uses an asynchronous callback to return the result.

Required permissions: ohos.permission.MANAGE_PRINT_JOB

System API: This is a system API.

System capability: SystemCapability.Print.PrintFramework

Parameters

NameTypeMandatoryDescription
printerIdstringYesPrinter ID.
statePrinterStateYesPrinter state.
callbackAsyncCallback<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.
202not system application
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 = '1212';
let state : print.PrinterState = print.PrinterState.PRINTER_CONNECTED;
print.updatePrinterState(printerId, state, (err: BusinessError, data : void) => {
    if (err) {
        console.log('updataPrinterState failed, because : ' + JSON.stringify(err));
    } else {
        console.log('updataPrinterState success, data : ' + JSON.stringify(data));
    }
})

print.updatePrinterState

updatePrinterState(printerId: string, state: PrinterState): Promise<void>

Updates the printer state. This API uses a promise to return the result.

Required permissions: ohos.permission.MANAGE_PRINT_JOB

System API: This is a system API.

System capability: SystemCapability.Print.PrintFramework

Parameters

NameTypeMandatoryDescription
printerIdstringYesPrinter ID.
statePrinterStateYesPrinter state.

Return value

TypeDescription
Promise<void>Promise 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.
202not system application
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 = '1212';
let state : print.PrinterState = print.PrinterState.PRINTER_CONNECTED;
print.updatePrinterState(printerId, state).then((data : void) => {
    console.log('update printer state data : ' + JSON.stringify(data));
}).catch((error: BusinessError) => {
    console.log('update printer state error : ' + JSON.stringify(error));
})

print.updatePrintJobState

updatePrintJobState(jobId: string, state: PrintJobState, subState: PrintJobSubState, callback: AsyncCallback<void>): void

Updates the print job state. This API uses an asynchronous callback to return the result.

Required permissions: ohos.permission.MANAGE_PRINT_JOB

System API: This is a system API.

System capability: SystemCapability.Print.PrintFramework

Parameters

NameTypeMandatoryDescription
jobIdstringYesID of the print job.
statePrintJobStateYesState of the print job.
subStatePrintJobSubStateYesSubstate of the print job.
callbackAsyncCallback<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.
202not system application
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 jobId : string = '3434';
let state : print.PrintJobState = print.PrintJobState.PRINT_JOB_PREPARE;
let subState : print.PrintJobSubState = print.PrintJobSubState.PRINT_JOB_COMPLETED_SUCCESS;
print.updatePrintJobState(jobId, state, subState, (err: BusinessError, data : void) => {
    if (err) {
        console.log('updataPrintJobState failed, because : ' + JSON.stringify(err));
    } else {
        console.log('updatePrintJobState success, data : ' + JSON.stringify(data));
    }
})

print.updatePrintJobState

updatePrintJobState(jobId: string, state: PrintJobState, subState: PrintJobSubState): Promise<void>

Updates the print job state. This API uses a promise to return the result.

Required permissions: ohos.permission.MANAGE_PRINT_JOB

System API: This is a system API.

System capability: SystemCapability.Print.PrintFramework

Parameters

NameTypeMandatoryDescription
jobIdstringYesID of the print job.
statePrintJobStateYesState of the print job.
subStatePrintJobSubStateYesSubstate of the print job.

Return value

TypeDescription
Promise<void>Promise 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.
202not system application
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 jobId : string = '3434';
let state : print.PrintJobState = print.PrintJobState.PRINT_JOB_PREPARE;
let subState : print.PrintJobSubState = print.PrintJobSubState.PRINT_JOB_COMPLETED_SUCCESS;
print.updatePrintJobState(jobId, state, subState).then((data : void) => {
    console.log('update print job state data : ' + JSON.stringify(data));
}).catch((error: BusinessError) => {
    console.log('update print job state error : ' + JSON.stringify(error));
})

print.updateExtensionInfo

updateExtensionInfo(info: string, callback: AsyncCallback<void>): void

Updates the printer extension information. This API uses an asynchronous callback to return the result.

Required permissions: ohos.permission.MANAGE_PRINT_JOB

System API: This is a system API.

System capability: SystemCapability.Print.PrintFramework

Parameters

NameTypeMandatoryDescription
infostringYesNew printer extension information.
callbackAsyncCallback<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.
202not system application
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 info : string = 'WIFI_INACTIVE';
print.updateExtensionInfo(info, (err: BusinessError, data : void) => {
    if (err) {
        console.log('updateExtensionInfo failed, because : ' + JSON.stringify(err));
    } else {
        console.log('updateExtensionInfo success, data : ' + JSON.stringify(data));
    }
})

print.updateExtensionInfo

updateExtensionInfo(info: string): Promise<void>

Updates the printer extension information. This API uses a promise to return the result.

Required permissions: ohos.permission.MANAGE_PRINT_JOB

System API: This is a system API.

System capability: SystemCapability.Print.PrintFramework

Parameters

NameTypeMandatoryDescription
infostringYesNew printer extension information.

Return value

TypeDescription
Promise<void>Promise 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.
202not system application
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 info : string = 'WIFI_INACTIVE';
print.updateExtensionInfo(info).then((data : void) => {
    console.log('update print job state data : ' + JSON.stringify(data));
}).catch((error: BusinessError) => {
    console.log('update print job state error : ' + JSON.stringify(error));
})

print.queryAllPrintJobs(deprecated)

This API is supported since API version 10 and deprecated since API version 11. You are advised to use queryPrintJobList instead.

queryAllPrintJobs(callback: AsyncCallback<void>): void

Queries all print jobs. This API uses an asynchronous callback to return the result.

Required permissions: ohos.permission.MANAGE_PRINT_JOB

System API: This is a system API.

System capability: SystemCapability.Print.PrintFramework

Parameters

NameTypeMandatoryDescription
callbackAsyncCallback<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.
202not system application

Example

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

print.queryAllPrintJobs((err: BusinessError, data : void) => {
    if (err) {
        console.log('queryAllPrintJobs failed, because : ' + JSON.stringify(err));
    } else {
        console.log('queryAllPrintJobs success, data : ' + JSON.stringify(data));
    }
})

print.queryAllPrintJobs(deprecated)

This API is supported since API version 10 and deprecated since API version 11. You are advised to use queryPrintJobList instead.

queryAllPrintJobs(): Promise<void>

Queries all print jobs. This API uses a promise to return the result.

Required permissions: ohos.permission.MANAGE_PRINT_JOB

System API: This is a system API.

System capability: SystemCapability.Print.PrintFramework

Return value

TypeDescription
Promise<void>Promise 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.
202not system application

Example

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

print.queryAllPrintJobs().then((data : void) => {
    console.log('queryAllPrintJobs success, data : ' + JSON.stringify(data));
}).catch((error: BusinessError) => {
    console.log('queryAllPrintJobs failed, error : ' + JSON.stringify(error));
})

print.queryPrintJobList11+

queryPrintJobList(callback: AsyncCallback<Array<PrintJob>>): void

Queries all print jobs. This API uses an asynchronous callback to return the result.

Required permissions: ohos.permission.MANAGE_PRINT_JOB

System API: This is a system API.

System capability: SystemCapability.Print.PrintFramework

Parameters

NameTypeMandatoryDescription
callbackAsyncCallback<Array<PrintJob>>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.
202not system application

Example

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

print.queryPrintJobList((err: BusinessError, printJobs : print.PrintJob[]) => {
    if (err) {
        console.log('queryPrintJobList failed, because : ' + JSON.stringify(err));
    } else {
        console.log('queryPrintJobList success, data : ' + JSON.stringify(printJobs));
    }
})

print.queryPrintJobList11+

queryPrintJobList(): Promise<Array<PrintJob>>

Queries all print jobs. This API uses a promise to return the result.

Required permissions: ohos.permission.MANAGE_PRINT_JOB

System API: This is a system API.

System capability: SystemCapability.Print.PrintFramework

Return value

TypeDescription
Promise<Array<PrintJob>>Promise 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.
202not system application

Example

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

print.queryPrintJobList().then((printJobs : print.PrintJob[]) => {
    console.log('queryPrintJobList success, data : ' + JSON.stringify(printJobs));
}).catch((error: BusinessError) => {
    console.log('queryPrintJobList failed, error : ' + JSON.stringify(error));
})

print.queryPrintJobById11+

queryPrintJobById(jobId: string, callback: AsyncCallback<PrintJob>): void

Queries a print job by ID. This API uses an asynchronous callback to return the result.

Required permissions: ohos.permission.MANAGE_PRINT_JOB

System API: This is a system API.

System capability: SystemCapability.Print.PrintFramework

Parameters

NameTypeMandatoryDescription
jobIdstringYesID of the print job.
callbackAsyncCallback<PrintJob>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.
202not system application
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 jobId : string = '1';
print.queryPrintJobById(jobId, (err: BusinessError, printJob : print.PrintJob) => {
    if (err) {
        console.log('queryPrintJobById failed, because : ' + JSON.stringify(err));
    } else {
        console.log('queryPrintJobById success, data : ' + JSON.stringify(printJob));
    }
})

print.queryPrintJobById11+

queryPrintJobById(jobId: string): Promise<PrintJob>

Queries a print job by ID. This API uses a promise to return the result.

Required permissions: ohos.permission.MANAGE_PRINT_JOB

System API: This is a system API.

System capability: SystemCapability.Print.PrintFramework

Parameters

NameTypeMandatoryDescription
jobIdstringYesID of the print job.

Return value

TypeDescription
Promise<PrintJob>Promise 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.
202not system application
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 jobId : string = '1';
print.queryPrintJobById(jobId).then((printJob : print.PrintJob) => {
    console.log('queryPrintJobById data : ' + JSON.stringify(printJob));
}).catch((error: BusinessError) => {
    console.log('queryPrintJobById error : ' + JSON.stringify(error));
})

print.startGettingPrintFile11+

startGettingPrintFile(jobId: string, printAttributes: PrintAttributes, fd: number, onFileStateChanged: Callback<PrintFileCreationState>): void

Starts to obtain the print file. This API uses an asynchronous callback to return the result.

Required permissions: ohos.permission.MANAGE_PRINT_JOB

System API: This is a system API.

System capability: SystemCapability.Print.PrintFramework

Parameters

NameTypeMandatoryDescription
jobIdstringYesID of the print job.
printAttributesPrintAttributesYesPrint attributes.
fdnumberYesFile descriptor.
onFileStateChangedCallback<PrintFileCreationState>YesCallback for updating the file state.

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.
202not system application
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 jobId : string= '1';
class MyPrintAttributes implements print.PrintAttributes {
    copyNumber?: number;
    pageRange?: print.PrintPageRange;
    pageSize?: print.PrintPageSize|print.PrintPageType;
    directionMode?: print.PrintDirectionMode;
    colorMode?: print.PrintColorMode;
    duplexMode?: print.PrintDuplexMode;
}

class MyPrintPageRange implements print.PrintPageRange {
    startPage?: number;
    endPage?: number;
    pages?: Array<number>;
}

class MyPrintPageSize implements print.PrintPageSize {
    id: string = '0';
    name: string = '0';
    width: number = 210;
    height: number = 297;
}

let printAttributes = new MyPrintAttributes();
printAttributes.copyNumber = 2;
printAttributes.pageRange = new MyPrintPageRange();
printAttributes.pageRange.startPage = 0;
printAttributes.pageRange.endPage = 5;
printAttributes.pageRange.pages = [1, 3];
printAttributes.pageSize = print.PrintPageType.PAGE_ISO_A3;
printAttributes.directionMode = print.PrintDirectionMode.DIRECTION_MODE_AUTO;
printAttributes.colorMode = print.PrintColorMode.COLOR_MODE_MONOCHROME;
printAttributes.duplexMode = print.PrintDuplexMode.DUPLEX_MODE_NONE;

let fd : number = 1;
print.startGettingPrintFile(jobId, printAttributes, fd, (state: print.PrintFileCreationState) => {
    console.log('onFileStateChanged success, data : ' + JSON.stringify(state));
})

print.notifyPrintService11+

notifyPrintService(jobId: string, type: 'spooler_closed_for_cancelled'|'spooler_closed_for_started', callback: AsyncCallback<void>): void

Notifies the print service of the spooler shutdown information. This API uses an asynchronous callback to return the result.

Required permissions: ohos.permission.MANAGE_PRINT_JOB

System API: This is a system API.

System capability: SystemCapability.Print.PrintFramework

Parameters

NameTypeMandatoryDescription
jobIdstringYesID of the print job.
type'spooler_closed_for_cancelled' |'spooler_closed_for_started'YesSpooler shutdown information.
callbackAsyncCallback<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.
202not system application
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 jobId : string = '1';
print.notifyPrintService(jobId, 'spooler_closed_for_started', (err: BusinessError, data : void) => {
    if (err) {
        console.log('notifyPrintService failed, because : ' + JSON.stringify(err));
    } else {
        console.log('notifyPrintService success, data : ' + JSON.stringify(data));
    }
})

print.notifyPrintService11+

notifyPrintService(jobId: string, type: 'spooler_closed_for_cancelled'|'spooler_closed_for_started'): Promise<void>

Notifies the print service of the spooler shutdown information. This API uses a promise to return the result.

Required permissions: ohos.permission.MANAGE_PRINT_JOB

System API: This is a system API.

System capability: SystemCapability.Print.PrintFramework

Parameters

NameTypeMandatoryDescription
jobIdstringYesID of the print job.
type'spooler_closed_for_cancelled' |'spooler_closed_for_started'YesSpooler shutdown information.

Return value

TypeDescription
Promise<void>Promise 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.
202not system application
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 jobId : string = '1';
print.notifyPrintService(jobId, 'spooler_closed_for_started').then((data : void) => {
    console.log('notifyPrintService data : ' + JSON.stringify(data));
}).catch((error: BusinessError) => {
    console.log('notifyPrintService error : ' + JSON.stringify(error));
})

print.getAddedPrinters12+

getAddedPrinters(): Promise<Array<string>>

Obtains the list of printers that have been added to the CUPS. This API uses a promise to return the result.

Required permissions: ohos.permission.MANAGE_PRINT_JOB

System API: This is a system API.

System capability: SystemCapability.Print.PrintFramework

Return value

TypeDescription
Promise<Array<string>>Promise 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.
202not system application

Example

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

print.getAddedPrinters().then((printers: string[]) => {
    console.log('getAddedPrinters success ' + JSON.stringify(printers));
    // ...
}).catch((error: BusinessError) => {
    console.log('failed to getAddedPrinters bacause ' + JSON.stringify(error));
})

print.getPrinterInfoById12+

getPrinterInfoById(printerId: string): Promise<PrinterInfo>

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

Required permissions: ohos.permission.MANAGE_PRINT_JOB

System API: This is a system API.

System capability: SystemCapability.Print.PrintFramework

Parameters

NameTypeMandatoryDescription
printerIdstringYesPrinter ID.

Return value

TypeDescription
Promise<PrinterInfo>Promise 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.
202not system application
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 = '1';
print.getPrinterInfoById(printerId).then((printerInfo : print.PrinterInfo) => {
    console.log('getPrinterInfoById data : ' + JSON.stringify(printerInfo));
}).catch((error: BusinessError) => {
    console.log('getPrinterInfoById error : ' + JSON.stringify(error));
})

print.notifyPrintServiceEvent12+

notifyPrintServiceEvent(event: ApplicationEvent): Promise<void>

Notifies the print service of the print application events. This API uses a promise to return the result.

Required permissions: ohos.permission.MANAGE_PRINT_JOB

System API: This is a system API.

System capability: SystemCapability.Print.PrintFramework

Parameters

NameTypeMandatoryDescription
eventApplicationEventYesPrint application events.

Return value

TypeDescription
Promise<void>Promise 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.
202not system application
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 event : print.ApplicationEvent = print.ApplicationEvent.APPLICATION_CREATED;
print.notifyPrintServiceEvent(event).then((data : void) => {
    console.log('notifyPrintServiceEvent data : ' + JSON.stringify(data));
}).catch((error: BusinessError) => {
    console.log('notifyPrintServiceEvent error : ' + JSON.stringify(error));
})

你可能感兴趣的鸿蒙文章

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/9yLLUk