Uploaded image for project: 'Qt'
  1. Qt
  2. QTBUG-72939

QPrinter gives different font spacing for PDF and physical printer

    XMLWordPrintable

Details

    • Windows

    Description

      I'm using QPrinter to fill forms with text and print them on the printer.

      For tests I generate pdf files to avoid paper and ink waste.
      I figured out that font spacings are different for PDF's and factical prints.

      Results:

      **PDF: Its fine
      pdf

      **Print from printer:
      printer

      Code:

       

      #include <QApplication>
      #include <QPrinter>
      #include <QPrinterInfo>
      #include <QPainter>
      #include <QFontDatabase>
      
      void printForm(bool pdf);
      QString g_fontFamily = "";
      
      int main(int argc, char *argv[])
      {
          QApplication a(argc, argv);    
          int id = QFontDatabase::addApplicationFont(":/IBMPlexMono-Medium.ttf");
          g_fontFamily = QFontDatabase::applicationFontFamilies(id).at(0);  
          printForm(true); //pdf
          printForm(false); //physical printer    
          return a.exec();
      }
      void printForm(bool pdf)
      {
          //setup printer
          QPrinter *printer;
          if(pdf)
          {
              printer = new QPrinter(QPrinter::ScreenResolution);
              printer->setOutputFormat(QPrinter::PdfFormat);
              printer->setOutputFileName("./regular.pdf");
          }
          else
          {
              printer = new QPrinter(QPrinterInfo::defaultPrinter(), QPrinter::ScreenResolution);
              printer->setOutputFormat(QPrinter::NativeFormat);
          }
          printer->setOrientation(QPrinter::Orientation::Landscape);
          QPageSize customSize(QSizeF(105.0, 157.76), QPageSize::Millimeter, "", QPageSize::ExactMatch);
          printer->setPageSize(customSize);    //draw background
          QImage img(":/regular_transfer_form");
          QPainter painter(printer);
          painter.drawImage(QRect(0,0, printer->width(), printer->height()), img, QRect(0,0, img.width(), img.height()));    //draw text
          painter.setPen(Qt::black);
          QFont font(g_fontFamily, 11);
          font.setStyleHint(QFont::TypeWriter);
          font.setLetterSpacing(QFont::PercentageSpacing, 212);    
          painter.setFont(font);    
          QTextOption textOption(Qt::AlignVCenter | Qt::AlignLeft);
          textOption.setWrapMode(QTextOption::NoWrap);    
          QString text = "Lorem ipsum dolor sit amet, consectetur adipiscing elit";    
          QRectF relativeArea(0.05838, 0.0783, 0.8971, 0.04583); //coords relative to background    
          QRectF areaOnPaper(relativeArea.x() * printer->width(),
                                      relativeArea.y() * printer->height(),
                                      relativeArea.width() * printer->width(),
                                      relativeArea.height() * printer->height()
                                      );    
          painter.drawText(areaOnPaper, text, textOption); //draw 1-st line wit percentage Spacing    //set spacing to absolute
          font.setLetterSpacing(QFont::AbsoluteSpacing, 10);
          painter.setFont(font);    
          areaOnPaper.setRect(areaOnPaper.x(), areaOnPaper.y() + 28, areaOnPaper.width(), areaOnPaper.height());
          painter.drawText(areaOnPaper, text, textOption); //draw 2-nd line wit absolute Spacing
      }
      

       

       

      As you can see - on the paper the letters are not in boxes, but the code is identical for both.

      I tried with QFont::PercentageSpacing (1-st line) and QFont::AbsoluteSpacing (2nd line) due to Note in  QPrinter documentation No difference.

       

      Full example with font and background image attached.

       

      Attachments

        No reviews matched the request. Check your Options in the drop-down menu of this sections header.

        Activity

          People

            esabraha Eskil Abrahamsen Blomfeldt
            maciej_gurgul Maciej Gurgul
            Votes:
            2 Vote for this issue
            Watchers:
            7 Start watching this issue

            Dates

              Created:
              Updated:

              Gerrit Reviews

                There are no open Gerrit changes