#include "catprinter.h"
#include <QtWidgets/QApplication>
#include <QtPrintSupport\QPrinter>
#include <QtPrintSupport\QPrintDialog>
#include <QtWebKitWidgets\QWebFrame>
#include <QtWebKitWidgets\QWebPage>
#include <QtWebKit\QWebElement>
#include <QtGui\QPageSize>
#include <QtCore\QRectF>
#include <QtGui\QPainter>
#include <QtGui\QTextDocument>
#include <QtCore\QFile>
#include <QtCore\QTextCodec>
#include <QPrinterInfo>
#include <windows.h>
#include <QStringList>
#include "zint.h"
#ifdef _DEBUG
#pragma comment(lib, "../Win32/Debug/zintd.dll.lib")
#else
#pragma comment(lib, "../Win32/Release/zint.dll.lib")
#endif
typedef struct stHtml
{
bool bBarcode;
int nTicketSize;
int nPages;
QString strPrinterName;
QString strTicket;
}S_HTML;
void printHtml(const QString &strHtml, S_HTML &shtml);
bool drawBarcode(QPainter &painter, const QString &strImagePath, const QRect &rtBarcode, int width);
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
QString strHtml("");
QStringList list = QCoreApplication::arguments();
S_HTML shtml;
if (list.count() > 6)
{
strHtml = list[1];
shtml.nTicketSize = list[2].toInt();//stSetInfo.nTicketSize;
shtml.nPages = list[3].toInt();//stSetInfo.nPages;
shtml.strPrinterName = list[4];//stSetInfo.strPrinterName;
shtml.bBarcode = list[5].toInt();//mapFormat[E_PRINT_BARCODE]; //modified by zrq 20150930
shtml.strTicket = list[6];//stInfo.strTicket;
printHtml(strHtml, shtml);
//return a.exec();
}
else
//return 0;
//这里调用打印机进行打印
return 0; //a.exec();
}
void printHtml(const QString &strHtml, S_HTML &shtml)
{
if (shtml.strPrinterName.isEmpty())
return;
QPrinter printer;
printer.setPrinterName(shtml.strPrinterName);
//貌似没什么用,不过我还是设置了下
printer.setFullPage(true);
//计算在打印机的DPI下,58mm对应的点数
int dpi=printer.logicalDpiX();
int width=min(shtml.nTicketSize, printer.paperRect(QPrinter::Millimeter).width())*(dpi*1.0/25.4);//58mm
//使用QWebPage来解析并输出解析后的文档到打印机
QWebPage page;
page.mainFrame()->setHtml(strHtml);
QWebFrame *frame=page.mainFrame();
//设置网页视口大小,因为我在html文档中用相对大小布局的
page.setViewportSize(QSize(width,frame->findFirstElement("body").geometry().height()));
if (!shtml.bBarcode)
{
for (int npage = 0; npage < shtml.nPages;)
{
QPainter painter(&printer);
if (painter.isActive())
frame->render(&painter);
++npage;
}
}
else
{
QString strTicket(shtml.strTicket);
if (strTicket.length() > 3)
strTicket = strTicket.mid(3);
QString strImagePath("");
strImagePath = QCoreApplication::applicationDirPath() + "//test.png";
struct zint_symbol * my_symbol = ZBarcode_Create();
my_symbol->symbology = BARCODE_CODE93;//BARCODE_CODE11;
strcpy(my_symbol->outfile, strImagePath.toLocal8Bit().toStdString().c_str());
ZBarcode_Encode(my_symbol, (unsigned char*)strTicket.toLocal8Bit().toStdString().c_str(), 0);
ZBarcode_Print(my_symbol, 0);
ZBarcode_Delete(my_symbol);
QSize imgsize(width, frame->findFirstElement("table.barcode").geometry().height());
QRect rtBarcode(2, frame->findFirstElement("table.barcode").geometry().top(),
imgsize.width(), imgsize.height());
for (int npage = 0; npage < shtml.nPages;)
{
//这里修改下直接打印到图片上看下效果
QPainter painter(&printer);
if (painter.isActive())
frame->render(&painter);
drawBarcode(painter, strImagePath, rtBarcode, width);
++npage;
}
}
}
bool drawBarcode(QPainter &painter, const QString &strImagePath, const QRect &rtBarcode, int width)
{
QImage image(strImagePath, "PNG");
QRect rt1 = image.rect();
QRect rtwindow = painter.window();
QRect rtview = painter.viewport();
int nmax = 0;
if (image.width() + 20 < width)
nmax = max(image.width() + 20, width);
else
nmax = min(image.width() + 20, width);
QRect rtimage(0, 0, nmax, rtwindow.height());
QRect rtimageview(0, 0, nmax, rtview.height());
int nmin = min(image.width(), nmax - 80);
QRectF rtdes(40, rtBarcode.top(), nmin, rtBarcode.height());
QRectF rtsrc(0, 0, image.width(), image.height());
painter.setWindow(rtimage);
painter.setViewport(rtimageview);
painter.drawImage(rtdes, image, rtsrc);
painter.setWindow(rtwindow);
painter.setViewport(rtview);
return true;
}
评论2
最新资源