QtWidget将HTML转换为PDF

时间:2016-07-12 03:03:58

标签: c++ qt pdf

以下代码基于this question

但我仍然无法让它发挥作用。从基本调试我认为将文件名传递给函数有问题。当我点击"打印为PDF"没有任何反应。按钮。

主要代码如下。如果您想进一步了解,请随时向我询问。感谢。

main.cpp的一部分

    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

#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

#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()
{

}

1 个答案:

答案 0 :(得分:0)

我建议您从QTWebKit移植到QTWebEngine,它使用Chromium作为本机浏览器,速度更快,更新。检查细节:

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

使用5.6时,将不再支持Qt WebKit和Qt Quick 1,并且将从该版本中删除。 Qt 5.7集成了printupport for browser,让我们尝试使用Chromium:

首先,包括:

QT       += webengine webenginewidgets printsupport

现在,尝试这样的事情:

// 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);