Printing Documents Using HTML and QText Document

The first approach we will show involves creating a string containing HTML, and using a QTextDocument to render the HTML to a QPrinter. The printVia-Html() method is quite long, so we will look at it in three parts.

def printViaHtml(self): html = u""

for statement in self.statements:

date = QDate.currentDate().toString(DATE_FORMAT) address = Qt.escape(statement.address).replace(",", "<br>") contact = Qt.escape(statement.contact) balance = statement.balance()

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>&nbsp;</p><p>"

"<table border=1 cellpadding=2 " "cellspacing=2><tr><td colspan=3>" "Transactions</td></tr>")

We create an empty unicode variable called html. Then we iterate over all the statements. The contact and address contain text, so we take the precaution of escaping any HTML characters. The address is stored as a single line with commas separating each part; we replace commas with line breaks. The logo is in a resource file, as indicated by the :/ prefix; it could have been any file in the filesystem, and it could be in any of the image formats that PyQt supports.

Up to now we have formatted strings using Python's % operator. But in some cases, using PyQt's string formatting is advantageous. The QString class has an arg() method that can be given an object, usually a string or a number, with some optional parameters. Each arg() call replaces the leftmost %n item in the QString with suitable text. For example:

QString("Copied %1 bytes to %2").arg(5387).arg("log.txt")

results in the string:

Copied 5387 bytes to log.txt

The %n items have no formatting syntax like Python's % operator, but formatting can be achieved by passing additional arguments to the arg() method. It is also possible to localize the formatting by using %Ln. For example:

QString("Copied %L1 bytes to %2").arg(5387).arg("log.txt")

results in the string:

Copied 5,387 bytes to log.txt in the United States and the United Kingdom, with the number coming out as 5.387 in some other countries.

In the case of our example, we want to print the amounts using two decimal digits, and with the whole number part having its digits grouped in threes. This can be achieved by using arg() with four arguments—the amount as a float, the minimum number of characters for the number to occupy, the output format ("f" for normal floating-point numbers and "e" or "E" for scientific notation) and the number of digits after the decimal place. It is also possible to give a fifth argument, a padding character, if a minimum number of characters is specified.

In the example we have used:

QString("$ %L1").arg(float(balance), 0, "f", 2))

A balance of 64 325.852 would be output as the string $ 64,325.85 in the United States.

Returning to the code, we add some text that varies depending on whether the customer is in debit or credit. Then we create the head of an HTML table with three columns, with the first row spanning all the columns and containing the title "Transactions". The &nbsp; is an HTML entity that signifies a nonbreaking space.

for date, amount in statement.transactions: color, status = "black", "Credit" if amount < 0:

color, status = "red", "Debit"

html += ("<tr><td align=right>%s</td>" "<td>%s</td><td align=right>" "<font color=%s>%s</font></td></tr>" % ( date.toString(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,<br>Yours sincerely," "<br><br>K.&nbsp;Longrey, Manager</p>")

dialog = QPrintDialog(self.printer, self) if dialog.exec_():

document = QTextDocument()

document.setHtml(html)

document.print_(self.printer)

At the end we simply pop up a print dialog, and if the user clicks Print, we create a new QTextDocument, set its text to the HTML we have generated in the html string, and tell the document to print itself on the printer.

Creating an HTML string and printing it using a QTextDocument is probably the quickest and easiest way to produce printed output in PyQt. The only downside is that it can be tricky to exercise fine control, although we can use style attributes and set a style sheet.

Tube Traffic Ninja

Tube Traffic Ninja

Discover How You Can Quickly And Easily Dominate Google and YouTube... With Simple Cash Generating Videos. Did you know that YouTube is the second largest search website on the entire Internet? YouTube gets more daily searches than Bing and Yahoo. In fact, there is only one search engine that gets more action.

Get My Free Ebook


Responses

  • ALEXANDER
    How to add a table to qtextdocument, pyqt?
    7 years ago
  • IRMINA
    How to set background image in qdocument html pyqt?
    3 years ago

Post a comment