PDA

View Full Version : Form as PDF



premroxx
3rd December 2014, 22:48
I want to create a form that the user fills in and when he clicks save. it saves in the Pdf format(Letter size)

1) What are the dimension of the gui/form to fit the Letter format(81/2 by 11 inches)
2) What method do I use to print to pdf

The following is what I have so far. The issues I have with is that it prints completely out of proportion. I have a feeling there is better way to print it to pdf



QPixmap pixmap;
pixmap=QPixmap::grabWindow(ui->centralWidget->winId());

QPrinter printer;
printer.setOrientation(QPrinter::Portrait);
printer.setPageSize(QPrinter::A4);
printer.setOutputFormat(QPrinter::PdfFormat);

QString fileName = QFileDialog::getSaveFileName(this, tr("Save File"),"untitled",tr("PDF Document (*.pdf)"));
printer.setOutputFormat(QPrinter::PdfFormat);
printer.setOutputFileName(fileName);

QPainter painter(&printer);
QRect rect=painter.viewport();
QSize size=pixmap.size();

size.scale(rect.size(), Qt::KeepAspectRatio);
painter.setViewport(rect.x(), rect.y(), size.width(), size.height());
painter.setWindow(pixmap.rect());

painter.drawPixmap(0,0,pixmap);

ChrisW67
4th December 2014, 20:20
The typical way to get a good looking PDF output is to draw a form and its content using QPrinter or QPdfWriter. Trying to get a screen capture to fill a whole page by scaling will almost certainly give an ugly result

premroxx
5th December 2014, 22:09
Thats exactly what i'm trying to accomplish. So, what should be form size so its 1:1 scale as an A4 sheet? the QpdfWriter also seems to use the paint method from wha i've seen wouldn't that give the same output as well??

anda_skoa
6th December 2014, 09:49
The size of an A4 page is standardized.
Given any DPI resolution that will allow to calculate the size in pixels.

QPageSize can easily do that.

Cheers,
_

premroxx
7th December 2014, 22:21
But how would that work for different computers with different DPI ? What would the form size be?

anda_skoa
7th December 2014, 23:43
But how would that work for different computers with different DPI ? What would the form size be?

It really doesn't matter at which DPI you generate/print, i.e. what you pass to QPdfWriter::setResolution(), the calculation for pixels is always done the same way.

If you don't want to handles this as dynamically just always use the same DPI value.

Cheers,
_

ChrisW67
8th December 2014, 20:57
An A4 sheet at 300dpi (a typical resolution for a printer) is something like 2480x3500 pixels. Any on screen form that size is likely to be unusable. So, you need to pick a size (as big as possible) and scale the pixmap to fit the page. You will not have a good result a lot of the time.

At 72dpi the dimensions are 595x842 and you could conceivably have a screen form that size on some devices. The output will stil be poor for printing but at least your desire for 1:1 could be met.

The typical way to get a good looking PDF output is to draw a form and its content using QPrinter or QPdfWriter. By "draw" I mean render lines, text and small images specifcally sized for print to make a form that contains the data. I do not mean draw a screen captured raster image on the page.