Python Forum
[PyQt] Saving file to html or pdf stopped working in PyQt6 - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: GUI (https://python-forum.io/forum-10.html)
+--- Thread: [PyQt] Saving file to html or pdf stopped working in PyQt6 (/thread-41747.html)



Saving file to html or pdf stopped working in PyQt6 - ejKDE - Mar-12-2024

I'm currently trying to port PyQt5 to PyQt6 piece by piece. Functions that use html whether to save as html or pdf don't work and i couldn't find any information on the changes https://doc.qt.io/qt-6

Function formHTML returns html inside export_pdf function. But it doesn't store the file. Maybe it should be formed differently?

    def formHTML(self, tableV):
        model = tableV.model()
        rows = model.rowCount()
        cols = model.columnCount()

        html = "<table border='1' width='100%' cellspacing='0' cellpadding='3'>"

        for row in range(rows):
            html += "<tr>"
            for column in range(cols):
                index = model.index(row, column)
                data = index.data()
                html += f"<td>{data}</td>"
            html += "</tr>"

        html += "</table>"

        return html

    def export_pdf(self, tableV, file_path):
        html = self.formHTML(tableV)
        document = QTextDocument()
        document.setHtml(html)

        print(file_path)
        pdf_writer = QPdfWriter(file_path)
        pdf_writer.setPageSize(QPageSize(QPageSize.A4))
        pdf_writer.setPageMargins(QMarginsF(5, 5, 5, 5))
        document.print(pdf_writer)

        # printer = QPrinter(QPrinter.PrinterMode.HighResolution)
        # printer.setOutputFormat(QPrinter.OutputFormat.PdfFormat)
        # printer.setOutputFileName(file_path)
        # printer.setPageSize(QPageSize(QPageSize.A4))
        # printer.setPageMargins(QMarginsF(5, 5, 5, 5))

        # document.print(printer)

    def export_html1(self, tableV, file_path):
        html = self.formHTML(tableV)
        document = QTextDocument()
        document.setHtml(html)

        file = QFile(file_path)
        file.open(QFile.WriteOnly | QFile.Text)
        out_stream = QTextStream(file)
        out_stream << document.toHtml()
        file.close()



RE: Saving file to html or pdf stopped working in PyQt6 - deanhystad - Mar-12-2024

Why not this?
def export_html1(self, tableV, file_path):
    with open(file_path, "w") as file:
        file.write(self.formHTML(tableV))



RE: Saving file to html or pdf stopped working in PyQt6 - ejKDE - Mar-12-2024

(Mar-12-2024, 03:28 PM)deanhystad Wrote: Why not this?
def export_html1(self, tableV, file_path):
    with open(file_path, "w") as file:
        file.write(self.formHTML(tableV))

Thank you, this worked for html. Apparently i went too far with Qt way on this one. Right now looking for some other way to create pdf...


Ok, i've found the problem. I don't know how to control Margins but at least without them it works:

    def export_pdf(self, tableV, file_path):
        html = self.formHTML(tableV)

        document = QTextDocument()
        document.setHtml(html)

        printer = QPrinter(QPrinter.PrinterMode.HighResolution)
        printer.setOutputFormat(QPrinter.OutputFormat.PdfFormat)
        printer.setOutputFileName(file_path)
        printer.setPageSize(QPageSize(QPageSize.PageSizeId.A4))

        document.print(printer)



RE: Saving file to html or pdf stopped working in PyQt6 - deanhystad - Mar-12-2024

printer.setPageMargins(5, 5, 5, 5, QPrinter.Millimeters)
I'm not sure about the units for the margins. 5mm is a bit small but 5 Inch is way too big.


RE: Saving file to html or pdf stopped working in PyQt6 - ejKDE - Mar-12-2024

(Mar-12-2024, 05:24 PM)deanhystad Wrote:
printer.setPageMargins(5, 5, 5, 5, QPrinter.Millimeters)
I'm not sure about the units for the margins. 5mm is a bit small but 5 Inch is way too big.

Thanks for pointing into the right direction. This is how it works:
printer.setPageMargins(QMarginsF(0, 0, 5, 5), QPageLayout.Unit.Millimeter)