PyQt5 possible to use QWebEngineView to print PDF in background, no GUI?











up vote
0
down vote

favorite












I want to use Python to convert some html files to PDF.
After looking at the modules and possibilities to do this, I settled on Qt5.



I have code that works, but it doesn't work exactly how I want.
I want to run my program via the command line, and it to exit once finished.
Currently, it produces the PDF but stays running.



If I uncomment and show the QWebEngineView with view.show(), then I can close the window that opens and the program will exit. (But I don't want to have any GUI, so this isn't good)



Next, I tried to show the view, then immediately close it after with view.close(), but when I do this a PDF doesn't get generated. I tried adding a 5 second sleep after view.show() and before view.close(), but still the same result.



I'm not sure if what I want is possible, I understand Qt is for GUIs but it would be very convenient if I could get this working completely through the command line.



import sys
import os
from time import sleep

from PyQt5.QtWidgets import QApplication
from PyQt5.QtWebEngineWidgets import QWebEngineView
from PyQt5.QtCore import QDir, QUrl

# needed to load local html files
sys.argv.append("--disable-web-security")
app = QApplication(sys.argv)

raw_html = ""
with open('webpage.htm', 'r') as myfile:
raw_html = myfile.read()

view = QWebEngineView()
view.setHtml(raw_html)

def save_pdf(finished):
view.page().printToPdf("output.pdf")
# view.show()
# view.close()

view.loadFinished.connect(save_pdf)

sys.exit(app.exec_())









share|improve this question
























  • what is your pyqt version?
    – eyllanesc
    Nov 9 at 14:47















up vote
0
down vote

favorite












I want to use Python to convert some html files to PDF.
After looking at the modules and possibilities to do this, I settled on Qt5.



I have code that works, but it doesn't work exactly how I want.
I want to run my program via the command line, and it to exit once finished.
Currently, it produces the PDF but stays running.



If I uncomment and show the QWebEngineView with view.show(), then I can close the window that opens and the program will exit. (But I don't want to have any GUI, so this isn't good)



Next, I tried to show the view, then immediately close it after with view.close(), but when I do this a PDF doesn't get generated. I tried adding a 5 second sleep after view.show() and before view.close(), but still the same result.



I'm not sure if what I want is possible, I understand Qt is for GUIs but it would be very convenient if I could get this working completely through the command line.



import sys
import os
from time import sleep

from PyQt5.QtWidgets import QApplication
from PyQt5.QtWebEngineWidgets import QWebEngineView
from PyQt5.QtCore import QDir, QUrl

# needed to load local html files
sys.argv.append("--disable-web-security")
app = QApplication(sys.argv)

raw_html = ""
with open('webpage.htm', 'r') as myfile:
raw_html = myfile.read()

view = QWebEngineView()
view.setHtml(raw_html)

def save_pdf(finished):
view.page().printToPdf("output.pdf")
# view.show()
# view.close()

view.loadFinished.connect(save_pdf)

sys.exit(app.exec_())









share|improve this question
























  • what is your pyqt version?
    – eyllanesc
    Nov 9 at 14:47













up vote
0
down vote

favorite









up vote
0
down vote

favorite











I want to use Python to convert some html files to PDF.
After looking at the modules and possibilities to do this, I settled on Qt5.



I have code that works, but it doesn't work exactly how I want.
I want to run my program via the command line, and it to exit once finished.
Currently, it produces the PDF but stays running.



If I uncomment and show the QWebEngineView with view.show(), then I can close the window that opens and the program will exit. (But I don't want to have any GUI, so this isn't good)



Next, I tried to show the view, then immediately close it after with view.close(), but when I do this a PDF doesn't get generated. I tried adding a 5 second sleep after view.show() and before view.close(), but still the same result.



I'm not sure if what I want is possible, I understand Qt is for GUIs but it would be very convenient if I could get this working completely through the command line.



import sys
import os
from time import sleep

from PyQt5.QtWidgets import QApplication
from PyQt5.QtWebEngineWidgets import QWebEngineView
from PyQt5.QtCore import QDir, QUrl

