goprint

package module
v0.0.0-...-f66dd30 Latest Latest
Warning

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

Go to latest
Published: Aug 25, 2022 License: MIT Imports: 6 Imported by: 0

README

goprint

UPDATE 11 August 2022: Hello! I just discovered today that there was a conflict of what was posted here in this repo and the code I had originally developed for the commit done on 2021-03-05. Therefore, the example in the examples folder obviously didn't work. I currently do not have a windows computer nor do I have access to a printer network so I can't verify that this code is correct and works. Therefore, a new branch was created named 'dev'. I encourage anyone who can to please download that branch and test it. It adds features which allow you to control setting printer options such as duplex, changing your default printer, etc. This repo may be useful to you but I cannot promise all features will work for you there are many different kinds of printers and as I do not have the ability to test it. If you are testing the dev branch and run into issues, please post them and I will do my best to assist with any corretions.

See printer repo for a much more user friendly printer. After many hours of trying to get my printer working, I ended up using some of the code found there so I could print PDFs on windows.

NOTE: This implementation heavily depends on the printer itself to draw complex document types such as PDFs as the code isn't doing any of the drawing. Therefore, try sending a simple text file to your printer first. This should work in all cases that I have tested. Then if more complex document types don't work, this will be due to the printer not being able to read and convert that document type itself. If posting issues, please try this first and specify which printer you're using.

In the end, I did not end up using this package for production and switched to a Linux server and use CUPS with the proper drivers. This allows you to fully control the printer(duplex, stapling, etc) from a CLI.

Example usage:

package main

import (
	"log"
	"github.com/jadefox10200/goprint"
)

func main() {

	printerName, _ := goprint.GetDefaultPrinterName()

	//open the printer
	printerHandle, err := goprint.GoOpenPrinter(printerName)	
	if err != nil {log.Fatalln("Failed to open printer")}
	defer goprint.GoClosePrinter(printerHandle)
	
	filePath := "C:/test/myPDF.pdf"
		
	//Send to printer:		
	err = goprint.GoPrint(printerHandle, filePath)
	if err != nil {	log.Fatalln("during the func sendToPrinter, there was an error") }


}

Documentation

Index

Constants

View Source
const (
	PRINTER_ATTRIBUTE_QUEUED            uint32 = 0x00000001
	PRINTER_ATTRIBUTE_DIRECT            uint32 = 0x00000002
	PRINTER_ATTRIBUTE_DEFAULT           uint32 = 0x00000004
	PRINTER_ATTRIBUTE_SHARED            uint32 = 0x00000008
	PRINTER_ATTRIBUTE_NETWORK           uint32 = 0x00000010
	PRINTER_ATTRIBUTE_HIDDEN            uint32 = 0x00000020
	PRINTER_ATTRIBUTE_LOCAL             uint32 = 0x00000040
	PRINTER_ATTRIBUTE_ENABLE_DEVQ       uint32 = 0x00000080
	PRINTER_ATTRIBUTE_KEEPPRINTEDJOBS   uint32 = 0x00000100
	PRINTER_ATTRIBUTE_DO_COMPLETE_FIRST uint32 = 0x00000200
	PRINTER_ATTRIBUTE_WORK_OFFLINE      uint32 = 0x00000400
	PRINTER_ATTRIBUTE_ENABLE_BIDI       uint32 = 0x00000800
	PRINTER_ATTRIBUTE_RAW_ONLY          uint32 = 0x00001000
	PRINTER_ATTRIBUTE_PUBLISHED         uint32 = 0x00002000
)

PRINTER_INFO_2 attribute values

