Skip to content

Commit efb9971

Browse files
committedSep 27, 2021
Also respect layout dpi when rendering pdf using older deprecated code
paths
1 parent f01022e commit efb9971

File tree

1 file changed

+30
-0
lines changed

1 file changed

+30
-0
lines changed
 

‎src/core/layout/qgslayoutitempicture.cpp

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -384,6 +384,21 @@ void QgsLayoutItemPicture::loadRemotePicture( const QString &url )
384384
if ( reply )
385385
{
386386
QImageReader imageReader( reply );
387+
388+
if ( imageReader.format() == "pdf" )
389+
{
390+
// special handling for this format -- we need to pass the desired target size onto the image reader
391+
// so that it can correctly render the (vector) pdf content at the desired dpi. Otherwise it returns
392+
// a very low resolution image (the driver assumes points == pixels!)
393+
// For other image formats, we read the original image size only and defer resampling to later in this
394+
// function. That gives us more control over the resampling method used.
395+
396+
// driver assumes points == pixels, so driver image size is reported assuming 72 dpi.
397+
const QSize sizeAt72Dpi = imageReader.size();
398+
const QSize sizeAtTargetDpi = sizeAt72Dpi * mLayout->renderContext().dpi() / 72;
399+
imageReader.setScaledSize( sizeAtTargetDpi );
400+
}
401+
387402
mImage = imageReader.read();
388403
mMode = FormatRaster;
389404
}
@@ -434,6 +449,21 @@ void QgsLayoutItemPicture::loadLocalPicture( const QString &path )
434449
{
435450
//try to open raster with QImageReader
436451
QImageReader imageReader( pic.fileName() );
452+
453+
if ( imageReader.format() == "pdf" )
454+
{
455+
// special handling for this format -- we need to pass the desired target size onto the image reader
456+
// so that it can correctly render the (vector) pdf content at the desired dpi. Otherwise it returns
457+
// a very low resolution image (the driver assumes points == pixels!)
458+
// For other image formats, we read the original image size only and defer resampling to later in this
459+
// function. That gives us more control over the resampling method used.
460+
461+
// driver assumes points == pixels, so driver image size is reported assuming 72 dpi.
462+
const QSize sizeAt72Dpi = imageReader.size();
463+
const QSize sizeAtTargetDpi = sizeAt72Dpi * mLayout->renderContext().dpi() / 72;
464+
imageReader.setScaledSize( sizeAtTargetDpi );
465+
}
466+
437467
if ( imageReader.read( &mImage ) )
438468
{
439469
mMode = FormatRaster;

0 commit comments

Comments
 (0)
Please sign in to comment.