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?
3 Answers
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.

- 4,400
- 1
- 26
- 40
-
How to know whether i have the pdf viewer installed? And if not, how to install the plugin? – lionel319 Jul 08 '11 at 01:55
-
The easiest way to see if you have the plugin installed is to just visit your PDF link in your default browser and see what happens. Are you on Windows? You can download Acrobat Reader from http://get.adobe.com/reader/ and QtWebKit should automatically pick this up. – Gary Hughes Jul 08 '11 at 08:16
-
Also, Chrome has a PDF reader built in so should always show PDF files even if you don't have a 3rd party plugin installed. Typing about:plugins into the Chrome address bar will tell you if you have a PDF reader other than the built in reader available. – Gary Hughes Jul 08 '11 at 08:17
-
I'm on linux. Default browser is firefox. I can open the pdf file from my firefox. But then, when i did what u shows as above, still, nothing happens, I got this error printed on my terminal:- ** Message: NP_Initialize ** Message: plugin_get_value 1 (1) ** Message: plugin_get_value 2 (2) ** Message: NP_Initialize ** Message: plugin_get_value 1 (1) ** Message: plugin_get_value 2 (2) ** Message: NP_Initialize ** Message: plugin_get_value 1 (1) ** Message: plugin_get_value 2 (2) ** Message: NP_Initialize ** Message: plugin_get_value 1 (1) ** Message: plugin_get_value 2 (2) – lionel319 Jul 08 '11 at 11:10
-
Amazing, been searching for a solution for over an hour. Thank you very much. – Edward Severinsen Feb 23 '20 at 16:11
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).

- 86,207
- 24
- 208
- 215
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_())

- 3,218
- 2
- 31
- 46
-
When I run this, it shows a window saying "Frame load interrupted by policy change, Failed to load URL file:///PathToMyPDF" – Lacrosse343 Jun 12 '20 at 15:34