2017-03-20 17 views
0

QStringを使ってHTMLを生成し、QTextDocumentでpdfに変換しました。 ただし、空白は1つだけ残っています。qt5私が挿入した空白をすべて食べる

は、私は「」で指定されたASC値を置き換えるためにGoogleからの方法を試してみました。このような& NBSP &#160 &#8194 &#8195など。

私はHTMLでそれらを入力すると、それはOKです。

QStringの中にHTMLコンテンツを設定した場合、それは が直接白space.eg HTMLで表示されていないが、OKではありません。

"<p>&nbsp</p>" 

誰でもお手伝いできますか?

答えて

2

だけでは、PDFの例available from Qtに、一般的なHTMLを試してみました。ここで

は私のhtmlコードです:

Chromeで次のようにレンダリングし
<!DOCTYPE html> 
<html> 

<head> 
    <meta charset="UTF-8"> 
    <title>title</title> 
</head> 

<body> 
    <h1>Hello, World!</h1> 
    <p>Words&nbsp;separated&nbsp;with&nbsp;non&nbsp;breakable&nbsp;space</p> 
    <p>Lorem ipsum dolor sit amet, consectitur adipisci elit.</p> 
</body> 

</html> 

:QTextDocumentクラスを経由して、PDFへの変換、単純なHTMLを使用して次に

enter image description here

#include <QApplication> 

#include <QString> 
#include <QPrinter> 
#include <QTextDocument> 
#include <QWidget> 
#include <QFileDialog> 

int main(int argc, char *argv[]) 
{ 
    QApplication a(argc, argv); 

    QString fileName = QFileDialog::getSaveFileName((QWidget*)0, "Export PDF", QString(), "*.pdf"); 
    if (QFileInfo(fileName).suffix().isEmpty()) { fileName.append(".pdf"); } 

    QPrinter printer(QPrinter::PrinterResolution); 
    printer.setOutputFormat(QPrinter::PdfFormat); 
    printer.setPaperSize(QPrinter::A4); 
    printer.setOutputFileName(fileName); 

    QTextDocument doc; 
    doc.setHtml("<h1>Hello, World!</h1><p>Words&nbsp;separated&nbsp;with&nbsp;non&nbsp;breakable&nbsp;space</p><p>Lorem ipsum dolor sit amet, consectitur adipisci elit.</p>"); 
    doc.setPageSize(printer.pageRect().size()); // This is necessary if you want to hide the page number 
    doc.print(&printer); 

    return a.exec(); 
} 

これをレンダリングする出力PDFファイルを与えます: enter image description here

私はスペースが削除されていないことがわかっています。 このコードスニペットは、あなたが望むものを達成するのに役立ちますか?

またはあなたの簡単なコンパイルの例を供給することができますので、私たちが間違っているかどうか確認できますか?

関連する問題