PDA

View Full Version : [SOLVED] Print to pdf



Morea
7th April 2006, 09:58
I have made som drawings in a mainwindow and now I would like to export it to a pdf file or postscript. How can I do that?

jpn
7th April 2006, 10:21
Try painting on a QPrinter (whose output format is set to QPrinter::PdfFormat).

EDIT: an example:


QPrinter printer(QPrinter::HighResolution);
printer.setOutputFormat(QPrinter::PdfFormat);
printer.setOutputFileName("test.pdf");
QPainter painter;
painter.begin(&printer);
// draw the content
// painter.draw..(..);
painter.end();

mickey
7th April 2006, 11:01
..and if I want print into a pdf the content of myGLWidget(it draw with paintGL())?

wysota
7th April 2006, 11:06
Save it into QImage or QPixmap using appropriate methods from QGLWidget and then print as usual.

mickey
7th April 2006, 13:50
thanks, to print directly my scene do I need insert an istruction like :


QImage qi=qp.convertToImage();
qi.save(base_file + ".png","PNG");
painter.begin(&printer);
/////painter.draw(qi);??????????????? Is it there?
painter.end();
because my printer starts but print nothing...

Uwe
7th April 2006, 13:55
Save it into QImage or QPixmap using appropriate methods from QGLWidget and then print as usual.

PDF is a scalable format, what gets lost doing it via a pixmap/image.
If vector data has to be painted, one should always try to render the PDF documents with QPainter using drawing primitives like lines, rects ... . Of course the geometry of these primitives has to be in qreals.

HTH,
Uwe

mickey
7th April 2006, 14:55
sorry but my question isn't relative to pdf format; I save the content of myGLWidget in a QImage qi; now I'd like to print it! the printer starts but the sheet is white. What am I missing? Thanks

jpn
7th April 2006, 15:03
Do you paint anything? See QPainter::drawImage() (http://doc.trolltech.com/3.3/qpainter.html#drawImage).
For example:

painter.drawImage(0,0,qi);

mickey
7th April 2006, 19:12
I tried this but print nothing again....


qi.save(base_file + ".png","PNG");
QPrinter printer(QPrinter::HighResolution);
printer.setFullPage( TRUE );
printer.setPageSize(QPrinter::A4);
if ( printer.setup(this) ) { // printer dialog
statusBar()->message( "Printing..." );
QPainter p;
QPainter painter;
painter.drawImage(0,0,qi, 10,10, 20,20);
if( !painter.begin( &printer ) ) { // paint on printer
statusBar()->message( "Printing aborted", 2000 );
return;
}

painter.end();
statusBar()->message("");
}

wysota
7th April 2006, 21:34
PDF is a scalable format, what gets lost doing it via a pixmap/image.
If vector data has to be painted, one should always try to render the PDF documents with QPainter using drawing primitives like lines, rects ... . Of course the geometry of these primitives has to be in qreals.

HTH,
Uwe

The point is this is an OpenGL widget and I doubt you'll be able to use QPainter directly with it (meaning without GL context). IMHO you have to convert it to a bitmap first.

mickey
8th April 2006, 11:12
if (printer.setup(this)) {
QPainter painter(&printer);
QRect rect = painter.viewport();
QSize size = qi.size();
size.scale(rect.size(), QSize::ScaleMin);
painter.setViewport(rect.x(), rect.y(),
size.width(), size.height());
painter.setWindow(qi.rect());
painter.drawImage(0, 0, qi);
}