def saveAsPdf(self, fn, force=False): """ Save bar image as a eps file. Args: fn: Filename force: if True, overwrites an existing file. If false, raises a RuntimeError if file already exists. """ printer = QPrinter(QPrinter.HighResolution) printer.setOutputFormat(QPrinter.PdfFormat) printer.setOutputFileName(fn) printer.setFullPage(True) printer.setPageSize(QPrinter.Custom) printer.setPaperSize(QSizeF(*self.size), QPrinter.Millimeter) printer.setPageMargins(0, 0, 0, 0, QPrinter.Millimeter) painter = QPainter(printer) painter.setRenderHint(QPainter.Antialiasing) painter.setBrush(Qt.white) painter.setPen(Qt.white) painter.drawRect(QRect(0, 0, *self.size)) targetrect = QRectF(0, 0, printer.width(), printer.height()) sourcerect = QRectF(0, 0, *self.size) self.render(painter, targetrect, sourcerect) painter.end() return True
def printIndex(self): widget = QApplication.focusWidget() if self.state.printer is None: self.state.printer = QPrinter(QPrinter.HighResolution) self.state.printer.setColorMode(QPrinter.GrayScale) settings = QSettings() size = PaperSizeKind( int(settings.value(Gopt.Key.PaperSize, Gopt.Default.PaperSize))) self.state.printer.setPaperSize( QPrinter.Letter if size is PaperSizeKind.LETTER else QPrinter. A4) with Lib.Qt.DisableUI(*self.window.widgets(), forModalDialog=True): form = QPrintDialog(self.state.printer, self.state.window) form.setWindowTitle("Print Index") if form.exec_(): try: with Lib.DisableUI(*self.window.widgets()): config = self.state.model.configs().copy() config.Filename = "print.$$$" config.Printer = self.state.printer Output.outputEntries(self.state.model, config, "Printing", self.window.reportProgress) say("Printed") except Output.Error as err: say("Failed to print: {}".format(err)) logging.error("printIndex failed: {}".format(err)) Lib.restoreFocus(widget)
def print_to_pdf(self, path, paper_size=(8.5, 11.0), paper_margins=(0, 0, 0, 0), paper_units=QPrinter.Inch, zoom_factor=1.0): """Saves page as a pdf file. See qt4 QPrinter documentation for more detailed explanations of options. :param path: The destination path. :param paper_size: A 2-tuple indicating size of page to print to. :param paper_margins: A 4-tuple indicating size of each margin. :param paper_units: Units for pager_size, pager_margins. :param zoom_factor: Scale the output content. """ assert len(paper_size) == 2 assert len(paper_margins) == 4 printer = QPrinter(mode=QPrinter.ScreenResolution) printer.setOutputFormat(QPrinter.PdfFormat) printer.setPaperSize(QtCore.QSizeF(*paper_size), paper_units) printer.setPageMargins(*(paper_margins + (paper_units, ))) printer.setFullPage(True) printer.setOutputFileName(path) if self.webview is None: self.webview = QtWebKit.QWebView() self.webview.setPage(self.page) self.webview.setZoomFactor(zoom_factor) self.webview.print_(printer)
def testQPrinterGetPageMargins(self): # Bug #742 obj = QPrinter() values = (10.0, 20.0, 30.0, 40.0, QPrinter.Point) obj.setPageMargins(*values) self.assert_( self.compareTuples(obj.getPageMargins(QPrinter.Point), values[:-1]))
def OnPrint(self): """""" document = self.document() printer = QPrinter() dlg = QPrintDialog(printer, self) if dlg.exec_() != QDialog.Accepted: return document.print_(printer)
def capture_pdf(self): printer = QPrinter(QPrinter.HighResolution) printer.setResolution(300) printer.setOutputFileName("QtPrinter.pdf") printer.setPaperSize(QPrinter.A4) printer.setOrientation(QPrinter.Landscape) printer.setOutputFormat(QPrinter.PdfFormat) painter = QPainter(printer) self.main_frame.render(painter) painter.end()
def print_(self): canvas = self.mw.get_current_canvas() if not canvas: return printer = QPrinter() dlg = QPrintDialog(printer, self.mw) if dlg.exec_() == QDialog.Accepted: painter = QPainter(printer) painter.drawImage(0, 0, canvas.image) painter.end()
def saveReport(self, filename): directory = os.path.dirname(filename) if not os.path.isdir(directory): os.makedirs(directory) printer = QPrinter() printer.setOutputFormat(QPrinter.PdfFormat) printer.setOrientation(QPrinter.Landscape) printer.setPageSize(QPrinter.A4) printer.setOutputFileName(filename) if filename: self.print_(printer)
def actionPrint(self): """打印报告""" printer = QPrinter() printer.setOutputFormat(QPrinter.NativeFormat) printer.setOutputFileName("") printer.setOrientation(QPrinter.Landscape) printer.setPageSize(QPrinter.A4) #=======================================================# #无需选择 printDialog = QPrintDialog(printer, self) #self.print_(printer) #如果需要选择打印机 if printDialog.exec_(): self.print_(printer)
def convert_html_to_pdf(source, destination, page_size=QPrinter.Letter, print_format=QPrinter.PdfFormat, timeout=10000, app=None): """Converts an .html file at the source to a .pdf at the destination Any external files linked in the source file must be paths relative to the location of the source file itself. If building the html file dynamically, the rel_path function can be used to create proper relative paths using the .html location as the source location. The conversion is done using th ability of a QPrinter to print a QWebView to a PDF. This means we must have some Qt bindings, either PySide (default) or PyQt4 (5 support incoming?), but then are only limited by what the QWebView can display. While the intent is so print to a PDF, the format parameter is exposed so that this can be used to print directly to a printer or (if >=Qt4.2) PostScript format. If this is being used in a larger QApplication, we should only have one instance of QApplication, so pass the existing instance to the `app` parameter. """ if app is None: app = QApplication(sys.argv) view = QWebView() # We want to ensure the page was fully loaded before printing, so # we wait for the loadFinished event to fire. with wait_for_signal(view.loadFinished, timeout=timeout): # QUrl requires absolute path names view.load(QUrl.fromLocalFile(os.path.abspath(source))) # With the QWebView loaded, we now print to the destination PDF printer = QPrinter() printer.setPageSize(page_size) printer.setOutputFormat(print_format) printer.setOutputFileName(destination) view.print_(printer) # Exit the application app.exit()
def output(config, document): p = ( """<p style="font-family: '{family}'; font-size: {size}pt;">""".format( family=config.StdFont, size=config.StdFontSize)) pad = lambda match: p + (int(match.group("level")) * 4 * " ") doc = QTextDocument() doc.setHtml(INDENT_RX.sub(pad, document)) printer = getattr(config, "Printer", None) if printer is None: printer = QPrinter(QPrinter.HighResolution) printer.setCreator("{} {}".format(QApplication.applicationName(), QApplication.applicationVersion())) printer.setOutputFormat(QPrinter.PdfFormat) printer.setOrientation(QPrinter.Portrait) settings = QSettings() size = PaperSizeKind( int(settings.value(Gopt.Key.PaperSize, Gopt.Default.PaperSize))) printer.setPaperSize( QPrinter.A4 if size is PaperSizeKind.A4 else QPrinter.Letter) printer.setOutputFileName(config.Filename) doc.print_(printer)
def __init__(self, mainwindow, settings=None): """ Initializes the text editor widget. """ super(TextEditor, self).__init__(mainwindow) self.setupUi(self.mw) self.plainTextEdit.clear() self.plainTextEdit.setEnabled(False) self.plainTextEdit.show() self.highlighter = SyntaxHighlighter(self.plainTextEdit.document(), settings) self.initAutocomplete() self._initNumberBar() self.hidden = {} self.printer = QPrinter(QPrinterInfo.defaultPrinter()) self.plainTextEdit.setTextCursor( self.plainTextEdit.cursorForPosition(QPoint(0, 0))) self.canUndo = False self.canRedo = False self.ontologySelector.setCurrentIndex(-1) self.timer = QTimer(self) self.timer.setSingleShot(True) self.timer.timeout.connect(self.commit) #Connects self.getWidget().textChanged.connect(self.searchCompletion) self.plainTextEdit.undoAvailable.connect(self.setCanUndo) self.plainTextEdit.redoAvailable.connect(self.setCanRedo) self.ontologySelector.currentIndexChanged[int].connect( self.showOtherOntology) self.plainTextEdit.textChanged.connect(self.expandIfBracketRemoved) self.plainTextEdit.textChanged.connect(self.setTextChanged) self._updateOntologySelector() #must be after connects
def print_(self): """Print note with preview""" printer = QPrinter() dialog = QPrintPreviewDialog(printer) dialog.paintRequested.connect(self.page.view().print_) dialog.exec_()