Print a PDF from Flask
Flask can post a PDF with urllib in the request that generates it. Keep the API key in the environment. There is no Flask extension to install.
For Flask apps generating PDFs
Overview
Flask does not have a printer object. After send_file would have downloaded the PDF to a human, you can instead POST those same bytes to PrintBase and return the job id. Offer a download only if someone still wants the file.
Use the app context's configuration for the key, loaded from the environment. The debug reloader must not print on every reload.
A form posted twice by the browser needs the idempotency key on the server, derived from the document id, not from a new uuid per request.
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.
Python
# Flask view: PDF
import json, os, urllib.request
body = json.dumps({
"printer_code": int(os.environ["PRINTER_CODE"]),
"content_type": "pdf",
"content": base64.b64encode(pdf).decode(),,
"copies": 1,
}).encode()
req = urllib.request.Request(
"https://api.printbase.cloud/v1/print-jobs",
data=body,
method="POST",
headers={
"Authorization": f"Bearer {os.environ['PRINTBASE_API_KEY']}",
"Content-Type": "application/json",
"Idempotency-Key": document_id,
},
)
with urllib.request.urlopen(req) as res:
job = json.load(res) # {"id": "job_abc", "status": "queued"}
Details for this setup
Do not print in a before_request hook
That runs for static files too. The agent prints only what you send; it does not fetch the document from the platform for you.
Same bytes as the download
If you already build a PDF response, base64 that buffer. Do not ask the agent to fetch a Flask route that requires a session cookie.
Questions
Is there a Flask-Print extension from PrintBase?
No. The call is still POST /v1/print-jobs with a Bearer API key, and HTTP 201 means queued rather than printed.
gunicorn workers?
Any worker can POST. Share the idempotency key via the document id.
Can Flask return the PDF and print it?
Yes. They are two uses of the same bytes. The call is still POST /v1/print-jobs with a Bearer API key, and HTTP 201 means queued rather than printed.
Local development?
Point printer_code at a dev queue. Do not hard-code production codes in the Flask config you commit.
Free plan: 100 jobs a month on one computer, no card.