printer

package module
v0.0.0-...-4df9979 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 14, 2021 License: BSD-3-Clause Imports: 8 Imported by: 0

README

Windows printing.

See http://godoc.org/github.com/alexbrainman/printer for details.

Documentation

Overview

Windows printing.

Index

Constants

View Source
const (
	PRINTER_ENUM_LOCAL       = 2
	PRINTER_ENUM_CONNECTIONS = 4

	PRINTER_DRIVER_XPS = 0x00000002

	PRINTER_CHANGE_ADD_PRINTER               = 0x00000001
	PRINTER_CHANGE_SET_PRINTER               = 0x00000002
	PRINTER_CHANGE_DELETE_PRINTER            = 0x00000004
	PRINTER_CHANGE_FAILED_CONNECTION_PRINTER = 0x00000008
	PRINTER_CHANGE_PRINTER                   = 0x000000FF
	PRINTER_CHANGE_ADD_JOB                   = 0x00000100
	PRINTER_CHANGE_SET_JOB                   = 0x00000200
	PRINTER_CHANGE_DELETE_JOB                = 0x00000400
	PRINTER_CHANGE_WRITE_JOB                 = 0x00000800
	PRINTER_CHANGE_JOB                       = 0x0000FF00
	PRINTER_CHANGE_ADD_FORM                  = 0x00010000
	PRINTER_CHANGE_SET_FORM                  = 0x00020000
	PRINTER_CHANGE_DELETE_FORM               = 0x00040000
	PRINTER_CHANGE_FORM                      = 0x00070000
	PRINTER_CHANGE_ADD_PORT                  = 0x00100000
	PRINTER_CHANGE_CONFIGURE_PORT            = 0x00200000
	PRINTER_CHANGE_DELETE_PORT               = 0x00400000
	PRINTER_CHANGE_PORT                      = 0x00700000
	PRINTER_CHANGE_ADD_PRINT_PROCESSOR       = 0x01000000
	PRINTER_CHANGE_DELETE_PRINT_PROCESSOR    = 0x04000000
	PRINTER_CHANGE_PRINT_PROCESSOR           = 0x07000000
	PRINTER_CHANGE_SERVER                    = 0x08000000
	PRINTER_CHANGE_ADD_PRINTER_DRIVER        = 0x10000000
	PRINTER_CHANGE_SET_PRINTER_DRIVER        = 0x20000000
	PRINTER_CHANGE_DELETE_PRINTER_DRIVER     = 0x40000000
	PRINTER_CHANGE_PRINTER_DRIVER            = 0x70000000
	PRINTER_CHANGE_TIMEOUT                   = 0x80000000
	PRINTER_CHANGE_ALL                       = 0x7F77FFFF

	JOB_NOTIFY_FIELD_PRINTER_NAME        = 0x00
	JOB_NOTIFY_FIELD_MACHINE_NAME        = 0x01
	JOB_NOTIFY_FIELD_PORT_NAME           = 0x02
	JOB_NOTIFY_FIELD_USER_NAME           = 0x03
	JOB_NOTIFY_FIELD_NOTIFY_NAME         = 0x04
	JOB_NOTIFY_FIELD_DATATYPE            = 0x05
	JOB_NOTIFY_FIELD_PRINT_PROCESSOR     = 0x06
	JOB_NOTIFY_FIELD_PARAMETERS          = 0x07
	JOB_NOTIFY_FIELD_DRIVER_NAME         = 0x08
	JOB_NOTIFY_FIELD_DEVMODE             = 0x09
	JOB_NOTIFY_FIELD_STATUS              = 0x0A
	JOB_NOTIFY_FIELD_STATUS_STRING       = 0x0B
	JOB_NOTIFY_FIELD_SECURITY_DESCRIPTOR = 0x0C
	JOB_NOTIFY_FIELD_DOCUMENT            = 0x0D
	JOB_NOTIFY_FIELD_PRIORITY            = 0x0E
	JOB_NOTIFY_FIELD_POSITION            = 0x0F
	JOB_NOTIFY_FIELD_SUBMITTED           = 0x10
	JOB_NOTIFY_FIELD_START_TIME          = 0x11
	JOB_NOTIFY_FIELD_UNTIL_TIME          = 0x12
	JOB_NOTIFY_FIELD_TIME                = 0x13
	JOB_NOTIFY_FIELD_TOTAL_PAGES         = 0x14
	JOB_NOTIFY_FIELD_PAGES_PRINTED       = 0x15
	JOB_NOTIFY_FIELD_TOTAL_BYTES         = 0x16
	JOB_NOTIFY_FIELD_BYTES_PRINTED       = 0x17
	JOB_NOTIFY_FIELD_REMOTE_JOB_ID       = 0x18

	PRINTER_NOTIFY_TYPE = 0 // TODO: Implement support for this
	JOB_NOTIFY_TYPE     = 1

	PRINTER_NOTIFY_INFO_DISCARDED    = 1
	PRINTER_NOTIFY_OPTIONS_REFRESH   = 1
	PRINTER_NOTIFY_MAX_NOTIFICATIONS = 0xffff

	JOB_STATUS_PAUSED            = 0x00000001 // Job is paused
	JOB_STATUS_ERROR             = 0x00000002 // An error is associated with the job
	JOB_STATUS_DELETING          = 0x00000004 // Job is being deleted
	JOB_STATUS_SPOOLING          = 0x00000008 // Job is spooling
	JOB_STATUS_PRINTING          = 0x00000010 // Job is printing
	JOB_STATUS_OFFLINE           = 0x00000020 // Printer is offline
	JOB_STATUS_PAPEROUT          = 0x00000040 // Printer is out of paper
	JOB_STATUS_PRINTED           = 0x00000080 // Job has printed
	JOB_STATUS_DELETED           = 0x00000100 // Job has been deleted
	JOB_STATUS_BLOCKED_DEVQ      = 0x00000200 // Printer driver cannot print the job
	JOB_STATUS_USER_INTERVENTION = 0x00000400 // User action required
	JOB_STATUS_RESTART           = 0x00000800 // Job has been restarted
	JOB_STATUS_COMPLETE          = 0x00001000 // Job has been delivered to the printer
	JOB_STATUS_RETAINED          = 0x00002000 // Job has been retained in the print queue
	JOB_STATUS_RENDERING_LOCALLY = 0x00004000 // Job rendering locally on the client

	JOB_CONTROL_PAUSE             = 1
	JOB_CONTROL_RESUME            = 2
	JOB_CONTROL_CANCEL            = 3
	JOB_CONTROL_RESTART           = 4
	JOB_CONTROL_DELETE            = 5
	JOB_CONTROL_SENT_TO_PRINTER   = 6
	JOB_CONTROL_LAST_PAGE_EJECTED = 7
	JOB_CONTROL_RETAIN            = 8
	JOB_CONTROL_RELEASE           = 9

	JOB_POSITION_UNSPECIFIED = 0
)

Variables

View Source
var ErrNoNotification = errors.New("no notification information")

JobNotifyAll is a util providing a slice of all JOB_NOTIFY_FIELD_* values

Functions

func ClosePrinter

func ClosePrinter(h syscall.Handle) (err error)

func Default

func Default() (string, error)

func EndDocPrinter

func EndDocPrinter(h syscall.Handle) (err error)

func EndPagePrinter

func EndPagePrinter(h syscall.Handle) (err error)

func EnumJobs

func EnumJobs(h syscall.Handle, firstJob uint32, noJobs uint32, level uint32, buf *byte, bufN uint32, bytesNeeded *uint32, jobsReturned *uint32) (err error)

func EnumPrinters

func EnumPrinters(flags uint32, name *uint16, level uint32, buf *byte, bufN uint32, needed *uint32, returned *uint32) (err error)

func FindClosePrinterChangeNotification

func FindClosePrinterChangeNotification(h syscall.Handle) (err error)

func FindFirstPrinterChangeNotification

func FindFirstPrinterChangeNotification(h syscall.Handle, filter uint32, options uint32, notifyOptions *PRINTER_NOTIFY_OPTIONS) (rtn syscall.Handle, err error)

func FindNextPrinterChangeNotification

func FindNextPrinterChangeNotification(h syscall.Handle, cause *uint16, options *PRINTER_NOTIFY_OPTIONS, info **PRINTER_NOTIFY_INFO) (err error)

func FreePrinterNotifyInfo

func FreePrinterNotifyInfo(info *PRINTER_NOTIFY_INFO) (err error)

func GetDefaultPrinter

func GetDefaultPrinter(buf *uint16, bufN *uint32) (err error)

func GetJob

func GetJob(h syscall.Handle, jobId uint32, level uint32, buf *byte, bufN uint32, bytesNeeded *uint32) (err error)

func GetPrinter

func GetPrinter(h syscall.Handle, level uint32, buf *byte, bufN uint32, needed *uint32) (err error)

func GetPrinterDriver

func GetPrinterDriver(h syscall.Handle, env *uint16, level uint32, di *byte, n uint32, needed *uint32) (err error)

func JobNotifyFieldToString

func JobNotifyFieldToString(field uint16) string

JobNotifyFieldToString maps all JOB_NOTIFY_FIELD_* values to a human readable string

func OpenPrinter

func OpenPrinter(name *uint16, h *syscall.Handle, defaults uintptr) (err error)

func ReadNames

func ReadNames() ([]string, error)

ReadNames return printer names on the system

func SetJob

func SetJob(h syscall.Handle, jobId uint32, level uint32, buf *byte, command uint32) (err error)

func StartDocPrinter

func StartDocPrinter(h syscall.Handle, level uint32, docinfo *DOC_INFO_1) (rtn uint32, err error)

func StartPagePrinter

func StartPagePrinter(h syscall.Handle) (err error)

func WritePrinter

func WritePrinter(h syscall.Handle, buf *byte, bufN uint32, written *uint32) (err error)

Types

type ChangeNotificationHandle

type ChangeNotificationHandle struct {
	// contains filtered or unexported fields
}

ChangeNotificationHandle wraps the change notification object created by Printer::ChangeNotifications

func (*ChangeNotificationHandle) Close

func (c *ChangeNotificationHandle) Close() error

Close closes the change notification handle, wrapping FindClosePrinterChangeNotification see https://docs.microsoft.com/en-us/windows/desktop/printdocs/findcloseprinterchangenotification

func (*ChangeNotificationHandle) Next

func (c *ChangeNotificationHandle) Next(printerNotifyOptions *PRINTER_NOTIFY_OPTIONS) (*NotifyInfo, error)

Next retrieves information about the most recent change notification for a change notification object associated with a printer or print server It effectively wraps FindNextPrinterChangeNotification see https://docs.microsoft.com/en-us/windows/desktop/printdocs/findnextprinterchangenotification

func (*ChangeNotificationHandle) Wait

func (c *ChangeNotificationHandle) Wait(milliseconds uint32) (uint32, error)

Wait calls WaitForSingleObject on the change notification handle

type DOC_INFO_1

type DOC_INFO_1 struct {
	DocName    *uint16
	OutputFile *uint16
	Datatype   *uint16
}

type DRIVER_INFO_8

type DRIVER_INFO_8 struct {
	Version                  uint32
	Name                     *uint16
	Environment              *uint16
	DriverPath               *uint16
	DataFile                 *uint16
	ConfigFile               *uint16
	HelpFile                 *uint16
	DependentFiles           *uint16
	MonitorName              *uint16
	DefaultDataType          *uint16
	PreviousNames            *uint16
	DriverDate               syscall.Filetime
	DriverVersion            uint64
	MfgName                  *uint16
	OEMUrl                   *uint16
	HardwareID               *uint16
	Provider                 *uint16
	PrintProcessor           *uint16
	VendorSetup              *uint16
	ColorProfiles            *uint16
	InfPath                  *uint16
	PrinterDriverAttributes  uint32
	CoreDriverDependencies   *uint16
	MinInboxDriverVerDate    syscall.Filetime
	MinInboxDriverVerVersion uint32
}

type DriverInfo

type DriverInfo struct {
	Name        string
	Environment string
	DriverPath  string
	Attributes  uint32
}

DriverInfo stores information about printer driver.

type Info

type Info struct {
	PrinterName              string
	PortName                 string
	Attributes               uint32
	DeviceNotSelectedTimeout uint32
	TransmissionRetryTimeout uint32
}

PRINTER_INFO_5 as a Golang struct

type JOB_INFO_4

type JOB_INFO_4 struct {
	JobID              uint32
	PrinterName        *uint16
	MachineName        *uint16
	UserName           *uint16
	Document           *uint16
	NotifyName         *uint16
	DataType           *uint16
	PrintProcessor     *uint16
	Parameters         *uint16
	DriverName         *uint16
	Devmode            unsafe.Pointer
	Status             *uint16
	SecurityDescriptor unsafe.Pointer
	StatusCode         uint32
	Priority           uint32
	Position           uint32
	StartTime          uint32
	UntilTime          uint32
	TotalPages         uint32
	Size               uint32
	Submitted          syscall.Systemtime
	Time               uint32
	PagesPrinted       uint32
	SizeHigh           uint32
}

func (*JOB_INFO_4) ToJobInfo

func (j *JOB_INFO_4) ToJobInfo() *JobInfo

type JobInfo

type JobInfo struct {
	JobID           uint32        // a job identifier value
	PrinterName     string        // the name of the printer for which the job is spooled
	UserMachineName string        // the name of the machine that created the print job
	UserName        string        // the name of the user who owns the print job
	DocumentName    string        // the name of the print job (for example, "MS-WORD: Review.doc")
	NotifyName      string        // the name of the user who should be notified when the job has been printed or when an error occurs while printing the job
	DataType        string        // the type of data used to record the print job
	PrintProcessor  string        // the name of the print processor that should be used to print the job
	Parameters      string        // print-processor parameters
	DriverName      string        // the name of the printer driver that should be used to process the print job
	Status          string        // the status of the print job. This member should be checked prior to StatusCode and takes precedence over it
	StatusCode      uint32        // the job status as a bitmap of JOB_STATUS_* constants
	Priority        uint32        // the job priority. This member can be one of the following values or in the range between 1 through 99 (MIN_PRIORITY through MAX_PRIORITY)
	Position        uint32        // the job's position in the print queue
	StartTime       uint32        // the earliest time that the job can be printed
	UntilTime       uint32        // the latest time that the job can be printed
	TotalPages      uint32        // the number of pages required for the job. This value may be zero if the print job does not contain page delimiting information
	Size            uint64        // the size, in bytes, of the job.
	Time            time.Duration // the total time, in milliseconds, that has elapsed since the job began printing
	PagesPrinted    uint32        // the number of pages that have printed. This value may be zero if the print job does not contain page delimiting information
	Submitted       time.Time     // the time when the job was submitted
}

JobInfo stores information about a print job.

type NOTIFY_DATA

type NOTIFY_DATA struct {
	Datasz  uint32
	Dataptr unsafe.Pointer
}

type NotifyInfo

type NotifyInfo struct {
	Version int
	Flags   uint
	Cause   uint
	Data    []*NotifyInfoData
}

NotifyInfo is a golang friendly PRINTER_NOTIFY_INFO struct see https://docs.microsoft.com/en-us/windows/desktop/printdocs/printer-notify-info

func (*NotifyInfo) String

func (pni *NotifyInfo) String() string

type NotifyInfoData

type NotifyInfoData struct {
	Type  uint16 // one of PRINTER_NOTIFY_TYPE or JOB_NOTIFY_TYPE
	Field uint16 // JOB_NOTIFY_FIELD_* or PRINTER_NOTIFY_FIELD_* depending on the above
	ID    uint32 // if JOB_NOTIFY_TYPE, this is the print job ID

	Value interface{}
}

NotifyInfoData is a golang friendly PRINTER_NOTIFY_INFO_DATA notably the union is now expressed as an interface{} type See https://docs.microsoft.com/en-us/windows/desktop/printdocs/printer-notify-info-data

func (*NotifyInfoData) String

func (pnid *NotifyInfoData) String() string

type PRINTER_INFO_5

type PRINTER_INFO_5 struct {
	PrinterName              *uint16
	PortName                 *uint16
	Attributes               uint32
	DeviceNotSelectedTimeout uint32
	TransmissionRetryTimeout uint32
}

type PRINTER_NOTIFY_INFO

type PRINTER_NOTIFY_INFO struct {
	Version uint32
	Flags   uint32
	Count   uint32
	PData   [PRINTER_NOTIFY_MAX_NOTIFICATIONS]PRINTER_NOTIFY_INFO_DATA
}

