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. Qt printer font size
Forum Updated to NodeBB v4.3 + New Features

Qt printer font size

Scheduled Pinned Locked Moved Unsolved General and Desktop
11 Posts 4 Posters 2.4k Views 3 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.
  • N narayanan.krish

    Hello all,

    In my application, I’m printing ad slips using Qprinter and Qwebview. I observed the font size is getting reduced in the print when I set the Paper size. Is this expected ? Any chance to maintain same font size irrespective of the paper size ? Any help is highly appreciated. My printer paper size is 71mm x 73mm.

    Thanks and regards,
    nara

    RatzzR Offline
    RatzzR Offline
    Ratzz
    wrote on last edited by
    #2

    @narayanan.krish said in Qt printer font size:

    Is this expected ?

    Do you set setFullPage to true ?
    http://doc.qt.io/qt-5/qprinter.html#PaperSize-typedef

    --Alles ist gut.

    N 2 Replies Last reply
    1
    • RatzzR Ratzz

      @narayanan.krish said in Qt printer font size:

      Is this expected ?

      Do you set setFullPage to true ?
      http://doc.qt.io/qt-5/qprinter.html#PaperSize-typedef

      N Offline
      N Offline
      narayanan.krish
      wrote on last edited by
      #3

      @Ratzz

      Thanks for the reply. Yes I'm setting setFullPage to true.

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

        Please provide the following values from your QPrinter instance before and after you assign the paper size:

        • pageRect(QPrinter::DevicePixel)
        • pageRect(QPrinter::Millimeter)
        • logicalDpiX
        • logicalDpiY

        Somewhere between these values, it should be possible to solve this mystery.

        N 1 Reply Last reply
        2
        • N narayanan.krish

          Hello all,

          In my application, I’m printing ad slips using Qprinter and Qwebview. I observed the font size is getting reduced in the print when I set the Paper size. Is this expected ? Any chance to maintain same font size irrespective of the paper size ? Any help is highly appreciated. My printer paper size is 71mm x 73mm.

          Thanks and regards,
          nara

          K Offline
          K Offline
          kenchan
          wrote on last edited by
          #5

          @narayanan.krish
          Do you set the font size in pixels or in points?

          N 1 Reply Last reply
          0
          • K kenchan

            @narayanan.krish
            Do you set the font size in pixels or in points?

            N Offline
            N Offline
            narayanan.krish
            wrote on last edited by
            #6

            @kenchan

            I'm not setting font size.

            Let me elaborate the issue I'm facing.

            I'm displaying a html page using QWebEngineView. This html page contains text and image. I need to print this page with QPrinter with custom paper size.- ~ 71mmx73mm. I'm doing following steps before printing :

            1. Converting the html contents to pdf using libpoppler's printTopdf().

            2. Rendering the pdf pages into QImage using lbpoppler's renderToImage().

            Issue I'm facing:

            When I print the page using QPrinter, the font size of the rendered text (as I said before, the html page has both text and image) is small, but the rendered image from html looks alright.

            Attaching the pdf file for your reference.[0_1535209737836_adSlip.pdf](Uploading 100%)

            Thanks,
            Nara

            1 Reply Last reply
            0
            • A Asperamanca

              Please provide the following values from your QPrinter instance before and after you assign the paper size:

              • pageRect(QPrinter::DevicePixel)
              • pageRect(QPrinter::Millimeter)
              • logicalDpiX
              • logicalDpiY

              Somewhere between these values, it should be possible to solve this mystery.

              N Offline
              N Offline
              narayanan.krish
              wrote on last edited by
              #7

              @Asperamanca

              Let me elaborate the issue I'm facing.

              I'm displaying a html page in using QWebEngineView. This html page contains text and image. I need to print this page with QPrinter with custom paper size.- ~ 71mmx73mm. I'm doing following steps before printing :

              1. Converting the html contents to pdf using libpoppler's printTopdf().

              2. Rendering the pdf pages into QImage using lbpoppler's renderToImage().

              Issue I'm facing:

              When I print the page using QPrinter, the font size of the rendered text (as I said before, the html page has both text and image) is small, but the rendered image from html looks alright.

              Thanks,
              Nara

              1 Reply Last reply
              0
              • RatzzR Ratzz

                @narayanan.krish said in Qt printer font size:

                Is this expected ?

                Do you set setFullPage to true ?
                http://doc.qt.io/qt-5/qprinter.html#PaperSize-typedef

                N Offline
                N Offline
                narayanan.krish
                wrote on last edited by
                #8

                @Ratzz

                Let me elaborate the issue I'm facing.

                I'm displaying a html page in using QWebEngineView. This html page contains text and image. I need to print this page with QPrinter with custom paper size.- ~ 71mmx73mm. I'm doing following steps before printing :

                1. Converting the html contents to pdf using libpoppler's printTopdf().

                2. Rendering the pdf pages into QImage using lbpoppler's renderToImage().

                Issue I'm facing:

                When I print the page using QPrinter, the font size of the rendered text (as I said before, the html page has both text and image) is small, but the rendered image from html looks alright.

                Thanks,
                Nara

                K 1 Reply Last reply
                0
                • N narayanan.krish

                  @Ratzz

                  Let me elaborate the issue I'm facing.

                  I'm displaying a html page in using QWebEngineView. This html page contains text and image. I need to print this page with QPrinter with custom paper size.- ~ 71mmx73mm. I'm doing following steps before printing :

                  1. Converting the html contents to pdf using libpoppler's printTopdf().

                  2. Rendering the pdf pages into QImage using lbpoppler's renderToImage().

                  Issue I'm facing:

                  When I print the page using QPrinter, the font size of the rendered text (as I said before, the html page has both text and image) is small, but the rendered image from html looks alright.

                  Thanks,
                  Nara

                  K Offline
                  K Offline
                  kenchan
                  wrote on last edited by
                  #9

                  @narayanan.krish
                  Ok, I see. can you show the code where you do the printer setup and printing please?

                  N 1 Reply Last reply
                  0
                  • K kenchan

                    @narayanan.krish
                    Ok, I see. can you show the code where you do the printer setup and printing please?

                    N Offline
                    N Offline
                    narayanan.krish
                    wrote on last edited by
                    #10

                    @kenchan

                    PFB - the code I'm using for conversion and printing.

                    namespace
                    {
                    const double RealPaperWidth = 63; // in mm
                    const double RealPaperHeight = 65;
                    const double CanvasDpi = 180;
                    const double PaperDpi = 205;
                    const double PaperWidth = RealPaperWidth * PaperDpi / CanvasDpi;
                    const double PaperHeight = RealPaperHeight * PaperDpi / CanvasDpi;

                    const double MmToInches = 0.0393701;
                    const double CanvasScale = MmToInches * PaperDpi;
                    const double CanvasWidth = floor(PaperWidth * MmToInches * CanvasDpi);
                    const double CanvasHeight = floor(PaperHeight * MmToInches * CanvasDpi);
                    }

                    void CupsPrinter::printAdSlips(QWebEngineView *webView, const int &copies,
                    const models::ClientConfig *clientConfig,
                    bool manualPrint, const QString &printerName)
                    {
                    Q_UNUSED(manualPrint)

                    QPrinterInfo printerInfo = clientConfig->dualPrintEnabled() == "ENABLED"
                                                   ? QPrinterInfo::printerInfo(printerName)
                                                   : QPrinterInfo::defaultPrinter();
                    
                    if (webView) {
                        webView->page()->printToPdf([this, printerName, printerInfo,
                                                     copies](const QByteArray &data) {
                    
                            QPrinter printer(printerInfo, QPrinter::HighResolution);
                            printer.setPageMargins(0, 0, 0, 0, QPrinter::Millimeter);
                            printer.setResolution(CanvasDpi);
                            printer.setFullPage(true);
                            printer.setNumCopies(copies);
                    
                            if (!data.size()) {
                                qWarning(
                                    "Failure to print on printer %ls: Print result data is "
                                    "empty.",
                                    qUtf16Printable(printer.printerName()));
                                return;
                            }
                    
                            int standardPaperSize = PaperHeight;
                            // Reading document using Poppler
                            // Poppler is python based library for Qt for dealing with pdf
                            // files.
                            Poppler::Document *pdfDocument =
                                Poppler::Document::loadFromData(data);
                    
                            printer.setPaperSize(QSizeF(PaperWidth, standardPaperSize),
                                                 QPrinter::Millimeter);
                            // Saving standard paper size
                            QRect printerPageRect = printer.pageRect();
                            // Setting actual paper size
                            printer.setPaperSize(QSizeF(PaperWidth, (standardPaperSize *
                                                                     pdfDocument->numPages())),
                                                 QPrinter::Millimeter);
                    
                            int toPage = pdfDocument->numPages();
                            int fromPage = 1;
                    
                            bool ascendingOrder = true;
                            if (printer.pageOrder() == QPrinter::LastPageFirst) {
                                qSwap(fromPage, toPage);
                                ascendingOrder = false;
                            }
                    
                            int pageCopies = 1;
                            int documentCopies = 1;
                            if (!printer.supportsMultipleCopies())
                                documentCopies = printer.copyCount();
                    
                            if (printer.collateCopies()) {
                                pageCopies = documentCopies;
                                documentCopies = 1;
                            }
                    
                            QPainter painter;
                            if (!painter.begin(&printer)) {
                                qWarning(
                                    "Failure to print on printer %ls: Could not open printer "
                                    "for painting.",
                                    qUtf16Printable(printer.printerName()));
                                return;
                            }
                    
                            // Printing logic
                            QRect actualPrintRect = printerPageRect;
                            for (int printedDocuments = 0; printedDocuments < documentCopies;
                                 printedDocuments++) {
                                int currentPageIndex = fromPage;
                                int y = 0;
                                // Print entire document to one page
                                while (true) {
                                    for (int printedPages = 0; printedPages < pageCopies;
                                         printedPages++) {
                                        if (printer.printerState() == QPrinter::Aborted ||
                                            printer.printerState() == QPrinter::Error)
                                            return;
                                        QImage currentImage =
                                            pdfDocument->page(currentPageIndex - 1)
                                                ->renderToImage();
                                        if (currentImage.isNull())
                                            return;
                    
                                        actualPrintRect.setY(y);
                                        actualPrintRect.setRect(0, y, printerPageRect.width(),
                                                                printerPageRect.height());
                                        painter.drawImage(actualPrintRect, currentImage,
                                                          currentImage.rect());
                    
                                        if (printedPages < pageCopies - 1)
                                            printer.newPage();
                    
                                        y = y + (printerPageRect.height());
                                    }
                                    if (currentPageIndex == toPage)
                                        break;
                    
                                    if (ascendingOrder)
                                        currentPageIndex++;
                                    else
                                        currentPageIndex--;
                                }
                                // cut the pae for multiple copies
                                if (printedDocuments < documentCopies - 1)
                                    printer.newPage();
                            }
                            painter.end();
                    
                        });
                    
                    } else {
                        qWarning() << "Warning: failed to open the printer" << m_printerName;
                    }
                    

                    }

                    K 1 Reply Last reply
                    0
                    • N narayanan.krish

                      @kenchan

                      PFB - the code I'm using for conversion and printing.

                      namespace
                      {
                      const double RealPaperWidth = 63; // in mm
                      const double RealPaperHeight = 65;
                      const double CanvasDpi = 180;
                      const double PaperDpi = 205;
                      const double PaperWidth = RealPaperWidth * PaperDpi / CanvasDpi;
                      const double PaperHeight = RealPaperHeight * PaperDpi / CanvasDpi;

                      const double MmToInches = 0.0393701;
                      const double CanvasScale = MmToInches * PaperDpi;
                      const double CanvasWidth = floor(PaperWidth * MmToInches * CanvasDpi);
                      const double CanvasHeight = floor(PaperHeight * MmToInches * CanvasDpi);
                      }

                      void CupsPrinter::printAdSlips(QWebEngineView *webView, const int &copies,
                      const models::ClientConfig *clientConfig,
                      bool manualPrint, const QString &printerName)
                      {
                      Q_UNUSED(manualPrint)

                      QPrinterInfo printerInfo = clientConfig->dualPrintEnabled() == "ENABLED"
                                                     ? QPrinterInfo::printerInfo(printerName)
                                                     : QPrinterInfo::defaultPrinter();
                      
                      if (webView) {
                          webView->page()->printToPdf([this, printerName, printerInfo,
                                                       copies](const QByteArray &data) {
                      
                              QPrinter printer(printerInfo, QPrinter::HighResolution);
                              printer.setPageMargins(0, 0, 0, 0, QPrinter::Millimeter);
                              printer.setResolution(CanvasDpi);
                              printer.setFullPage(true);
                              printer.setNumCopies(copies);
                      
                              if (!data.size()) {
                                  qWarning(
                                      "Failure to print on printer %ls: Print result data is "
                                      "empty.",
                                      qUtf16Printable(printer.printerName()));
                                  return;
                              }
                      
                              int standardPaperSize = PaperHeight;
                              // Reading document using Poppler
                              // Poppler is python based library for Qt for dealing with pdf
                              // files.
                              Poppler::Document *pdfDocument =
                                  Poppler::Document::loadFromData(data);
                      
                              printer.setPaperSize(QSizeF(PaperWidth, standardPaperSize),
                                                   QPrinter::Millimeter);
                              // Saving standard paper size
                              QRect printerPageRect = printer.pageRect();
                              // Setting actual paper size
                              printer.setPaperSize(QSizeF(PaperWidth, (standardPaperSize *
                                                                       pdfDocument->numPages())),
                                                   QPrinter::Millimeter);
                      
                              int toPage = pdfDocument->numPages();
                              int fromPage = 1;
                      
                              bool ascendingOrder = true;
                              if (printer.pageOrder() == QPrinter::LastPageFirst) {
                                  qSwap(fromPage, toPage);
                                  ascendingOrder = false;
                              }
                      
                              int pageCopies = 1;
                              int documentCopies = 1;
                              if (!printer.supportsMultipleCopies())
                                  documentCopies = printer.copyCount();
                      
                              if (printer.collateCopies()) {
                                  pageCopies = documentCopies;
                                  documentCopies = 1;
                              }
                      
                              QPainter painter;
                              if (!painter.begin(&printer)) {
                                  qWarning(
                                      "Failure to print on printer %ls: Could not open printer "
                                      "for painting.",
                                      qUtf16Printable(printer.printerName()));
                                  return;
                              }
                      
                              // Printing logic
                              QRect actualPrintRect = printerPageRect;
                              for (int printedDocuments = 0; printedDocuments < documentCopies;
                                   printedDocuments++) {
                                  int currentPageIndex = fromPage;
                                  int y = 0;
                                  // Print entire document to one page
                                  while (true) {
                                      for (int printedPages = 0; printedPages < pageCopies;
                                           printedPages++) {
                                          if (printer.printerState() == QPrinter::Aborted ||
                                              printer.printerState() == QPrinter::Error)
                                              return;
                                          QImage currentImage =
                                              pdfDocument->page(currentPageIndex - 1)
                                                  ->renderToImage();
                                          if (currentImage.isNull())
                                              return;
                      
                                          actualPrintRect.setY(y);
                                          actualPrintRect.setRect(0, y, printerPageRect.width(),
                                                                  printerPageRect.height());
                                          painter.drawImage(actualPrintRect, currentImage,
                                                            currentImage.rect());
                      
                                          if (printedPages < pageCopies - 1)
                                              printer.newPage();
                      
                                          y = y + (printerPageRect.height());
                                      }
                                      if (currentPageIndex == toPage)
                                          break;
                      
                                      if (ascendingOrder)
                                          currentPageIndex++;
                                      else
                                          currentPageIndex--;
                                  }
                                  // cut the pae for multiple copies
                                  if (printedDocuments < documentCopies - 1)
                                      printer.newPage();
                              }
                              painter.end();
                      
                          });
                      
                      } else {
                          qWarning() << "Warning: failed to open the printer" << m_printerName;
                      }
                      

                      }

                      K Offline
                      K Offline
                      kenchan
                      wrote on last edited by
                      #11

                      @narayanan.krish
                      I know nothing of lbpoppler so if this problem has got anything to do with that I can't help.
                      Looking at you code, which I find to be rather involved and difficult to follow, I see that you have this...

                      QRect printerPageRect = printer.pageRect();
                      

                      and then you set the paper size of the printer.

                      printer.setPaperSize(QSizeF(PaperWidth, (standardPaperSize *
                                                                       pdfDocument->numPages())),
                                                   QPrinter::Millimeter);
                      

                      and later you do this....

                      QRect actualPrintRect = printerPageRect;
                      

                      but the printerPageRect value has not been updated since you changed the paper size, could this not now be the wrong size?
                      In any event, the reason for the output being smaller than expected is most likely to be related to the rectangles you are passing to the painter render function or the resolution of the painter/printer not being what you expect.

                      1 Reply Last reply
                      1

                      • Login

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