Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

Qt printer font size



  • 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



  • @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



  • @Ratzz

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



  • 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.



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



  • @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



  • @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



  • @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



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



  • @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;
    }
    

    }



  • @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.


Log in to reply