printers

package module
v0.0.0-...-2aa73ff Latest Latest
Warning

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

Go to latest
Published: Nov 1, 2024 License: BSD-3-Clause Imports: 8 Imported by: 0

README

🖨 Windows printing

Call Windows operating system printer in Golang.

✨ Features

See http://godoc.org/github.com/godoes/printers for details.

🔰 Installation

go get -d github.com/godoes/printers

📝 Usage

package main

import (
    "log"

    "github.com/godoes/printers"
)

func main() {
    name, err := printers.GetDefault()
    if err != nil {
        log.Fatalln("GetDefault error:", err)
    }

    printer, err := printers.Open(name)
    if err != nil {
        log.Fatalln("Open error:", err)
    }
    defer func() {
        _ = printer.Close()
    }()

    jobs, err := printer.Jobs()
    if err != nil {
        log.Fatalln("Jobs error:", err)
    }
    log.Println("jobs:", jobs)
}

Forked from alexbrainman/printer.

📃 LICENSE

BSD-3-Clause

Documentation

Overview

Package printers Windows printing.

Index

Constants

View Source
const (
	CCHDEVICENAME = 32
	CCHFORMNAME   = 32

	DM_SPECVERSION uint16 = 0x00000401
	DM_COPY        uint32 = 2
	DM_MODIFY      uint32 = 8

	DM_ORIENTATION        uint32 = 0x00000001
	DM_PAPERSIZE          uint32 = 0x00000002
	DM_PAPERLENGTH        uint32 = 0x00000004
	DM_PAPERWIDTH         uint32 = 0x00000008
	DM_SCALE              uint32 = 0x00000010
	DM_POSITION           uint32 = 0x00000020
	DM_NUP                uint32 = 0x00000040
	DM_DISPLAYORIENTATION uint32 = 0x00000080
	DM_COPIES             uint32 = 0x00000100
	DM_DEFAULTSOURCE      uint32 = 0x00000200
	DM_PRINTQUALITY       uint32 = 0x00000400
	DM_COLOR              uint32 = 0x00000800
	DM_DUPLEX             uint32 = 0x00001000
	DM_YRESOLUTION        uint32 = 0x00002000
	DM_TTOPTION           uint32 = 0x00004000
	DM_COLLATE            uint32 = 0x00008000
	DM_FORMNAME           uint32 = 0x00010000
	DM_LOGPIXELS          uint32 = 0x00020000
	DM_BITSPERPEL         uint32 = 0x00040000
	DM_PELSWIDTH          uint32 = 0x00080000
	DM_PELSHEIGHT         uint32 = 0x00100000
	DM_DISPLAYFLAGS       uint32 = 0x00200000
	DM_DISPLAYFREQUENCY   uint32 = 0x00400000
	DM_ICMMETHOD          uint32 = 0x00800000
	DM_ICMINTENT          uint32 = 0x01000000
	DM_MEDIATYPE          uint32 = 0x02000000
	DM_DITHERTYPE         uint32 = 0x04000000
	DM_PANNINGWIDTH       uint32 = 0x08000000
	DM_PANNINGHEIGHT      uint32 = 0x10000000
	DM_DISPLAYFIXEDOUTPUT uint32 = 0x20000000

	DMORIENT_PORTRAIT  int16 = 1
	DMORIENT_LANDSCAPE int16 = 2

	DMCOLOR_MONOCHROME int16 = 1
	DMCOLOR_COLOR      int16 = 2

	DMDUP_SIMPLEX    int16 = 1
	DMDUP_VERTICAL   int16 = 2
	DMDUP_HORIZONTAL int16 = 3

	DMCOLLATE_FALSE int16 = 0
	DMCOLLATE_TRUE  int16 = 1

	DMNUP_SYSTEM uint32 = 1
	DMNUP_ONEUP  uint32 = 2
)
View Source
const (
	PRINTER_ENUM_LOCAL       = 2
	PRINTER_ENUM_CONNECTIONS = 4

	PRINTER_DRIVER_XPS = 0x00000002
)
View Source
const (
	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
)

Variables

This section is empty.

Functions

func AddCustomPaperSize

