Python Forum

Full Version: Displaying html and pdf in a QtWidget
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Pages: 1 2
(Dec-13-2020, 04:14 PM)singletonamos50 Wrote: [ -> ]would recommend many of the tutorials available on specifically PyQt5

The problem isn't PyQt5, it's a bad PDF.
Hi Axel

From two examples you gave me, the one in our preceding post, to display an image in a pdf file, and one on QTabWidget, I wrote the following script to display several pdf images in a qtabwidget. The main window shows up, but without any qtabwidget
from PyQt5.QtWidgets import QMainWindow, QApplication, QTabWidget, QStackedLayout, QWidget
import sys
import os
from PyQt5.QtCore import QUrl
from PyQt5.QtWebEngineWidgets import QWebEngineView
import requests
PDFJS = f"file://{os.path.abspath('./Documents/programmation/java/web/viewer.html')}"
		
        
class PagePdf(QWebEngineView):
	def __init__(self):
		super(PagePdf, self).__init__()
	def chargePdf (self, page):
		url="http://www.tabularium.be/bailly/"+page+'.pdf'
		with open("/tmp/"+page+".pdf", 'wb') as f:
			f.write(requests.get(url).content)
		PDF = "file:///tmp/"+page+".pdf"
		self.load(QUrl.fromUserInput(f'{PDFJS}?file={PDF}'))
		print("loading PDF:", PDF)
		self.setZoomFactor(0.75)


class MainWindow(QMainWindow):
 
	def __init__(self, *args, **kwargs):
		super(MainWindow, self).__init__(*args, **kwargs)
         
		self.setWindowTitle("My Awesome App")
		tabs = QTabWidget()
		tabs.setDocumentMode(True)
		tabs.setTabPosition(QTabWidget.North)
		tabs.setMovable(True)

		for n, page in enumerate(['0001','0120','1360']):
			p=PagePdf()
			print(page)
			tabs.addTab( p.chargePdf(page), page)
		self.setCentralWidget(tabs)
		self.setGeometry(0, 0, 600, 400)
         
if __name__ == '__main__':
	import sys
	app = QApplication(sys.argv)
	win = MainWindow()
	win.setWindowTitle("QStackedLayout")
	win.show()
	sys.exit(app.exec_())
Can you please, give me a little help.

Arbiel
You did not set a layout to the tab widgets

from PyQt5.QtWidgets import (QMainWindow, QApplication, QTabWidget, 
                             QWidget, QVBoxLayout)
import sys
import os
from PyQt5.QtCore import QUrl
from PyQt5.QtWebEngineWidgets import QWebEngineView
import requests
PDFJS = f"file://{os.path.abspath('./Documents/programmation/java/web/viewer.html')}" 
         
class PagePdf(QWebEngineView):
    def __init__(self):
        super(PagePdf, self).__init__()
        
    def chargePdf (self, page):
        url="http://www.tabularium.be/bailly/"+page+'.pdf'
        with open("/tmp/"+page+".pdf", 'wb') as f:
            f.write(requests.get(url).content)
        PDF = "file:///tmp/"+page+".pdf"
        self.load(QUrl.fromUserInput(f'{PDFJS}?file={PDF}'))
        print("loading PDF:", PDF)
        self.setZoomFactor(0.75)
 
 
class MainWindow(QMainWindow):
  
    def __init__(self, *args, **kwargs):
        super(MainWindow, self).__init__(*args, **kwargs)
          
        self.setWindowTitle("My Awesome App")
        self.tabs = QTabWidget(self)
        self.tabs.setDocumentMode(True)
        self.tabs.setTabPosition(QTabWidget.North)
        self.tabs.setMovable(True)
 
        for n, page in enumerate(['0001','0120','1360']):
            p=PagePdf()
            print(page)
            tab = QWidget()
            self.tabs.addTab(tab, f"Tab{n + 1}")
            layout = QVBoxLayout()
            layout.addWidget(p)
            p.chargePdf(page)
            tab.setLayout(layout)
        self.setCentralWidget(self.tabs)
        self.setGeometry(0, 0, 600, 400)
          
if __name__ == '__main__':
    import sys
    app = QApplication(sys.argv)
    win = MainWindow()
    win.setWindowTitle("QStackedLayout")
    win.show()
    sys.exit(app.exec_())
I have an example on github that shows PDF
Hi

Thank's to both of you.

@pitterbrayn

I did not find you on github:
Error:
We couldn’t find any repositories matching 'pitterbrayn'
Arbiel
(Jan-20-2021, 02:17 PM)pitterbrayn Wrote: [ -> ]I have an example on github that shows PDF

Hi, please can you provide the github link ?
In newer versions of PyQt5 you can use QWebEngineView to show pdf

from PyQt5.QtCore import QUrl
from PyQt5.QtWidgets import QApplication, QMainWindow, QWidget
from PyQt5.QtWebEngineWidgets import QWebEngineSettings, QWebEngineView

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

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

        self.webView = QWebEngineView()
        self.webView.settings().setAttribute(QWebEngineSettings.PluginsEnabled, True)
        self.webView.settings().setAttribute(QWebEngineSettings.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()
    test_pdf = "https://www.areopage.net/nvlexique/DictionnaireGrecFrancaisNouveauTestament.pdf"
    win.webView.setUrl(QUrl(f"{test_pdf}"))
    sys.exit(app.exec_())
Pages: 1 2