Technology Sharing

Printing a document from a QWebEnginePage

2024-07-12

한어Русский языкEnglishFrançaisIndonesianSanskrit日本語DeutschPortuguêsΕλληνικάespañolItalianoSuomalainenLatina

QWebEnginePage Is a class in the Qt WebEngine module that provides an interface for processing web page content. If you want to printQWebEnginePage You can use the content inQPrinter andQPrintDialog class to implement it.

The project is configured with the Qt WebEngine module and includes the corresponding configuration in your .pro file:

QT += webenginewidgets

The implementation code is as follows, reference

printme « webenginewidgets « examples - qt/qtwebengine.git - Qt WebEngine

main.cpp

  1. #include "printhandler.h"
  2. #include <QApplication>
  3. #include <QShortcut>
  4. #include <QWebEngineView>
  5. int main(int argc, char *argv[])
  6. {
  7. QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
  8. QApplication app(argc, argv);
  9. view.setUrl(QUrl(QStringLiteral("qrc:/index.html")));
  10. view.resize(1024, 750);
  11. view.show();
  12. PrintHandler handler;
  13. handler.setPage(view.page());
  14. auto printPreviewShortCut = new QShortcut(QKeySequence(Qt::CTRL + Qt::Key_P), &view);
  15. auto printShortCut = new QShortcut(QKeySequence(Qt::CTRL + Qt::SHIFT + Qt::Key_P), &view);
  16. QObject::connect(printPreviewShortCut, &QShortcut::activated, &handler, &PrintHandler::printPreview);
  17. QObject::connect(printShortCut, &QShortcut::activated, &handler, &PrintHandler::print);
  18. return app.exec();
  19. }

printhandler.h

  1. #ifndef PRINTHANDLER_H
  2. #define PRINTHANDLER_H
  3. #include <qapplication.h>
  4. #include <QObject>
  5. #include <QWebEngineView>
  6. #include <QPrinter>
  7. #include <QPrintPreviewDialog>
  8. #include <QEventLoop>
  9. #include <QPrintDialog>
  10. #include <QWidget>
  11. #include <QMainWindow>
  12. #include <QPrinterInfo>
  13. #include "CustomWebEnginePage.h"
  14. #include <qapplication.h>
  15. #include <qtimer.h>
  16. class PrintHandler : public QObject
  17. {
  18. Q_OBJECT
  19. public:
  20. explicit PrintHandler(QString printName, QObject *parent = nullptr);
  21. signals:
  22. public slots:
  23. public:
  24. void setPrintUrl(const QString& url);
  25. void setPrintName(const QString& name);
  26. void printPreview();
  27. void printDocument(QPrinter *printer);
  28. void print();
  29. private:
  30. //void print();
  31. private:
  32. QString mPrinterName;
  33. CustomWebEnginePage* mPage;
  34. QString mUrl;
  35. bool mInPrintPreview;
  36. QTimer* waitTimer;
  37. };
  38. #endif // PRINTHANDLER_H

printhandler.cpp

  1. #include "printhandler.h"
  2. #include <QDebug>
  3. PrintHandler::PrintHandler(QString printName,QObject *parent) : mPrinterName(printName),QObject(parent)
  4. {
  5. mInPrintPreview=false;
  6. mPage = new CustomWebEnginePage();
  7. waitTimer = new QTimer();
  8. waitTimer->setInterval(500);
  9. waitTimer->setSingleShot(true);
  10. connect(waitTimer, &QTimer::timeout, this, &PrintHandler::print);
  11. connect(mPage, &CustomWebEnginePage::loadFinished, waitTimer, static_cast<void(QTimer::*)()>(&QTimer::start));
  12. }
  13. void PrintHandler::setPrintUrl(const QString& url)
  14. {
  15. mUrl = url;
  16. mPage->setUrl(url);
  17. }
  18. void PrintHandler::printPreview()
  19. {
  20. if (!mPage)
  21. return;
  22. if (mInPrintPreview)
  23. return;
  24. mInPrintPreview = true;
  25. QPrinter printer;
  26. QPrintPreviewDialog preview(&printer, mPage->view());
  27. connect(&preview, &QPrintPreviewDialog::paintRequested,
  28. this, &PrintHandler::printDocument);
  29. preview.exec();
  30. mInPrintPreview = false;
  31. }
  32. void PrintHandler::printDocument(QPrinter *printer)
  33. {
  34. QEventLoop loop;
  35. bool result;
  36. auto printPreview = [&](bool success) { result = success; loop.quit(); };
  37. mPage->print(printer, std::move(printPreview));
  38. loop.exec();
  39. if (!result) {
  40. QPainter painter;
  41. if (painter.begin(printer)) {
  42. QFont font = painter.font();
  43. font.setPixelSize(15);
  44. painter.setFont(font);
  45. painter.drawText(QPointF(10,25),QStringLiteral("Could not generate print preview."));
  46. painter.end();
  47. }
  48. }
  49. }
  50. void PrintHandler::setPrintName(const QString& name)
  51. {
  52. mPrinterName = name;
  53. }
  54. void PrintHandler::print()
  55. {
  56. QStringList printerNameList=QPrinterInfo::availablePrinterNames();
  57. foreach (QString printerStr, printerNameList)
  58. {
  59. if(printerStr== mPrinterName)
  60. {
  61. QPrinter printer(QPrinter::HighResolution);
  62. printer.setOutputFormat(QPrinter::NativeFormat);
  63. printer.setPrinterName(printerStr);
  64. //打印界面
  65. /* QPrintDialog dialog(&printer, mPage->view());
  66. if (dialog.exec() != QDialog::Accepted)
  67. return;*/
  68. printDocument(&printer);
  69. return;
  70. }
  71. }
  72. qDebug() << "not find printer";
  73. }

For https URL connections, the "Your connection is not a dedicated connection" alarm will appear. You need to skip this alarm, so you need to inherit QWebEnginePage to make a class

CustomWebEnginePage.h

  1. #pragma once
  2. #include <QWebEnginePage>
  3. class CustomWebEnginePage : public QWebEnginePage
  4. {
  5. Q_OBJECT
  6. public:
  7. CustomWebEnginePage();
  8. ~CustomWebEnginePage();
  9. virtual bool certificateError(const QWebEngineCertificateError &certificateError);
  10. };

CustomWebEnginePage.cpp

  1. #include "CustomWebEnginePage.h"
  2. CustomWebEnginePage::CustomWebEnginePage()
  3. {
  4. }
  5. CustomWebEnginePage::~CustomWebEnginePage()
  6. {
  7. }
  8. bool CustomWebEnginePage::certificateError(const QWebEngineCertificateError &certificateError)
  9. {
  10. return true;
  11. }