Created
November 2, 2022 10:53
-
-
Save iydon/bcd48df38837d777f1b4213e132b1a9a to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
''' | |
Reference: | |
- https://www.cups.org/doc/options.html#OPTIONS | |
''' | |
import hashlib | |
import pathlib as p | |
import subprocess as sp | |
import streamlit as st | |
directory = p.Path('cache') | |
meta = directory / 'meta.tsv' | |
printer = 'MFC8510DN' | |
with st.form('print'): | |
uploaded_file = st.file_uploader('Choose a file') | |
media = st.multiselect('Media', ['A4', 'Letter', 'Legal', 'COM10', 'DL', 'Transparency', 'Upper', 'Lower', 'MultiPurpose', 'LargeCapacity'], ['A4']) | |
number = st.number_input('Number', min_value=1) | |
landscape = st.checkbox('Landscape', False) | |
fit_to_page = st.checkbox('Fit to Page', False) | |
sides = st.selectbox('Sides', ['one-sided', 'two-sided-short-edge', 'two-sided-long-edge']) | |
page_ranges = st.text_input('Page Ranges (1-4,7,9-12)', '') | |
submitted = st.form_submit_button('Print') | |
if submitted and uploaded_file is not None: | |
bytes_data = uploaded_file.getvalue() | |
md5 = hashlib.md5(bytes_data).hexdigest() | |
suffix = p.Path(uploaded_file.name).suffix | |
path = (directory / f'{md5}{suffix}').absolute() | |
path.write_bytes(bytes_data) | |
with open(meta, 'a+') as f: | |
f.write(f'{path.name}\t{uploaded_file.name}\t{uploaded_file.type}\n') | |
# command | |
args = [ | |
'lpr', | |
'-P', printer, | |
'-o', f'media={",".join(media)}', | |
'-#', f'{number}', | |
'-o', f'sides={sides}', | |
] | |
if landscape: | |
args += ['-o', 'landscape'] | |
if fit_to_page: | |
args += ['-o', 'fit-to-page'] | |
if page_ranges: | |
args += ['-o', f'page-ranges={page_ranges.replace(" ", "")}'] | |
cp = sp.run(args+[path.as_posix()], capture_output=True) | |
st.write({ | |
'returncode': cp.returncode, | |
'stderr': cp.stderr.decode(), | |
'stdout': cp.stdout.decode(), | |
'args': ' '.join(args), | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment