首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >QtWidget将HTML转换为PDF

QtWidget将HTML转换为PDF
EN

Stack Overflow用户
提问于 2016-07-12 11:03:58
回答 1查看 759关注 0票数 0

下面的代码基于this question

但我还是不能让它工作。从基本的调试来看,我认为将文件名传递给函数是有问题的。当我点击"Print as PDF“按钮时,没有任何反应。

主要代码如下所示。如果您想了解更多,请随时向我询问。谢谢。

main.cpp的一部分

代码语言:javascript
复制
    QApplication app(argc, argv);

    //Get filename for all documents
    QString filename = get_filename(Var_STR);
    QString txt = txt_filename(filename);
    QString csv = csv_filename(filename);
    QString html = html_filename(filename);

    //Print report in .txt, .csv and .html accordingly
    heading(txt, csv, html, Var_STR, Var_INT[6], Var_INT[16], Var_INT[17]);
    pp_prerinse(txt, csv, html, Var_INT[18], Var_INT[19], Var_INT[20], Var_INT[21], Var_INT[22], Var_INT[23], Var_INT[24], Var_INT[25], Var_INT[26], Var_REAL[11], Var_INT[30], Var_INT[31], Var_INT[32]);
    pp_wash(txt, csv, html, Var_STR, Var_INT[33], Var_INT[34], Var_INT[35], Var_INT[36], Var_INT[37], Var_INT[38], Var_REAL[23], Var_INT[41], Var_INT[42], Var_INT[43], Var_REAL[28], Var_INT[47], Var_INT[48], Var_INT[49], Var_INT[50], Var_INT[51], Var_INT[52], Var_INT[53], Var_INT[54], Var_INT[55], Var_REAL[40]);
    pp_rinse(txt, csv, html, Var_INT[59], Var_INT[60], Var_INT[61], Var_INT[62], Var_INT[63], Var_INT[64], Var_INT[65], Var_INT[66], Var_INT[67], Var_INT[68], Var_INT[69], Var_INT[70], Var_INT[71], Var_INT[72], Var_INT[73], Var_INT[74], Var_INT[75], Var_INT[76], Var_INT[77], Var_REAL[61]);
    phase_prerinse(txt, csv, html, Var_STR, Var_REAL[64], Var_REAL[67], Var_REAL[70]);
    wash(txt, csv, html, Var_STR, Var_REAL[73], Var_REAL[75], Var_REAL[77], Var_REAL[79], Var_REAL[81], Var_REAL[83], Var_REAL[85], Var_REAL[87], Var_REAL[89], Var_REAL[91], Var_REAL[94], Var_REAL[97]);
    rinse(txt, csv, html, Var_INT[129], Var_STR);
    basin_flush(txt, csv, html, Var_STR);
    alarms_code(txt, csv, html, Var_REAL[100], Var_REAL[103], Var_REAL[106]);
    tail(txt, csv, html);

    //Report preview UI and convert .html to .pdf
    QDir htmlpath = QFileInfo(html).absoluteDir();
    MainWindow mainWindow((htmlpath.absolutePath())+"/"+html, filename);
    mainWindow.setWindowTitle("Print Preview");
    mainWindow.showMaximized();
    return app.exec();

mainwindow.h

代码语言:javascript
复制
#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QObject>
#include <QMainWindow>
#include <QWebView>
#include <QUrl>
#include <QPushButton>
#include <QString>
#include "windows.h"
#include <algorithm>
#include "qt_windows.h"
#include "qwindowdefs_win.h"
#include <ShellAPI.h>
namespace Ui
{
    class MainWindow;
    class QPrinter;
}

class MainWindow : public QMainWindow
{
    Q_OBJECT

public:
    explicit MainWindow(QString previewfile = "", QString file = "", QWidget *parent = 0);
    virtual ~MainWindow();

public slots:
    void buttonPrint(QString filepath);
    void buttonCancel();

private:
    QWebView *m_pWebView; //Preview of the report layout
    QPushButton *m_button; //Print button
    QPushButton *n_button; //Cancel button
    QString printfile;
    QString filepath;
};

#endif // MAINWINDOW_H

mainwindow.cpp

代码语言:javascript
复制
#include "stdafx.h"
#include "mainwindow.h"
#include <QPrinter>
#include <QTextDocument>
#include <QTextStream>
#include <QFile>
#include <QDir>


MainWindow::MainWindow(QString previewfile, QString file, QWidget *parent)
    : QMainWindow(parent)
{
    //Open html version of report
    m_pWebView = new QWebView(this);

    //Set position and size
    m_pWebView->setGeometry(0, 0, 1000, 735);
    m_pWebView->load(QUrl::fromLocalFile(previewfile));

    //Create "print" button
    m_button = new QPushButton("Print as PDF", this);

    //Set location of button
    m_button->setGeometry(QRect(QPoint(1100, 150), QSize(75, 23)));

    //Print button action
    connect(m_button, SIGNAL(clicked()), this, SLOT(buttonPrint(file)));

    n_button = new QPushButton("Cancel", this);

    n_button->setGeometry(QRect(QPoint(1100, 179), QSize(75, 23)));

    connect(n_button, SIGNAL(clicked()), this, SLOT(buttonCancel()));
}

void MainWindow::buttonPrint(QString filepath)
{
    QFile htmlfile(filepath+".html");
    if(htmlfile.open(QIODevice::ReadOnly | QIODevice::Text))
    {
        QString htmlContent;
        QTextStream in(&htmlfile);
        htmlContent = in.readAll();

        QTextDocument *document = new QTextDocument();
        document->setHtml(htmlContent);

        QPrinter printer(QPrinter::HighResolution);
        printer.setOutputFormat(QPrinter::PdfFormat);
        printer.setOutputFileName(filepath+".pdf");

        document->print(&printer);
        delete document;
        buttonCancel();
    }
}

void MainWindow::buttonCancel()
{
    QApplication::quit();
}

MainWindow::~MainWindow()
{

}
EN

回答 1

Stack Overflow用户

发布于 2016-07-12 16:25:42

我建议你从QTWebKit移植到QTWebEngine,它使用Chromium作为原生浏览器,速度更快,而且更新。请查看详细信息:

http://doc.qt.io/qt-5/qtwebenginewidgets-qtwebkitportingguide.html

在5.6版本中,Qt WebKit和Qt Quick1将不再受支持,并从版本中删除。Qt 5.7集成了对浏览器的打印支持,所以让我们尝试使用Chromium:

首先,包括:

代码语言:javascript
复制
QT       += webengine webenginewidgets printsupport

现在,尝试如下所示:

代码语言:javascript
复制
// Create the webview
QWebEngineView *webView = new QWebEngineView(this);
webView->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
ui->centralWidget->layout()->addWidget(webView);

// Path to the HTML file
const QString filePath =  QFileDialog::getOpenFileName(this, "Import HTML", ".", "HTML Files (*.html)");
// Check if the file exist
QFileInfo fileInfo(filePath);
if (!fileInfo.isFile()) {
    qDebug() << "Warning, file not found!";
}
// Preview the HTML file
webView->load(QUrl::fromLocalFile(fileInfo.filePath()));
// How to print the page?
// Get the path to the pdf file
const QString pdfPath = QFileDialog::getSaveFileName(this, "Export to pdf", ".", "PDF Files (*.pdf)");
// Print the page in pdf format
webView->page()->printToPdf(pdfPath);
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/38319368

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档