PDA

View Full Version : QPainting to an eps file



netterfield
11th August 2009, 17:25
In my app, we are able to export to bitmap graphics files with QImage, and with qprinter, to export proper PDF and PS files.

However, we also need to be able to export to encapsulated postscript (which is just a ps file with bounding box info). Using qimage produces a bitmap, which isn't what we want - we need a proper postscript file with vector lines and proper fonts.

In the past, we've exported to a regular .ps file, edited it to insert the bounding box info, and renamed it. This really sounds like a hack. Is there a better way to do this?

qwertyone
12th August 2009, 01:21
Hello

I just did something similar: I draw an image and some markings into a pdf file. If you look at the generated pdf file, you can see that it generates vector graphics.

Here is a fragment of the code:


// Setup printer
QPrinter printer(QPrinter::HighResolution);
printer.setOutputFormat(QPrinter::PdfFormat);
printer.setOutputFileName(pdfFileName);
printer.setFullPage(true);
printer.setPageSize(QPrinter::Custom);
printer.setPaperSize(QSizeF(pdfWidth,pdfHeight),QP rinter::Millimeter);
printer.setPageMargins(0,0,0,0,QPrinter::Millimete r);

// Get the painting context from the printer
QPainter painter(&printer); // Destructing painter writes the pdf file

// Set the scale, so we can draw in millimeter
double scale = printer.width()/pdfWidth;
painter.setTransform(QTransform(scale,0,0, 0,scale,0, 0,0,1.0));

// Draw the image
QRectF src(0,0,image.width(),image.height());
QRectF dst(imageOffsetX,imageOffsetY,imageWidth,imageHeig ht);
painter.drawImage(dst,image,src);

// Draw the markings
QPen pen;
pen.setWidthF(m_lineWidth);
pen.setColor(Qt::red);
QVector<qreal> dashes;
dashes << m_solidDashLength/m_lineWidth << m_blankDashLength/m_lineWidth;
pen.setDashPattern(dashes);
painter.setPen(pen);
painter.drawRect(QRectF(m_pdfMargin-m_lineWidth/2,m_pdfMargin-m_lineWidth/2,productionWidth+m_lineWidth,productionHeight+m_l ineWidth));

netterfield
13th August 2009, 03:29
Yes... painting to a QPrinter works perfectly, and does what I want for postscript and pdf files.

The question is, how can I make an *encapsulated* postscript file, other than by printing to a postscript file, and then editing it? Or is that the only way (I'm guessing that it is...:( )

http://en.wikipedia.org/wiki/Encapsulated_PostScript

wysota
13th August 2009, 09:34
I think it is - Qt doesn't support EPS natively.

Piskvorkar
17th September 2009, 16:56
Hi!
I have small question. By this way of generating postscript file, is it realy vector format postscript file or is it some sort of bitmap wrapped inside generated postscript file. I have similar problem with EPS and I rather use handly maked export to EPS :-). Do you think it's realy so hard to implement EPS support, even if it needs only Bounding box to define? :-).
Is here some way how to easy reimplment Qt provided printer by own and use it in my program?
Thanks for replay.

enno
11th October 2009, 22:07
Qt 4.5.3 (and maybe 4.4) solved this problem by the including a new overloaded setPaperSize function in the QPrinter class:

printer.setPaperSize(QSizeF(80, 80), QPrinter::Millimeter);

The paperSize code gets set to Unknown/user defined but when printing to a file the size set above is set as the bounding box for the eps file.

By the way the output file is proper PostScript, not an encapsulated bitmap.

cafu
13th October 2009, 13:45
Thanks this is exactly what i was looking for

Piskvorkar
13th November 2009, 09:31
thanks, it works realy prety fine :-)

dexli
6th April 2011, 23:17
Qt 4.5.3 (and maybe 4.4) solved this problem by the including a new overloaded setPaperSize function in the QPrinter class:

printer.setPaperSize(QSizeF(80, 80), QPrinter::Millimeter);

The paperSize code gets set to Unknown/user defined but when printing to a file the size set above is set as the bounding box for the eps file.


Do I have to provide the code which sets the bounding box or is it done simply by calling the above code ??



By the way the output file is proper PostScript, not an encapsulated bitmap.

:confused: But without the ps showpage command and a bounding box ??

Have a nice day
dexli