2012-07-20 89 views
0

在我的應用程序中,我有一種方法可讓用戶將報告轉換爲PDF文檔。這完美 - 一次。如果用戶再次點擊該按鈕,則轉換會掛起。QPrinter不會多次打印

這是我的代碼:

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) 

我的想法是,打印機仍有打開該文件。如果是這樣的話,我該如何關閉文件?

如果我重新啓動應用程序並且文件已經存在,它就可以工作。出於這個原因,我不相信它是掛起的,因爲文件已經存在。

回答

1

測試你的代碼我注意到對我來說,它只有當我把web.setHtml(html)放在print_report方法的末尾(最後一個語句)時才起作用。這樣做,我可以根據需要多次生成file.pdf。

+0

工作。謝謝。 – Andy 2012-07-23 14:57:27