@@ -383,26 +383,32 @@ QByteArray* QgsWMSServer::getPrint( const QString& formatString )
383
383
QPainter p ( &generator );
384
384
QRectF sourceArea ( 0 , 0 , c->paperWidth (), c->paperHeight () );
385
385
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
+ }
387
399
p.end ();
388
400
}
389
401
else if ( formatString.compare ( " png" , Qt::CaseInsensitive ) == 0 || formatString.compare ( " jpg" , Qt::CaseInsensitive ) == 0 )
390
402
{
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;
406
412
}
407
413
else if ( formatString.compare ( " pdf" , Qt::CaseInsensitive ) == 0 )
408
414
{
@@ -419,11 +425,22 @@ QByteArray* QgsWMSServer::getPrint( const QString& formatString )
419
425
printer.setOutputFormat ( QPrinter::PdfFormat );
420
426
printer.setOutputFileName ( tempFile.fileName () );
421
427
printer.setPaperSize ( QSizeF ( c->paperWidth (), c->paperHeight () ), QPrinter::Millimeter );
422
-
423
428
QRectF paperRectMM = printer.pageRect ( QPrinter::Millimeter );
424
429
QRectF paperRectPixel = printer.pageRect ( QPrinter::DevicePixel );
425
430
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
+ }
427
444
p.end ();
428
445
429
446
ba = new QByteArray ();
@@ -438,6 +455,22 @@ QByteArray* QgsWMSServer::getPrint( const QString& formatString )
438
455
return ba;
439
456
}
440
457
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
+
441
474
QImage* QgsWMSServer::getMap ()
442
475
{
443
476
QStringList layersList, stylesList, layerIdList;
0 commit comments