1

I'm learning how to use pyqt, I'm using Windows 10 and python 3.10 and i'm testing a code to open pdf files, this allows you to select the file and then opens it on a new window. It works almost correctly except that doesn't show zoom buttons, or zoom percentage.

This is the code:

import sys

from PyQt5 import QtCore, QtWidgets, QtWebEngineWidgets


def main():

    print(
        f"PyQt5 version: {QtCore.PYQT_VERSION_STR}, Qt version: {QtCore.QT_VERSION_STR}"
    )

    app = QtWidgets.QApplication(sys.argv)
    filename, _ = QtWidgets.QFileDialog.getOpenFileName(None, filter="PDF (*.pdf)")
    if not filename:
        print("please select the .pdf file")
        sys.exit(0)
    view = QtWebEngineWidgets.QWebEngineView()
    settings = view.settings()
    settings.setAttribute(QtWebEngineWidgets.QWebEngineSettings.PluginsEnabled, True)
    url = QtCore.QUrl.fromLocalFile(filename)
    view.load(url)
    view.resize(640, 480)
    view.show()
    sys.exit(app.exec_())


if __name__ == "__main__":
    main()

I was wondering if I need to pass an extra setting to display zoom buttons on pdf viewer. Anyone can help me? I highlighted where buttons/actions supposed to display in the image below.

I highlighted where buttons/actions supposed to display

Ticsup
  • 85
  • 1
  • 6
  • This is similar to https://stackoverflow.com/questions/72863750/how-to-set-the-default-zoom-of-a-pdf-in-qwebengineview-on-pyside6-i-want-to-be – Alexander Aug 12 '22 at 02:10
  • You could try the `pdfjs` viewer instead, which has more features. For more details, see [this answer](https://stackoverflow.com/a/48053017/984421). – ekhumoro Aug 12 '22 at 10:52

1 Answers1

1

That problem is because Qt version, if you test with the most recent version, PyQt6 you will see that works.

You need to install PyQt6:

pip install pyqt6

You also need to install webengine for pyqt6:

pip install PyQt6-WebEngine

And you need to change your code to this:

import sys

from PyQt6 import QtCore, QtWidgets, QtWebEngineWidgets


def main():

    print(
        f"PyQt6 version: {QtCore.PYQT_VERSION_STR}, Qt version: {QtCore.QT_VERSION_STR}"
    )

    app = QtWidgets.QApplication(sys.argv)
    filename, _ = QtWidgets.QFileDialog.getOpenFileName(None, filter="PDF (*.pdf)")
    if not filename:
        print("please select the .pdf file")
        sys.exit(0)
    view = QtWebEngineWidgets.QWebEngineView()
    settings = view.settings()
    settings.setAttribute(view.settings().WebAttribute.PluginsEnabled, True)
    url = QtCore.QUrl.fromLocalFile(filename)
    view.load(url)
    view.resize(640, 480)
    view.show()
    sys.exit(app.exec())


if __name__ == "__main__":
    main()

And now should look like this:

loaded pdf

Kevin Ramirez Zavalza
  • 1,629
  • 1
  • 22
  • 34