2016-09-30 12 views
1

我想從HTML和CSS文件創建一個PDF文件。目前我正在使用QWeb使用QWebView,因爲QwebEngineView不支持Ubuntu,因爲沒有MVC2013或鉻。QT的html和CSS創建PDF而不QWebEngineView

我希望創作者能夠獨立於平臺。我顯示的所有代碼在這裏

QDir::setCurrent(QCoreApplication::applicationDirPath()); 

    QFile htmlFile ("myhtml.html"); 
    if (!htmlFile.open(QIODevice::ReadOnly | QIODevice::Text)){ 
     return -1; 
    } 


    QWebView *pWebView = new QWebView(); 
    pWebView->load(QUrl::fromLocalFile(QFileInfo(htmlFile).path())); 


    QPrinter printer(QPrinter::HighResolution); 
    printer.setPageSize(QPrinter::A4); 
    printer.setOutputFormat(QPrinter::PdfFormat); 

    printer.setOutputFileName("output.pdf"); 

    pWebView->print(&printer); 
    delete pWebView; 

HTML文件如下:

<html> 

<head> 
    <link rel="stylesheet" href="style.css" /> 
</head> 
<body> 
    <h1> <center> ULTRA TECH LABORATORIES PRIVATE LIMITED </center> </h1> 
    <h3> <center> CLOTH MARKET, G.E. ROAD KUMHARI, DIST-DRUG(C.G.) </center> </h3> 
    <h1> <center> TLD MONITORING SERVICE - DOSE REPORT </center> </h1> 
    <p> 
     INSTITUTION NO. 017303 
     <span>SERVICE PERIOD: APR-JUN 2012</span> 
    </p> 
    <p> 
     ASHOK LABORATORY, KOLKATA 
     <span>SERVICE FREQUENCY: QUARTERLY</span> 
    </p> 
    <center> 
     <ul> 
     <li>lorem ipsum dolor</li> 
     <li>sit amet</li> 
     <li>foo</li> 
     <li>bar</li> 
     </ul> 
    </center> 
</body> 

的CSS文件如下

ul { 
    width: 500px; 
    } 
li { 
     width: 50%; 
     float: left; 
     border: 1px solid #000; 
     margin: 0; 
     list-style-type: none; 
     box-sizing: border-box; 
} 
li:nth-child(odd) { 
    clear: left; 
} 

p { 
    text-align:left; 
} 
span { 
    float:right; 
} 
+0

我也曾嘗試QWebView :: setHtml()和QWebView :: setDefaultStyleSheet()他們也給格式化輸出。 – user3864806

+0

我在找這個庫wkhtmltopdf,它爲此使用了QtWebKit。我試圖找出他們是如何做到的,但代碼搞砸了。 – user3864806

回答

0

首先,更換

pWebView->load(QUrl::fromLocalFile(QFileInfo(htmlFile).path())); 

pWebView->load(QUrl::fromLocalFile(QFileInfo(htmlFile).absoluteFilePath())); 

我不改變HTML或CSS的東西。 Css位於html附近。

然後,在連接到loadFinished信號的插槽中實施保存到pdf。因爲當您將頁面保存爲pdf時,它可能尚未加載。

connect (pWebView,SIGNAL(loadFinished(bool)), this,SLOT(savePDF(bool))); 

void Form::savePDF(bool ok) 
{ 
    QPrinter printer(QPrinter::HighResolution); 
    printer.setPageSize(QPrinter::A4); 
    printer.setOutputFormat(QPrinter::PdfFormat); 
    printer.setOutputFileName("output.pdf"); 
    ui->webView->print(&printer); 
} 

對我來說,你的代碼工作以後。

編輯:

我用Form類只是用於測試,您需要把代碼添加到您的類。

form.h:

class Form: public QObject{ 
    Q_OBJECT 

public: 
    explicit Form(QObject * parent = 0); 

private: 
    QWebView* webView; 

private slots: 
    void savePDF(bool ok); 
}; 

form.cpp:

Form::Form(QObject *parent): 
    QObject(parent) 
{ 

    webView = new QWebView(); 
    QFile htmlFile ("html/index.html"); 
    if (!htmlFile.open(QIODevice::ReadOnly | QIODevice::Text)){ 
     qDebug()<<"nosuch file"; 
    } 
    webView->load(QUrl::fromLocalFile(QFileInfo(htmlFile).absoluteFilePath())); 
    connect (webView,SIGNAL(loadFinished(bool)), this,SLOT(savePDF(bool))); 
    webView->show(); //comment it to hide webview windows 
} 

void Form::savePDF(bool ok) 
{ 
    QPrinter printer(QPrinter::HighResolution); 
    printer.setPageSize(QPrinter::A4); 
    printer.setOutputFormat(QPrinter::PdfFormat); 
    printer.setOutputFileName("output.pdf"); 
    webView->print(&printer); 
} 
+0

你能給我所有的文件嗎?什麼是Form類?你在哪裏寫連接功能? – user3864806

+0

Object :: connect:沒有這樣的插槽MainWindow :: savePDF(布爾)這個錯誤出現在我的代碼在運行時。 – user3864806