作者:奋斗ing丶孩子
转自:http://blog.sina.com.cn/s/blog_a6fb6cc90101gvnx.html
图片生成pdf
代码如下:
QPrinter printer_pixmap(QPrinter::HighResolution);
printer_pixmap.setPageSize(QPrinter::A4);
//设置纸张大小为A4
printer_pixmap.setOutputFormat(QPrinter::PdfFormat);
//设置输出格式为pdf
printer_pixmap.setOutputFileName("E:\\test_pixmap.pdf");
//设置输出路径
QPixmap pixmap = QPixmap::grabWidget(main_widget,main_widget->rect());
//获取界面的图片
QPainter painter_pixmap;
painter_pixmap.begin(&printer_pixmap);
QRect rect = painter_pixmap.viewport();
int multiple = rect.width()/pixmap.width();
painter_pixmap.scale(multiple, multiple);//将图像(所有要画的东西)在pdf上放大multiple-1倍
painter_pixmap.drawPixmap(0, 0, pixmap);
//画图
painter_pixmap.end();
文本生成pdf
代码如下:
QPrinter printer_text;
printer_text.setOutputFormat(QPrinter::PdfFormat);
printer_text.setOutputFileName("E:\\test_text.pdf");
QPainter painter_text;
painter_text.begin(&printer_text);
QDateTime current_date_time =QDateTime::currentDateTime();
QString current_date = current_date_time.toString("yyyy-MM-ddhh:mm:ss ddd");
QPoint point(10, 10);
for(int i=1; i<=5; i++)
{
QString message = QString("%1
%2
%3").arg(QString::number(i)).arg(current_date).arg(QStringLiteral("北京"));
int y = point.y();
point.setY(y+20);
painter_text.drawText(point, message);
message.clear();
}
//printer_text.newPage();
//生成新的一页,可继续插入
painter_text.end();
效果如下:
HTML生成pdf
QStringList title;
title.push_back(QStringLiteral("名称"));
title.push_back(QStringLiteral("修改日期"));
title.push_back(QStringLiteral("类型"));
title.push_back(QStringLiteral("大小"));
注: