Skip to content
Snippets Groups Projects
Commit d3756116 authored by Radek Hušek's avatar Radek Hušek
Browse files

init

parents
No related branches found
No related tags found
No related merge requests found
LICENSE 0 → 100644
Copyright (c) 2017 Radek Hušek
Permission is hereby granted, free of charge, to any person
obtaining a copy of this software and associated documentation
files (the "Software"), to deal in the Software without
restriction, including without limitation the rights to use,
copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the
Software is furnished to do so, subject to the following
conditions:
The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
OTHER DEALINGS IN THE SOFTWARE.
[Desktop Entry]
Encoding=UTF-8
Type=Service
ServiceTypes=KonqPopupMenu/Plugin
MimeType=application/pdf;
Actions=PrintPdf;
[Desktop Action PrintPdf]
Name=Print PDF
Exec=cups-pdf-postprocess.sh %F ""
Icon=printer
#!/usr/bin/python3
# -*- coding: utf-8 -*-
"""
XXX
Author: Radek Hušek
"""
from PyQt5.QtWidgets import *
from PyQt5.QtCore import *
import sys
import subprocess as sp
import ast
def exec_pipeline(pipeline, inp, out = None, err_callback = None):
def make_proc(cmd):
p = QProcess()
p.setProgram(cmd[0])
p.setArguments(cmd[1:])
return p
def readChunk(cb, proc, channel):
def x():
proc.setReadChannel(channel)
cb(str(proc.readAll(), encoding='utf-8'))
return x
procs = [ make_proc(cmd) for cmd in pipeline ]
for a, b in zip(procs, procs[1:]):
a.setStandardOutputProcess(b)
procs[0].setStandardInputFile(inp)
if out is None:
if err_callback is not None:
procs[-1].readyReadStandardOutput.connect(
readChunk(err_callback, procs[-1], QProcess.StandardOutput))
else:
procs[-1].setStandardOutputFile(out)
if err_callback is not None:
for p in procs:
p.readyReadStandardError.connect(
readChunk(err_callback, p, QProcess.StandardError))
err_callback("< '%s' \\\n" % inp)
err_callback(" |\n".join(("'%s'" % "' '".join(cmd)) for cmd in pipeline))
err_callback("\n")
for p in procs:
p.start(QProcess.Unbuffered | QProcess.ReadWrite)
for p in procs:
while not p.waitForFinished(30):
QApplication.instance().processEvents()
SAVE_AS_FILE = object()
PRINTERS = ast.literal_eval("[\n%s\n]" % "".join(sys.stdin))
PRINTERS.append(("Save as ...", SAVE_AS_FILE))
for l in sp.check_output(["lpstat", "-a"]).decode("UTF-8").split("\n"):
p = l.split(" ")[0]
if p == "":
continue
PRINTERS.append((p, ["lp", "-d", p]))
class CPP(QWidget):
def __init__(self, pdf, user):
super().__init__()
self._pdf = pdf
vbox = QVBoxLayout()
vbox.addStretch(1)
o = QLabel("Printing %s" % (pdf,), self)
o.setMinimumWidth(400)
o.setMaximumWidth(800)
o.setWordWrap(True)
vbox.addWidget(o)
hbox = QHBoxLayout()
hbox.addWidget(QLabel("Duplex:"))
self._duplex = QComboBox(self)
self._duplex.addItem("None", 'duplex=one-sided')
self._duplex.addItem("Long Edge", "duplex=two-sided-long-edge")
self._duplex.addItem("Short Side", "duplex=two-sided-long-edge")
self._duplex.setCurrentIndex(1)
self._pdfbook = QCheckBox('Use pdfbook', self)
self._stample = QCheckBox('Stample', self)
self._stample.setEnabled(False)
def _pdfbook_changed(enabled):
self._stample.setEnabled(enabled)
self._duplex.setEnabled(not enabled)
if enabled:
self._duplex.setCurrentIndex(1)
else:
self._stample.setChecked(False)
self._pdfbook.clicked[bool].connect(_pdfbook_changed)
hbox.addWidget(self._duplex)
vbox.addLayout(hbox)
vbox.addWidget(self._pdfbook)
vbox.addWidget(self._stample)
o = QComboBox(self)
for (p, cmd) in PRINTERS:
o.addItem(p, cmd)
vbox.addWidget(o)
self._printer = o
hbox = QHBoxLayout()
b_p = QPushButton("Print", self)
b_p.clicked.connect(self.print)
hbox.addWidget(b_p)
self._button_print = b_p
b_c = QPushButton("Cancel", self)
b_c.clicked.connect(self.close)
hbox.addWidget(b_c)
self._button_cancel = b_c
vbox.addLayout(hbox)
self._logger = QTextEdit()
self._logger.setReadOnly(True)
vbox.addWidget(self._logger)
self.setLayout(vbox)
self.setWindowTitle('CUPS pdf printer postprocessor')
self.show()
def print(self):
lp = QProcess()
self._button_print.setEnabled(False)
log = []
pipeline = []
def logMsg(msg):
log.append(msg)
self._logger.setPlainText("".join(log))
if self._pdfbook.isChecked():
pipeline.append([ "pdfbook", "/dev/stdin", "-o", "/dev/stdout" ])
if self._printer.currentData() is SAVE_AS_FILE:
f = QFileDialog.getSaveFileName(self, 'Save as ...')
if not f[0]:
self.close()
pipeline.append([ "dd", "of=%s" % (f[0],) ])
else:
cmd = self._printer.currentData().copy()
cmd += [ "-o", self._duplex.currentData() ]
if self._stample.isChecked:
cmd += [ "-o", "XRFold=BiFoldStaple" ]
cmd.append("-")
pipeline.append(cmd)
exec_pipeline(pipeline, self._pdf, err_callback=logMsg)
self._button_cancel.setText("Close")
if __name__ == '__main__':
app = QApplication(sys.argv[2:])
ex = CPP(sys.argv[1], sys.argv[2])
sys.exit(app.exec_())
#!/bin/bash
export \
DISPLAY=:0 \
XAUTHORITY=/tmp/kde-radek/xauth-1000-_0 \
HOME=/home/radek \
KDE_SESSION_VERSION=4 \
XDG_CURRENT_DESKTOP=KDE
exec "$(dirname "`readlink -f "$0"`")/cups-pdf-postprocess.py" "$@" <<'EOF'
("Nessie gray", ["ssh", "iuuk", "lp", "-d", "nessie-gray"]),
("Nessie color", ["ssh", "iuuk", "lp", "-d", "nessie-color"]),
EOF
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment