Mar-12-2024, 02:25 PM
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?
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()
