gpt4 book ai didi

html - 如何使 QTextDocument 适合打印机的整个页面宽度

转载 作者:行者123 更新时间:2023-11-28 00:50:15 30 4
gpt4 key购买 nike

在较早的 stackoverflow 之后 discussion ,我正在尝试使用 QtQTextDocument 生成包含文本和图像的 pdf。

这是我作为 MCVE 的代码:

#include <QApplication>
#include <QIcon>
#include <QDesktopServices>
#include <QWidget>
#include <QPrinter>
#include <QPainter>
#include <QPagedPaintDevice>
#include <QUrl>
#include <QFile>
#include <QTextDocument>

#include <sstream>
#include <memory>
#include <assert.h>

std::shared_ptr<QPrinter> getPrinter()
{
std::shared_ptr<QPrinter> printer( new QPrinter( QPrinter::ScreenResolution ) );
printer->setOutputFormat(QPrinter::PdfFormat);
printer->setOrientation(QPrinter::Portrait);
printer->setPaperSize(QPrinter::A4);
printer->setPageMargins(10.0,10.0,10.0,10.0,QPrinter::Millimeter);
printer->setOutputFormat(QPrinter::PdfFormat);
printer->setColorMode(QPrinter::Color);
printer->setFullPage( true );
return printer;
}

bool generateReport( const std::string& fileName )
{
auto printer = getPrinter();
QSize pageSize = printer->pageRect().size();

QTextDocument qtdoc; // start with a QTextDocument
qtdoc.setPageSize(pageSize);
qtdoc.setDocumentMargin( 10 );

std::stringstream str;
str << "<html><head/><body>";
str << "<table style=\"width: 100%\" border=\"1\"><tbody><tr><td>Foo</td><td>Bar</td></tr></tbody></table>";
str << "</body></html>";

qtdoc.setHtml(str.str().c_str());

printer->setOutputFileName(fileName.c_str());
qtdoc.print(printer.get());

QDesktopServices::openUrl(QUrl::fromLocalFile(fileName.c_str()));

return true;
}

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

generateReport( "report.pdf" );

return 0;
}

它并没有那么糟糕,但是 html 内容不适合页面:标有“width=100%”的表格实际上并没有占据 pdf 的整个宽度:

enter image description here

我们如何强制 html 内容适合页面(意味着 width=100% 的表格应该占据整个页面宽度!)

最佳答案

问题只是 Qt 无法识别 <table style=\"width: 100%\" border=1>

将其更改为 <table width=100% border=1>修复了问题,表格没有占据整个页面宽度!

关于html - 如何使 QTextDocument 适合打印机的整个页面宽度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47906629/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com