Created script

This commit is contained in:
2026-03-27 11:25:00 +01:00
parent bbcd398de4
commit 7590997255
3 changed files with 73 additions and 1 deletions

View File

@@ -1,3 +1,30 @@
# escwebprint
Print any webpage on a ESCPOS compatible thermal printer
Print any webpage on a ESCPOS compatible thermal printer
## Usage
Create a virtual environment and install requirements.
```
python3 -m venv .venv
source .venv/bin/activate
pip install -r requirements.txt
```
Install headless firefox
```
playwright install firefox
```
Print your page
```
./escwebprint.py https://distorsion.interhacker.space/ /dev/usb/lp0 --profile TM-T20II
```
### Printer profile
In order to correctly configure your printer, you should select its model in supported profiles of python-escpos.
Check the page [python-escpos.readthedocs.io/en/latest/printer_profiles/available-profiles.html](https://python-escpos.readthedocs.io/en/latest/printer_profiles/available-profiles.html) for a list of available profiles.
If not defined, creenshot will be only 300 pixels wide.

43
escwebprint.py Executable file
View File

@@ -0,0 +1,43 @@
#!/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()

2
requirements.txt Normal file
View File

@@ -0,0 +1,2 @@
python-escpos
playwright