在我的应用程序中,我有一个方法可以让用户将报表转换为PDF文档。这个很好用--一次。如果用户再次单击该按钮,转换将挂起。
这是我的代码:
def print_report(self):
web = QtWebKit.QWebView()
filename = "reporttemplate.html"
file = open(filename,'r')
html = file.read()
file.close()
web.setHtml(html)
#web.show()
printer = QtGui.QPrinter()
printer.setPageSize(QtGui.QPrinter.Letter)
printer.setOutputFormat(QtGui.QPrinter.PdfFormat)
# ---- BROKEN ----
# This next line is where it hangs on the second call to this function.
# The first time it works, and generates the PDF as expected.
# ---- BROKEN ON THE NEXT LINE! ----
printer.setOutputFileName(r'C:\path\to\report\directory\file.pdf')
def convertIt():
web.print_(printer)
print "Pdf generated"
web.close()
QtCore.QObject.connect(web, QtCore.SIGNAL("loadFinished(bool)"), convertIt)我的想法是打印机仍然有打开的文件。如果是这种情况,我如何关闭该文件?
如果我重新启动应用程序,并且该文件已经存在,则可以正常工作。出于这个原因,我不认为它是挂起的,因为文件已经存在。
发布于 2012-07-21 10:03:43
在测试您的代码时,我注意到只有当我在print_report方法的末尾(最后一条语句)放入web.setHtml(html)时,它才能工作。这样一来,我就可以生成任意次数的file.pdf了。
https://stackoverflow.com/questions/11583458
复制相似问题