I am trying to save the contents of a Text Editor as a pdf file. The text Editor has been made using PyQt (i didn't make the text Editor), i got the code of the text editor from here. I have done some changes to the editor but that wont be a problem.
After some initial research i found that i need to use ReportLab to publish a pdf file.But i can't find a way to do this .
Does anyone know how this could be accomplished ?
The source code for the Text Editor already has a PDF
method, but it is unused, and possibly won't work properly as it stands.
A basic re-write of the method that should work on all platforms, would look like this:
def SavetoPDF(self):
filename = QtGui.QFileDialog.getSaveFileName(self, 'Save to PDF')
if filename:
printer = QtGui.QPrinter(QtGui.QPrinter.HighResolution)
printer.setPageSize(QtGui.QPrinter.A4)
printer.setColorMode(QtGui.QPrinter.Color)
printer.setOutputFormat(QtGui.QPrinter.PdfFormat)
printer.setOutputFileName(filename)
self.text.document().print_(printer)
The only other thing you'll need is a menu item to run it, so in Main.initUI
just add:
pdfAction = QtGui.QAction("Save to PDF", self)
pdfAction.setStatusTip("Save to PDF")
pdfAction.triggered.connect(self.SavetoPDF)
...
file.addAction(pdfAction)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With