harmony 鸿蒙OH_Print

2025-06-12 浏览 (1)

OH_Print

Overview

Provides applications with the ability to access the printing system using CAPI.

Since: 12

Summary

Type Definitions

NameDescription
Print_StringListPrinter device list.
Print_PropertyPrinter property.
Print_PropertyListPrinter property list.
Print_ResolutionPrint resolution unit.
Print_MarginPrint margin.
Print_PageSizePrint paper size.
Print_PrinterCapabilityPrinter capability.
Print_DefaultValuePrint default information.
Print_PrinterInfoPrinter information.
Print_PrintJobPrint job.
Print_RangePrint range.
Print_PrintAttributesPrint attributes.
Print_WriteResultCallbackWrite result callback.
Print_OnStartLayoutWriteStart layout callback.
Print_OnJobStateChangedPrint job state callback.
Print_PrintDocCallbackPrint document state callback.
Print_PrinterDiscoveryCallbackPrinter discovery callback.
Print_PrinterChangeCallbackPrinter state change callback.

Enumerations

NameDescription
Print_ErrorCodeError codes.
Print_PrinterStatePrinter state codes.
Print_DiscoveryEventPrinter discovery events.
Print_PrinterEventPrinter state change events.
Print_DuplexModePrint duplex mode.
Print_ColorModePrint color mode.
Print_OrientationModePrint orientation.
Print_QualityPrint quality.
Print_DocumentFormatPrinter document format.
Print_JobDocAdapterStatePrint job state.

Functions

NameDescription
OH_Print_InitStarts the print service, initializes the print client, and establishes a connection to the print service.
OH_Print_ReleaseCloses the connection to the print service, unregisters callbacks, and releases print client resources.
OH_Print_StartPrinterDiscoveryStarts printer discovery.
OH_Print_StopPrinterDiscoveryStops printer discovery.
OH_Print_ConnectPrinterConnects to a printer using the printer ID.
OH_Print_StartPrintJobStarts a print job.
OH_Print_RegisterPrinterChangeListenerRegisters a printer state change event listener.
OH_Print_UnregisterPrinterChangeListenerUnregisters the printer state change event listener.
OH_Print_QueryPrinterListQueries the printer list.
OH_Print_ReleasePrinterListReleases the printer list.
OH_Print_QueryPrinterInfoQueries printer information.
OH_Print_ReleasePrinterInfoReleases printer information memory.
OH_Print_LaunchPrinterManagerLaunches the printer manager window.
OH_Print_QueryPrinterPropertiesQueries printer properties.
OH_Print_ReleasePrinterPropertiesReleases printer properties.
OH_Print_UpdatePrinterPropertiesUpdates printer properties.
OH_Print_RestorePrinterPropertiesRestores printer properties to default settings based on the property key list.
OH_Print_StartPrintByNativeStarts printing.

Type Definitions

Print_StringList

Description

Printer device list.

Since: 12

MemberDescription
countNumber of strings.
listString pointer array.

Print_Property

Description

Printer property.

Since: 12

MemberDescription
keyProperty key.
valueProperty value.

Print_PropertyList

Description

Printer property list.

Since: 12

MemberDescription
countNumber of properties.
listProperty pointer array.

Print_Resolution

Description

Print resolution unit.

Since: 12

ParameterDescription
horizontalDpiHorizontal resolution (dpi).
verticalDpiVertical resolution (dpi).

Print_Margin

Description

Print margin.

Since: 12

ParameterDescription
leftMarginLeft margin (microns).
topMarginTop margin (microns).
rightMarginRight margin (microns).
bottomMarginBottom margin (microns).

Print_PageSize

Description

Print paper size.

Since: 12

ParameterDescription
idPaper ID.
namePaper name.
widthPaper width (microns).
heightPaper height (microns).

Print_PrinterCapability

Description

Printer capability.

Since: 12

