Hello
I'm making an interactive report generator based on RichText handling and editing, I have content with text, tables and images, my QML part is TextArea, and in C++ QQuickTextDocument and QTextDocument
When I paste html into TextArea, all the images of the table and text are visible, but when I give it to QPrinter and output the preview to QPrintPreviewDialog, I have there are no images, here are my 2 approaches for adding images. In the first case, I converted to base64 and stuffed it into Html, but it doesn't work.:
void ReportBuilder::addInlineImageElement(const QImage &image, qreal width, qreal height)
{
QString style;
if (width > 0) style += QString("width:%1px;").arg(width);
if (height > 0) style += QString("height:%1px;").arg(height);
if (width == 0 && height == 0) style += "max-width:100%;height:auto;";
QString imgTag = QString("<img width='%1' src='data:image/png;base64,%2'/>")
.arg(width)
.arg(imageToBase64(image));
append(imgTag);
}
QString ReportBuilder::imageToBase64(const QImage &image)
{
QImage scaled = image.scaled(640, 640, Qt::KeepAspectRatio, Qt::SmoothTransformation);
QByteArray ba;
QBuffer buffer(&ba);
buffer.open(QIODevice::WriteOnly);
scaled.save(&buffer, "PNG");
return QString::fromLatin1(ba.toBase64());
}
My second way is to add my images to the QTextDocument resources, I generate a QUrl, send a QUrl and a QImage to a model that inserts html into the QTextDocument, but before that it registers the images, but for some reason it doesn't work either:
void ReportBuilder::addImageElement(const QImage &image, qreal width, qreal height)
{
QImage scaled = image.scaled(640, 640, Qt::KeepAspectRatio, Qt::SmoothTransformation);
m_imageCounter++;
QString imageName = QString("image_%1.jpg").arg(m_imageCounter);
QString resourceUrl = QString("cid_%1").arg(imageName);
QUrl imgUrl(QString("myimg://%1").arg(resourceUrl));
m_images.insert(imgUrl, scaled);
QString imgTag = QString("<img src=\"%1\"/>").arg(imgUrl.toString());
qDebug()<<imgTag;
m_html.append(imgTag);
}
void ReportEditorModel::insertReport(const QString &html, const QMap<QUrl, QImage> &images)
{
QTextDocument *doc = textDocument();
if (!doc) {
qWarning() << "No document available to insert Report.";
return;
}
for (auto it = images.constBegin(); it != images.constEnd(); ++it) {
doc->addResource(QTextDocument::ImageResource, QUrl(it.key()), it.value());
}
QTextCursor cursor = textCursor();
QString wrappedHtml = QString("<div style='"
"margin-left:%1px;"
"margin-right:%2px;"
"margin-top:%3px;"
"margin-bottom:%4px;'>"
"%5"
"</div>")
.arg(m_leftMargin)
.arg(m_rightMargin)
.arg(m_topOffset)
.arg(m_bottomOffset)
.arg(html);
// cursor.movePosition()
cursor.insertHtml(wrappedHtml);
QPrinter printer(QPrinter::HighResolution);
QPrintPreviewDialog preview(&printer);
connect(&preview, &QPrintPreviewDialog::paintRequested, this, [this, doc](QPrinter *printer){doc->print(printer);});
preview.exec();
}
Here is my curent html:
"<div style='margin-left:85.0394px;margin-right:85.0394px;margin-top:85.0394px;margin-bottom:85.0394px;'>
<div style='font-size:10pt;'></div>
<hr />
<div style='font-size:16pt;'>Image 1.jpg</div>
<hr />
<div style='font-size:10pt;'></div>
<img src=\"myimg://cid_image_1.jpg\"/>
<div style='font-size:10pt;'></div>
<hr />
<div style='font-size:16pt;'>Info</div>
<hr />
<div style='font-size:10pt;'>Name: Image 1.jpg</div>
<div style='font-size:10pt;'>Size: 1824x446</div>
<div style='font-size:10pt;'>Resolution: 623 dpi</div>
<div style='font-size:10pt;'>Bit depth: 24 bit(s)</div>
<div style='font-size:10pt;'>Creation time: 14:24:07 03.09.2024</div>
</div>"
Any ideas how to make it work?
Currently on Qt 6.2.0, but on 6.5.3 same situation.