Creating print option for simple calculation tool
I made a small app for some calculations as you can see from the screenshot and I would like to add a printing functionality to this.
But as I'm a python as well as PyQt beginner, I'm struggling from the beginning.
My aim is to get a document with small header followed by the content consisting of the input and output data from the calculation from the actual tab.
So my question is: What is the usual way to achieve this?
So far I think printDialog (from Qt Print Support) could do the job. But all the examples I found only show how to print the content of a textedit-widget.
So, besides the question how to deal with the header, I'm stuck at this point:
def printDialog(): printer = QPrinter(QPrinter.HighResolution) dialog = QPrintDialog(printer, self)
Any ideas how to start?
Hi and welcome to devnet,
Do you want to print the content of the widget or rather some small report based on the numbers input and the result ?
thank you. Actually I would prefer a small report as you mentioned. I would like to create a template with some general information (address of the office, software version etc.) in the header and then a content area where the input and output of the actual tab is described and printed.
Then you should check the Printing chapter and examples from the "C++ GUI Programming with Qt 4 (2nd Edition) ". They show various ways for printing generated document.
In short, one simple way is to generate a basic html document with tables containing your values and then print that document.
thanks a lot for your answer. This seems to be a very interesting way to do it. Unfortunately it's in C++ an Qt4 (I forgot to mention that I'm using Qt5). But now I know what I need to search for.
Despite the Qt version, most of the book is still relevant for Qt 5.
I made some progress..
I'm now using QTextDocument. But I'm stuck how to set the right paper margins. Here's my code:
printer = QPrinter(QPrinter.HighResolution) printer.setFullPage(True) printer.setPageMargins(10,10.0,10.0,10.0, QPrinter.Millimeter) dialog = QPrintDialog(printer) header = """ <html> <table width=\"100%" border=1 cellspacing=0>\n <tr><td bgcolor=\"lightgray\"><font size=\"+1\"> <b><i>Softwarename</i></b></font>\n <tr><td>Version 0.1 | Copyright |\n </table> </html>""" html = header + "\n Content..." document = QtGui.QTextDocument() document.setHtml(html) document.setPageSize(???)
I want to get rid of the extra space of 20 mm from the QTextDocument. Do you know how to do this?
Denni 0 Banned last edited by
Be careful as printers do have hardware limits to how close to the edge you can get and those involve interacting with the specific printer drivers and as such I think there is a standardized minimum border these days (but then I might be wrong on that) just a word of warning if you bump into that limit you will have problems
As @Denni-0 says. each driver might/will have a non printable area
where you cannot print.
(unless special label printer)
Did you try
and see if it then right next to the non printable area ?
Thanks for your advice. As I'm using a PDF printer the printable area of a printer doesn't need to be considered.
I have read in another thread, that QTextDocument just puts a border of 20 mm as a standard. And with the given printer.setPageMargins(10,10.0,10.0,10.0, QPrinter.Millimeter) the actual margin from the left is exactly 30 mm.
So the question is how to tell QTextDocument to make no extra margins. Unfortunately I'm not able to understand how to use the Qt Documentation..
JonB last edited by JonB
I have read in another thread, that QTextDocument just puts a border of 20 mm as a standard.
Can you supply a link to wherever you read that?
Or, are you just talking about https://doc.qt.io/qt-5/qtextdocument.html#documentMargin-prop? You should check what your
QTextDocument ::documentMargin()currently returns. If it's non-zero you can alter via
setDocumentMargin(). The docs sate "The default is 4.", so you might save a bit of what you are looking for by reducing to 0?
Unfortunately I missed the Thread where I read it. But I made some progres anyway. This line "document.setPageSize(QtCore.QSizeF(printer.pageRect().size()))" seems do to what I want.
With this there are all margins gone. But now the text height got extremely small :/
Here's my example:
def printcontent(self): # Create printer header = """ <html> <table width=\"100%" border=1 cellspacing=0>\n <tr><td bgcolor=\"lightgray\"><font size=\"+1\"> <b><i>Softwarename</i></b></font>\n <tr><td>Version 0.01 | © Copyright 2019 |\n </table> </html>""" html = header + "<p>Content...</p>" printer = QPrinter(QPrinter.HighResolution) #printer.setFullPage(True) printer.setPageMargins(0,0,0,0, QPrinter.Millimeter) printer.setPageSize(QPrinter.A4) dialog = QPrintDialog(printer) document = QtGui.QTextDocument() document.setHtml(html) document.setPageSize(QtCore.QSizeF(printer.pageRect().size()))
Edit: Here's the link where it's said that the margin is 20 mm by default: click me
JonB last edited by JonB
The size of what text height? You're not outputting much.
You sure that's right?
Separate matter: your content is going to go after the
</html>. Not good.
Denni 0 Banned last edited by Denni 0
Click Me -- extrapolation for convenience -- the reference the individual makes is to the following documentation:
Where in it says:
PySide2.QtGui.QTextDocument.print_(printer)¶ Prints the document to the given printer . The QPagedPaintDevice must be set up before being used with this function. This is only a convenience method to print the whole document to the printer. If the document is already paginated through a specified height in the pageSize() property it is printed as-is. If the document is not paginated, like for example a document used in a QTextEdit , then a temporary copy of the document is created and the copy is broken into multiple pages according to the size of the paint device’s paperRect(). By default a 2 cm margin is set around the document contents. In addition the current page number is printed at the bottom of each page.
So that covers the specific issue of that margin and why its there and there appears to be a lot of other functionality that would address what you are trying to accomplish (but I could be wrong on that did not dive into great detail in that documentation)