Outputting SVG as PDF with Qt (QSvgRenderer, QPainter, QPrinter)

I am attempting to use QSvgRenderer, QPainter, and QPrinter to output SVG as PDF.

I was expecting that the SVG would be placed on the paper and potentially be cut off if too large.
However, it seems that QPainter is just stretching the SVG image to fill the page size, destroying its aspect ratio.
QSvgRenderer renderer( data.toUtf8() );

QPrinter printer;
printer.setOutputFileName( filePathWithExtension );		
printer.setOutputFormat( QPrinter::PdfFormat );		
printer.setPaperSize( QPrinter::Letter );
printer.setOrientation( QPrinter::Landscape );

QPainter painter( &printer );

renderer.render( &painter );

painter.end();

Open in new window


I was hoping to simply scale my SVG image to fit within the dimensions of the selected paper size.
But the scaled output is not as expected.
QSvgRenderer renderer( data.toUtf8() );

QPrinter printer;
printer.setOutputFileName( filePathWithExtension );		
printer.setOutputFormat( QPrinter::PdfFormat );		
printer.setPaperSize( QPrinter::Letter );
printer.setOrientation( QPrinter::Landscape );

double paperWidth = 11.0 * static_cast< double >( printer.resolution() );
double paperHeight = 8.5 * static_cast< double >( printer.resolution() );

double widthScale = paperWidth / static_cast< double >( width );
double heightScale = paperHeight / static_cast< double >( height );
double minScale = qMin( widthScale, heightScale );
		
QPainter painter( &printer );
painter.scale( minScale, minScale );

renderer.render( &painter );

painter.end();

Open in new window


I was easily able to output SVG as PNG by sizing the QImage to match the SVG size.
However, PDF output is a more complicated due to QPrinter being restricted to a paper size.

Any help is appreciated.
no-scale.PNG
with-scale.PNG
svg.PNG
LVL 1
bejhanAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

sarabandeCommented:
I was expecting that the SVG would be placed on the paper and potentially be cut off if too large.
how should the software know where to cut your image?

see http://www.digicamguides.com/print/aspect-ratio.html for the basic principles that need to be considered for a different aspect ratio of input and output.

most paint programs provide a manual "crop" tool, but I failed into finding any automatic tool.

Sara
0
bejhanAuthor Commented:
how should the software know where to cut your image?
The software shouldn't know where to crop the image, but I would expect that it would crop arbitrarily rather than stretching the image to fit the paper.

Regardless, my question is not about cropping.
I need to know how to resize the image such that it can fit on the paper, while constraining the proportions of the original image, and leaving the unused space blank, rather than stretching the image to fit.
0
sarabandeCommented:
I found the following about scaling and coordinate transformation:

Normally, the QPainter operates on the associated device's own coordinate system, but it also has good support for coordinate transformations.

The default coordinate system of a paint device has its origin at the top-left corner. The x values increase to the right and the y values increase downwards. You can scale the coordinate system by a given offset using the QPainter::scale() function, you can rotate it clockwise using the QPainter::rotate() function and you can translate it (i.e. adding a given offset to the points) using the QPainter::translate() function. You can also twist the coordinate system around the origin (called shearing) using the QPainter::shear() function.

you find the statement in http://www.bogotobogo.com/Qt/Qt5_QPainter_Transformation.php.

from that I would say that the scale call you made is not suitable to perform the transformation you intended, instead you may calculate the dimensions of the new image including the right or bottom space which is caused by the difference of the aspect ratio. then create a new image with space block and use that as input for rendering.

Sara
0
Get expert help—faster!

Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.

bejhanAuthor Commented:
instead you may calculate the dimensions of the new image including the right or bottom space which is caused by the difference of the aspect ratio. then create a new image with space block and use that as input for rendering.

I believe the calculations are as follows:
double paperWidth = 11.0 * static_cast< double >( printer.resolution() );
double paperHeight = 8.5 * static_cast< double >( printer.resolution() );

double widthScale = paperWidth / static_cast< double >( width );
double heightScale = paperHeight / static_cast< double >( height );
double minScale = qMin( widthScale, heightScale );
	
double scaledWidth = static_cast< double >( width ) * minScale;
double scaledHeight = static_cast< double >( height ) * minScale;

double blankWidth = scaledWidth - static_cast< double >( width );
double blankHeight = scaledHeight - static_cast< double >( width );

Open in new window


I am unsure of the API to actually render to an image of this scaled size as well as how to then apply that image to the QPrinter. Do you know of the classes I can use to do this work?
0
sarabandeCommented:
i didn't use SVG format myself and it is some time ago, when i lastly made qt programming.