func (*PRINTER_NOTIFY_INFO) ToNotifyInfo

func (pni *PRINTER_NOTIFY_INFO) ToNotifyInfo() *NotifyInfo

ToNotifyInfo converts the C-like PRINTER_NOTIFY_INFO struct to a more Golang friendly NotifyInfo

type PRINTER_NOTIFY_INFO_DATA

type PRINTER_NOTIFY_INFO_DATA struct {
	Type       uint16
	Field      uint16
	Reserved   uint32
	ID         uint32
	NotifyData NOTIFY_DATA
}

func (*PRINTER_NOTIFY_INFO_DATA) ToNotifyInfoData

func (pnid *PRINTER_NOTIFY_INFO_DATA) ToNotifyInfoData() *NotifyInfoData

ToNotifyInfoData converts the C-like PRINTER_NOTIFY_INFO_DATA struct to a more Golang friendly NotifyInfoData

type PRINTER_NOTIFY_OPTIONS

type PRINTER_NOTIFY_OPTIONS struct {
	Version uint32
	Flags   uint32
	Count   uint32
	PTypes  *PRINTER_NOTIFY_OPTIONS_TYPE
}

type PRINTER_NOTIFY_OPTIONS_TYPE

type PRINTER_NOTIFY_OPTIONS_TYPE struct {
	Type      uint16
	Reserved0 uint16
	Reserved1 uint32
	Reserved2 uint32
	Count     uint32
	PFields   *uint16
}

type Printer

type Printer struct {
	// contains filtered or unexported fields
}

func Open

func Open(name string) (*Printer, error)

func (*Printer) ChangeNotifications

func (p *Printer) ChangeNotifications(filter uint32, options uint32, printerNotifyOptions *PRINTER_NOTIFY_OPTIONS) (*ChangeNotificationHandle, error)

ChangeNotifications gets a handle that can be used to query for spooler notifications. It effectively wraps FindFirstPrinterChangeNotification see https://docs.microsoft.com/en-us/windows/desktop/printdocs/findfirstprinterchangenotification

func (*Printer) Close

func (p *Printer) Close() error

func (*Printer) DriverInfo

func (p *Printer) DriverInfo() (*DriverInfo, error)

DriverInfo returns information about a printer's driver.

func (*Printer) EndDocument

func (p *Printer) EndDocument() error

func (*Printer) EndPage

func (p *Printer) EndPage() error

func (*Printer) GetNotifications

func (p *Printer) GetNotifications(done <-chan struct{}, filter uint32, options uint32, printerNotifyOptions *PRINTER_NOTIFY_OPTIONS) (<-chan *NotifyInfo, error)

GetNotifications wraps the whole FindFirstPrinterChangeNotification, WaitForSingleObject, FindNextPrinterChangeNotification, FindClosePrinterChangeNotification process and vends notifications out of a channel To finish notifications and cleanup, close the passed in done channel

func (*Printer) Job

func (p *Printer) Job(jobId uint32) (*JobInfo, error)

func (*Printer) Jobs

func (p *Printer) Jobs() ([]JobInfo, error)

Jobs returns information about all print jobs on this printer

func (*Printer) PrinterInfo

func (p *Printer) PrinterInfo() (*Info, error)

PrinterInfo returns information about a printer

func (*Printer) SetJob

func (p *Printer) SetJob(jobID uint32, jobInfo *JobInfo, command uint32) error

func (*Printer) StartDocument

func (p *Printer) StartDocument(name, outputFile, datatype string) (uint32, error)

StartDocument wraps StartDocPrinter windows API call Empty strings translate to NULL arguments in DOC_INFO_1 returns job id of new job, if no error

func (*Printer) StartPage

func (p *Printer) StartPage() error

func (*Printer) StartRawDocument

func (p *Printer) StartRawDocument(name, outputFile string) (uint32, error)

StartRawDocument calls StartDocument and passes either "RAW" or "XPS_PASS" as a document type, depending if printer driver is XPS-based or not. returns job id of new job, if no error

func (*Printer) Write

func (p *Printer) Write(b []byte) (int, error)

Directories

Path Synopsis
cmd
print
print command prints text documents to selected printer.
print command prints text documents to selected printer.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL