Merge pull request #4 from willglynn/print_job

Add IPP `PrintJob()`
This commit is contained in:
Fabian 2019-06-04 23:02:23 +02:00 committed by GitHub
commit e221cdb59e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 73 additions and 20 deletions

View File

@ -308,37 +308,42 @@ var (
AttributeTagMapping = map[string]Tag{
"attributes-charset": TagCharset,
"attributes-natural-language": TagLanguage,
"printer-uri": TagUri,
"requesting-user-name": TagName,
"job-id": TagInteger,
"document-name": TagName,
"job-name": TagName,
"document-format": TagMimeType,
"last-document": TagBoolean,
"copies": TagInteger,
"device-uri": TagUri,
"document-format": TagMimeType,
"document-name": TagName,
"document-number": TagInteger,
"document-state": TagEnum,
"finishings": TagEnum,
"hold-job-until": TagKeyword,
"job-hold-until": TagKeyword,
"job-id": TagInteger,
"job-name": TagName,
"job-printer-uri": TagUri,
"job-priority": TagInteger,
"number-up": TagInteger,
"job-sheets": TagName,
"job-uri": TagUri,
"job-state": TagEnum,
"job-state-reason": TagKeyword,
"requested-attributes": TagKeyword,
"job-uri": TagUri,
"last-document": TagBoolean,
"media": TagKeyword,
"member-uris": TagUri,
"my-jobs": TagBoolean,
"number-up": TagInteger,
"orientation-requested": TagEnum,
"ppd-name": TagName,
"printer-state-reason": TagKeyword,
"printer-is-shared": TagBoolean,
"print-quality": TagEnum,
"printer-error-policy": TagName,
"printer-info": TagText,
"which-jobs": TagKeyword,
"my-jobs": TagBoolean,
"purge-jobs": TagBoolean,
"hold-job-until": TagKeyword,
"job-printer-uri": TagUri,
"printer-is-shared": TagBoolean,
"printer-location": TagText,
"document-number": TagInteger,
"printer-resolution": TagResolution,
"printer-state": TagEnum,
"document-state": TagEnum,
"device-uri": TagUri,
"printer-state-reason": TagKeyword,
"printer-uri": TagUri,
"purge-jobs": TagBoolean,
"requested-attributes": TagKeyword,
"requesting-user-name": TagName,
"which-jobs": TagKeyword,
}
)

View File

@ -125,6 +125,7 @@ func (c *IPPClient) SendRequest(url string, req *Request) (*Response, error) {
//return NewResponseDecoder(httpResp.Body).Decode()
}
// Print one or more `Document`s using IPP `Create-Job` followed by `Send-Document` request(s).
func (c *IPPClient) Print(docs []Document, printer, jobName string, copies, priority int) (int, error) {
printerURI := c.getPrinterUri(printer)
@ -168,6 +169,53 @@ func (c *IPPClient) Print(docs []Document, printer, jobName string, copies, prio
return jobID, nil
}
// Print a `Document` using an IPP `Print-Job` request.
//
// `jobAttributes` can contain arbitrary key/value pairs to control the way in which the
// document is printed. [RFC 2911 § 4.2](https://tools.ietf.org/html/rfc2911#section-4.2)
// defines some useful attributes:
//
// * [`job-priority`](https://tools.ietf.org/html/rfc2911#section-4.2.1): an integer between 1-100
// * [`copies`](https://tools.ietf.org/html/rfc2911#section-4.2.5): a positive integer
// * [`finishings`](https://tools.ietf.org/html/rfc2911#section-4.2.6): an enumeration
// * [`number-up`](https://tools.ietf.org/html/rfc2911#section-4.2.9): a positive integer
// * [`orientation-requested`](https://tools.ietf.org/html/rfc2911#section-4.2.10): an enumeration
// * [`media`](https://tools.ietf.org/html/rfc2911#section-4.2.11): a string
// * [`printer-resolution`](https://tools.ietf.org/html/rfc2911#section-4.2.12): a `Resolution`
// * [`print-quality`](https://tools.ietf.org/html/rfc2911#section-4.2.13): an enumeration
//
// Your print system may provide other attributes. Define custom attributes as needed in
// `AttributeTagMapping` and provide values here.
func (c *IPPClient) PrintJob(doc Document, printer string, jobAttributes map[string]interface{}) (int, error) {
printerURI := c.getPrinterUri(printer)
req := NewRequest(OperationPrintJob, 1)
req.OperationAttributes["printer-uri"] = printerURI
req.OperationAttributes["requesting-user-name"] = c.username
req.OperationAttributes["job-name"] = doc.Name
req.OperationAttributes["document-format"] = doc.MimeType
for key, value := range jobAttributes {
req.JobAttributes[key] = value
}
req.File = doc.Document
req.FileSize = doc.Size
resp, err := c.SendRequest(c.getHttpUri("printers", printer), req)
if err != nil {
return -1, err
}
if len(resp.Jobs) == 0 {
return 0, errors.New("server doesn't returned a job id")
}
jobID := resp.Jobs[0]["job-id"][0].Value.(int)
return jobID, nil
}
func (c *IPPClient) PrintFile(filePath, printer string, copies, priority int) (int, error) {
fileStats, err := os.Stat(filePath)
if os.IsNotExist(err) {