printquote

package module
v0.1.1 Latest Latest
Warning

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

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

README

printquote

printquote is a Go library designed to automate printing cost 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

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.SizePrices{
			"A4":       {BW: 0.05, Color: 0.15, FullColor: 0.30},
			"A3":       {BW: 0.10, Color: 0.25, FullColor: 0.50},
			"Letter":   {BW: 0.05, Color: 0.15, FullColor: 0.30},
			"Legal":    {BW: 0.07, Color: 0.18, FullColor: 0.35},
			"Fallback": {BW: 0.08, Color: 0.20, FullColor: 0.40},
		},
		FullColorThreshold: 0.25,
	})

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

	fmt.Println("=== Print Quote Estimate ===")
	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)
}

And that should print something like:

=== Print Quote Estimate ===

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

Documentation

Index

Constants

View Source
const (
	PAGE_SIZE_TOLERANCE_MM     = 6
	PS_TO_MM_CONVERSION_FACTOR = 0.352778
)

Variables

This section is empty.

Functions

func AddPaperStandard

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

AddPaperStandard adds a new paper size to the list.

func RemovePaperStandard

func RemovePaperStandard(name string)

RemovePaperStandard removes a paper size from the list.

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
	FullColorThreshold float64                      // Percentage of color pixels to consider the page "Full color" (e.g., 0.25 for 25%)
}

QuoteConfig holds the price per page type

type QuoteResult

type QuoteResult struct {
	Pages      map[string]SizeQuantitiesByColor // Key is "A4", "Legal", etc.
	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).

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 {
	BW        float64
	Color     float64
	FullColor float64
}

SizePricesByColor holds the price per color category

type SizeQuantitiesByColor added in v0.1.1

type SizeQuantitiesByColor struct {
	BW        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