def savePdf(self): """ Save to pdf file """ fileName = QFileDialog.getSaveFileName( self, 'Save to PDF', "", "PDF file (*.pdf);;All Files (*.*)") # new in v17.1 if QtHelper.IS_QT5: _filename, _type = fileName else: _filename = fileName # end of new if _filename: printer = QPrinter(QPrinter.HighResolution) printer.setPageSize(QPrinter.A4) printer.setColorMode(QPrinter.Color) printer.setOutputFormat(QPrinter.PdfFormat) printer.setOutputFileName(_filename) doc = QTextDocument() if self.toXml: doc.setPlainText(self.txtEdit.text()) else: doc.setHtml(self.txtEdit.toHtml()) doc.print_(printer)
def writePdf(dir_): newbody = originHTML.format(**htmlpara) printer = QPrinter() printer.setOutputFormat(QPrinter.PdfFormat) printer.setOutputFileName(dir_+'.pdf') printer.setPageSize(QPrinter.A4) text = QTextDocument() text.setHtml(newbody.decode('utf-8')) text.print_(printer)
def writePdf(dir_): newbody = originHTML.format(**htmlpara) printer = QPrinter() printer.setOutputFormat(QPrinter.PdfFormat) printer.setOutputFileName(dir_ + '.pdf') printer.setPageSize(QPrinter.A4) text = QTextDocument() text.setHtml(newbody.decode('utf-8')) text.print_(printer)
def printDocument1(self): html = u"" date = QDate.currentDate().toString(self.DATE_FORMAT) address = Qt.escape("Bario francisco mesa").replace(",", "<br>") contact = Qt.escape("Luis Mejia") balance = 5000 html += ("<p align=right><img src=':/logo.png'></p>" "<p> align = right>Greasy hands ltd." "<br>New Lombard Street" "<br>London<br>WC13 4PX<br>%s</p>" "<p>%s</p><p>Dear %s, </p>" "<p>The balance of your account is %s.") % ( date, address, contact, QString("$ %L1").arg( float(balance), 0, "f", 2)) if balance < 0: html += ( "<p><font color =red><b> Please remit the amount owing immediately.</b></font>" ) else: html += "We are delighted to have done business with you." html += ( "</p><p> </p><p>" "<table border=1 cellpadding=2 cellspacing=2><tr><td colspan=3>Transaction</td></tr>" ) transactions = [(QDate.currentDate(), 500), (QDate.currentDate(), 500), (QDate.currentDate(), -500), (QDate.currentDate(), 500)] for date, amount in transactions: color, status = "black", "Credit" if amount < 0: color, status = "red", "Debid" html += ( "<tr>" "<td align= right>%s</td>" "<td>%s</td><td align=right><font color=%s>%s</font></td></tr>" % (date.toString(self.DATE_FORMAT), status, color, QString("$ %L1").arg(float(abs(amount)), 0, "f", 2))) html += ("</table></p><p style='page-break-after=always;'>" "We hope to continue doing business with you</p>") pdialog = QPrintDialog() if pdialog.exec_() == QDialog.Accepted: printer = pdialog.printer() document = QTextDocument() document.setHtml(html) document.print_(printer)
def writePdfabs(dir_): updates = SETTING().get('pdfpara') htmlpara.update(updates) newbody = originHTML.format(**htmlpara) # with open('t.html', 'wb') as f: # f.write(newbody) # f.close() printer = QPrinter() printer.setOutputFormat(QPrinter.PdfFormat) printer.setOutputFileName(dir_) printer.setPageSize(QPrinter.A4) text = QTextDocument() text.setHtml(newbody.decode('utf-8')) text.print_(printer)
def writePdfabs(dir_): updates = PDF_PARAMETER htmlpara.update(updates) newbody = originHTML.format(**htmlpara) # with open('t.html', 'wb') as f: # f.write(newbody) # f.close() printer = QPrinter() printer.setOutputFormat(QPrinter.PdfFormat) printer.setOutputFileName(dir_) printer.setPageSize(QPrinter.A4) text = QTextDocument() text.setHtml(newbody.decode('utf-8')) text.print_(printer)
def init_print(self, linenos=True, style="default"): app = QApplication(sys.argv) # noqa doc = QTextDocument() doc.setHtml( self.highlight_file(linenos=linenos, style=style) ) printer = QPrinter() printer.setOutputFileName(self.pdf_file) printer.setOutputFormat(QPrinter.PdfFormat) page_size_dict = {"a2": QPrinter.A2, "a3": QPrinter.A3, "a4": QPrinter.A4} printer.setPageSize(page_size_dict.get(self.size.lower(), QPrinter.A4)) printer.setPageMargins(15, 15, 15, 15, QPrinter.Millimeter) doc.print_(printer) logging.info("PDF created at %s" % (self.pdf_file))
def savePrinter(self): """ Save to printer """ printer = QPrinter() dialog = QPrintDialog(printer, self) dialog.setWindowTitle("Print") if dialog.exec_() != QDialog.Accepted: return doc = QTextDocument() doc.setPlainText(self.txtEdit.text()) doc.print_(printer)
def printDocument1(self): html = u"" date = QDate.currentDate().toString(self.DATE_FORMAT) address = Qt.escape("Bario francisco mesa").replace(",","<br>") contact = Qt.escape("Luis Mejia") balance = 5000 html += ("<p align=right><img src=':/logo.png'></p>" "<p> align = right>Greasy hands ltd." "<br>New Lombard Street" "<br>London<br>WC13 4PX<br>%s</p>" "<p>%s</p><p>Dear %s, </p>" "<p>The balance of your account is %s.")% ( date, address, contact, QString("$ %L1").arg(float(balance),0,"f",2)) if balance <0 : html += ("<p><font color =red><b> Please remit the amount owing immediately.</b></font>") else: html += "We are delighted to have done business with you." html += ("</p><p> </p><p>" "<table border=1 cellpadding=2 cellspacing=2><tr><td colspan=3>Transaction</td></tr>") transactions = [ (QDate.currentDate(),500), (QDate.currentDate(),500), (QDate.currentDate(),-500), (QDate.currentDate(),500) ] for date, amount in transactions: color, status = "black", "Credit" if amount <0: color, status = "red", "Debid" html += ("<tr>" "<td align= right>%s</td>" "<td>%s</td><td align=right><font color=%s>%s</font></td></tr>" % ( date.toString(self.DATE_FORMAT), status,color, QString("$ %L1").arg(float(abs(amount)), 0, "f",2))) html += ("</table></p><p style='page-break-after=always;'>" "We hope to continue doing business with you</p>") pdialog = QPrintDialog() if pdialog.exec_() == QDialog.Accepted: printer = pdialog.printer() document = QTextDocument() document.setHtml(html) document.print_(printer)
def init_print(self, linenos=True, style="default"): app = QApplication(sys.argv) # noqa doc = QTextDocument() doc.setHtml(self.highlight_file(linenos=linenos, style=style)) printer = QPrinter() printer.setOutputFileName(self.pdf_file) printer.setOutputFormat(QPrinter.PdfFormat) page_size_dict = { "a2": QPrinter.A2, "a3": QPrinter.A3, "a4": QPrinter.A4 } printer.setPageSize(page_size_dict.get(self.size.lower(), QPrinter.A4)) printer.setPageMargins(15, 15, 15, 15, QPrinter.Millimeter) doc.print_(printer) logging.info("PDF created at %s" % (self.pdf_file))
def savePrinter(self): """ Save to printer """ printer = QPrinter() dialog = QPrintDialog(printer, self) dialog.setWindowTitle("Print") if dialog.exec_() != QDialog.Accepted: return if QtHelper.IS_QT5: # new in v18 self.fileName = printer self.txtEdit.page().toHtml(self.__toPrinter) else: doc = QTextDocument() doc.setPlainText(self.txtEdit.text()) doc.print_(printer)
def main(): app = QApplication(sys.argv) # Preparing the pdf doc = QTextDocument() printer = QPrinter() printer.setOutputFileName("KabitGurdas.pdf") printer.setOutputFormat(QPrinter.PdfFormat) # Setting page size as A5 printer.setPaperSize(QPrinter.A5); # page margin printer.setPageMargins(12, 16, 12, 20, QPrinter.Millimeter) doc.setPageSize(QSizeF(printer.pageRect().size())) painter = QPainter() painter.begin( printer ) # Set poem book and get each poem in pdf book = kb() for KabitNumber in range(1,676): # Add text in html to page doc.setHtml(book.getKabitHTML(KabitNumber)) doc.defaultFont().setPointSize(20) # A new page printer.newPage() # Create a QPainter to draw our content doc.drawContents(painter)#,printer.pageRect().translated( -printer.pageRect().x(), -printer.pageRect().y() )) #QTextDocument.drawContents(QPainter, QRectF rect=QRectF()) doc.print_(printer) # Done. painter.end()
def drucken(self): # für später printer = QPrinter() printer.setPaperSize(QPrinter.A5) doc = QTextDocument('Ich bin ein Text.') doc.print_(printer)
def printDocument2(self): dialog = QPrintDialog() if not dialog.exec_(): return self.printer = dialog.printer() headFormat = QTextBlockFormat() headFormat.setAlignment(Qt.AlignLeft) headFormat.setTextIndent( self.printer.pageRect().width()-216) bodyFormat = QTextBlockFormat() bodyFormat.setAlignment(Qt.AlignJustify) lastParaBodyFormat = QTextBlockFormat(bodyFormat) lastParaBodyFormat.setPageBreakPolicy(QTextFormat.PageBreak_AlwaysAfter) rightBodyFormat = QTextBlockFormat() rightBodyFormat.setAlignment(Qt.AlignRight) headCharFormat = QTextCharFormat() headCharFormat.setFont(QFont("Helvetica",10)) bodyCharFormat = QTextCharFormat() bodyCharFormat.setFont(QFont("Times",11)) redBodyCharFormat = QTextCharFormat(bodyCharFormat) redBodyCharFormat.setForeground(Qt.red) tableFormat = QTextTableFormat() tableFormat.setBorder(1) tableFormat.setCellPadding(2) document = QTextDocument() cursor = QTextCursor(document) mainFrame = cursor.currentFrame() page = 1 cursor.insertBlock(headFormat, headCharFormat) for text in ("Greasy Hands Ltd.", "New Lombard Street","London" , "WC13", QDate.currentDate().toString(self.DATE_FORMAT)): cursor.insertBlock(headFormat,headCharFormat) cursor.insertText(text) cursor.insertBlock(bodyFormat,bodyCharFormat) cursor.insertText("Barrio Francisco Meza") cursor.insertBlock(bodyFormat) cursor.insertBlock(bodyFormat,bodyCharFormat) cursor.insertText("Dear Lyuis") cursor.insertBlock(bodyFormat) cursor.insertBlock(bodyFormat,bodyCharFormat) cursor.insertText(QString("The balance of your account is $ %L1.").arg(float(500.987),0,"f",2)) cursor.insertBlock(bodyFormat,redBodyCharFormat) cursor.insertText("Please remit the amount") cursor.insertBlock(bodyFormat,bodyCharFormat) cursor.insertText("Transaction") transactions = [ (QDate.currentDate(),500), (QDate.currentDate(),500), (QDate.currentDate(),-500), (QDate.currentDate(),500) ] table = cursor.insertTable(len(transactions), 3, tableFormat) row = 0 for date, amount in transactions: cellCursor = table.cellAt(row,0).firstCursorPosition() cellCursor.setBlockFormat(rightBodyFormat) cellCursor.insertText(date.toString(self.DATE_FORMAT),bodyCharFormat) cellCursor = table.cellAt(row,1).firstCursorPosition() cellCursor.insertText("Credit",bodyCharFormat) cellCursor = table.cellAt(row,2).firstCursorPosition() cellCursor.setBlockFormat(rightBodyFormat) cellCursor.insertText(QString("The balance of your account is $ %L1.").arg(float(amount),0,"f",2),redBodyCharFormat) row += 1 cursor.setPosition(mainFrame.lastPosition()) cursor.insertBlock(bodyFormat,bodyCharFormat) cursor.insertText("We hope") document.print_(self.printer)
def printDocument2(self): dialog = QPrintDialog() if not dialog.exec_(): return self.printer = dialog.printer() headFormat = QTextBlockFormat() headFormat.setAlignment(Qt.AlignLeft) headFormat.setTextIndent(self.printer.pageRect().width() - 216) bodyFormat = QTextBlockFormat() bodyFormat.setAlignment(Qt.AlignJustify) lastParaBodyFormat = QTextBlockFormat(bodyFormat) lastParaBodyFormat.setPageBreakPolicy( QTextFormat.PageBreak_AlwaysAfter) rightBodyFormat = QTextBlockFormat() rightBodyFormat.setAlignment(Qt.AlignRight) headCharFormat = QTextCharFormat() headCharFormat.setFont(QFont("Helvetica", 10)) bodyCharFormat = QTextCharFormat() bodyCharFormat.setFont(QFont("Times", 11)) redBodyCharFormat = QTextCharFormat(bodyCharFormat) redBodyCharFormat.setForeground(Qt.red) tableFormat = QTextTableFormat() tableFormat.setBorder(1) tableFormat.setCellPadding(2) document = QTextDocument() cursor = QTextCursor(document) mainFrame = cursor.currentFrame() page = 1 cursor.insertBlock(headFormat, headCharFormat) for text in ("Greasy Hands Ltd.", "New Lombard Street", "London", "WC13", QDate.currentDate().toString(self.DATE_FORMAT)): cursor.insertBlock(headFormat, headCharFormat) cursor.insertText(text) cursor.insertBlock(bodyFormat, bodyCharFormat) cursor.insertText("Barrio Francisco Meza") cursor.insertBlock(bodyFormat) cursor.insertBlock(bodyFormat, bodyCharFormat) cursor.insertText("Dear Lyuis") cursor.insertBlock(bodyFormat) cursor.insertBlock(bodyFormat, bodyCharFormat) cursor.insertText( QString("The balance of your account is $ %L1.").arg( float(500.987), 0, "f", 2)) cursor.insertBlock(bodyFormat, redBodyCharFormat) cursor.insertText("Please remit the amount") cursor.insertBlock(bodyFormat, bodyCharFormat) cursor.insertText("Transaction") transactions = [(QDate.currentDate(), 500), (QDate.currentDate(), 500), (QDate.currentDate(), -500), (QDate.currentDate(), 500)] table = cursor.insertTable(len(transactions), 3, tableFormat) row = 0 for date, amount in transactions: cellCursor = table.cellAt(row, 0).firstCursorPosition() cellCursor.setBlockFormat(rightBodyFormat) cellCursor.insertText(date.toString(self.DATE_FORMAT), bodyCharFormat) cellCursor = table.cellAt(row, 1).firstCursorPosition() cellCursor.insertText("Credit", bodyCharFormat) cellCursor = table.cellAt(row, 2).firstCursorPosition() cellCursor.setBlockFormat(rightBodyFormat) cellCursor.insertText( QString("The balance of your account is $ %L1.").arg( float(amount), 0, "f", 2), redBodyCharFormat) row += 1 cursor.setPosition(mainFrame.lastPosition()) cursor.insertBlock(bodyFormat, bodyCharFormat) cursor.insertText("We hope") document.print_(self.printer)
from PyQt4.QtGui import QTextDocument, QPrinter, QApplication import sys app = QApplication(sys.argv) doc = QTextDocument() location = sys.argv[1] html = open(location).read() doc.setHtml(html) printer = QPrinter() printer.setOutputFileName(sys.argv[2]) printer.setOutputFormat(QPrinter.PdfFormat) printer.setPageSize(QPrinter.A4) printer.setPageMargins(15,15,15,15,QPrinter.Millimeter); doc.print_(printer) print "\n" + sys.argv[2] + " was created successfully.\n"
from PyQt4.QtGui import QTextDocument, QPrinter, QApplication import sys app = QApplication(sys.argv) doc = QTextDocument() location = "https://translate.google.cn/" html = open(location).read() doc.setHtml(html) printer = QPrinter() printer.setOutputFileName("foo.pdf") printer.setOutputFormat(QPrinter.PdfFormat) printer.setPageSize(QPrinter.A4) printer.setPageMargins (15,15,15,15,QPrinter.Millimeter) doc.print_(printer) print ("done!")