View Source
const (
	PRINTER_STATUS_PAUSED               uint32 = 0x00000001
	PRINTER_STATUS_ERROR                uint32 = 0x00000002
	PRINTER_STATUS_PENDING_DELETION     uint32 = 0x00000004
	PRINTER_STATUS_PAPER_JAM            uint32 = 0x00000008
	PRINTER_STATUS_PAPER_OUT            uint32 = 0x00000010
	PRINTER_STATUS_MANUAL_FEED          uint32 = 0x00000020
	PRINTER_STATUS_PAPER_PROBLEM        uint32 = 0x00000040
	PRINTER_STATUS_OFFLINE              uint32 = 0x00000080
	PRINTER_STATUS_IO_ACTIVE            uint32 = 0x00000100
	PRINTER_STATUS_BUSY                 uint32 = 0x00000200
	PRINTER_STATUS_PRINTING             uint32 = 0x00000400
	PRINTER_STATUS_OUTPUT_BIN_FULL      uint32 = 0x00000800
	PRINTER_STATUS_NOT_AVAILABLE        uint32 = 0x00001000
	PRINTER_STATUS_WAITING              uint32 = 0x00002000
	PRINTER_STATUS_PROCESSING           uint32 = 0x00004000
	PRINTER_STATUS_INITIALIZING         uint32 = 0x00008000
	PRINTER_STATUS_WARMING_UP           uint32 = 0x00010000
	PRINTER_STATUS_TONER_LOW            uint32 = 0x00020000
	PRINTER_STATUS_NO_TONER             uint32 = 0x00040000
	PRINTER_STATUS_PAGE_PUNT            uint32 = 0x00080000
	PRINTER_STATUS_USER_INTERVENTION    uint32 = 0x00100000
	PRINTER_STATUS_OUT_OF_MEMORY        uint32 = 0x00200000
	PRINTER_STATUS_DOOR_OPEN            uint32 = 0x00400000
	PRINTER_STATUS_SERVER_UNKNOWN       uint32 = 0x00800000
	PRINTER_STATUS_POWER_SAVE           uint32 = 0x01000000
	PRINTER_STATUS_SERVER_OFFLINE       uint32 = 0x02000000
	PRINTER_STATUS_DRIVER_UPDATE_NEEDED uint32 = 0x04000000
)

PRINTER_INFO_2 status values.

View Source
const (
	CCHDEVICENAME = 32
	CCHFORMNAME   = 32

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

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

Variables

This section is empty.

Functions

func GetDefaultPrinterName

func GetDefaultPrinterName() (string, []uint16)

func GetPrinterNames

func GetPrinterNames() ([]string, error)

func GoClosePrinter

func GoClosePrinter(printerHandle uintptr)

func GoOpenPrinter

func GoOpenPrinter(printerName string) (uintptr, error)

Opens a printer which can then be used to send documents to. Must be closed by user once.

func GoPrint

func GoPrint(printerHandle uintptr, path string) error

func GoPrintFromOpenFile

func GoPrintFromOpenFile(printerHandle uintptr, docName string, fileContents []byte) error

func IsvalidDevMode

func IsvalidDevMode(dev *DevMode, buf uint16) (b bool)

func OpenPrinter2

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

func SetDefaultPrinter

func SetDefaultPrinter(printerName string) (b bool)

Types

type DOC_INFO_1

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

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 DocInfo

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

DOCINFO struct.

type HANDLE

type HANDLE uintptr

func OpenPrinter

func OpenPrinter(printerName string) (HANDLE, error)

func (*HANDLE) ClosePrinter

func (hPrinter *HANDLE) ClosePrinter() error

func (HANDLE) DocumentPropertiesGet

func (hPrinter HANDLE) DocumentPropertiesGet(deviceName string) (*DevMode, error)

func (HANDLE) DocumentPropertiesSet

func (hPrinter HANDLE) DocumentPropertiesSet(deviceName string, devMode *DevMode) error

func (HANDLE) GetPrinter2

func (hPrinter HANDLE) GetPrinter2() (*PRINTER_INFO_2, error)

func (HANDLE) GetPrinter9

func (hPrinter HANDLE) GetPrinter9() (*DevMode, error)

func (*HANDLE) Print

func (hPrinter *HANDLE) Print(path string) error

func (HANDLE) SetDuplexPrinter2

func (hPrinter HANDLE) SetDuplexPrinter2(printerInfo *PRINTER_INFO_2)

func (HANDLE) SetPrinter

func (hPrinter HANDLE) SetPrinter(printerInfo *PRINTER_INFO_2) error

type PRINTER_INFO_2

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

PRINTER_INFO_2 struct.

func (*PRINTER_INFO_2) GetDataType

func (pi *PRINTER_INFO_2) GetDataType() string

type PRINTER_INFO_5

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

type PRINTER_INFO_9

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

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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