Qt4中用QPrinter实现
QPrinter不止可以操作打印机来打印纸张文件,并且可以将文件保存至磁盘,存储为pdf格式的文件。
首先在pro文件中加入
QT+=printsupport
然后包含头文件
#include <QPrinter>
文本生成pdf:
//文本生成pdf
QPrinter text_printer; //文本生成不要设置resolution,否则输出会乱掉
text_printer.setOutputFormat(QPrinter::PdfFormat);
text_printer.setOutputFileName("test_text.pdf"); //直接设置输出文件路径,相对或者绝对路径
text_painter.begin(&text_printer);
for(int i=0;i<5;i++)
text_painter.drawText(10,i*30,"hello world"); //按照坐标(或矩形)输出,文本为QString类型,有多种重载函数,随便用哪一种
text_painter.end();
图片生成pdf:
//图片生成pdf
QPrinter pic_printer(QPrinter::ScreenResolution); //用一种分辨率初始化
//pic_printer.setResolution(QPrinter::HighResolution); //也可以设置其他的分辨率
pic_printer.setPageSize(QPrinter::A4); //设置纸张尺寸,默认不设置就是A4
pic_printer.setOutputFormat(QPrinter::PdfFormat); //设置输出格式pdf
QString file_path = QFileDialog::getSaveFileName(this,"Export PDF",QString(),"*.pdf"); //用文件对话框设置输出路径
if(!file_path.isEmpty())
{
//如果没有写后缀就自动加上
if(QFileInfo(file_path).suffix().isEmpty())
{
file_path.append(".pdf"); //或者 file_path+=".pdf"
}
}
pic_printer.setOutputFileName(file_path);
QPixmap pixmap=QPixmap::grabWidget(ui->centralWidget,ui->centralWidget->rect()); //抓取界面widget区域,可以抓取任意控件区域,Qt5推荐新的API QWidget::grab
QPainter pic_painter;
pic_painter.begin(&pic_printer);
QRect rect=pic_painter.viewport(); //获取painter的视口区域
int factor=rect.width()/pixmap.width(); //计算painter视口区域与抓取图片区域的尺寸比例因子
pic_painter.scale(factor,factor); //绘制时按照比例因子放大
pic_painter.drawPixmap(10,10,pixmap); //按照坐标画图
pic_printer.newPage(); //另起一页
//pic_painter.scale(1,1); //回复比例,否则字体很大不正常,此步貌似不需要
pic_painter.drawText(10,10,"this is another page"); //图文混排
pic_painter.end();
html生成pdf:
建立的test.thml文档为
<!DOCTYPE html>
<html>
<body>
<h1><font color=red>pdf test</font></h1>
<p>below is a table</p>
<table width="200" border="1">
<tr>
<th scope="row">id</th>
<td>书名</td>
<td>价格</td>
</tr>
<tr>
<th scope="row">1</th>
<td>C++ primer</td>
<td>102元</td>
</tr>
<tr>
<th scope="row">2</th>
<td>Thinking in Java</td>
<td>96元</td>
</tr>
</table>
<body>
</html>
//html生成PDF
QFile file;
file.setFileName("test.html");
if(!file.open(QIODevice::ReadOnly))
return;
QTextStream in(&file);
QString html_text=in.readAll();
file.close();
QPrinter html_printer;
html_printer.setOutputFormat(QPrinter::PdfFormat);
html_printer.setOutputFileName("test_html.pdf");
QTextDocument doc;
doc.setHtml(html_text);
doc.print(&html_printer);
Qt5中可以用QPdfWriter实现
包含头文件
#include <QPdfWriter>
Qt5中新出来的类QPdfWriter,可以很快实现导出pdf,只需要调用QPainter绘画文字、图片即可。
QPdfWriter中还有如下方法:
- void setCreator(const QString & creator)//设置PDF作者
- bool setPageLayout(const QPageLayout & newPageLayout)//设置新页面的页面布局
- bool setPageMargins(const QMarginsF & margins)//设置页边距
- bool setPageMargins(const QMarginsF & margins, QPageLayout::Unit units)
- bool setPageOrientation(QPageLayout::Orientation orientation)
- bool setPageSize(const QPageSize & pageSize)//设置页面大小A4,B4
- void setResolution(int resolution)//设置分辨率
- void setTitle(const QString & title)//设置标题
实例:
//Qt5 pdfwriter生成pdf
QFile pdf_file("QPdfWriter.pdf");
pdf_file.open(QIODevice::WriteOnly);
QPdfWriter *pdf_writer = new QPdfWriter(&pdf_file);
QPainter *pdf_painter= new QPainter(pdf_writer);
pdf_writer->setPageSize(QPagedPaintDevice::A5);
pdf_painter->drawText(QRect(100, 100, 2000, 200), "pdf writer1");//第一个页面
pdf_writer->newPage();
pdf_painter->drawText(QRect(100, 100, 2000, 200), "pdf writer2");//第二个页面
delete pdf_painter;
delete pdf_writer;
pdf_file.close();