Files
zineshop/models/printer.go
2025-04-11 13:12:39 +02:00

89 lines
1.9 KiB
Go

package models
import (
"fmt"
"os/exec"
"strings"
)
type PrintOption string
const (
CoverPage PrintOption = "-o FrontCoverPage=Printed -o FrontCoverTray=BypassTray"
Colored PrintOption = "-o SelectColor=Color"
Grayscale PrintOption = "-o SelectColor=Grayscale"
LongEdge PrintOption = ""
ShortEdge PrintOption = "-o Binding=TopBinding"
CreateBooklet PrintOption = "-o Combination=Booklet -o PageSize=A5"
)
type PrintJob struct {
Pdf string
Amount uint
Options []PrintOption
}
func GetPrintMode(mode string) PrintOption {
if mode == "LongEdge" {
return LongEdge
}
if mode == "ShortEdge" {
return ShortEdge
}
return CreateBooklet
}
func NewPrintJob(shopItem ShopItem, variant ItemVariant, coverPage bool, amount uint) (PrintJob, error) {
if shopItem.Pdf == "" {
return PrintJob{}, fmt.Errorf("ShopItem has no PDF assigned")
}
if amount > 100 {
return PrintJob{}, fmt.Errorf("Amount to big. This is denied for security reasons")
}
var result PrintJob
result.Pdf = shopItem.Pdf
result.Amount = amount
if variant.Name == "Colored" {
result.Options = append(result.Options, Colored)
}
if coverPage {
result.Options = append(result.Options, CoverPage)
}
result.Options = append(result.Options, GetPrintMode(shopItem.PrintMode))
return result, nil
}
func (p *PrintJob) Execute() error {
baseCommand := "lp -d KONICA_MINOLTA_KONICA_MINOLTA_bizhub_C258/BookletPrint -o Fold=HalfFold"
baseCommand += fmt.Sprintf(" -n %v ", p.Amount)
for _, option := range p.Options {
baseCommand += fmt.Sprintf(" %v ", option)
}
baseCommand += fmt.Sprintf(" -- %s", p.Pdf)
parts := strings.Fields(baseCommand)
// The first part is the command, the rest are the arguments
fmt.Println(parts)
cmd := exec.Command(parts[0], parts[1:]...)
output, err := cmd.CombinedOutput()
if err != nil {
fmt.Printf("Error: %s\n", err)
return err
}
fmt.Printf("Output:\n%s\n", output)
return nil
}