gpt4 book ai didi

qt - Qt 项目的 Svg 到 pdf 转换器

转载 作者:行者123 更新时间:2023-12-05 06:33:21 28 4
gpt4 key购买 nike

我有多个 svg 文件,我希望将它们转换为 PDF 格式。问题是我需要使用多种文本字体,而一个简单的 QSvgRenderer 是不够的。

例如: enter image description here

我的问题是我需要在我的项目中集成一个不使用外部依赖项的库(只是一个可以用作独立实体的库)。

唯一能部分满足我需求的图书馆是librsvg但我没有经验,也找不到将其集成到 Qt 项目中的方法。

有人可以给我一些提示或者可以指出正确的方向吗?

编辑

对于基本的东西,使用 QSvgRenderer 时没有一种字体可以显示在右边:

  QSvgRenderer renderer;
renderer.load((QString)"E:/TEXT4.svg");

QPrinter printer;
printer.setOutputFormat(QPrinter::PdfFormat);
printer.setOutputFileName("E:/TEXT4.pdf");

QPainter painter(&printer);
renderer.render(&painter);
painter.end();

enter image description here

最佳答案

可能有点晚了,但遇到了同样的问题并且找到了仅使用 Qt 的解决方案。你的问题是你需要手动安装字体并且需要一个QApplication。例如:

int main(int argc, char** argv)
{
// A QApplication instance is necessary if fonts are used in the SVG
QApplication app(argc, argv);

int id = QFontDatabase::addApplicationFont(":/DejaVuSans.ttf");
if (id != -1)
{
QStringList font_families = QFontDatabase::applicationFontFamilies(id);
// This prints: DejaVu Sans
for (const auto& item : font_families)
{
std::cout << item.toStdString() << std::endl;
}
}

// Load your SVG
QSvgRenderer renderer(QString("myimage.svg"));

QPdfWriter pdfWriter(QString("mydoc.pdf"));
pdfWriter.setPageSize(QPageSize(QPageSize::A4));
QPainter painter(&pdfWriter);
renderer.render(&painter);
}

还使用多个不同的字体系列进行了测试,并且有效。

注意:据我所知,您不能在 Qt 中更改字体名称,因此在 SVG 中 font-family 必须与添加的字体名称完全匹配。在此示例中,font-family:DejaVu Sans

关于qt - Qt 项目的 Svg 到 pdf 转换器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50796677/

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