Print a PDF from Go
Read a PDF in Go and post it as base64. Use content_url if the object store link is public. No CGO PDF library is required to transport the file.
For Go services that already produce PDFs
Overview
Generating a PDF in Go (or writing one from another service) is separate from printing it. Once you have []byte starting with %PDF-, base64 it and set content_type pdf. Sending those bytes as raw fails validation.
An S3 or similar presigned URL can be content_url if the agent can GET it before it expires. A URL that only works inside your VPC will fail with DOWNLOAD_FAILED.
Check the status code. 201 with a JSON id is success to queue. 4xx is a body you should log, without logging the PDF.
Create the job with POST /v1/print-jobs, an Authorization: Bearer header, printer_code, content_type pdf or raw, and exactly one of content or content_url. The free plan is 100 jobs a month on one computer.
Integration steps
- 01
Create an API key on the server
Use an API key (pb_live_…) in the process environment. Authorization is Bearer. Do not ship the key to browsers or mobile apps.
- 02
Install the agent beside the printer
Windows, macOS, or Linux. GET /v1/printers and keep the numeric code.
- 03
POST /v1/print-jobs
content_type is pdf or raw. Send exactly one of content (standard base64) or content_url.
- 04
Reuse the idempotency key on retry
A new key means a second physical print. HTTP 409 means the same key was reused with a different body.
- 05
Read the job before you tell the user
GET /v1/print-jobs/{id}. Paid plans can also receive print_job.completed and print_job.failed webhooks.
Go
// Go PDF
body, _ := json.Marshal(map[string]any{
"printer_code": printerCode,
"content_type": "pdf",
"content": base64.StdEncoding.EncodeToString(pdfBytes),,
"copies": 1,
})
req, _ := http.NewRequest(http.MethodPost, "https://api.printbase.cloud/v1/print-jobs", bytes.NewReader(body))
req.Header.Set("Authorization", "Bearer "+os.Getenv("PRINTBASE_API_KEY"))
req.Header.Set("Content-Type", "application/json")
req.Header.Set("Idempotency-Key", idempotencyKey)
res, err := http.DefaultClient.Do(req)
if err != nil { log.Fatal(err) }
defer res.Body.Close()
// 201 {"id":"job_abc","status":"queued"}
Details for this setup
Do not log the body
The PDF is customer data. Log the job id and the status code only. The agent prints only what you send; it does not fetch the document from the platform for you.
Presign long enough
The agent downloads after the job is dispatched. A URL that expires in one second is DOWNLOAD_FAILED.
Questions
Which PDF library?
Whichever produces bytes. The print call does not import it.
pdfcpu or jung-kurt?
Fine for generation. Not involved in the HTTP call. The call is still POST /v1/print-jobs with a Bearer API key, and HTTP 201 means queued rather than printed.
Can Go print on Windows without an agent?
Only with Windows APIs on that machine. This page is the remote case.
Copies? in this setup
The copies field. The PDF itself should be one document. The call is still POST /v1/print-jobs with a Bearer API key, and HTTP 201 means queued rather than printed.
Free plan: 100 jobs a month on one computer, no card.