This Python code works in Windows but does not work in Raspbian. How do I print PDF files using the Raspberry Pi?
import os
fd = os.startfile("/home/pi/Desktop/a.pdf", "print")
This Python code works in Windows but does not work in Raspbian. How do I print PDF files using the Raspberry Pi?
import os
fd = os.startfile("/home/pi/Desktop/a.pdf", "print")
You could use CUPS
import cups
conn = cups.Connection()
printers = conn.getPrinters()
printer_name = printers.keys()[0]
conn.printFile(printer_name,'/home/pi/Desktop/a.pdf',"",{})
os.startfile()'s "print" option is not available in Raspbian (the OS of the Raspberry Pi). Make sure that your printer is connected to the Pi, and use Popen and lpr to communicate with the printer daemon.
You could use the below code(NOTE: code starts from import cups)
import cups
def print_file(file_path, printer_name):
conn = cups. Connection()
printers = conn.getPrinters()
if printer_name in printers:
try:
conn.printFile(printer_name, file_path, "Print Job", {})
print("File sent to printer successfully.")
except cups.IPPError as e:
print(f"Error printing file: {e}")
else:
print(f"Printer '{printer_name}' not found.")
Example usage
file_to_print = "/path/to/your/file.pdf"
printer_name = "Your_Printer_Name"
print_file(file_to_print, printer_name)