Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Size of PDF files generated with Qt
Qt 6.11 is out! See what's new in the release blog

Size of PDF files generated with Qt

Scheduled Pinned Locked Moved General and Desktop
5 Posts 3 Posters 5.8k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • K Offline
    K Offline
    Kevainxxx
    wrote on last edited by
    #1

    Hello,

    I'm trying to make a little app to create a PDF file from pictures. The problem is the size of the generated PDF, it's big !
    By exemple I generated a PDF file with 93 pictures (about 330 Ko each) and the final size is 81Mo.

    Is there any way to reduce the size ?

    Here is how I make the PDF file :
    @
    QPrinter printer;
    printer.setOutputFormat(QPrinter::PdfFormat);
    printer.setFullPage(true);
    printer.setPageSize(QPrinter::A4);
    printer.setOutputFileName(m_url + m_pdfName);
    printer.setOrientation(QPrinter::Portrait);
    printer.setPrinterName(printer.printerName());
    printer.setResolution(600);

    QPainter paint;
    
    if(!paint.begin(&printer))
        return;
    
    QMatrix matrix;
    matrix.rotate(270);
    QRect rect = paint.viewport();
    
    for (int i = 0; i < m_view->topLevelItemCount(); i++)
    {
        if(i > 0)
        {
            if(!printer.newPage())
                return;
        }
    
        QString imageUrl = m_view->topLevelItem(i)->text(1);
    
        if(imageUrl.isEmpty())
            continue;
    
        QImage image(imageUrl);
        QSize size = image.size();
    
        if(size.width() > size.height())
        {
            image = image.transformed(matrix);
            size = image.size();
        }
    
        size.scale(rect.size(), Qt::KeepAspectRatio);
        int x = rect.x() + ((rect.size().width() - size.width())/2);
        int y = rect.y() + ((rect.size().height() - size.height())/2);
    
        paint.setViewport(x, y, size.width(), size.height());
        paint.setWindow(image.rect());
        paint.drawImage(0, 0, image);
    }
    
    paint.end();
    exit();
    

    @

    Thank you very much,
    Kevin

    1 Reply Last reply
    0
    • A Offline
      A Offline
      alexisdm
      wrote on last edited by
      #2

      QPrinter decompresses the image to raw RGB32 format and compresses the image to JPEG (at 94% quality) if the QJPEG plugin is available or with zlib if it isn't.

      So, just to be sure, that JPEG compression is used, can you test (with qDebug() for example), the result of @QImageWriter::supportedImageFormats().contains("jpeg")@

      -Also, you can use drawPixmap/drawImage to scale the image by specifying the rectangle (QRect) where you want to draw the image as the first parameter rather than scaling the image yourself (the image will be stored with its original size, and scaled by the PDF viewer).-
      Edit: it seems that using viewport and window does the same thing as specifying a QRect to draw the image.

      1 Reply Last reply
      0
      • K Offline
        K Offline
        Kevainxxx
        wrote on last edited by
        #3

        Hello,

        Thank you for your response, the result is "true".
        If I can use the same compression as initially for the image I'm happy :) Do I need to use QImageWriter instead of QImage and set the compression ?

        Thank you,
        Kevin

        1 Reply Last reply
        0
        • A Offline
          A Offline
          alexisdm
          wrote on last edited by
          #4

          You can't choose the compression options, they are hard-coded in the private class that generate the pdf. Since the JPEG plugin is found, you can't improve the PDF size on that point.

          You might have to find an external tool that can recompress the images in the pdf.

          1 Reply Last reply
          0
          • J Offline
            J Offline
            jamess32
            Banned
            wrote on last edited by jamess32
            #5
            This post is deleted!
            1 Reply Last reply
            0

            • Login

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • Users
            • Groups
            • Search
            • Get Qt Extensions
            • Unsolved