ParameterDescription
supportedColorModesSupported color modes list.
supportedColorModesCountNumber of supported color modes.
supportedDuplexModesSupported duplex modes list.
supportedDuplexModesCountNumber of supported duplex modes.
supportedPageSizesSupported paper sizes list.
supportedPageSizesCountNumber of supported paper sizes.
supportedMediaTypesSupported media types (JSON string array format).
supportedQualitiesSupported print qualities list.
supportedQualitiesCountNumber of supported print qualities.
supportedPaperSourcesSupported paper sources (JSON string array format).
supportedCopiesMaximum supported copies.
supportedResolutionsSupported resolutions list.
supportedResolutionsCountNumber of supported resolutions.
supportedOrientationsSupported orientations list.
supportedOrientationsCountNumber of supported orientations.
advancedCapabilityAdvanced capability (JSON format).

Print_DefaultValue

Description

Printer default information.

Since: 12

ParameterDescription
defaultColorModeDefault color mode.
defaultDuplexModeDefault duplex mode.
defaultMediaTypeDefault media type.
defaultPageSizeIdDefault paper size ID.
defaultMarginDefault margin.
defaultPaperSourceDefault paper source.
defaultPrintQualityDefault print quality.
defaultCopiesDefault number of copies.
defaultResolutionDefault resolution.
defaultOrientationDefault orientation.
otherDefaultValuesOther default values (JSON format).

Print_PrinterInfo

Description

Printer information.

Since: 12

ParameterDescription
printerStatePrinter state.
capabilityPrinter capability.
defaultValuePrinter default properties.
isDefaultPrinterWhether it is the default printer.
printerIdPrinter ID.
printerNamePrinter name.
descriptionPrinter description.
locationPrinter location.
makeAndModelPrinter manufacturer and model information.
printerUriPrinter URI.
detailInfoDetailed information (JSON format).

Print_PrintJob

Description

Print job.

Since: 12

ParameterDescription
jobNamePrint job name.
fdListPrint file descriptor array.
fdListCountNumber of print file descriptors.
printerIdPrinter ID.
copyNumberNumber of copies.
paperSourcePaper source.
mediaTypeMedia type.
pageSizeIdPaper size ID.
colorModeColor mode.
duplexModeDuplex mode.
resolutionResolution.
printMarginPrint margin.
borderlessWhether to print borderless.
orientationModePrint orientation.
printQualityPrint quality.
documentFormatDocument format.
advancedOptionsAdvanced options (JSON format).

Print_Range

Description

Print range.

Since: 13

ParameterDescription
startPageStart page number.
endPageEnd page number.
pagesArrayLenPage array length.
pagesArrayPage array.

Print_PrintAttributes

Description

Print attributes.

Since: 13

ParameterDescription
pageRangePrint range.
pageSizePaper size.
pageMarginPage margin.
copyNumberNumber of copies.
duplexModeDuplex mode.
colorModeColor mode.
isSequentialWhether to print sequentially.
isLandscapeWhether to print in landscape.
hasOptionWhether there are print options.
optionsPrint options (256 bytes).

Print_WriteResultCallback

Description

Write result callback.

Since: 13

Parameters:

NameDescription
jobIdPrint job ID.
codeWrite result status code.

Print_OnStartLayoutWrite

Description

Start layout callback.

Since: 13

Parameters:

NameDescription
jobIdPrint job ID.
fdFile descriptor.
oldAttrsPrevious print attributes.
newAttrsCurrent print attributes.
writeCallbackWrite result callback function.

Print_OnJobStateChanged

Description

Print job state change callback.

Since: 13

Parameters:

NameDescription
jobIdPrint job ID.
statePrint job state.

Print_PrintDocCallback

Description

Print document state callback.

Since: 13

ParameterDescription
startLayoutWriteCbStart layout write callback.
jobStateChangedCbJob state change callback.

Print_PrinterDiscoveryCallback

Description

Printer discovery callback.

Since: 12

Parameters:

NameDescription
eventDiscovery event.
printerInfoPrinter information.

Print_PrinterChangeCallback

Description

Printer state change callback.

