4

I have createated a QTextDocument with a table in it. Now I'm trying to render it into PDF format using QPdfWriter (Qt 5.2.1). This is how I do it:

QPdfWriter pdfWriter(output);
QPainter painter(&pdfWriter);
doc->drawContents(&painter);

It works, but the problem is that the table in PDF is really, really tiny. What can I do to scale it up? I mean to scale up the whole document, not just this table, because I plan to add more contents to the document.

Ilya
  • 4,583
  • 4
  • 26
  • 51
Googie
  • 5,742
  • 2
  • 19
  • 31

4 Answers4

2

With current Qt (>= 5.3) you just need to call: QPdfWriter::setResolution(int dpi)

eql
  • 21
  • 2
1

You can use QPdfWriter::setPageSizeMM() or QPdfWriter::setPageSize() to set size of a page. To test this idea you can just add pdfWriter.setPageSize(QPagedPaintDevice::A0); in your code.

Ilya
  • 4,583
  • 4
  • 26
  • 51
  • 1
    But this doesn't scale up the rendered table. It just cuts the page to desired size. What if then I wanted to print it on A4? I mean, I can set page size to 3cm x 3cm and my table looks fine then, but I cannot print on 3x3cm paper. – Googie Jun 23 '14 at 17:22
1

The answer is to use QPainter::scale(), so in my case:

QPdfWriter pdfWriter(output);
QPainter painter(&pdfWriter);
painter.scale(20.0, 20.0);
doc->drawContents(&painter);

This causes painter to paint everything 20 times bigger.

I still don't know why QPdfWriter paints everything so tiny, but the problem can be solved as above.

Googie
  • 5,742
  • 2
  • 19
  • 31
1

I've just been caught out by this too. I figured out what was going on when I called the widthMM() and width() functions of QPdfWriter. The widthMM was about 200 which is right for an A4/Letter page (a sensible default), but the width was returned as about 9600. I called logicalDpiX() and that returned 1200.

So what this indicates is that the logical unit of the QPdfWriter is the 'dot', where there are by default 1200 dots per inch. You therefore need to scale between your own logical units and 'dots'. For instance if your logical unit is a Point then you need to do something like this:

QPdfWriter writer(filename);
int logicalDPIX=writer.logicalDpiX();
const int PointsPerInch=72;

QPainter painter;
painter.begin(&writer)
QTransform t;
float scaling=(float)logicalDPIX/PointsPerInch;  // 16.6
t.scale(scaling,scaling);

// do drawing with painter
painter.end()
painter.setTransform(t);
the_mandrill
  • 29,792
  • 6
  • 64
  • 93