import os
import requests
import win32print
import win32api
from PyPDF2 import PdfReader
def download_pdf(url, save_path):
"""下载PDF文件到本地"""
response = requests.get(url)
with open(save_path, 'wb') as f:
f.write(response.content)
def list_printers():
"""获取系统所有打印机"""
return win32print.EnumPrinters(win32print.PRINTER_ENUM_LOCAL)
def print_pdf(file_path, printer_name, copies=1, landscape=False):
"""打印PDF文件"""
if not os.path.exists(file_path):
raise FileNotFoundError(f"文件 {file_path} 不存在")
# 设置打印方向
orientation = "LANDSCAPE" if landscape else "PORTRAIT"
win32api.ShellExecute(
0,
"print",
file_path,
f'/d:"{printer_name}" /orientation:{orientation} /copies:{copies}',
".",
0
)
if __name__ == "__main__":
# 示例使用
pdf_url = "https://example.com/sample.pdf"
save_path = "sample.pdf"
# 1. 下载PDF
download_pdf(pdf_url, save_path)
# 2. 列出所有打印机
printers = list_printers()
print("可用打印机:")
for i, printer in enumerate(printers):
print(f"{i+1}. {printer[2]}")
# 3. 选择打印机和打印参数
printer_index = int(input("选择打印机编号: ")) - 1
copies = int(input("打印份数: "))
landscape = input("横向打印? (y/n): ").lower() == 'y'
# 4. 执行打印
print_pdf(save_path, printers[printer_index][2], copies, landscape)
print("打印任务已发送")站长微信:xiaomao0055
站长QQ:14496453