# needed to load local html files
sys.argv.append("--disable-web-security")
app = QApplication(sys.argv)

raw_html = ""
with open('webpage.htm', 'r') as myfile:
raw_html = myfile.read()

view = QWebEngineView()
view.setHtml(raw_html)

def save_pdf(finished):
view.page().printToPdf("output.pdf")
# view.show()
# view.close()

view.loadFinished.connect(save_pdf)

sys.exit(app.exec_())









share|improve this question















I want to use Python to convert some html files to PDF.
After looking at the modules and possibilities to do this, I settled on Qt5.



I have code that works, but it doesn't work exactly how I want.
I want to run my program via the command line, and it to exit once finished.
Currently, it produces the PDF but stays running.



If I uncomment and show the QWebEngineView with view.show(), then I can close the window that opens and the program will exit. (But I don't want to have any GUI, so this isn't good)



Next, I tried to show the view, then immediately close it after with view.close(), but when I do this a PDF doesn't get generated. I tried adding a 5 second sleep after view.show() and before view.close(), but still the same result.



I'm not sure if what I want is possible, I understand Qt is for GUIs but it would be very convenient if I could get this working completely through the command line.



import sys
import os
from time import sleep

from PyQt5.QtWidgets import QApplication
from PyQt5.QtWebEngineWidgets import QWebEngineView
from PyQt5.QtCore import QDir, QUrl

# needed to load local html files
sys.argv.append("--disable-web-security")
app = QApplication(sys.argv)

raw_html = ""
with open('webpage.htm', 'r') as myfile:
raw_html = myfile.read()

view = QWebEngineView()
view.setHtml(raw_html)

def save_pdf(finished):
view.page().printToPdf("output.pdf")
# view.show()
# view.close()

view.loadFinished.connect(save_pdf)

sys.exit(app.exec_())






python pyqt pyqt5






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 9 at 14:47









eyllanesc

68.7k93052




68.7k93052










asked Nov 9 at 10:53









Ben Kailon

556




556












  • what is your pyqt version?
    – eyllanesc
    Nov 9 at 14:47


















  • what is your pyqt version?
    – eyllanesc
    Nov 9 at 14:47
















what is your pyqt version?
– eyllanesc
Nov 9 at 14:47




what is your pyqt version?
– eyllanesc
Nov 9 at 14:47












1 Answer
1






active

oldest

votes

















up vote
1
down vote



accepted










Try it:



import sys
from PyQt5 import QtCore, QtWidgets, QtWebEngineWidgets

app = QtWidgets.QApplication(sys.argv)

loader = QtWebEngineWidgets.QWebEngineView()
loader.setZoomFactor(1)

loader.page().pdfPrintingFinished.connect(loader.close) # <---

loader.load(QtCore.QUrl('https://en.wikipedia.org/wiki/Main_Page'))

def emit_pdf(finished):
#loader.show()
loader.page().printToPdf("test.pdf")

loader.loadFinished.connect(emit_pdf)

app.exec()





share|improve this answer





















  • Thanks, but: AttributeError: 'QWebEnginePage' object has no attribute 'pdfPrintingFinished'
    –  Ben Kailon
    Nov 9 at 12:35










  • @BenKailon update your pyqt5, the pdfPrintingFinished signal was recently implemented in Qt 5.9
    – eyllanesc
    Nov 9 at 14:49










  • @eyllanesc ah it was 5.8.1, I didn't even think about that. Sorry. Now, after updating, I get a different error. ImportError: cannot import name 'QtWebEngineWidgets' After a little research it looks like the 32 bit Windows PyQt5 wheels that pip installs don't have QtWebEngineWidgets anymore... This is getting complicated!
    –  Ben Kailon
    Nov 12 at 8:54












  • Just a win32 issue, but after downgrading pyqt5 to 5.10, it's working perfectly. Thanks to both of you.
    –  Ben Kailon
    Nov 12 at 9:06











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














 

draft saved


draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53224317%2fpyqt5-possible-to-use-qwebengineview-to-print-pdf-in-background-no-gui%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes








up vote
1
down vote



accepted










Try it:



import sys
from PyQt5 import QtCore, QtWidgets, QtWebEngineWidgets

app = QtWidgets.QApplication(sys.argv)

loader = QtWebEngineWidgets.QWebEngineView()
loader.setZoomFactor(1)

loader.page().pdfPrintingFinished.connect(loader.close) # <---

loader.load(QtCore.QUrl('https://en.wikipedia.org/wiki/Main_Page'))

def emit_pdf(finished):
#loader.show()
loader.page().printToPdf("test.pdf")

loader.loadFinished.connect(emit_pdf)

app.exec()





share|improve this answer





















  • Thanks, but: AttributeError: 'QWebEnginePage' object has no attribute 'pdfPrintingFinished'
    –  Ben Kailon
    Nov 9 at 12:35










  • @BenKailon update your pyqt5, the pdfPrintingFinished signal was recently implemented in Qt 5.9
    – eyllanesc
    Nov 9 at 14:49










  • @eyllanesc ah it was 5.8.1, I didn't even think about that. Sorry. Now, after updating, I get a different error. ImportError: cannot import name 'QtWebEngineWidgets' After a little research it looks like the 32 bit Windows PyQt5 wheels that pip installs don't have QtWebEngineWidgets anymore... This is getting complicated!
    –  Ben Kailon
    Nov 12 at 8:54












  • Just a win32 issue, but after downgrading pyqt5 to 5.10, it's working perfectly. Thanks to both of you.
    –  Ben Kailon
    Nov 12 at 9:06















up vote
1
down vote



accepted










Try it:



import sys
from PyQt5 import QtCore, QtWidgets, QtWebEngineWidgets

app = QtWidgets.QApplication(sys.argv)

loader = QtWebEngineWidgets.QWebEngineView()
loader.setZoomFactor(1)

loader.page().pdfPrintingFinished.connect(loader.close) # <---

loader.load(QtCore.QUrl('https://en.wikipedia.org/wiki/Main_Page'))

def emit_pdf(finished):
#loader.show()
loader.page().printToPdf("test.pdf")

loader.loadFinished.connect(emit_pdf)

app.exec()





share|improve this answer





















  • Thanks, but: AttributeError: 'QWebEnginePage' object has no attribute 'pdfPrintingFinished'
    –  Ben Kailon
    Nov 9 at 12:35










  • @BenKailon update your pyqt5, the pdfPrintingFinished signal was recently implemented in Qt 5.9
    – eyllanesc
    Nov 9 at 14:49










  • @eyllanesc ah it was 5.8.1, I didn't even think about that. Sorry. Now, after updating, I get a different error. ImportError: cannot import name 'QtWebEngineWidgets' After a little research it looks like the 32 bit Windows PyQt5 wheels that pip installs don't have QtWebEngineWidgets anymore... This is getting complicated!
    –  Ben Kailon
    Nov 12 at 8:54












  • Just a win32 issue, but after downgrading pyqt5 to 5.10, it's working perfectly. Thanks to both of you.
    –  Ben Kailon
    Nov 12 at 9:06













up vote
1
down vote



accepted







up vote
1
down vote



accepted






Try it:



import sys
from PyQt5 import QtCore, QtWidgets, QtWebEngineWidgets

app = QtWidgets.QApplication(sys.argv)

loader = QtWebEngineWidgets.QWebEngineView()
loader.setZoomFactor(1)

loader.page().pdfPrintingFinished.connect(loader.close) # <---

loader.load(QtCore.QUrl('https://en.wikipedia.org/wiki/Main_Page'))

def emit_pdf(finished):
#loader.show()
loader.page().printToPdf("test.pdf")

loader.loadFinished.connect(emit_pdf)

app.exec()





share|improve this answer












Try it:



import sys
from PyQt5 import QtCore, QtWidgets, QtWebEngineWidgets

app = QtWidgets.QApplication(sys.argv)

loader = QtWebEngineWidgets.QWebEngineView()
loader.setZoomFactor(1)

loader.page().pdfPrintingFinished.connect(loader.close) # <---

loader.load(QtCore.QUrl('https://en.wikipedia.org/wiki/Main_Page'))