Since: 12

Parameters:

NameDescription
eventChange event.
printerInfoPrinter information.

Enumerations

Print_ErrorCode

Description

Error codes.

Since: 12

Enum ValueDescriptionValue
PRINT_ERROR_NONEOperation successful.0
PRINT_ERROR_NO_PERMISSIONPermission verification failed.201
PRINT_ERROR_INVALID_PARAMETERInvalid parameter.401
PRINT_ERROR_GENERIC_FAILUREGeneric internal error.24300001
PRINT_ERROR_RPC_FAILURERPC communication error.24300002
PRINT_ERROR_SERVER_FAILUREServer error.24300003
PRINT_ERROR_INVALID_EXTENSIONInvalid extension.24300004
PRINT_ERROR_INVALID_PRINTERInvalid printer.24300005
PRINT_ERROR_INVALID_PRINT_JOBInvalid print job.24300006
PRINT_ERROR_FILE_IOFile I/O error.24300007
PRINT_ERROR_UNKNOWNUnknown error.24300255

Print_PrinterState

Description

Printer state codes.

Since: 12

Enum ValueDescription
PRINTER_IDLEPrinter idle.
PRINTER_BUSYPrinter busy.
PRINTER_UNAVAILABLEPrinter unavailable.

Print_DiscoveryEvent

Description

Printer discovery events.

Since: 12

Enum ValueDescription
PRINTER_DISCOVEREDPrinter discovered.
PRINTER_LOSTPrinter lost.
PRINTER_CONNECTINGPrinter connecting.
PRINTER_CONNECTEDPrinter connected.

Print_PrinterEvent

Description

Printer events.

Since: 12

Enum ValueDescription
PRINTER_ADDEDPrinter added.
PRINTER_DELETEDPrinter deleted.
PRINTER_STATE_CHANGEDPrinter state changed.
PRINTER_INFO_CHANGEDPrinter information changed.
PRINTER_PREFERENCE_CHANGEDPrinter preference changed.

Print_DuplexMode

Description

Print duplex mode.

Since: 12

Enum ValueDescription
DUPLEX_MODE_ONE_SIDEDOne-sided printing.
DUPLEX_MODE_TWO_SIDED_LONG_EDGETwo-sided long edge printing.
DUPLEX_MODE_TWO_SIDED_SHORT_EDGETwo-sided short edge printing.

Print_ColorMode

Description

Print color mode.

Since: 12

Enum ValueDescription
COLOR_MODE_MONOCHROMEMonochrome mode.
COLOR_MODE_COLORColor mode.
COLOR_MODE_AUTOAuto mode.

Print_OrientationMode

Description

Print orientation.

Since: 12

Enum ValueDescription
ORIENTATION_MODE_PORTRAITPortrait.
ORIENTATION_MODE_LANDSCAPELandscape.
ORIENTATION_MODE_REVERSE_LANDSCAPEReverse landscape.
ORIENTATION_MODE_REVERSE_PORTRAITReverse portrait.
ORIENTATION_MODE_NONENot specified.

Print_Quality

Description

Print quality.

Since: 12

Enum ValueDescription
PRINT_QUALITY_DRAFTDraft quality.
PRINT_QUALITY_NORMALNormal quality.
PRINT_QUALITY_HIGHHigh quality.

Print_DocumentFormat

Description

Document format.

Since: 12

Enum ValueDescriptionMIME Type
DOCUMENT_FORMAT_AUTOAuto detect.application/octet-stream.
DOCUMENT_FORMAT_JPEGJPEG image.image/jpeg.
DOCUMENT_FORMAT_PDFPDF document.application/pdf.
DOCUMENT_FORMAT_POSTSCRIPTPostScript document.application/postscript.
DOCUMENT_FORMAT_TEXTPlain text.text/plain.

Print_JobDocAdapterState

Description

Print job document adapter state.

Since: 13

