Documentation
¶
Overview ¶
Package goprint prints documents on Linux/BSD, macOS and Windows through one API, either headless or through the platform's native print dialog.
The package is pure Go: it builds with CGO_ENABLED=0 for every supported GOOS/GOARCH from any host. Documents are handled as PDF internally; raster sources ([]image.Image) are wrapped into an image-only PDF so that there is a single print path.
The API has three verbs:
- Printers and GetCapabilities for discovery,
- Print for headless printing,
- Dialog for interactive printing through the native dialog.
Settings flows both ways: into Dialog as presets, out of it as the user's choice.
Index ¶
- Constants
- Variables
- func Dialog(ctx context.Context, doc Document, opts DialogOptions) (*Job, Settings, error)
- func RunMain(f func())
- func SetMainThreadRunner(run func(f func()))
- type Capabilities
- type ColorMode
- type Credentials
- type DialogOptions
- type DialogStyle
- type Document
- type Duplex
- type Job
- type JobState
- type Media
- type Orientation
- type PageRange
- type Printer
- type Quality
- type Resolution
- type Scaling
- type Settings
- type Warning
Constants ¶
const ( // VendorOutputFile (Windows): the printer output is written to this file // instead of the device (print to file). With "Microsoft Print to PDF" // this yields a PDF without the save dialog. VendorOutputFile = "windows:output-file" // VendorGTKPrefix (Linux/BSD dialog) marks keys passed to the print // portal as GTK print settings, e.g. Vendor["gtk:output-bin"] = "top". // On the IPP path, keys without a namespace are sent as IPP attributes. VendorGTKPrefix = "gtk:" )
Settings.Vendor keys with platform-specific meaning. They are defined on every platform so that portable code compiles everywhere; backends that do not know a key report it as a warning.
Variables ¶
var ( ErrCanceled = errdefs.ErrCanceled ErrNoPrinter = errdefs.ErrNoPrinter ErrPrinterNotFound = errdefs.ErrPrinterNotFound ErrUnsupported = errdefs.ErrUnsupported ErrNoDialog = errdefs.ErrNoDialog ErrWrongThread = errdefs.ErrWrongThread ErrBusy = errdefs.ErrBusy ErrInvalid = errdefs.ErrInvalid )
Sentinel errors. Detailed errors wrap them and are reachable via errors.Is.
var ( MediaA3 = Media{"iso_a3_297x420mm", 297000, 420000} MediaA4 = Media{"iso_a4_210x297mm", 210000, 297000} MediaA5 = Media{"iso_a5_148x210mm", 148000, 210000} MediaLetter = Media{"na_letter_8.5x11in", 215900, 279400} MediaLegal = Media{"na_legal_8.5x14in", 215900, 355600} )
Common media.
Functions ¶
func Dialog ¶
Dialog shows the native print dialog, preset with opts.Settings, and returns the job (if printed) together with the settings the user chose. It returns ErrCanceled if the user cancels and ErrNoDialog if the platform has no dialog available.
func RunMain ¶
func RunMain(f func())
RunMain runs f and, while f runs, lets Dialog use the calling thread for the native UI. It returns when f returns.
On macOS, AppKit (and so the print panel) works only on the process's main thread. A program that shows dialogs locks its main goroutine to the main thread in an init function of package main and runs its work through RunMain:
func init() { runtime.LockOSThread() }
func main() {
goprint.RunMain(func() {
// Dialog may be called from any goroutine in here.
})
}
Alternatively Dialog can be called directly from the (locked) main goroutine. Called from another thread without RunMain, Dialog returns ErrWrongThread. Programs that already run a Cocoa event loop on the main thread (e.g. a GUI toolkit) call Dialog from that thread instead.
On other platforms RunMain just calls f.
func SetMainThreadRunner ¶ added in v0.2.0
func SetMainThreadRunner(run func(f func()))
SetMainThreadRunner tells goprint how to run code on the process's main thread in programs whose main thread is owned by a GUI toolkit, so that RunMain cannot be used. run must execute its argument on the main thread and return after it returned, e.g. fyne.DoAndWait. nil removes the runner. The package github.com/timzifer/goprint/fyneprint installs one for Fyne.
Only macOS needs the main thread (for the print panel); elsewhere run is never called. If run does not reach the main thread, Dialog returns ErrWrongThread. A RunMain loop, when active, takes precedence.
Types ¶
type Capabilities ¶
type Capabilities struct {
Media []Media
Duplex bool
Color bool
Resolutions []Resolution
// Formats lists accepted document MIME types, e.g. "application/pdf".
Formats []string
// DialogPreview reports whether the native dialog shows a print preview.
DialogPreview bool
}
Capabilities lists what a printer (and the platform's dialog) supports.
func GetCapabilities ¶
func GetCapabilities(ctx context.Context, printer string) (Capabilities, error)
GetCapabilities reports the capabilities of the named printer. An empty name refers to the default printer.
type Credentials ¶
Credentials for authenticated print queues.
type DialogOptions ¶
type DialogOptions struct {
// Settings are the presets shown in the dialog.
Settings Settings
// Owner is the parent window: HWND, NSWindow* or X11 window / portal
// parent. 0 means a private helper window.
Owner uintptr
// Style selects the dialog on Windows.
Style DialogStyle
// RequirePrinter forces a dialog that preselects Settings.Printer.
RequirePrinter bool
// PrintNow prints after confirmation. If false, Dialog only returns the
// chosen settings and a nil Job.
//
// On Windows, StyleAuto without PrintNow shows the classic dialog: it
// reports the chosen printer and full settings but has no preview, also
// where Windows 11 draws it in the modern look. StyleModern previews,
// but cannot report the printer and makes print-to-file printers ask
// for a file name.
PrintNow bool
}
DialogOptions configures Dialog.
type DialogStyle ¶
type DialogStyle int
DialogStyle selects the native dialog on platforms that offer several.
const ( // StyleAuto picks the best dialog for the platform and settings. StyleAuto DialogStyle = iota // StyleModern is the dialog with print preview (Windows 10+). StyleModern // StyleClassic is the dialog with full presets, including the printer. StyleClassic )
func (DialogStyle) String ¶
func (s DialogStyle) String() string
type Document ¶
type Document struct {
// Title is the job name in the spooler and the dialog title.
Title string
// PDF opens the PDF content. It is called once per consumer (preview
// and print run separately), so it must return a fresh reader each time.
PDF func() (io.ReadSeekCloser, error)
// Images is a raster source, one image per page.
Images []image.Image
// DPI is the resolution of Images. Required when Images is set.
DPI int
}
Document is the content to print. Exactly one of PDF or Images is set.
type Job ¶
type Job struct {
// contains filtered or unexported fields
}
Job is a submitted print job.
type Media ¶
Media is a paper size, identified by its PWG 5101.1 self-describing name (e.g. "iso_a4_210x297mm"). Width and Height are in micrometers.
func ParseMedia ¶
ParseMedia parses a PWG 5101.1 self-describing media name of the form "class_name_WxHunit" where unit is "mm" or "in".
type Orientation ¶
type Orientation int
Orientation of the content on the page.
const ( OrientationDefault Orientation = iota Portrait Landscape ReversePortrait ReverseLandscape )
func (Orientation) String ¶
func (o Orientation) String() string
type PageRange ¶
type PageRange struct {
From, To int
}
PageRange is an inclusive, 1-based page range. To == 0 means "to the end".
type Printer ¶
type Printer struct {
Name string
Description string
Location string
Default bool
Caps Capabilities
}
Printer describes a print queue.
type Resolution ¶
type Resolution struct {
X, Y int
}
Resolution is a printer resolution in dots per inch.
type Settings ¶
type Settings struct {
// Printer is the queue or printer name; empty means the default printer.
Printer string
// Copies is the number of copies; 0 means 1.
Copies int
// Collate requests collated copies; nil means printer default.
Collate *bool
// PageRanges selects pages (1-based, inclusive); empty means all pages.
PageRanges []PageRange
Media Media
Orientation Orientation
Duplex Duplex
Color ColorMode
Quality Quality
Scaling Scaling
// Tray is the media source, e.g. "tray-1" or "manual".
Tray string
// Vendor carries platform-specific values verbatim: IPP attributes on
// Unix and macOS, PrintTicket features on Windows.
Vendor map[string]string
// Strict turns warnings about unsupported settings into errors.
Strict bool
// Credentials authenticate against queues that require it (CUPS).
Credentials *Credentials
}
Settings are print settings. Zero values mean "printer default".
Source Files
¶
Directories
¶
| Path | Synopsis |
|---|---|
|
examples
|
|
|
printdemo
command
Command printdemo exercises goprint by hand: list printers, show capabilities, open the native print dialog, or print headless.
|
Command printdemo exercises goprint by hand: list printers, show capabilities, open the native print dialog, or print headless. |
|
fyneprint
module
|
|
|
internal
|
|
|
com
Package com calls COM and WinRT interfaces through hand-written vtables and implements COM objects in Go, without cgo.
|
Package com calls COM and WinRT interfaces through hand-written vtables and implements COM objects in Go, without cgo. |
|
core
Package core holds the platform-independent core: settings mapping, media tables and the PDF source.
|
Package core holds the platform-independent core: settings mapping, media tables and the PDF source. |
|
errdefs
Package errdefs defines the sentinel errors shared by goprint and its internal backends.
|
Package errdefs defines the sentinel errors shared by goprint and its internal backends. |
|
macprint
Package macprint shows the macOS print panel and runs PDFKit print operations.
|
Package macprint shows the macOS print panel and runs PDFKit print operations. |
|
portal
Package portal talks to the print portal of xdg-desktop-portal (org.freedesktop.portal.Print) over D-Bus.
|
Package portal talks to the print portal of xdg-desktop-portal (org.freedesktop.portal.Print) over D-Bus. |
|
portal/portaltest
Package portaltest runs a private D-Bus session with a fake org.freedesktop.portal.Print implementation for tests.
|
Package portaltest runs a private D-Bus session with a fake org.freedesktop.portal.Print implementation for tests. |
|
testpdf
Package testpdf generates small, valid PDF documents for tests.
|
Package testpdf generates small, valid PDF documents for tests. |
|
winprint
Package winprint implements printing on Windows: PDF pages are rendered by Windows.Data.Pdf onto Direct2D command lists, which ID2D1PrintControl turns into an XPS package for the spooler.
|
Package winprint implements printing on Windows: PDF pages are rendered by Windows.Data.Pdf onto Direct2D command lists, which ID2D1PrintControl turns into an XPS package for the spooler. |
|
Package ipp implements the Internet Printing Protocol (RFC 8010/8011): an attribute codec and a client for CUPS and IPP Everywhere printers.
|
Package ipp implements the Internet Printing Protocol (RFC 8010/8011): an attribute codec and a client for CUPS and IPP Everywhere printers. |
|
ipptest
Package ipptest provides an in-process IPP mock server for tests.
|
Package ipptest provides an in-process IPP mock server for tests. |
|
raster
module
|