0

私の要件は非常に基本的なものなので、HTML + QTextBrowser を使用することを考えたので、PDF 請求書を作成しようとしています。必要に応じて HTML コードを作成する関数を作成しました。たとえば、テキストを挿入したい場合はそのための関数があり、テーブルを挿入するために関数などを作成しました。setHtml() 関数を使用してこの HTML コードを QTextBrowser に設定してから、PDF を作成しようとしています。問題は、期待どおりの結果が得られないことです。同じコードを使用し、Chrome ブラウザーですべて問題なく表示されているため、HTML コードに問題はありません。しかし、setHtml 関数で使用した瞬間、Qt は html コードを変更しており、結果は望ましくありません。以下に、予想される画像と結果の画像も含めてコードを投稿しています。

void PDF_API::Save(const QString &filePath)
{ 
    QTextDocument document;
    document.setHtml(HTMLCode); // HTMLCode is a QString obj that holds all my html code

    // Creates a copy in html to check the html code. Comment the code if not needed
    QFile file(QDir::currentPath()+"/report.html");
    if(file.open(QFile::WriteOnly))
    {
        file.write(HTMLCode.toStdString().c_str());
        file.close();
    }

    // Printing begins here
    QPrinter printer(QPrinter::PrinterResolution);
    printer.setOutputFormat(QPrinter::PdfFormat);
    printer.setPaperSize(QPrinter::Tabloid);
    printer.setOutputFileName(filePath);
    printer.setPageMargins(QMarginsF(0.1, 0.01, 0.1, 0.1));
    document.print(&printer);
}

QTextBrowser に設定する前の元の HTML コード

"<html><body><p align=\"center\" style=\"color: #000000; font-family: Times New Roman; font-size: 12pt; line-height: 100%;\">  <b>CENTER </b> </p><hr><p style = \"text-align:left; color: #ff0000; font-family: Times New Roman; font-size: 14pt; line-height: 100%;\"> <b>Left Text</b> <span style = \"float:right; color: #ffff00; font-family: Times New Roman; font-size: 14pt; line-height: 100%;\"> <b>Right Text</b> </span> </p></body></html>"

QTextBrowser によって生成された変更された HTML コード

"<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.0//EN\" \"http://www.w3.org/TR/REC-html40/strict.dtd\">\n<html><head><meta name=\"qrichtext\" content=\"1\" /><style type=\"text/css\">\np, li { white-space: pre-wrap; }\n</style></head><body style=\" font-family:'MS Shell Dlg 2'; font-size:8.25pt; font-weight:400; font-style:normal;\">\n<p align=\"center\" style=\" margin-top:12px; margin-bottom:12px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px; line-height:100%;\"><span style=\" font-family:'Times New Roman'; font-size:12pt; font-weight:600; color:#000000;\">CENTER </span></p>\n<hr />\n<p style=\" margin-top:12px; margin-bottom:12px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px; line-height:100%;\"><span style=\" font-family:'Times New Roman'; font-size:14pt; font-weight:600; color:#ff0000;\">Left Text</span><span style=\" font-family:'Times New Roman'; font-size:14pt; color:#ff0000;\"> </span><span style=\" font-family:'Times New Roman'; font-size:14pt; font-weight:600; color:#ffff00;\">Right Text</span><span style=\" font-family:'Times New Roman'; font-size:14pt; color:#ffff00;\"> </span></p></body></html>"

注::上記の HTML コードは、QString に保持されたデバッグ コンソールで見られるものです。

元の html コードを使用して Chrome ブラウザで見られるように
ここに画像の説明を入力

QTextBrowser で見られるように ここに画像の説明を入力

QTextBrowser が HTML コードを台無しにして、目的の結果を得るのを防ぐにはどうすればよいですか?

PS:私の要件は pdf を印刷することだけだったので、これは以前に何度も発生しました。wkhtmltopdf プロセスと呼ばれる外部プロセスを使用して pdf を作成しましたが、このシナリオではアプリケーション自体で結果を確認したいと考えています。

4

1 に答える 1