Python Forum
[PyQt] [solved] How to display a pdf-file in a PyQt6 widget
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[PyQt] [solved] How to display a pdf-file in a PyQt6 widget
#11
I have run the above python script. But I get only blank widget. What additional python packages are to be installed? Thankyou.
Reply
#12
You must provide a path to a PDF file.

Example:

python3 script.py path/to/your.pdf
Reply
#13
(May-05-2023, 05:49 PM)Axel_Erfurt Wrote: You must provide a path to a PDF file.

Example:

python3 script.py path/to/your.pdf

I provided the full path. May be I did not understand you. Below is my code.
from qtpy.QtCore import QUrl
from qtpy.QtWidgets import QApplication, QMainWindow
from qtpy.QtWebEngineWidgets import QWebEngineView
from os import path
 
class MainWindow(QMainWindow):
    def __init__(self):
        super(QMainWindow, self).__init__()
 
        self.setWindowTitle("PDF Viewer")
        self.setGeometry(0, 28, 1000, 750)
 
        self.webView = QWebEngineView()
        self.webView.settings().setAttribute(self.webView.settings().WebAttribute.PluginsEnabled, True)
        self.webView.settings().setAttribute(self.webView.settings().WebAttribute.PdfViewerEnabled, True)
        self.setCentralWidget(self.webView)
 
    def url_changed(self):
        self.setWindowTitle(self.webView.title())
 
    def go_back(self):
        self.webView.back()
 
if __name__ == '__main__':
 
    import sys
    app = QApplication(sys.argv)
    win = MainWindow()
    win.show()
    if len(sys.argv) > 1:
        win.webView.setUrl(QUrl(f"file://{sys.argv[1]}"))
    else:
        wd = path.dirname(sys.argv[0])
#        wd = path.dirname(path.abspath(sys.argv[0]))
        print(f"wd: {wd}")      #   wd: E:\MyProjects\PDFviewer_2
        test_pdf = "test.pdf"
        win.webView.setUrl(QUrl(f"file://{wd}/{test_pdf}"))
        
    sys.exit(app.exec())
Please advice.
Larz60+ write May-06-2023, 09:23 AM:
Please post all code, output and errors (it it's entirety) between their respective tags. Refer to BBCode help topic on how to post. Use the "Preview Post" button to make sure the code is presented as you expect before hitting the "Post Reply/Thread" button.
Fixed for you this time. Please use BBCode tags on future posts.
Reply
#14
try this

from qtpy.QtCore import QUrl
from qtpy.QtWidgets import QApplication, QMainWindow
from qtpy.QtWebEngineWidgets import QWebEngineView

class MainWindow(QMainWindow):
    def __init__(self):
        super(QMainWindow, self).__init__()

        self.setWindowTitle("PDF Viewer")
        self.setGeometry(0, 28, 1000, 750)

        self.webView = QWebEngineView()
        self.webView.settings().setAttribute(self.webView.settings().WebAttribute.PluginsEnabled, True)
        self.webView.settings().setAttribute(self.webView.settings().WebAttribute.PdfViewerEnabled, True)
        self.setCentralWidget(self.webView)

    def url_changed(self):
        self.setWindowTitle(self.webView.title())

    def go_back(self):
        self.webView.back()

if __name__ == '__main__':

    import sys
    app = QApplication(sys.argv)
    win = MainWindow()
    win.show()
    if len(sys.argv) > 1:
        url = sys.argv[1]
        win.webView.setUrl(QUrl(f"file://{url}"))

    sys.exit(app.exec())
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  [PyQt] Saving file to html or pdf stopped working in PyQt6 ejKDE 4 475 Mar-12-2024, 07:45 PM
Last Post: ejKDE
  event driven coding PyQt6 on Combobox change merrittr 3 2,046 May-03-2023, 03:35 AM
Last Post: merrittr
Question [PyQt] Application desktop toolbar with PyQt6 bunz 4 1,508 Mar-09-2023, 08:09 PM
Last Post: bunz
  PyQt6 QAction with icon and string malonn 2 1,688 Sep-12-2022, 11:59 AM
Last Post: malonn
  [PyQt] [Solved]Display PyQtTable results from A->Z & Z->A Extra 2 1,171 Jul-18-2022, 04:04 PM
Last Post: Extra
  [PyQt] [Solved]Display Search Results in QTable Extra 5 2,449 Jun-29-2022, 10:20 PM
Last Post: Extra
  [PyQt] Embed Google Maps in PyQt6 Widget Raures 2 3,108 Sep-11-2021, 04:32 PM
Last Post: Raures
  [PyQt] Collect entry from textline Widget via UI file mart79 3 2,906 Aug-05-2019, 01:40 PM
Last Post: Denni
  Display and update the label text which display the serial value jenkins43 5 9,069 Feb-04-2019, 04:36 AM
Last Post: Larz60+
  Display more than one button in GUI to display MPU6000 Sensor readings barry76 4 3,885 Jan-05-2019, 01:48 PM
Last Post: wuf

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020