void PdfGenerate::generatePdf() {
printer.
setOrientation(QPrinter::Portrait);
//set theorientation of the paper printer.
setOutputFormat(QPrinter::PdfFormat);
//make that printer as a PDF printer.setOutputFileName("test.pdf"); //set the PDF file name
printer.
setPaperSize(QPrinter::Letter);
//set paper size printer.setFullPage(true); //let our app use the full page (we handle margins ourself)
setupLetterPoints(); //set up reference points for our coordinate system
QPainter painter
(&printer
);
//make a painter, which uses this printer,
clause.setHtml("<img src=\"logo.jpg\"><br><b>clause:</b> Here is some text<br>Here is another line of text");
clause.
setDefaultFont(QFont(fontFamily, fontsize
));
QRectF r5
(leftmargin, topmargin,
500,
500);
//space our textdocument is allowed to take up and where it should be located drawrichtext(&painter, r5, 0, clause);
painter.end(); //done drawing, so save the PDF
//open the PDF for viewing:
}
//set up the point coordinates for a letter sheet of paper:
void PdfGenerate::setupLetterPoints() {
//(86 / inch) so it seems..
pagewidth = 731; //8.5 inch
pageheight = 946; //11 inch
margin = 43; // 0.5 inch margin
topmargin = margin;
bottommargin = pageheight - margin;
leftmargin = margin;
rightmargin = pagewidth - margin;
vertmarginwidth = pageheight - (margin * 2);
horizmarginwidth = pagewidth - (margin * 2);
//font sizes:
fontFamily = "Times New Roman";
fontsize = 15;
}
text.
setPageSize(QSize(rect.
width(), QWIDGETSIZE_MAX
));
const int height = qRound(layout->documentSize().height());
int y = rect.y();
if (flags & Qt::AlignBottom)
y += (rect.height() - height);
else if (flags & Qt::AlignVCenter)
y += (rect.height() - height)/2;
context.
palette.
setColor(QPalette::Text, painter
->pen
().
color());
painter->save();
painter->translate(rect.x(), rect.y());
layout->draw(painter, context);
painter->restore();
}
void PdfGenerate::generatePdf() {
QPrinter printer; //create a printer
printer.setOrientation(QPrinter::Portrait); //set theorientation of the paper
printer.setOutputFormat(QPrinter::PdfFormat); //make that printer as a PDF
printer.setOutputFileName("test.pdf"); //set the PDF file name
printer.setPaperSize(QPrinter::Letter); //set paper size
printer.setFullPage(true); //let our app use the full page (we handle margins ourself)
setupLetterPoints(); //set up reference points for our coordinate system
QPainter painter(&printer); //make a painter, which uses this printer,
QTextDocument clause;
QPixmap logo("logo.jpg", "JPG");
clause.addResource(QTextDocument::ImageResource, QUrl("logo.jpg"), logo);
clause.setHtml("<img src=\"logo.jpg\"><br><b>clause:</b> Here is some text<br>Here is another line of text");
clause.setDefaultFont(QFont(fontFamily, fontsize));
QRectF r5(leftmargin, topmargin, 500, 500); //space our textdocument is allowed to take up and where it should be located
drawrichtext(&painter, r5, 0, clause);
painter.end(); //done drawing, so save the PDF
//open the PDF for viewing:
QString sOldPath = QCoreApplication::applicationDirPath();
QDesktopServices::openUrl(QUrl::fromLocalFile(sOldPath + "/test.pdf"));
}
//set up the point coordinates for a letter sheet of paper:
void PdfGenerate::setupLetterPoints() {
//(86 / inch) so it seems..
pagewidth = 731; //8.5 inch
pageheight = 946; //11 inch
margin = 43; // 0.5 inch margin
topmargin = margin;
bottommargin = pageheight - margin;
leftmargin = margin;
rightmargin = pagewidth - margin;
vertmarginwidth = pageheight - (margin * 2);
horizmarginwidth = pagewidth - (margin * 2);
//font sizes:
fontFamily = "Times New Roman";
fontsize = 15;
}
void PdfGenerate::drawrichtext(QPainter *painter, QRectF &rect, int flags, QTextDocument &text) {
text.setPageSize(QSize(rect.width(), QWIDGETSIZE_MAX));
QAbstractTextDocumentLayout* layout = text.documentLayout();
const int height = qRound(layout->documentSize().height());
int y = rect.y();
if (flags & Qt::AlignBottom)
y += (rect.height() - height);
else if (flags & Qt::AlignVCenter)
y += (rect.height() - height)/2;
QAbstractTextDocumentLayout::PaintContext context;
context.palette.setColor(QPalette::Text, painter->pen().color());
painter->save();
painter->translate(rect.x(), rect.y());
layout->draw(painter, context);
painter->restore();
}
To copy to clipboard, switch view to plain text mode
Bookmarks