func AddCustomPaperSize(printerName, paperName string, widthMM, heightMM, leftMM, topMM uint32) (err error)

AddCustomPaperSize 添加自定义纸张规格

Reference C# code

- https://www.cnblogs.com/datacool/p/datacool_windowsapi_printerhelper.html - https://github.com/vanloc0301/ecouponsprinter/blob/master/ECouponsPrinter/ECouponsPrinter/Printer.cs

func AddForm

func AddForm(h syscall.Handle, level uint32, form *FORM_INFO_1) (err error)

func ClosePrinter

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

func DeleteCustomPaperSize

func DeleteCustomPaperSize(printerName, paperName string) (err error)

DeleteCustomPaperSize 删除自定义纸张规格

func DeleteForm

func DeleteForm(h syscall.Handle, pFormName *uint16) (err error)

func DocumentProperties

func DocumentProperties(hWnd uint32, h syscall.Handle, pDeviceName *uint16, devModeOut *DevMode, devModeIn *DevMode, fMode uint32) (err error)

func EndDocPrinter

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

func EndPagePrinter

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

func EnumForms

func EnumForms(h syscall.Handle, level uint32, pForm *byte, cbBuf uint32, pcbNeeded *uint32, pcReturned *uint32) (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 EnumMonitors

func EnumMonitors(name *uint16, level uint32, pMonitors *byte, cbBuf uint32, pcbNeeded *uint32, pcReturned *uint32) (err error)

func EnumPrinters

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

func GetDefault

func GetDefault() (printer string, err error)

GetDefault 获取默认打印机名称

func GetDefaultPrinter

func GetDefaultPrinter(buf *uint16, bufN *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 OpenPrinter

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

func ReadNames

func ReadNames() ([]string, error)

ReadNames return printer names on the system

func SetDefault

func SetDefault(printer string) (err error)

SetDefault 根据打印机名称设置默认打印机

func SetDefaultPrinter

func SetDefaultPrinter(name *uint16) (err error)

func SetPrinter

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

func StartDocPrinter

func StartDocPrinter(h syscall.Handle, level uint32, docInfo *DOC_INFO_1) (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 DOC_INFO_1

type DOC_INFO_1 struct {
	/*
	  LPTSTR pDocName;
	  LPTSTR pOutputFile;
	  LPTSTR pDatatype;
	*/
	DocName    *uint16
	OutputFile *uint16
	Datatype   *uint16
}

type DRIVER_INFO_8

type DRIVER_INFO_8 struct {
	/*
	  DWORD     cVersion;
	  LPTSTR    pName;
	  LPTSTR    pEnvironment;
	  LPTSTR    pDriverPath;
	  LPTSTR    pDataFile;
	  LPTSTR    pConfigFile;
	  LPTSTR    pHelpFile;
	  LPTSTR    pDependentFiles;
	  LPTSTR    pMonitorName;
	  LPTSTR    pDefaultDataType;
	  LPTSTR    pszzPreviousNames;
	  FILETIME  ftDriverDate;
	  DWORDLONG dwlDriverVersion;
	  LPTSTR    pszMfgName;
	  LPTSTR    pszOEMUrl;
	  LPTSTR    pszHardwareID;
	  LPTSTR    pszProvider;
	  LPTSTR    pszPrintProcessor;
	  LPTSTR    pszVendorSetup;
	  LPTSTR    pszzColorProfiles;
	  LPTSTR    pszInfPath;
	  DWORD     dwPrinterDriverAttributes;
	  LPTSTR    pszzCoreDriverDependencies;
	  FILETIME  ftMinInboxDriverVerDate;
	  DWORDLONG dwlMinInboxDriverVerVersion;
	*/
	Version                  uint32
	Name                     *uint16
	Environment              *uint16
	DriverPath               *uint16
	DataFile                 *uint16
	ConfigFile               *uint16
	HelpFile                 *uint16
	DependentFiles           *uint16
	MonitorName              *uint16
	DefaultDataType          *uint16
	PreviousNames            *uint16
	DriverDate               windows.Filetime
	DriverVersion            uint64
	MfgName                  *uint16
	OEMUrl                   *uint16
	HardwareID               *uint16
	Provider                 *uint16
	PrintProcessor           *uint16
	VendorSetup              *uint16
	ColorProfiles            *uint16
	InfPath                  *uint16
	PrinterDriverAttributes  uint32
	CoreDriverDependencies   *uint16
	MinInboxDriverVerDate    windows.Filetime
	MinInboxDriverVerVersion uint32
}

type DevMode

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

DevMode struct.

func (*DevMode) ClearPaperLength

func (dm *DevMode) ClearPaperLength()

func (*DevMode) ClearPaperSize

func (dm *DevMode) ClearPaperSize()

func (*DevMode) ClearPaperWidth

func (dm *DevMode) ClearPaperWidth()

func (*DevMode) GetCollate

func (dm *DevMode) GetCollate() (int16, bool)

func (*DevMode) GetColor

func (dm *DevMode) GetColor() (int16, bool)

func (*DevMode) GetCopies

func (dm *DevMode) GetCopies() (int16, bool)

func (*DevMode) GetDeviceName

func (dm *DevMode) GetDeviceName() string

func (*DevMode) GetDuplex

func (dm *DevMode) GetDuplex() (int16, bool)

func (*DevMode) GetOrientation

func (dm *DevMode) GetOrientation() (int16, bool)

func (*DevMode) GetPaperLength

func (dm *DevMode) GetPaperLength() (int16, bool)

func (*DevMode) GetPaperSize

func (dm *DevMode) GetPaperSize() (int16, bool)

func (*DevMode) GetPaperWidth

func (dm *DevMode) GetPaperWidth() (int16, bool)

func (*DevMode) SetCollate

func (dm *DevMode) SetCollate(collate int16)

func (*DevMode) SetColor

func (dm *DevMode) SetColor(color int16)

func (*DevMode) SetCopies

func (dm *DevMode) SetCopies(copies int16)

func (*DevMode) SetDuplex

func (dm *DevMode) SetDuplex(duplex int16)

func (*DevMode) SetOrientation

func (dm *DevMode) SetOrientation(orientation int16)

func (*DevMode) SetPaperLength

func (dm *DevMode) SetPaperLength(length int16)

func (*DevMode) SetPaperSize

func (dm *DevMode) SetPaperSize(paperSize int16)

func (*DevMode) SetPaperWidth

func (dm *DevMode) SetPaperWidth(width int16)

func (*DevMode) String

func (dm *DevMode) String() string

type DriverInfo

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

DriverInfo stores information about printer driver.

type FORM_INFO_1

type FORM_INFO_1 struct {
	/*
	  DWORD  Flags;
	  LPTSTR pName;
	  SIZEL  Size;
	  RECTL  ImageableArea;
	*/
	Flags uint32

	Size          SIZE
	ImageableArea Rect
	// contains filtered or unexported fields
}

type FormInfo

type FormInfo struct {
	Flags         uint32
	Name          string
	Size          SIZE
	ImageableArea Rect
}

FormInfo stores information about a print form.

type JOB_INFO_1

type JOB_INFO_1 struct {
	/*
	  DWORD      JobId;
	  LPTSTR     pPrinterName;
	  LPTSTR     pMachineName;
	  LPTSTR     pUserName;
	  LPTSTR     pDocument;
	  LPTSTR     pDatatype;
	  LPTSTR     pStatus;
	  DWORD      Status;
	  DWORD      Priority;
	  DWORD      Position;
	  DWORD      TotalPages;
	  DWORD      PagesPrinted;
	  SYSTEMTIME Submitted;
	*/
	JobID        uint32
	PrinterName  *uint16
	MachineName  *uint16
	UserName     *uint16
	Document     *uint16
	DataType     *uint16
	Status       *uint16
	StatusCode   uint32
	Priority     uint32
	Position     uint32
	TotalPages   uint32
	PagesPrinted uint32
	Submitted    windows.Systemtime
}

type JobInfo

type JobInfo struct {
	JobID           uint32
	UserMachineName string
	UserName        string
	DocumentName    string
	DataType        string
	Status          string
	StatusCode      uint32
	Priority        uint32
	Position        uint32
	TotalPages      uint32
	PagesPrinted    uint32
	Submitted       time.Time
}

JobInfo stores information about a print job.

type MONITOR_INFO_2

type MONITOR_INFO_2 struct {
	Name        *uint16 // Monitor name
	Environment *uint16 // Printer environment (e.g., Windows NT x86, Windows NT RISC, Windows 4.0)
	DLLName     *uint16 // Path to the DLL file for the monitor
}

MONITOR_INFO_2 corresponds to the Windows MONITOR_INFO_2 structure.

type MonitorInfo

type MonitorInfo struct {
	Name string
	Dll  string
}

func GetMonitorList

func GetMonitorList() ([]MonitorInfo, error)

type PRINTER_INFO_2

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

func (*PRINTER_INFO_2) GetDataType

func (pi *PRINTER_INFO_2) GetDataType() string

type PRINTER_INFO_5

type PRINTER_INFO_5 struct {
	/*
	  LPTSTR pPrinterName;
	  LPTSTR pPortName;
	  DWORD  Attributes;
	  DWORD  DeviceNotSelectedTimeout;
	  DWORD  TransmissionRetryTimeout;
	*/
	PrinterName              *uint16
	PortName                 *uint16
	Attributes               uint32
	DeviceNotSelectedTimeout uint32
	TransmissionRetryTimeout uint32
}

type PRINTER_INFO_9

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

type Printer

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

func Open

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

func OpenWithDefaults

func OpenWithDefaults(name string, defaults *PrinterDefaults) (*Printer, error)

func (*Printer) Close

func (p *Printer) Close() error

func (*Printer) DocumentPropertiesGet

func (p *Printer) DocumentPropertiesGet(deviceName string) (devMode *DevMode, err error)

func (*Printer) DocumentPropertiesSet

func (p *Printer) DocumentPropertiesSet(deviceName string, devMode *DevMode) (err error)

func (*Printer) DriverInfo

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

DriverInfo returns information about printer p driver.

func (*Printer) EndDocument

func (p *Printer) EndDocument() error

func (*Printer) EndPage

func (p *Printer) EndPage() error

func (*Printer) Forms

func (p *Printer) Forms() (forms []FormInfo, err error)

Forms returns information about all paper size forms on the print server

func (*Printer) GetDataType

func (p *Printer) GetDataType() (dataType string, err error)

func (*Printer) GetPrinter2

func (p *Printer) GetPrinter2() (printerInfo *PRINTER_INFO_2, err error)

GetPrinter2 get Printer Info 2

func (*Printer) GetPrinter9

func (p *Printer) GetPrinter9() (printerInfo *PRINTER_INFO_9, err error)

GetPrinter9 get Printer Info 9

func (*Printer) Jobs

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

Jobs returns information about all print jobs on this printer

func (*Printer) SetPrinter2

func (p *Printer) SetPrinter2(printerInfo *PRINTER_INFO_2) (err error)

func (*Printer) SetPrinter9

func (p *Printer) SetPrinter9(printerInfo *PRINTER_INFO_9) (err error)

func (*Printer) StartDocument

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

func (*Printer) StartPage

func (p *Printer) StartPage() error

func (*Printer) StartRawDocument

func (p *Printer) StartRawDocument(name string) error

StartRawDocument calls StartDocument and passes either "RAW" or "XPS_PASS" as a document type, depending on if printer driver is XPS-based or not.

func (*Printer) Write

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

type PrinterDefaults

type PrinterDefaults struct {
	Datatype *uint16

	DesiredAccess uint32
	// contains filtered or unexported fields
}

type PrinterInfo

type PrinterInfo struct {
	PrinterName string
	PortName    string
}

func GetPrinterList

func GetPrinterList() ([]PrinterInfo, error)

带端口的打印机列表

type Rect

type Rect struct {
	Left   uint32
	Top    uint32
	Right  uint32
	Bottom uint32
}

Rect windows.Rect

type SIZE

type SIZE struct {
	Width  uint32 // 宽度,以千毫米为单位
	Height uint32 // 高度,以千毫米为单位
}

SIZE windows.Coord

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