1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111
|
# This file is part of the Frescobaldi project, http://www.frescobaldi.org/
#
# Copyright (c) 2008 - 2014 by Wilbert Berendsen
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
# See http://www.gnu.org/licenses/ for more information.
"""
Constructs a printcommand to print a PDF file.
"""
import itertools
import os
from PyQt5.QtPrintSupport import QPrinter
lpr_commands = (
"lpr-cups",
"lpr.cups",
"lpr",
"lp",
)
def lprCommand():
"""Returns a suitable 'lpr'-like command to send a file to the printer queue.
Returns None if no such command could be found.
Prefers the CUPS command 'lpr' or 'lp' if it can be found.
"""
paths = os.environ.get("PATH", os.defpath).split(os.pathsep)
for cmd, path in itertools.product(lpr_commands, paths):
if os.access(os.path.join(path, cmd), os.X_OK):
return cmd
def printCommand(cmd, printer, filename):
"""Returns a commandline (list) to print a PDF file.
cmd: "lpr" or "lp" (or something like that)
printer: a QPrinter instance
filename: the filename of the PDF document to print.
"""
command = [cmd]
# printer name
if cmd == "lp":
command.append('-d')
else:
command.append('-P')
command.append(printer.printerName())
# copies
numCopies = 1
try:
numCopies = printer.copyCount()
except AttributeError: # only in Qt >= 4.7
try:
numCopies = printer.actualNumCopies()
except AttributeError: # only in Qt >= 4.6
numCopies = printer.numCopies()
if cmd == "lp":
command.append('-n')
command.append(format(numCopies))
else:
command.append('-#{0}'.format(numCopies))
# job name
if cmd == "lp":
command.append('-t')
else:
command.append('-J')
command.append(printer.docName() or os.path.basename(filename))
# page range
if printer.printRange() == QPrinter.PageRange:
pageRange = "{0}-{1}".format(printer.fromPage(), printer.toPage())
if cmd == "lp":
command.append('-P')
command.append(pageRange)
else:
command.append('-o')
command.append('page-ranges=' + pageRange)
# duplex mode
if printer.duplex() == QPrinter.DuplexLongSide:
command.extend(['-o', 'sides=two-sided-long-edge'])
elif printer.duplex() == QPrinter.DuplexShortSide:
command.extend(['-o', 'sides=two-sided-short-edge'])
command.append(filename)
return command
|