Important: Please read the Qt Code of Conduct -

Formal letter with QTextDocument

  • Hi everybody,

    this time I would like a more general question, conceptual, if you like.

    How would you implement a formal letter with header and fooder lines above /below everything on every page, logo on the first page top right, address field at the right positon etc.

    The goal would be to have pdf or odt files generated like a serial letter.
    The issue I see is, that it is hard, if not impossible, to position stuff on a page like with coordinates.
    I implemented the letter as a big table, but the code is long and does not look nice. It is also hard to maintain and not very flexible.
    Do you have other proposals?

  • Lifetime Qt Champion

    One option is to use a report generator like

    Other option is to use a SVG for the "fixed" stuff and
    paint the letter using QPainter.

  • @mrjj said in Formal letter with QTextDocument:

    SVG fo

    Thank you for your reply. I will have a look at the report generator. My first guess would be that serial letters are a kind of miss use - but however ;) if it works it would be fine.

    What exactly do you mean with "svg for the fixed stuff" ?

  • Lifetime Qt Champion

    Well a serial letter is not a report as such but its still just a layout which the report generator makes it easy to define.

    If you use inscape to make a template of the letter's layout and save as an SVG file you can use this file to paint first to a page using QPainter.
    SVG is vector and will scale to page size with no reduced quality,
    You then first paint the svg to a page with QPainter and then
    paint the text on top of this. Possible using a QTextDocument if you need rich text.
    If you tag elements in InkScape with an ID you know, you can get its area with
    That makes it possible to have text place holders in the SVG file that you can use to know where to paint
    the text. But if its a very simply layout, this might be overkill :)

  • Thanks again!

    I got your point about SVG now! That would be good for the logo - right. And maybe the page header and footer. It would probably not work for the address field, which also has to have the fixed position about 6cm from the top, next to the left border or so.
    Anyhow: Seems I need to get more familiar with the QPainter ...

    Meanwhile I found QPrinterEasy by Eric Meaker. My first impression is that he combines QTextDocument and QPrinter to format a document with header, footer and watermark. When I am deeper in his code I think I should be able to get my serial letter done.

    Best regards

  • Lifetime Qt Champion

    Well the SVG way is most useful if you got a very detailed page and you don't have to adjust positions
    according to content size.
    Anyhow, the QPrinterEasy sounds exactly what you want so I hope it will just compile in newer Qt versions.
    Good luck.

Log in to reply