I'm attempting to generate a PDF file from `QTextDocument`, which is working just fine. The issue comes when I want to use a custom font for just a specific `<p></p>` object. I tried using the standard means of `QFontDatabase::addApplicationFont()` which successfully gets the font loaded into the application, but for some reason it writes all of the `QString` based variables that are added to `messageBody` in this font instead of that specified by `QTextFocument.setDefaultFont()`, or even a manually via `style={font-family:}`.
Am I doing this wrong, or overlooking something here, or is this some kind of bug?
int main(int argc, char *argv[])
{
// With this added, all hell breaks loose
messageBody += "<p style='font-size: large;'><b>Serial Number:</b> " + serialNumber + "</p>";
messageBody += "<p style='font-size: large; font-family=\"Console\"'><b>Serial Number:</b> " + serialNumber + "</p>";
document.setHtml(messageBody);
document.setDefaultFont(myfont);
printer.
setOutputFormat(QPrinter::PdfFormat);
printer.setOutputFileName("test.pdf");
document.print(&printer);
return 0;
}
int main(int argc, char *argv[])
{
QApplication a(argc,argv);
// With this added, all hell breaks loose
QFontDatabase::addApplicationFont(":/Barcode.ttf");
QString messageBody;
QString serialNumber = "ABC123";
messageBody += "<p style='font-size: large;'><b>Serial Number:</b> " + serialNumber + "</p>";
messageBody += "<p style='font-size: large; font-family=\"Console\"'><b>Serial Number:</b> " + serialNumber + "</p>";
QTextDocument document;
document.setHtml(messageBody);
QFont myfont("Console", 8, QFont::Normal);
document.setDefaultFont(myfont);
QPrinter printer(QPrinter::PrinterResolution);
printer.setOutputFormat(QPrinter::PdfFormat);
printer.setOutputFileName("test.pdf");
document.print(&printer);
return 0;
}
To copy to clipboard, switch view to plain text mode
In either case I would get the output PDF like so:
SN.png
I can't wrap my head around why/how it would differentiate the base QString (messageBody) from those that are concatenated into it (serialNumber)?
Bookmarks