Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Save Contents of QTextEdit as *.pdf?

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 ?

like image 637
thecreator232 Avatar asked Feb 14 '23 10:02

thecreator232


1 Answers

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)
like image 64
ekhumoro Avatar answered Feb 16 '23 03:02

ekhumoro