Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. QTextDocument don't print images with QPrinter
Forum Updated to NodeBB v4.3 + New Features

QTextDocument don't print images with QPrinter

Scheduled Pinned Locked Moved Unsolved General and Desktop
qtextdocumenttextareaqimagerichtextqprinter
1 Posts 1 Posters 35 Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • D Offline
    D Offline
    Deymos
    wrote last edited by
    #1

    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.

    1 Reply Last reply
    0

    • Login

    • Login or register to search.
    • First post
      Last post
    0
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Get Qt Extensions
    • Unsolved