Link to home
Start Free TrialLog in
Avatar of bejhan
bejhan

asked on

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
Avatar of sarabande
sarabande
Flag of Luxembourg image

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
Avatar of bejhan
bejhan

ASKER

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.
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
Avatar of bejhan

ASKER

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?
ASKER CERTIFIED SOLUTION
Avatar of sarabande
sarabande
Flag of Luxembourg image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of bejhan

ASKER

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
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
Avatar of bejhan

ASKER

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.
Avatar of bejhan

ASKER

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

Avatar of bejhan

ASKER

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.
Avatar of bejhan

ASKER

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.
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
Avatar of bejhan

ASKER

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

Avatar of bejhan

ASKER

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