@@ -383,26 +383,32 @@ QByteArray* QgsWMSServer::getPrint( const QString& formatString )
383383 QPainter p ( &generator );
384384 QRectF sourceArea ( 0 , 0 , c->paperWidth (), c->paperHeight () );
385385 QRectF targetArea ( 0 , 0 , width, height );
386- c->render ( &p, targetArea, sourceArea );
386+ if ( c->printAsRaster () ) // embed one raster into the svg
387+ {
388+ QImage* img = printCompositionToImage ( c );
389+ if ( img )
390+ {
391+ p.drawImage ( targetArea, *img, QRectF ( 0 , 0 , img->width (), img->height () ) );
392+ }
393+ delete img;
394+ }
395+ else
396+ {
397+ c->render ( &p, targetArea, sourceArea );
398+ }
387399 p.end ();
388400 }
389401 else if ( formatString.compare ( " png" , Qt::CaseInsensitive ) == 0 || formatString.compare ( " jpg" , Qt::CaseInsensitive ) == 0 )
390402 {
391- int width = ( int )( c->paperWidth () * c->printResolution () / 25.4 ); // width in pixel
392- int height = ( int )( c->paperHeight () * c->printResolution () / 25.4 ); // height in pixel
393- QImage image ( QSize ( width, height ), QImage::Format_ARGB32 );
394- image.setDotsPerMeterX ( c->printResolution () / 25.4 * 1000 );
395- image.setDotsPerMeterY ( c->printResolution () / 25.4 * 1000 );
396- image.fill ( 0 );
397- QPainter p ( &image );
398- QRectF sourceArea ( 0 , 0 , c->paperWidth (), c->paperHeight () );
399- QRectF targetArea ( 0 , 0 , width, height );
400- c->render ( &p, targetArea, sourceArea );
401- p.end ();
402- ba = new QByteArray ();
403- QBuffer buffer ( ba );
404- buffer.open ( QIODevice::WriteOnly );
405- image.save ( &buffer, formatString.toLocal8Bit ().data (), -1 );
403+ QImage* image = printCompositionToImage ( c );
404+ if ( image )
405+ {
406+ ba = new QByteArray ();
407+ QBuffer buffer ( ba );
408+ buffer.open ( QIODevice::WriteOnly );
409+ image->save ( &buffer, formatString.toLocal8Bit ().data (), -1 );
410+ }
411+ delete image;
406412 }
407413 else if ( formatString.compare ( " pdf" , Qt::CaseInsensitive ) == 0 )
408414 {
@@ -419,11 +425,22 @@ QByteArray* QgsWMSServer::getPrint( const QString& formatString )
419425 printer.setOutputFormat ( QPrinter::PdfFormat );
420426 printer.setOutputFileName ( tempFile.fileName () );
421427 printer.setPaperSize ( QSizeF ( c->paperWidth (), c->paperHeight () ), QPrinter::Millimeter );
422-
423428 QRectF paperRectMM = printer.pageRect ( QPrinter::Millimeter );
424429 QRectF paperRectPixel = printer.pageRect ( QPrinter::DevicePixel );
425430 QPainter p ( &printer );
426- c->render ( &p, paperRectPixel, paperRectMM );
431+ if ( c->printAsRaster () ) // embed one raster into the pdf
432+ {
433+ QImage* img = printCompositionToImage ( c );
434+ if ( img )
435+ {
436+ p.drawImage ( paperRectPixel, *img, QRectF ( 0 , 0 , img->width (), img->height () ) );
437+ }
438+ delete img;
439+ }
440+ else // vector pdf
441+ {
442+ c->render ( &p, paperRectPixel, paperRectMM );
443+ }
427444 p.end ();
428445
429446 ba = new QByteArray ();
@@ -438,6 +455,22 @@ QByteArray* QgsWMSServer::getPrint( const QString& formatString )
438455 return ba;
439456}
440457
458+ QImage* QgsWMSServer::printCompositionToImage ( QgsComposition* c ) const
459+ {
460+ int width = ( int )( c->paperWidth () * c->printResolution () / 25.4 ); // width in pixel
461+ int height = ( int )( c->paperHeight () * c->printResolution () / 25.4 ); // height in pixel
462+ QImage* image = new QImage ( QSize ( width, height ), QImage::Format_ARGB32 );
463+ image->setDotsPerMeterX ( c->printResolution () / 25.4 * 1000 );
464+ image->setDotsPerMeterY ( c->printResolution () / 25.4 * 1000 );
465+ image->fill ( 0 );
466+ QPainter p ( image );
467+ QRectF sourceArea ( 0 , 0 , c->paperWidth (), c->paperHeight () );
468+ QRectF targetArea ( 0 , 0 , width, height );
469+ c->render ( &p, targetArea, sourceArea );
470+ p.end ();
471+ return image;
472+ }
473+
441474QImage* QgsWMSServer::getMap ()
442475{
443476 QStringList layersList, stylesList, layerIdList;
0 commit comments