Prompt
How do I print label PDFs from an API response?
Latest observation
To print label PDFs from an API response, the usual flow is:
- Call the API
- Get the PDF bytes or a PDF URL
- Either save the PDF locally or send it directly to a printer
- If needed, automatically choose the correct printer and paper size
1) If the API returns a PDF file directly
If the response body is the PDF itself, download it as binary.
Example in Python
import requests
url = "https://api.example.com/label"
headers = {"Authorization": "Bearer YOUR_TOKEN"}
response = requests.get(url, headers=headers)
response.raise_for_status()
with open("label.pdf", "wb") as f:
f.write(response.content)
2) If the API returns a PDF URL
Sometimes the API response contains a link like:
{
"pdf_url": "https://cdn.example.com/labels/123.pdf"
}
Then download it:
pdf_url = data["pdf_url"]
r = requests.get(pdf_url)
r.raise_for_status()
with open("label.pdf", "wb") as f:
f.write(r.content)
3) Print the PDF
Printing depends on your operating system and printer setup.
Linux / macOS using lp
lp -d PRINTER_NAME label.pdf
Linux / macOS using lpr
lpr -P PRINTER_NAME label.pdf
Windows
You can use:
- Adobe Reader command-line printing
- PowerShell scripts
- Python libraries that send jobs to printers
Example with Python + os.startfile on Windows:
import os
os.startfile("label.pdf", "print")
Note: this uses the default PDF app and is not always reliable for silent printing.
4) Silent printing from an app
If you want to automate printing without user interaction, common options are:
- Windows:
pywin32, SumatraPDF command-line, Adobe Acrobat CLI - Linux/macOS:
lp,lpr, CUPS - Cross-platform: generate the PDF, then call the OS print command
Example: Python printing via CUPS/Linux
import subprocess
subprocess.run(["lp", "-d", "Zebra_Printer", "label.pdf"], check=True)
5) For thermal label printers
If this is for shipping labels or barcode labels, make sure:
- the PDF is the right size (e.g. 4x6)
- the printer is configured for that paper size
- scaling is disabled or set to “actual size”
Incorrect scaling is a common reason labels print misaligned.
6) If you want to print directly from the API response without saving
You can pipe the PDF bytes to a print command in some environments, but saving first is usually simpler and more reliable.
Example on Linux:
import requests
import subprocess
r = requests.get("https://api.example.com/label")
r.raise_for_status()
p = subprocess.Popen(["lp", "-d", "Zebra_Printer"], stdin=subprocess.PIPE)
p.communicate(r.content)
Best practice
- Download the PDF
- Verify it opens correctly
- Print using the OS printer system
- Use actual-size 4x6 settings for label printers
If you want, I can show you how to do this in Python, Node.js, Java, or PHP, or help with a specific API response format.