Python Forum
[PyQt] Saving file to html or pdf stopped working in PyQt6
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[PyQt] Saving file to html or pdf stopped working in PyQt6
#1
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()
Reply
#2
Why not this?
def export_html1(self, tableV, file_path):
    with open(file_path, "w") as file:
        file.write(self.formHTML(tableV))
ejKDE likes this post
Reply
#3
(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)
Reply
#4
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.
Reply
#5
(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)
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  [PyQt] [solved] How to display a pdf-file in a PyQt6 widget BigMan 13 17,650 May-06-2023, 09:27 AM
Last Post: Axel_Erfurt
  event driven coding PyQt6 on Combobox change merrittr 3 2,504 May-03-2023, 03:35 AM
Last Post: merrittr
Question [PyQt] Application desktop toolbar with PyQt6 bunz 4 1,819 Mar-09-2023, 08:09 PM
Last Post: bunz
  PyQt6 QAction with icon and string malonn 2 1,963 Sep-12-2022, 11:59 AM
Last Post: malonn
  [PyQt] Embed Google Maps in PyQt6 Widget Raures 2 3,399 Sep-11-2021, 04:32 PM
Last Post: Raures
  [Tkinter] logical error - saving file rwahdan 4 2,438 Jul-01-2021, 12:59 PM
Last Post: rwahdan
  [PyQt] saving text file by FileDialog option atlass218 14 5,194 Feb-19-2020, 09:22 AM
Last Post: atlass218
  Part of code is adding extra new line when saving to text file. lovepeace 9 5,608 Aug-24-2019, 12:52 PM
Last Post: lovepeace
  [PyQt5] [QWebEngineView] HTML file download nolme 0 5,224 May-08-2018, 08:41 PM
Last Post: nolme

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020