you should try to create a copy of the SVG QImage but either the width or height increased such that the aspect ratio of the new image is the same as with the pdf print page. the points of the blank part should be made either white or transaparent.

if you achieved that, you should use the new image as input for the pdf with no scaling.

i will try to find some code how to do this.

Sara
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
bejhanAuthor Commented:
QImage has a scaled() method which allows specifying to keep the aspect ratio of the original image, i.e. the image will be scaled and unused space will be blank.

I attempted to scale the image to the paper size.
QPrinter printer;
printer.setOutputFileName( filePathWithExtension );		
printer.setOutputFormat( QPrinter::PdfFormat );		
printer.setPaperSize( QPrinter::Letter );
printer.setOrientation( QPrinter::Landscape );

QSvgRenderer renderer( data.toUtf8() );
QImage image( width, height, QImage::Format_ARGB32_Premultiplied );
QPainter painter( &image );

renderer.render( &painter );
image.setText( "Description", description );

double paperWidth = 11.0 * static_cast< double >( printer.resolution() );
double paperHeight = 8.5 * static_cast< double >( printer.resolution() );

QImage scaledImage = image.scaled( paperWidth, paperHeight, Qt::KeepAspectRatio );

Open in new window


However, the scaled image looks very grainy.
original.png
scaled.png
0
sarabandeCommented:
However, the scaled image looks very grainy.
I would assume that is because the scaled() doesn't "add" the space part to the image (as I suggested) but let one side constant what reduces the resolution for the other direction.

so, I still think that if you simply create an image with the wished aspect ratio which could be covered by the original image and then copy the pixel data from one to the other without scaling, you will have a better result.

Sara
0
bejhanAuthor Commented:
Actually it seems that QImage::scaled() has a Qt::TransformationMode transformMode parameter, which defaults to Qt::FastTransformation.

Changing this to Qt::SmoothTransformation removes the grainy look.
0
bejhanAuthor Commented:
My finally solution is as follows:
QSvgRenderer renderer( data.toUtf8() );
QImage image( width, height, QImage::Format_ARGB32_Premultiplied );
QPainter svgPainter( &image );

renderer.render( &svgPainter );

QPrinter printer;
printer.setOutputFileName( filePathWithExtension );		
printer.setOutputFormat( QPrinter::PdfFormat );		
printer.setPaperSize( QPrinter::Letter );
printer.setOrientation( QPrinter::Landscape );		
printer.setFullPage( true );

double paperWidth = 11.0 * static_cast< double >( printer.resolution() );
double paperHeight = 8.5 * static_cast< double >( printer.resolution() );

QImage scaledImage = image.scaled( paperWidth, paperHeight, Qt::KeepAspectRatio, Qt::SmoothTransformation );
QPainter pdfPainter( &printer );
pdfPainter.drawImage( 0, 0, scaledImage );
pdfPainter.end();

Open in new window

0
bejhanAuthor Commented:
You pointed in my the right direction but did not provide any API or code examples.
For this reason, I am only awarding you B.
0
bejhanAuthor Commented:
Note, the above solution generates a cloudy PDF, change
QPrinter printer;

Open in new window

to
QPrinter printer( QPrinter::HighResolution );

Open in new window

to correct this issue.
0
sarabandeCommented:
that is quite ok. I did something with qt but no graphics. so the main work to find a solution was on your side.

I meddled in the question not because I knew so much better but because of the scale statements of your original code which look wrong.  

Sara
0
bejhanAuthor Commented:
For anyone reading this in the future, the scaling dimensions can be determined using QPrinter instead of constants, giving more reliable results.

Change the following:
double paperWidth = 11.0 * static_cast< double >( printer.resolution() );
double paperHeight = 8.5 * static_cast< double >( printer.resolution() );

QImage scaledImage = image.scaled( paperWidth, paperHeight, Qt::KeepAspectRatio, Qt::SmoothTransformation );

Open in new window


To the following:
QSizeF paperSize = printer.paperSize( QPrinter::DevicePixel );

QImage scaledImage = image.scaled( paperSize.width(), paperSize.height(), Qt::KeepAspectRatio, Qt::SmoothTransformation );

Open in new window

0
bejhanAuthor Commented:
Note, instead of using high resolution, which results in a huge file size
QPrinter printer( QPrinter::HighResolution );

Open in new window

set a resolution of your choosing
QPrinter printer;
printer.setResolution( 300 ); //dpi

Open in new window

0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C++

From novice to tech pro — start learning today.