2

自从我从 Qt 4.7.4 更新到 Qt 4.8.7 之后,我不可能在不切断右侧的情况下打印一个简单的 HTML 表格。这是我在 QPrintPreviewDialog 中显示表格的 PySide 代码(该问题与 PySide 或 PyQt 无关,您可以使用 C++ 和 Qt 重现它):

app = QtGui.QApplication(sys.argv)

webView = QWebView()            
webView.load(QtCore.QUrl('table.html'))

printer = QtGui.QPrinter()
printer.setPaperSize(QtGui.QPrinter.A4)

printPreview = QtGui.QPrintPreviewDialog(printer)    
printPreview.paintRequested.connect(webView.print_)

#Avoid blank preview on startup                    
QtGui.QApplication.processEvents()

printPreview.show()        
app.exec_()

这是我的简单表格的 HTML 代码:

<!DOCTYPE html>  
<html>
<head>
    <meta http-equiv="content-type" content="text/html; charset=utf-8">    
    <style>
        table {
            width:100%;
            border-collapse:collapse;
        }
        table, td, tr, th{
            border:0.091em solid grey;                 
        }                        
    </style>
</head>

<body>
 <table>
  <tr>
    <th>Firstname</th>
    <th>Lastname</th>
    <th>Lastname</th>
    <th>Lastname</th>
    <th>Lastname</th>
    <th>Lastname</th>
    <th>Lastname</th>
    <th>Lastname</th>
    <th>Lastname</th>
    <th>Lastname</th>
    <th>Lastname</th>
    <th>Lastname</th>
    <th>Lastname</th>
    <th>Lastname</th>
    <th>Lastname</th>
    <th>Lastname</th>
    <th>Lastname</th>
    <th>Lastname</th>
    <th>Lastname</th>
    <th>Lastname</th>
    <th>Lastname</th>
    <th>Lastname</th>
    <th>Lastname</th>
    <th>Lastname</th>
    <th>Lastname</th>
    <th>Lastname</th>
    <th>Lastname</th>
    <th>ENDENAME</th>

  </tr>
  <tr>
    <td>Jill</td>
    <td>Smith</td>
    <td>Smith</td>    
    <td>Smith</td>
    <td>Smith</td>
    <td>Smith</td>
    <td>Smith</td>
    <td>Smith</td>
    <td>Smith</td>
    <td>Smith</td>
    <td>Smith</td>
    <td>Smith</td>
    <td>Smith</td>
    <td>Smith</td>
    <td>Smith</td>
    <td>Smith</td>
    <td>Smith</td>
    <td>Smith</td>
    <td>Smith</td>
    <td>Smith</td>
    <td>Smith</td>
    <td>Smith</td>
    <td>Smith</td>
    <td>Smith</td>
    <td>Smith</td>
    <td>Smith</td>
    <td>Smith</td>
    <td>ENDE</td>
  </tr>
</table>
</body> 
</html>

这是 Qt 4.8.7 问题的屏幕截图

如您所见,表格的右侧不见了。

使用 Qt 4.7.4 结果是正确的,这里是一个截图:

与 Qt 4.7.4 相同的代码和 WORKING HTML 表

我试图自己调查这个问题。我发现,如果 printer.pageRect().size().width() 和 webView.page().mainFrame().contentsSize().width() 之间的比率低于 0.5,则会发生切割(在 96打印机和屏幕的 DPI)。如果 DPI 值不同,则必须将两个值都转换为英寸以获得正确的比率。

    contentSize = webView.page().mainFrame().contentsSize().width()        
    pageSize = printer.pageRect().size().width()

    pageWidthNorm = float(pageWidth)/float(printer.resolution())
    contentWidthNorm = contentSize.width() / horizontalDpi

    #Cutting off at 0.5     
    magicFactor = float(pageWidthNorm)/float(contentWidthNorm)

但是,我无法使用此信息解决此问题。我尝试将此 magicFactor 设置为 QWebView 的 zoomFactor 并且它可以工作。但结果很丑陋,不是我想要的。

我希望有人能帮助我。谢谢你。

4

1 回答 1

0
from PyQt4.QtWebKit import QWebView
def handlePrint(self):
    dialog = QtGui.QPrintDialog()
    if dialog.exec_() == QtGui.QDialog.Accepted:
        self.handlePaintRequest(dialog.printer())


def handlePaintRequest(self, printer):
    html = """
<!DOCTYPE html>  
<html>
<head>
    <meta http-equiv="content-type" content="text/html; charset=utf-8">    
    <style>
        table {
            width:100%;
            border-collapse:collapse;
        }
        table, td, tr, th{
            border:0.091em solid grey;                 
        }                        
    </style>
</head>

<body>
 <table>
  <tr>
    <th>Firstname</th>
    <th>Lastname</th>
    <th>Lastname</th>
    <th>Lastname</th>
    <th>Lastname</th>
    <th>Lastname</th>
    <th>Lastname</th>
    <th>Lastname</th>
    <th>Lastname</th>
    <th>Lastname</th>
    <th>Lastname</th>
    <th>Lastname</th>
    <th>Lastname</th>
    <th>Lastname</th>
    <th>Lastname</th>
    <th>Lastname</th>
    <th>Lastname</th>
    <th>Lastname</th>
    <th>Lastname</th>
    <th>Lastname</th>
    <th>Lastname</th>
    <th>Lastname</th>
    <th>Lastname</th>
    <th>Lastname</th>
    <th>Lastname</th>
    <th>Lastname</th>
    <th>Lastname</th>
    <th>ENDENAME</th>

  </tr>
  <tr>
    <td>Jill</td>
    <td>Smith</td>
    <td>Smith</td>    
    <td>Smith</td>
    <td>Smith</td>
    <td>Smith</td>
    <td>Smith</td>
    <td>Smith</td>
    <td>Smith</td>
    <td>Smith</td>
    <td>Smith</td>
    <td>Smith</td>
    <td>Smith</td>
    <td>Smith</td>
    <td>Smith</td>
    <td>Smith</td>
    <td>Smith</td>
    <td>Smith</td>
    <td>Smith</td>
    <td>Smith</td>
    <td>Smith</td>
    <td>Smith</td>
    <td>Smith</td>
    <td>Smith</td>
    <td>Smith</td>
    <td>Smith</td>
    <td>Smith</td>
    <td>ENDE</td>
  </tr>
</table>
</body> 
</html>
"""
    web_view = QWebView()
    web_view.setHtml(html)
    web_view.print_(printer)
于 2017-06-27T04:42:43.837 回答