Skip to content

Commit 819ae20

Browse files
committed
fix: 通过网页打印,cups后台页码会+1 close #20
1 parent 83d55b3 commit 819ae20

2 files changed

Lines changed: 17 additions & 5 deletions

File tree

cmd/server/print_handlers.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -254,6 +254,7 @@ func printHandler(w http.ResponseWriter, r *http.Request) {
254254
PrintScaling: printScaling,
255255
PageRange: pageRange,
256256
Mirror: mirror,
257+
Pages: pages,
257258
}
258259

259260
job, err := ipp.SendPrintJob(printer, f, mime, sess.Username, fh.Filename, printOpts)

internal/ipp/client.go

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ type PrintJobOptions struct {
2525
PrintScaling string // "auto" | "auto-fit" | "fit" | "fill" | "none"
2626
PageRange string // e.g. "1-5 8 10-12"
2727
Mirror bool // mirror / horizontal flip
28+
Pages int // total document pages (for job-impressions hint)
2829
}
2930

3031
// SendPrintJob sends data to the printer via IPP using goipp to build the
@@ -71,12 +72,11 @@ func SendPrintJob(printerURI string, r io.Reader, mime string, username string,
7172
}
7273
req.Job.Add(goipp.MakeAttribute("copies", goipp.TagInteger, goipp.Integer(copies)))
7374

74-
// Orientation
75-
switch opts.Orientation {
76-
case "landscape":
75+
// Orientation – only override when landscape; portrait is the CUPS default
76+
// and explicitly sending orientation-requested=3 may cause pdftopdf to
77+
// re-process the document, leading to an inflated page count in CUPS.
78+
if opts.Orientation == "landscape" {
7779
req.Job.Add(goipp.MakeAttribute("orientation-requested", goipp.TagEnum, goipp.Integer(4)))
78-
default:
79-
req.Job.Add(goipp.MakeAttribute("orientation-requested", goipp.TagEnum, goipp.Integer(3)))
8080
}
8181

8282
// Paper size (media)
@@ -117,6 +117,17 @@ func SendPrintJob(printerURI string, r io.Reader, mime string, username string,
117117
req.Job.Add(goipp.MakeAttribute("mirror", goipp.TagBoolean, goipp.Boolean(true)))
118118
}
119119

120+
// Job impressions hint – tells CUPS the expected page count so that its
121+
// job-accounting display matches the actual document instead of relying
122+
// on the filter-reported count (which may be off by one).
123+
if opts.Pages > 0 {
124+
impressions := opts.Pages
125+
if copies > 1 {
126+
impressions = opts.Pages * copies
127+
}
128+
req.Job.Add(goipp.MakeAttribute("job-impressions", goipp.TagInteger, goipp.Integer(impressions)))
129+
}
130+
120131
payload, err := req.EncodeBytes()
121132
if err != nil {
122133
return "", fmt.Errorf("encode ipp request: %w", err)

0 commit comments

Comments
 (0)