我试图在从QWebView打印页面时打印页码,空白页面即将出现。还有一些帮助,当块被分割成两页时,如何在同一页面中保留一个文本块。
QPrinter printer(QPrinter::HighResolution);
printer.setPageSize(QPrinter::A4);
printer.setOrientation(QPrinter::Landscape);
printer.setOutputFormat(QPrinter::PdfFormat);
QString pdfName =QFileDialog::getSaveFileName(0,
tr("Save pdf"),
QFileDialog::getExistingDirectory(),
tr("PDF Files (*.pdf)"));
if( pdfName == "" )
return ;
printer.setOutputFileName(pdfName);
QWebPage page;
ui->webView->resize (printer.paperRect ().size ());
page.setView (ui->webView);
QPainter painter;
painter.begin (&printer);
page.mainFrame()->setHtml ("<h1> Page 1 </h1>");
page.mainFrame()->render (&painter);
printer.newPage();
page.mainFrame()->setHtml ("<h1> Page 2 </h1>");
page.mainFrame()->render (&painter);
painter.end ();
ui->webView->print(&printer);