Save QTextEdit contents as * .pdf?

I am trying to save the contents of a text editor as a pdf file. The text editor was created using PyQt (I did not do a text editor), I got the text editor code here . I made some changes to the editor, but this is not a problem.

After some initial research, I found that I needed to use ReportLab to publish a PDF file. But I can’t find a way to do this.

Does anyone know how to do this?

+4
source share
1 answer

The source code of the text editor already has a method PDF, but it is not used and may not work properly if it stands.

The basic rewrite of a method that should work on all platforms will 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 thing you need is a menu item to launch it, so in Main.initUIjust add:

    pdfAction = QtGui.QAction("Save to PDF", self)
    pdfAction.setStatusTip("Save to PDF")
    pdfAction.triggered.connect(self.SavetoPDF)
    ...

    file.addAction(pdfAction)
+8
source

Source: https://habr.com/ru/post/1533057/


All Articles