Enum ValueDescription
PRINT_DOC_ADAPTER_PREVIEW_ABILITY_DESTROYPrint preview ability destroyed.
PRINT_DOC_ADAPTER_PRINT_TASK_SUCCEEDPrint task succeeded.
PRINT_DOC_ADAPTER_PRINT_TASK_FAILPrint task failed.
PRINT_DOC_ADAPTER_PRINT_TASK_CANCELPrint task canceled.
PRINT_DOC_ADAPTER_PRINT_TASK_BLOCKPrint task blocked.
PRINT_DOC_ADAPTER_PREVIEW_ABILITY_DESTROY_FOR_CANCELEDPreview ability destroyed due to cancellation.
PRINT_DOC_ADAPTER_PREVIEW_ABILITY_DESTROY_FOR_STARTEDPreview ability destroyed due to task start.

Function Documentation

OH_Print_Init()

Print_ErrorCode OH_Print_Init();

Description

Starts the print service, initializes the print client, and establishes a connection to the print service.

Permission: ohos.permission.PRINT

System Capability: SystemCapability.Print.PrintFramework

Since: 12

Returns:

Error CodeDescription
PRINT_ERROR_NONEOperation successful.
PRINT_ERROR_NO_PERMISSIONMissing print permission.
PRINT_ERROR_RPC_FAILUREUnable to connect to print service.
PRINT_ERROR_SERVER_FAILURECUPS service failed to start.

OH_Print_Release()

Print_ErrorCode OH_Print_Release();

Description

Closes the connection to the print service, unregisters callbacks, and releases print client resources.

System Capability: SystemCapability.Print.PrintFramework

Since: 12

Returns:

Error CodeDescription
PRINT_ERROR_NONEOperation successful.

OH_Print_StartPrinterDiscovery()

Print_ErrorCode OH_Print_StartPrinterDiscovery(Print_PrinterDiscoveryCallback callback);

Description

Starts the printer discovery process.

Permission: ohos.permission.PRINT

System Capability: SystemCapability.Print.PrintFramework

Since: 12

Parameters:

NameDescription
callbackPrinter discovery event callback function.

Returns:

Error CodeDescription
PRINT_ERROR_NONEOperation successful.
PRINT_ERROR_NO_PERMISSIONMissing print permission.
PRINT_ERROR_RPC_FAILUREUnable to connect to print service.
PRINT_ERROR_SERVER_FAILUREFailed to query print extension list from BMS.
PRINT_ERROR_INVALID_EXTENSIONNo available print extensions found.

OH_Print_StopPrinterDiscovery()

Print_ErrorCode OH_Print_StopPrinterDiscovery();

Description

Stops the printer discovery process.

Permission: ohos.permission.PRINT

System Capability: SystemCapability.Print.PrintFramework

Since: 12

Returns:

Error CodeDescription
PRINT_ERROR_NONEOperation successful.
PRINT_ERROR_NO_PERMISSIONMissing print permission.
PRINT_ERROR_RPC_FAILUREUnable to connect to print service.

OH_Print_ConnectPrinter()

Print_ErrorCode OH_Print_ConnectPrinter(const char *printerId);

Description

Connects to a printer using the printer ID.

Permission: ohos.permission.PRINT

System Capability: SystemCapability.Print.PrintFramework

Since: 12

Parameters:

NameDescription
printerIdPrinter ID to connect to.

Returns:

Error CodeDescription
PRINT_ERROR_NONEOperation successful.
PRINT_ERROR_NO_PERMISSIONMissing print permission.
PRINT_ERROR_RPC_FAILUREUnable to connect to print service.
PRINT_ERROR_INVALID_PRINTERPrinter not in discovered list.
PRINT_ERROR_SERVER_FAILUREUnable to find extension responsible for the printer.

OH_Print_StartPrintJob()

Print_ErrorCode OH_Print_StartPrintJob(const Print_PrintJob *printJob);

Description

Starts a print job.

Permission: ohos.permission.PRINT

System Capability: SystemCapability.Print.PrintFramework

Since: 12

Parameters:

NameDescription
printJobPointer to Print_PrintJob structure containing print job information.

Returns:

Error CodeDescription
PRINT_ERROR_NONEOperation successful.
PRINT_ERROR_NO_PERMISSIONMissing print permission.
PRINT_ERROR_RPC_FAILUREUnable to connect to print service.
PRINT_ERROR_INVALID_PRINTERPrinter not in connected list.
PRINT_ERROR_SERVER_FAILUREFailed to create print job in print service.
PRINT_ERROR_INVALID_PRINT_JOBUnable to find job in job queue.

OH_Print_RegisterPrinterChangeListener()

Print_ErrorCode OH_Print_RegisterPrinterChangeListener(Print_PrinterChangeCallback callback);

Description

Registers a printer state change listener.

Permission: ohos.permission.PRINT

System Capability: SystemCapability.Print.PrintFramework

Since: 12

Parameters:

NameDescription
callbackPrinter state change callback function.

Returns:

Error CodeDescription
PRINT_ERROR_NONEOperation successful.
PRINT_ERROR_NO_PERMISSIONMissing print permission.
PRINT_ERROR_RPC_FAILUREUnable to connect to print service.

OH_Print_UnregisterPrinterChangeListener()

void OH_Print_UnregisterPrinterChangeListener();

Description

Unregisters the printer state change listener.

Permission: ohos.permission.PRINT

System Capability: SystemCapability.Print.PrintFramework

Since: 12

OH_Print_QueryPrinterList()

Print_ErrorCode OH_Print_QueryPrinterList(Print_StringList *printerIdList);

Description

Queries the list of added printers.

Permission: ohos.permission.PRINT

System Capability: SystemCapability.Print.PrintFramework

Since: 12

Parameters:

NameDescription
printerIdListPointer to Print_StringList to store the queried printer ID list.

Returns:

Error CodeDescription
PRINT_ERROR_NONEOperation successful.
PRINT_ERROR_NO_PERMISSIONMissing print permission.
PRINT_ERROR_INVALID_PARAMETERprinterIdList is NULL.
PRINT_ERROR_INVALID_PRINTERUnable to query any connected printers.
PRINT_ERROR_GENERIC_FAILUREUnable to copy printer ID list.

OH_Print_ReleasePrinterList()

void OH_Print_ReleasePrinterList(Print_StringList *printerIdList);

Description

Releases memory allocated for printer list query.

System Capability: SystemCapability.Print.PrintFramework

Since: 12

Parameters:

NameDescription
printerIdListPrinter ID list to be released.

OH_Print_QueryPrinterInfo()

Print_ErrorCode OH_Print_QueryPrinterInfo(const char *printerId, Print_PrinterInfo **printerInfo);

Description

Queries printer information based on printer ID.

Permission: ohos.permission.PRINT

System Capability: SystemCapability.Print.PrintFramework

Since: 12

Parameters:

NameDescription
printerIdPrinter ID to query.
printerInfoPointer to Print_PrinterInfo pointer to store printer information.

Returns:

Error CodeDescription
PRINT_ERROR_NONEOperation successful.
PRINT_ERROR_NO_PERMISSIONMissing print permission.
PRINT_ERROR_RPC_FAILUREUnable to connect to print service.
PRINT_ERROR_INVALID_PARAMETERprinterId or printerInfo is NULL.
PRINT_ERROR_INVALID_PRINTERUnable to find printer in connected printer list.

OH_Print_ReleasePrinterInfo()

void OH_Print_ReleasePrinterInfo(Print_PrinterInfo *printerInfo);

Description

Releases memory allocated for printer information query.

System Capability: SystemCapability.Print.PrintFramework

Since: 12

Parameters:

NameDescription
printerInfoPointer to queried printer information to be released.

OH_Print_LaunchPrinterManager()

Print_ErrorCode OH_Print_LaunchPrinterManager();

Description

Launches the system's printer management window.

System Capability: SystemCapability.Print.PrintFramework

Since: 12

Returns:

Error CodeDescription
PRINT_ERROR_NONEOperation successful.
PRINT_ERROR_GENERIC_FAILUREUnable to launch printer manager window.

OH_Print_QueryPrinterProperties()

Print_ErrorCode OH_Print_QueryPrinterProperties(const char *printerId, 
                                              const Print_StringList *propertyKeyList,
                                              Print_PropertyList *propertyList);

Description

Queries printer property values based on property key list.

Permission: ohos.permission.PRINT

System Capability: SystemCapability.Print.PrintFramework

Since: 12

Parameters:

NameDescription
printerIdPrinter ID to query.
propertyKeyListList of property keys to query.
propertyListList of queried printer property values.

Returns:

Error CodeDescription
PRINT_ERROR_NONEOperation successful.
PRINT_ERROR_NO_PERMISSIONMissing print permission.
PRINT_ERROR_INVALID_PARAMETEROne of the parameters is NULL or key list is empty.
PRINT_ERROR_INVALID_PRINTERPrinter properties for specified printer not found.
PRINT_ERROR_GENERIC_FAILUREUnable to copy printer properties.

OH_Print_ReleasePrinterProperties()

void OH_Print_ReleasePrinterProperties(Print_PropertyList *propertyList);

Description

Releases memory allocated for printer properties query.

System Capability: SystemCapability.Print.PrintFramework

Since: 12

Parameters:

NameDescription
propertyListPointer to queried printer properties to be released.

OH_Print_UpdatePrinterProperties()

Print_ErrorCode OH_Print_UpdatePrinterProperties(const char *printerId, 
                                               const Print_PropertyList *propertyList);

Description

Sets printer properties based on property key-value pair list.

Permission: ohos.permission.PRINT

System Capability: SystemCapability.Print.PrintFramework

Since: 12

Parameters:

NameDescription
printerIdPrinter ID to set.
propertyListList of printer property values to set.

Returns:

Error CodeDescription
PRINT_ERROR_NONEOperation successful.
PRINT_ERROR_NO_PERMISSIONMissing print permission.
PRINT_ERROR_RPC_FAILUREUnable to connect to print service.

OH_Print_RestorePrinterProperties()

Print_ErrorCode OH_Print_RestorePrinterProperties(const char *printerId, 
                                                const Print_StringList *propertyKeyList);

Description

Restores printer properties to default settings based on property key list.

Permission: ohos.permission.PRINT

System Capability: SystemCapability.Print.PrintFramework

Since: 12

Parameters:

NameDescription
printerIdPrinter ID to restore.
propertyKeyListList of property keys to restore.

Returns:

Error CodeDescription
PRINT_ERROR_NONEOperation successful.
PRINT_ERROR_NO_PERMISSIONMissing print permission.
PRINT_ERROR_RPC_FAILUREUnable to connect to print service.

OH_Print_StartPrintByNative()

Print_ErrorCode OH_Print_StartPrintByNative(const char *printJobName,
                                          Print_PrintDocCallback printDocCallback,
                                          void *context);

Description

Starts print service.

Permission: ohos.permission.PRINT

System Capability: SystemCapability.Print.PrintFramework

Since: 13

Parameters:

NameDescription
printJobNamePrint job name.
printDocCallbackPrint document state callback.
contextCaller application context.

Returns:

Error CodeDescription
PRINT_ERROR_NONEOperation successful.
PRINT_ERROR_NO_PERMISSIONMissing print permission.
PRINT_ERROR_RPC_FAILUREUnable to connect to print service.

你可能感兴趣的鸿蒙文章

harmony 鸿蒙Basic Services Kit

harmony 鸿蒙DeviceInfo

harmony 鸿蒙InitSync

harmony 鸿蒙OsAccount

harmony 鸿蒙Pasteboard

harmony 鸿蒙Print_Margin

harmony 鸿蒙Print_PageSize

harmony 鸿蒙Print_PrintAttributes

harmony 鸿蒙Print_PrintDocCallback

harmony 鸿蒙Print_Range

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