简体   繁体   中英

Is it possible to get QWebKit to display pdf files?

i have a link in my QWebkit, which points to a pdf file. But when the link is clicked, it can't display the pdf file. is there a way to make it happen?

If you enable plugins through QWebSettings and have a PDF viewer installed that provides a browser plugin such as Acrobat then you should see the PDF rendered using the plugin inside your QWebView:

import sys
from PyQt4.QtCore import *
from PyQt4.QtGui import *
from PyQt4.QtWebKit import *

app = QApplication(sys.argv)
web = QWebView()
web.settings().setAttribute(QWebSettings.PluginsEnabled, True)
web.show()
web.load(QUrl('file:///C:/test/test.pdf')) # Change path to actual file.
sys.exit(app.exec_())

This code isn't working for me on Windows with the latest version of Acrobat X (it just shows a progress bar but no PDF - proof that the plugin is loading, just not working) but I'm sure this is how I've done it before. Give it a try and let me know.

Webkit doesn't include a PDF viewer. You'll need to have some way of rendering it - whether you pass it off to a different viewer (Adobe PDF viewer or something else), render it in the control in some way you devise (you could even try rendering it in JavaScript for fun).

This is Gary Hudges code for PyQt5:

import sys
from PyQt5.QtWidgets import *
from PyQt5.QtWebKitWidgets import *
from PyQt5.QtWebKit import *
from PyQt5.QtCore import *

app = QApplication(sys.argv)
web = QWebView()
web.settings().setAttribute(QWebSettings.PluginsEnabled, True)
web.show()
web.load(QUrl('file:///C:/data/progetti_miei/python/test.pdf')) # Change path to actual file.
sys.exit(app.exec_())

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM