Files
escwebprint/escwebprint.py
2026-03-27 11:25:00 +01:00

44 lines
1.5 KiB
Python
Executable File

#!/bin/env python3
import argparse
from escpos import printer
from playwright import sync_api as playwright
from PIL import Image
parser = argparse.ArgumentParser(
prog='escwebprint',
description='Print a webpage on a thermal printer')
parser.add_argument("url", help="URL to print")
parser.add_argument("printer", help="DEV file of the printer")
parser.add_argument("--profile", default=None, help="Printer profile (See https://python-escpos.readthedocs.io/en/latest/printer_profiles/available-profiles.html)")
parser.add_argument("--cut", action=argparse.BooleanOptionalAction, help="Cut after printing")
args = parser.parse_args()
printer = printer.File(args.printer, profile=args.profile)
screenshot_width=printer.profile.profile_data["media"]["width"]["pixels"]
if screenshot_width == "Unknown":
screenshot_width = 300
screenshot=None
screenshot_file="screenshot.png"
print("Rendering webpage from {} of {} pixels wide".format(args.url, screenshot_width))
with playwright.sync_playwright() as p:
browser = p.firefox.launch()
page = browser.new_page(
viewport={"width": screenshot_width, "height": screenshot_width}
)
page.emulate_media(media="print")
page.goto(args.url)
screenshot = page.screenshot(full_page=True, path=screenshot_file)
browser.close()
if screenshot is not None:
print("Printing screenshot")
screenshot_img = Image.open(screenshot_file)
printer.image(screenshot_img, center=True)
if arg.cut or arg.cut is None:
printer.cut()