打印pdf

#include "pdf_print_helper.h"


pdf_print_helper::pdf_print_helper()
{

}

pdf_print_helper::~pdf_print_helper()
{
if (ctx != nullptr && init_status)
{
if (pix !=nullptr)
fz_drop_pixmap(ctx, pix);
if (doc != nullptr)
fz_drop_document(ctx, doc);
if (ctx != nullptr)
fz_drop_context(ctx);
}
}

bool pdf_print_helper::init(QString &message)
{
if (init_status)
return true;
else
{
init_status = init_mupdf_lib(message);
return init_status;
}
}

bool pdf_print_helper::init_mupdf_lib(QString &message)
{
//创建上下文
ctx = fz_new_context(NULL, NULL, FZ_STORE_UNLIMITED);
if (!ctx)
{
message = QString::fromLocal8Bit("不能创建PDF上下文!");
return false;
}
//注册文档控制
fz_try(ctx)
fz_register_document_handlers(ctx);
fz_catch(ctx)
{
message = QString::fromLocal8Bit("无法注册文档处理:") + QString::fromLocal8Bit(fz_caught_message(ctx));
fz_drop_context(ctx);
return false;
}
return true;
}


bool pdf_print_helper::print_pdf(printer_print_pdf_info &print_info, QString &message)
{
if (print_info.pdf_path.empty())
return false;

//打开文档
fz_try(ctx)
doc = fz_open_document(ctx, print_info.pdf_path.c_str());
fz_catch(ctx)
{
message = QString::fromLocal8Bit("打开文件pdf文件失败:")+ QString::fromLocal8Bit(fz_caught_message(ctx));
fz_drop_context(ctx);
return false;
}

//取得总的页数
fz_try(ctx)
page_count = fz_count_pages(ctx, doc);
fz_catch(ctx)
{
message = QString::fromLocal8Bit("不能计算pdf页数!") + QString::fromLocal8Bit(fz_caught_message(ctx));
fz_drop_document(ctx, doc);
fz_drop_context(ctx);
return false;
}

if (page_number < 0 || page_number >= page_count)
{
message = QString::fromLocal8Bit("当前页码: ") + QString::number(page_number + 1) + QString::fromLocal8Bit("超范围总数:") + QString::number(page_count);
fz_drop_document(ctx, doc);
fz_drop_context(ctx);
return false;
}
// (QPrinter::PrinterResolution);
if (print_info.printer_name.empty())
{
printer.setPrinterName(QPrinterInfo::defaultPrinterName());
}
else
printer.setPrinterName(QString::fromLocal8Bit(print_info.printer_name.c_str()));

printer.setFullPage(true);
printer.setOrientation(QPrinter::Portrait);
printer.setPaperSize(QPrinter::A4);
printer.setCopyCount(print_info.print_copies);
printer.setResolution(print_info.printer_dpi);

QPainter painter;
painter.begin(&printer);

//根据打印DPI默认300 计算缩放以及旋转
zoom = print_info.printer_dpi / 72.0f;
fz_scale(&ctm, zoom, zoom);
fz_pre_rotate(&ctm, rotate);

for (int i = 0; i < page_count; i++)
{
fz_try(ctx)
pix = fz_new_pixmap_from_page_number(ctx, doc, i, &ctm, fz_device_rgb(ctx), 0);
fz_catch(ctx)
{
message = QString::fromLocal8Bit("绘制pdf出错") + QString::fromLocal8Bit(fz_caught_message(ctx));
fz_drop_document(ctx, doc);
fz_drop_context(ctx);
return false;
}
unsigned char *samples = pix->samples;
int width = fz_pixmap_width(ctx, pix);
int height = fz_pixmap_height(ctx, pix);
QImage image(samples, width, height, pix->stride, QImage::Format_RGB888);
painter.drawImage(QRect(0, 0, width, height), image);
printer.setFullPage(true);
if (i < page_count - 1)
printer.newPage();
}
return painter.end();
}

posted @ 2019-02-22 16:15  lehoho  阅读(559)  评论(0编辑  收藏  举报