def emit_pdf(finished):
#loader.show()
loader.page().printToPdf("test.pdf")

loader.loadFinished.connect(emit_pdf)

app.exec()






share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 9 at 12:13









S. Nick

2,201247




2,201247












  • Thanks, but: AttributeError: 'QWebEnginePage' object has no attribute 'pdfPrintingFinished'
    –  Ben Kailon
    Nov 9 at 12:35










  • @BenKailon update your pyqt5, the pdfPrintingFinished signal was recently implemented in Qt 5.9
    – eyllanesc
    Nov 9 at 14:49










  • @eyllanesc ah it was 5.8.1, I didn't even think about that. Sorry. Now, after updating, I get a different error. ImportError: cannot import name 'QtWebEngineWidgets' After a little research it looks like the 32 bit Windows PyQt5 wheels that pip installs don't have QtWebEngineWidgets anymore... This is getting complicated!
    –  Ben Kailon
    Nov 12 at 8:54












  • Just a win32 issue, but after downgrading pyqt5 to 5.10, it's working perfectly. Thanks to both of you.
    –  Ben Kailon
    Nov 12 at 9:06


















  • Thanks, but: AttributeError: 'QWebEnginePage' object has no attribute 'pdfPrintingFinished'
    –  Ben Kailon
    Nov 9 at 12:35










  • @BenKailon update your pyqt5, the pdfPrintingFinished signal was recently implemented in Qt 5.9
    – eyllanesc
    Nov 9 at 14:49










  • @eyllanesc ah it was 5.8.1, I didn't even think about that. Sorry. Now, after updating, I get a different error. ImportError: cannot import name 'QtWebEngineWidgets' After a little research it looks like the 32 bit Windows PyQt5 wheels that pip installs don't have QtWebEngineWidgets anymore... This is getting complicated!
    –  Ben Kailon
    Nov 12 at 8:54












  • Just a win32 issue, but after downgrading pyqt5 to 5.10, it's working perfectly. Thanks to both of you.
    –  Ben Kailon
    Nov 12 at 9:06
















Thanks, but: AttributeError: 'QWebEnginePage' object has no attribute 'pdfPrintingFinished'
–  Ben Kailon
Nov 9 at 12:35




Thanks, but: AttributeError: 'QWebEnginePage' object has no attribute 'pdfPrintingFinished'
–  Ben Kailon
Nov 9 at 12:35












@BenKailon update your pyqt5, the pdfPrintingFinished signal was recently implemented in Qt 5.9
– eyllanesc
Nov 9 at 14:49




@BenKailon update your pyqt5, the pdfPrintingFinished signal was recently implemented in Qt 5.9
– eyllanesc
Nov 9 at 14:49












@eyllanesc ah it was 5.8.1, I didn't even think about that. Sorry. Now, after updating, I get a different error. ImportError: cannot import name 'QtWebEngineWidgets' After a little research it looks like the 32 bit Windows PyQt5 wheels that pip installs don't have QtWebEngineWidgets anymore... This is getting complicated!
–  Ben Kailon
Nov 12 at 8:54






@eyllanesc ah it was 5.8.1, I didn't even think about that. Sorry. Now, after updating, I get a different error. ImportError: cannot import name 'QtWebEngineWidgets' After a little research it looks like the 32 bit Windows PyQt5 wheels that pip installs don't have QtWebEngineWidgets anymore... This is getting complicated!
–  Ben Kailon
Nov 12 at 8:54














Just a win32 issue, but after downgrading pyqt5 to 5.10, it's working perfectly. Thanks to both of you.
–  Ben Kailon
Nov 12 at 9:06




Just a win32 issue, but after downgrading pyqt5 to 5.10, it's working perfectly. Thanks to both of you.
–  Ben Kailon
Nov 12 at 9:06


















 

draft saved


draft discarded



















































 


draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53224317%2fpyqt5-possible-to-use-qwebengineview-to-print-pdf-in-background-no-gui%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Schultheiß

Verwaltungsgliederung Dänemarks

Liste der Kulturdenkmale in Wilsdruff