1

我试图在从 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);
4

0 回答 0