PrintBase
  • Product
  • Pricing
  • Docs
  • Resources
PrintBase/Guides/Print from your language or framework

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

  1. 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.

  2. 02

    Install the agent beside the printer

    Windows, macOS, or Linux. GET /v1/printers and keep the numeric code.

  3. 03

    POST /v1/print-jobs

    content_type is pdf or raw. Send exactly one of content (standard base64) or content_url.

  4. 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.

  5. 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.

Related guides

  • Print a PDF from Python

    Use the Python standard library to POST a base64 PDF. No extra print package is required. The agent prints it on the computer you chose.

  • Print receipts from Django

    Print from a Django view or signal after the order is saved. Use the server environment for the API key, not settings committed to git.

  • Print PDF invoices from Laravel

    Render a Laravel invoice PDF with Dompdf or Snappy, then POST the bytes. The office printer is selected by printer_code.

  • Print a PDF from Node.js to a printer in another place

    From Node.js, base64 a PDF and POST it to the print-jobs API. The printer is attached to a computer running the agent, not to your server.

Free plan: 100 jobs a month on one computer, no card.

Create a free accountRead the API docs
PrintBase

PrintBase connects your applications to physical printers anywhere - securely, reliably, and in real time.

Product
Receipt PrintingLabel PrintingDashboardDevicesWebhooks
Resources
DocsBlogStatusAPI ReferenceDownloadPrintBase vs PrintNodePrintNode PricingFAQ
Guides
All guidesIntegrationsPrintersCode samplesComparisonsUse casesTroubleshooting
Company
ContactSecurityPrivacy

© 2026 Aveniq Technologies OÜ. All rights reserved. PrintBase is a service of Aveniq Technologies OÜ.

PrivacyTermsRefunds