How to render PDF using pdf.js viewer in PyQt?

后端 未结 3 1404
逝去的感伤
逝去的感伤 2020-12-05 22:03

I have tried adding the pdf.js viewer files in my project and it works in browsers like Chrome, Mozilla, Safari, etc, but it\'s not loading some pages in node-webkit and PyQ

3条回答
  •  庸人自扰
    2020-12-05 22:51

    From PyQt5 v5.13 you can load PDF files with the chromium API. According to the documentation https://doc.qt.io/qt-5/qtwebengine-features.html#pdf-file-viewing this option is by default enabled.

    This minimal example is adapted from Simple Browser

    import sys
    from pathlib import Path
    
    from PyQt5 import QAxContainer
    from PyQt5.QtCore import Qt
    from PyQt5.QtWidgets import QWidget, QVBoxLayout, QPushButton, QLineEdit, QApplication
    
    
    class Main(QWidget):
        def __init__(self, parent=None):
            super(Main, self).__init__(parent)
            self.main_layout = QVBoxLayout(self)
    
            self.qlineedit = QLineEdit()
            self.qlineedit.returnPressed.connect(self.go_action)
            self.main_layout.addWidget(self.qlineedit)
            self.read_btn = QPushButton('Test')
            self.read_btn.clicked.connect(self.go_action)
            self.main_layout.addWidget(self.read_btn)
    
            self.WebBrowser = QAxContainer.QAxWidget(self)
            self.WebBrowser.setFocusPolicy(Qt.StrongFocus)
            self.WebBrowser.setControl("{8856F961-340A-11D0-A96B-00C04FD705A2}")
            self.main_layout.addWidget(self.WebBrowser)
    
        def go_action(self):
            # convert system path to web path
            f = Path(self.qlineedit.text()).as_uri()
            # load object 
            self.WebBrowser.dynamicCall('Navigate(const QString&)', f)
    
    
    if __name__ == "__main__":
        a = QApplication(sys.argv)
        w = Main()
        w.show()
        sys.exit(a.exec_())
    
    

    This example

提交回复
热议问题