Help us shape the future of Go! Take the 2026 Go Developer Survey

printquote

package module
v0.1.5 Latest Latest
Warning

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

Go to latest
Published: Mar 6, 2026 License: MIT Imports: 6 Imported by: 0

README

printquote

printquote is a Go library designed to automate printing cost or price estimation. It analyzes documents page-by-page to detect dimensions (A4, Letter, Legal, etc.) and color coverage (Grayscale vs. Spot Color vs. Full Color).

Notes

This package works with PDF and it's not well tested with other formats.

Installation

This package requires MuPDF development headers installed on your system.

On debian/ubuntu:

sudo apt install libmupdf-dev

Then install the package:

go get github.com/lucasgaraglia/printquote

Usage

Single-threaded alternative:

package main

import (
	"fmt"
	"log"
	"os"

	"github.com/lucasgaraglia/printquote"
)

func main() {
	f, err := os.Open("test_files/Pragmatic_systemd.pdf")
	if err != nil {
		log.Fatalf("failed to open PDF: %v", err)
	}
	defer f.Close()

	quoter := printquote.NewQuoter(printquote.QuoteConfig{
		PricesPerPage: map[string]printquote.SizePricesByColor{
			"A4":     {BW: 0.05, Color: 0.15, FullColor: 0.30, PaperSheet: 0.05},
			"Letter": {BW: 0.05, Color: 0.15, FullColor: 0.30, PaperSheet: 0.05},
		},
		FullColorThreshold: 0.25,
		IsDoubleSided:      true,
	})

	result, err := quoter.Estimate(f)
	if err != nil {
		log.Fatalf("estimation failed: %v", err)
	}

	sideStr := "Single Sided"
	if result.IsDoubleSided {
		sideStr = "Double Sided"
	}

	fmt.Printf("=== Print Quote Estimate (%s) ===\n", sideStr)
	fmt.Println()
	fmt.Printf("%-12s  %5s  %7s  %10s\n", "Page Size", "BW", "Color", "Full Color")
	fmt.Println("-------------------------------------------")
	for size, qty := range result.Pages {
		fmt.Printf("%-12s  %5d  %7d  %10d\n", size, qty.BW, qty.Color, qty.FullColor)
	}
	fmt.Println("-------------------------------------------")
	fmt.Printf("Total Price: $%.2f\n", result.TotalPrice)
}

Multi-threaded alternative:

package main

import (
	"fmt"
	"log"
	"os"

	"github.com/lucasgaraglia/printquote"
)

func main() {
	f, err := os.Open("test_files/Pragmatic_systemd.pdf")
	if err != nil {
		log.Fatalf("failed to open PDF: %v", err)
	}
	defer f.Close()

	quoter := printquote.NewQuoter(printquote.QuoteConfig{
		PricesPerPage: map[string]printquote.SizePricesByColor{
			"A4":     {BW: 0.05, Color: 0.15, FullColor: 0.30, PaperSheet: 0.05},
			"Letter": {BW: 0.05, Color: 0.15, FullColor: 0.30, PaperSheet: 0.05},
		},
		FullColorThreshold: 0.25,
		IsDoubleSided:      true,
	})

	result, err := quoter.EstimateWithWorkers(f, 10)
	if err != nil {
		log.Fatalf("estimation failed: %v", err)
	}

	sideStr := "Single Sided"
	if result.IsDoubleSided {
		sideStr = "Double Sided"
	}

	fmt.Printf("=== Print Quote Estimate (%s) ===\n", sideStr)
	fmt.Println()
	fmt.Printf("%-12s  %5s  %7s  %10s\n", "Page Size", "BW", "Color", "Full Color")
	fmt.Println("-------------------------------------------")
	for size, qty := range result.Pages {
		fmt.Printf("%-12s  %5d  %7d  %10d\n", size, qty.BW, qty.Color, qty.FullColor)
	}
	fmt.Println("-------------------------------------------")
	fmt.Printf("Total Price: $%.2f\n", result.TotalPrice)
}

Both examples should print something like:

=== Print Quote Estimate (Double Sided) ===

Page Size        BW    Color  Full Color
-------------------------------------------
Letter           46       81           3
-------------------------------------------
Total Price: $18.60

Documentation

Index

Constants

View Source
const (
	PS_TO_MM_CONVERSION_FACTOR = 0.352778
)

