def print_(doc, filename=None, widget=None): """Prints the popplerqt4.Poppler.Document. The filename is used in the dialog and print job name. If the filename is not given, it defaults to a translation of "PDF Document". The widget is a widget to use as parent for the print dialog etc. """ cmd = fileprinter.lprCommand() if not cmd and QMessageBox.information(widget, _("Warning"), _( "No print command to print a PostScript file could be found.\n\n" "Therefore the document will be printed using raster images at {resolution} DPI. " "It is recommended to print using a dedicated PDF viewer.\n\n" "Do you want to continue?").format(resolution=300), QMessageBox.Yes | QMessageBox.No) != QMessageBox.Yes: return # cancelled filename = os.path.basename(filename) if filename else _("PDF Document") printer = QPrinter() printer.setDocName(filename) dlg = QPrintDialog(printer, widget) dlg.setMinMax(1, doc.numPages()) dlg.setOption(QPrintDialog.PrintToFile, False) dlg.setWindowTitle(app.caption(_("Print {filename}").format(filename=filename))) result = dlg.exec_() if widget: dlg.deleteLater() # because it has a parent if not result: return # cancelled if cmd: # make a PostScript file with the desired paper size ps = QTemporaryFile() if ps.open() and qpopplerview.printer.psfile(doc, printer, ps): ps.close() # let all converted pages print printer.setPrintRange(QPrinter.AllPages) command = fileprinter.printCommand(cmd, printer, ps.fileName()) if not subprocess.call(command): return # success! QMessageBox.warning(widget, _("Printing Error"), _("Could not send the document to the printer.")) else: # Fall back printing of rendered raster images. # It is unsure if the Poppler ArthurBackend ever will be ready for # good rendering directly to a painter, so we'll fall back to using # 300DPI raster images. p = Printer() p.setDocument(doc) p.setPrinter(printer) p.setResolution(300) d = QProgressDialog() d.setModal(True) d.setMinimumDuration(0) d.setRange(0, len(p.pageList()) + 1) d.canceled.connect(p.abort) def progress(num, total, page): d.setValue(num) d.setLabelText(_("Printing page {page} ({num} of {total})...").format( page=page, num=num, total=total)) def finished(): p.deleteLater() d.deleteLater() d.hide() if not p.success and not p.aborted(): QMessageBox.warning(widget, _("Printing Error"), _("Could not send the document to the printer.")) p.finished.connect(finished) p.printing.connect(progress) p.start()
def print_(doc, filename=None, widget=None): """Prints the popplerqt4.Poppler.Document. The filename is used in the dialog and print job name. If the filename is not given, it defaults to a translation of "PDF Document". The widget is a widget to use as parent for the print dialog etc. """ # Decide how we will print. # on Windows and Mac OS X a print command must be specified, otherwise # we'll use raster printing s = QSettings() s.beginGroup("helper_applications") cmd = s.value("printcommand", "", type("")) use_dialog = s.value("printcommand/dialog", False, bool) resolution = s.value("printcommand/dpi", 300, int) linux_lpr = False if os.name != 'nt' and not sys.platform.startswith('darwin'): # we're probably on Linux if not cmd: cmd = fileprinter.lprCommand() if cmd: linux_lpr = True elif cmd.split('/')[-1] in fileprinter.lpr_commands: linux_lpr = True print_file = filename title = os.path.basename(filename) if filename else _("PDF Document") printer = QPrinter() printer.setDocName(title) printer.setPrintRange(QPrinter.AllPages) if linux_lpr or use_dialog or not cmd: dlg = QPrintDialog(printer, widget) dlg.setMinMax(1, doc.numPages()) dlg.setOption(QPrintDialog.PrintToFile, False) dlg.setWindowTitle(app.caption(_("Print {filename}").format(filename=title))) result = dlg.exec_() if widget: dlg.deleteLater() # because it has a parent if not result: return # cancelled if linux_lpr or '$ps' in cmd: # make a PostScript file with the desired paper size ps = QTemporaryFile() if ps.open() and qpopplerview.printer.psfile(doc, printer, ps): ps.close() print_file = ps.fileName() elif cmd: if printer.printRange() != QPrinter.AllPages: cmd = None # we can't cut out pages from a PDF file elif '$pdf' not in cmd: cmd += ' $pdf' command = [] if linux_lpr: # let all converted pages print printer.setPrintRange(QPrinter.AllPages) command = fileprinter.printCommand(cmd, printer, ps.fileName()) elif cmd and print_file: for arg in helpers.shell_split(cmd): if arg in ('$ps', '$pdf'): command.append(print_file) else: arg = arg.replace('$printer', printer.printerName()) command.append(arg) if command: if subprocess.call(command): QMessageBox.warning(widget, _("Printing Error"), _("Could not send the document to the printer.")) return else: # Fall back printing of rendered raster images. # It is unsure if the Poppler ArthurBackend ever will be ready for # good rendering directly to a painter, so we'll fall back to using # raster images. p = Printer() p.setDocument(doc) p.setPrinter(printer) p.setResolution(resolution) d = QProgressDialog() d.setModal(True) d.setMinimumDuration(0) d.setRange(0, len(p.pageList()) + 1) d.canceled.connect(p.abort) def progress(num, total, page): d.setValue(num) d.setLabelText(_("Printing page {page} ({num} of {total})...").format( page=page, num=num, total=total)) def finished(): p.deleteLater() d.deleteLater() d.hide() if not p.success and not p.aborted(): QMessageBox.warning(widget, _("Printing Error"), _("Could not send the document to the printer.")) p.finished.connect(finished) p.printing.connect(progress) p.start()
def print_(doc, filename=None, widget=None): """Prints the popplerqt4.Poppler.Document. The filename is used in the dialog and print job name. If the filename is not given, it defaults to a translation of "PDF Document". The widget is a widget to use as parent for the print dialog etc. """ # Decide how we will print. # on Windows and Mac OS X a print command must be specified, otherwise # we'll use raster printing s = QSettings() s.beginGroup("helper_applications") cmd = s.value("printcommand", "", type("")) use_dialog = s.value("printcommand/dialog", False, bool) resolution = s.value("printcommand/dpi", 300, int) linux_lpr = False if os.name != 'nt' and not sys.platform.startswith('darwin'): # we're probably on Linux if not cmd: cmd = fileprinter.lprCommand() if cmd: linux_lpr = True elif cmd.split('/')[-1] in fileprinter.lpr_commands: linux_lpr = True print_file = filename title = os.path.basename(filename) if filename else _("PDF Document") printer = QPrinter() printer.setDocName(title) printer.setPrintRange(QPrinter.AllPages) if linux_lpr or use_dialog or not cmd: dlg = QPrintDialog(printer, widget) dlg.setMinMax(1, doc.numPages()) dlg.setOption(QPrintDialog.PrintToFile, False) dlg.setWindowTitle( app.caption(_("Print {filename}").format(filename=title))) result = dlg.exec_() if widget: dlg.deleteLater() # because it has a parent if not result: return # cancelled if linux_lpr or '$ps' in cmd: # make a PostScript file with the desired paper size ps = QTemporaryFile() if ps.open() and qpopplerview.printer.psfile(doc, printer, ps): ps.close() print_file = ps.fileName() elif cmd: if printer.printRange() != QPrinter.AllPages: cmd = None # we can't cut out pages from a PDF file elif '$pdf' not in cmd: cmd += ' $pdf' command = [] if linux_lpr: # let all converted pages print printer.setPrintRange(QPrinter.AllPages) command = fileprinter.printCommand(cmd, printer, ps.fileName()) elif cmd and print_file: for arg in helpers.shell_split(cmd): if arg in ('$ps', '$pdf'): command.append(print_file) else: arg = arg.replace('$printer', printer.printerName()) command.append(arg) if command: if subprocess.call(command): QMessageBox.warning( widget, _("Printing Error"), _("Could not send the document to the printer.")) return else: # Fall back printing of rendered raster images. # It is unsure if the Poppler ArthurBackend ever will be ready for # good rendering directly to a painter, so we'll fall back to using # raster images. p = Printer() p.setDocument(doc) p.setPrinter(printer) p.setResolution(resolution) d = QProgressDialog() d.setModal(True) d.setMinimumDuration(0) d.setRange(0, len(p.pageList()) + 1) d.canceled.connect(p.abort) def progress(num, total, page): d.setValue(num) d.setLabelText( _("Printing page {page} ({num} of {total})...").format( page=page, num=num, total=total)) def finished(): p.deleteLater() d.deleteLater() d.hide() if not p.success and not p.aborted(): QMessageBox.warning( widget, _("Printing Error"), _("Could not send the document to the printer.")) p.finished.connect(finished) p.printing.connect(progress) p.start()