Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

Image is visible after converting text to .pdf file



  • Hey,
    When I convert my document to a .pdf file the image is not visible but a table, text(bold color ), etc are exported perfectly.
    Here is the piece of code which converts to .pdf

     QTextDocument document;
            document.setHtml(ui->textEdit->toHtml());
    
            QPrinter printer(QPrinter::PrinterResolution);
            printer.setOutputFormat(QPrinter::PdfFormat);
            printer.setPageSize(QPageSize(QPageSize::A4));
            printer.setOutputFileName(fileName);
            printer.setPageMargins(QMarginsF(15, 15, 15, 15));
            document.print(&printer);
    

    First Page

    Here is the exported .pdf file:
    proof.jpg

    Second Page

    proof.jpg

    Edit: Unfortunately I write the title opposite of my problem


  • Lifetime Qt Champion

    Hi
    So when you print your QTextDocument to PDF, the image is no shown at all ?

    I would try to look at the HTML from ui->textEdit->toHtml()

    and see how it references the images. IF you didn't use resources for the image, it might not address the image right. and hence it "falls" off.

    But why do you convert to HTML at all ?

    You can ask TextEdit directly for its QTextDocument so not sure why you create a new doc ?



  • Ok,
    I have used tohtml here because i want the textformat in the .pdf file


  • Lifetime Qt Champion

    @UG-SEP
    Im not sure what you mean by

    • I have used tohtml here because i want the textformat in the .pdf file

    Im not sure why not
    just
    textedit->document()->print(&printer);

    is not what you are after ?

    https://doc.qt.io/qt-5/qtextedit.html#document-prop



  • Ok let me explain to you,
    I wanted to convert into HTML because if I simply convert text edit so It will not accept the color, font, list, used in the TextEdit so to export them also I have to use toHtml
    there
    May you understand


  • Lifetime Qt Champion

    @UG-SEP

    Ah. Yes I understand what you mean.
    Like you want none of the visual formatting.

    But won't some of it still show since you use HTML and HTML can also have font and bold etc ?

    In any case. you should inspect how the image refs are in the HTML.
    Since the images cannot be embedded, it's important how the images are referenced.



  • @mrjj so what should I do so image can be visible in .pdf


  • Lifetime Qt Champion

    @UG-SEP

    First find out why its not shown.

    So look in the HTML you get from
    toHtml()

    and see how the IMG ref tag is written.

    It can be its relative path or something that won't work.

    How do you insert the image in the first place ?
    Is it in a resource or do you point to a file some place on your hard drive?



  • no it is not in resource


Log in to reply