Variables

View Source
var (
	// PAGE_SIZE_TOLERANCE_MM is the tolerance for page size matching.
	// Defaults to 6.0 mm.
	PAGE_SIZE_TOLERANCE_MM = 6.0
)

Functions

func AddPaperStandard

func AddPaperStandard(name string, widthMm float64, heightMm float64)

AddPaperStandard adds a new paper size to the list.

func GetPageSizeToleranceMM added in v0.1.3

func GetPageSizeToleranceMM() float64

GetPageSizeToleranceMM returns the current tolerance for page size matching.

func RemovePaperStandard

func RemovePaperStandard(name string)

RemovePaperStandard removes a paper size from the list.

func SetPageSizeToleranceMM added in v0.1.3

func SetPageSizeToleranceMM(tolerance float64)

SetPageSizeToleranceMM sets the tolerance for page size matching. Defaults to 6.0 mm.

Types

type QuoteConfig

type QuoteConfig struct {
	PricesPerPage      map[string]SizePricesByColor // Key is "A4", "Legal", etc. Make sure to add a "Fallback" key for unknown sizes; otherwise, unknown sizes will be unsupported and will return an error
	ColorThreshold     float64                      // Minimum percentage of color pixels to consider the page "Color" (not B&W) (e.g., 0.05 for 5%)
	FullColorThreshold float64                      // Percentage of color pixels to consider the page "Full color" (e.g., 0.25 for 25%)
	FullBlackThreshold float64                      // Minimum percentage of non-white pixels to classify a B&W page as "Full black" (high ink coverage) (e.g., 0.3 for 30%)
	IsDoubleSided      bool                         // When true, two printed sides share one physical sheet
	BWQuote            bool                         // When true, the entire document is treated as black-and-white: pages that would be classified as "Color" become "BW", and pages that would be "FullColor" become "FullBlack"
}

QuoteConfig holds the price per page type

type QuoteResult

type QuoteResult struct {
	Pages         map[string]SizeQuantitiesByColor // Key is "A4", "Legal", etc.
	IsDoubleSided bool
	TotalPrice    float64
}

QuoteResult holds the result of the quote

type Quoter

type Quoter struct {
	Config QuoteConfig
}

Quoter holds the configuration and provides methods to calculate the quote

func NewQuoter

func NewQuoter(config QuoteConfig) *Quoter

NewQuoter creates a new Quoter with the given configuration

func (*Quoter) Estimate

func (q *Quoter) Estimate(r io.ReadSeeker) (QuoteResult, error)

Estimate receives any Go file-like object or bytes and returns the quote. It is equivalent to EstimateWithWorkers(r, 1). The BWQuote flag is read from the Quoter's Config.

func (*Quoter) EstimateWithWorkers added in v0.1.1

func (q *Quoter) EstimateWithWorkers(r io.ReadSeeker, workers int) (QuoteResult, error)

EstimateWithWorkers is like Estimate but renders pages concurrently using the given number of workers. Use workers > 1 to speed up large documents.

func (*Quoter) IdentifyPageSize

func (q *Quoter) IdentifyPageSize(wPts float64, hPts float64) StandardSize

IdentifyPageSize receives the width and height in points and returns the closest standard size. If no standard size is found, returns a fallback size.

type SizePricesByColor added in v0.1.1

type SizePricesByColor struct {
	PaperSheet float64
	BW         float64
	FullBlack  float64
	Color      float64
	FullColor  float64
}

SizePricesByColor holds the price per color category. PaperSheet is the cost of one physical sheet. BW, FullBlack, Color and FullColor are the costs per printed side.

type SizeQuantitiesByColor added in v0.1.1

type SizeQuantitiesByColor struct {
	BW        int
	FullBlack int
	Color     int
	FullColor int
}

SizeQuantitiesByColor holds the quantity per color category

type StandardSize

type StandardSize struct {
	Name     string
	WidthMm  float64
	HeightMm float64
}

StandardSize represents a standard paper size.

func GetPaperStandard

func GetPaperStandard(name string) StandardSize

GetPaperStandard returns the paper size with the given name.

func GetPaperStandards

func GetPaperStandards() []StandardSize

GetPaperStandards returns the list of standard paper sizes.

Jump to

Keyboard shortcuts

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