Commit 878caa62 by 李定达

1.添加标签打印模板支持;未做详细测试;

parent d8f2680b
No preview for this file type
...@@ -53,6 +53,63 @@ PrintLib::PrintLib(void) ...@@ -53,6 +53,63 @@ PrintLib::PrintLib(void)
//m_b_load_sucfl = LoadAddress(); //m_b_load_sucfl = LoadAddress();
} }
void PrintLib::_LabPrintLine(rapidxml::xml_node<> *node,rapidjson::Document &jsonDoc, rapidjson::Value &childNode)
{
LabLinePrintArguments lineArg = _LabParsePrintArgumnets(node);//属性值解析
//解析xml中math,获取信息
xml_node<> *mathNode = node->first_node();
while (mathNode != NULL) {
std::string formatRule;
std::vector<std::string> mathVariablVec;
MathArguments mathArg = _ParseMathArgumnets(mathNode);
bool isMath = false;
Calculator::SplitMath(mathArg.mathContent,mathVariablVec,formatRule ,isMath);
xml_attribute<>* attr = mathNode->first_attribute("index");
std::string mathIndex = attr->value();
if(!isMath){ //从json document中查找到content中的字段信息
std::string field_value = _GetMathValue(jsonDoc,mathArg.mathContent,childNode);
_ReplaceText(mathArg,field_value,lineArg.text,mathIndex);//替换lineArg中的text字段
}
else
{//math 中含有数学表达式
int paraSize =mathVariablVec.size();
int itr = 0;
for(int i = 0;i<paraSize;i++){
std::string index = "%";
char buffer[8];
sprintf(buffer, "%d", i+1);
index = index+buffer; //类似%1,%2
std::string key =mathVariablVec[i];
int nNum = 0;
int nret = sscanf(key.c_str(),"%d",&nNum);
if(0 !=nret ){
int nPos = formatRule.find(index);
if (nPos != std::string::npos)
{
formatRule.replace(nPos, 2, key);
}
continue;
}
//从json childNode中查找到content中的字段信息
std::string field_value = _GetMathValue(jsonDoc,key,childNode);
_ReplaceText(mathArg,field_value,formatRule,buffer);
}
formatRule.insert(0,"(");
formatRule.append(")");
double result=Calculator::parenth_final(formatRule.c_str());
std::string temp =std::to_string(static_cast<long double>(result));
std::string placeHold = "%"+mathIndex;
int mathPos = lineArg.text.find(placeHold);
if (mathPos != std::string::npos)
{
lineArg.text.replace(mathPos,2,temp);
}
}
mathNode = mathNode->next_sibling();
}
_LabPrintData(lineArg);
}
void PrintLib::_PrintLine(rapidxml::xml_node<> *node,rapidjson::Document &jsonDoc, rapidjson::Value &childNode) void PrintLib::_PrintLine(rapidxml::xml_node<> *node,rapidjson::Document &jsonDoc, rapidjson::Value &childNode)
{ {
LinePrintArguments lineArg = _ParsePrintArgumnets(node);//属性值解析 LinePrintArguments lineArg = _ParsePrintArgumnets(node);//属性值解析
...@@ -186,6 +243,32 @@ PrintLib& PrintLib::GetInstance() ...@@ -186,6 +243,32 @@ PrintLib& PrintLib::GetInstance()
return fp; return fp;
} }
bool PrintLib::_LabPrintOrder(rapidxml::xml_node<> * xmlNode,rapidjson::Document& doc)
{
rapidxml::xml_node<> *node;
node = xmlNode->first_node();
//循环解析xml
for(;node!=NULL;node = node->next_sibling())
{
rapidxml::xml_node<> * childMath = node->first_node(); //<math
if(childMath == NULL)
continue;
std::string nodeName = node->name();
LOG() << "_LabPrintOrder::node->name : " << node->name();
rapidjson::Value paraValue;
if (nodeName.compare("line") == 0)
{
_LabPrintLine(node,doc,paraValue);
}
}
return true;
}
bool PrintLib::_PrintOrder(rapidxml::xml_node<> * xmlNode,rapidjson::Document& doc) bool PrintLib::_PrintOrder(rapidxml::xml_node<> * xmlNode,rapidjson::Document& doc)
{ {
rapidxml::xml_node<> *node; rapidxml::xml_node<> *node;
...@@ -216,7 +299,7 @@ bool PrintLib::_PrintDoc( int typeCode,rapidxml::xml_node<> * xmlNode,rapidjson: ...@@ -216,7 +299,7 @@ bool PrintLib::_PrintDoc( int typeCode,rapidxml::xml_node<> * xmlNode,rapidjson:
return _PrintOrder(xmlNode,doc); return _PrintOrder(xmlNode,doc);
break; break;
case 2: case 2:
//return _PrintLab(e,json,error); return _LabPrintOrder(xmlNode,doc);
break; break;
default: default:
return _PrintOrder(xmlNode,doc); return _PrintOrder(xmlNode,doc);
...@@ -225,6 +308,46 @@ bool PrintLib::_PrintDoc( int typeCode,rapidxml::xml_node<> * xmlNode,rapidjson: ...@@ -225,6 +308,46 @@ bool PrintLib::_PrintDoc( int typeCode,rapidxml::xml_node<> * xmlNode,rapidjson:
return true; return true;
} }
bool PrintLib::DoLabPrint(std::string prtStr, std::string orderStr, std::string xmlStr, std::string &error)
{
LOG() << "Printer Config Info:" << prtStr;
LOG() << "Order Info:" << orderStr;
LOG() << "XML Info:" << xmlStr;
char prtjson[1024] = {0};
memcpy(prtjson, prtStr.c_str(), prtStr.size());
if(!LabPrtOpen(prtjson)){
f_ClosePrinter(2);
error = "LabPrtOpen failed";
LOG() << "LabPrtOpen failed";
return false;
}
Document dJson;
int intType = 2;
if (_ParseJson(orderStr,dJson))
{
stringstream stm;
stm << xmlStr;
file<> fdoc(stm);
xml_document<> doc;
doc.parse<0>(fdoc.data());
xml_node<>* rootTicket = doc.first_node();
xml_attribute<>* p = rootTicket->first_attribute("typeCode");
intType = strtol(p->value(), NULL, 10);
_PrintDoc(intType,rootTicket,dJson);
}
else
{
LOG() << "_ParseJson failed";
}
f_ClosePrinter(intType);
return true;
}
bool PrintLib::DoPrint(std::string prtStr, std::string orderStr, std::string xmlStr, std::string &error) bool PrintLib::DoPrint(std::string prtStr, std::string orderStr, std::string xmlStr, std::string &error)
{ {
LOG() << "Printer Config Info:" << prtStr; LOG() << "Printer Config Info:" << prtStr;
...@@ -244,7 +367,7 @@ bool PrintLib::DoPrint(std::string prtStr, std::string orderStr, std::string xml ...@@ -244,7 +367,7 @@ bool PrintLib::DoPrint(std::string prtStr, std::string orderStr, std::string xml
memcpy(prtjson, prtStr.c_str(), prtStr.size()); memcpy(prtjson, prtStr.c_str(), prtStr.size());
if(!f_OpenPrinter(prtjson)){ if(!f_OpenPrinter(prtjson)){
f_ClosePrinter(); f_ClosePrinter(1);
error = "OpenPrinter failed"; error = "OpenPrinter failed";
LOG() << "OpenPrinter failed"; LOG() << "OpenPrinter failed";
return false; return false;
...@@ -252,6 +375,7 @@ bool PrintLib::DoPrint(std::string prtStr, std::string orderStr, std::string xml ...@@ -252,6 +375,7 @@ bool PrintLib::DoPrint(std::string prtStr, std::string orderStr, std::string xml
Document dJson; Document dJson;
int intType = 1;
if (_ParseJson(orderStr,dJson)) if (_ParseJson(orderStr,dJson))
{ {
stringstream stm; stringstream stm;
...@@ -261,14 +385,14 @@ bool PrintLib::DoPrint(std::string prtStr, std::string orderStr, std::string xml ...@@ -261,14 +385,14 @@ bool PrintLib::DoPrint(std::string prtStr, std::string orderStr, std::string xml
doc.parse<0>(fdoc.data()); doc.parse<0>(fdoc.data());
xml_node<>* rootTicket = doc.first_node(); xml_node<>* rootTicket = doc.first_node();
xml_attribute<>* p = rootTicket->first_attribute("typeCode"); xml_attribute<>* p = rootTicket->first_attribute("typeCode");
int intType = strtol(p->value(), NULL, 10); intType = strtol(p->value(), NULL, 10);
_PrintDoc(intType,rootTicket,dJson); _PrintDoc(intType,rootTicket,dJson);
} }
else else
{ {
LOG() << "_ParseJson failed"; LOG() << "_ParseJson failed";
} }
f_ClosePrinter(); f_ClosePrinter(intType);
return true; return true;
} }
...@@ -750,6 +874,60 @@ std::string PrintLib::_GetPrintIni() ...@@ -750,6 +874,60 @@ std::string PrintLib::_GetPrintIni()
return parseValue; return parseValue;
} }
LabLinePrintArguments PrintLib::_LabParsePrintArgumnets( const rapidxml::xml_node<>* e )
{
LabLinePrintArguments arguments;
// 打印的数据
rapidxml::xml_attribute<char> * attr=e->first_attribute("text");
if(attr != NULL)
{
arguments.text = attr->value();
}
// 打印类型
attr=e->first_attribute("printType");
if(attr != NULL)
{
arguments.printType = attr->value();
}
// x
attr=e->first_attribute("x");
if(attr != NULL)
{
arguments.x = std::atoi(attr->value());
}
else
{
arguments.x = 0;
}
// y
attr=e->first_attribute("y");
if(attr != NULL)
{
arguments.y = std::atoi(attr->value());
}
else
{
arguments.y = 0;
}
//line
attr=e->first_attribute("wordwrap");
if(attr != NULL)
{
arguments.line = std::atoi(attr->value());
}
else
{
arguments.line = 0;
}
return arguments;
}
LinePrintArguments PrintLib::_ParsePrintArgumnets( const rapidxml::xml_node<>* e ) LinePrintArguments PrintLib::_ParsePrintArgumnets( const rapidxml::xml_node<>* e )
{ {
LinePrintArguments arguments; LinePrintArguments arguments;
...@@ -910,6 +1088,25 @@ void PrintLib::_PrintData( const LinePrintArguments& arguments ) ...@@ -910,6 +1088,25 @@ void PrintLib::_PrintData( const LinePrintArguments& arguments )
} }
void PrintLib::_LabPrintData( const LabLinePrintArguments& arguments )
{
std::string text = arguments.text;
std::string printType = arguments.printType;
int x = arguments.x;
int y = arguments.y;
int line = arguments.line;
char *temchar = new char[2048];
memset(temchar, 0, 2048);
strcpy(temchar,text.c_str());
if(!printType.compare("Text")){
//调用FMPOSDLL打印单行数据
LabPrtLine(x,y,line,temchar) ;
}
delete []temchar;
}
//void GetProcPath(std::string &str) //void GetProcPath(std::string &str)
//{ //{
// int curPos; // int curPos;
...@@ -1036,7 +1233,7 @@ bool PrintLib::_ParseJson(std::string jsonStr,rapidjson::Document &jsonDoc) ...@@ -1036,7 +1233,7 @@ bool PrintLib::_ParseJson(std::string jsonStr,rapidjson::Document &jsonDoc)
jsonDoc.Parse<0>(jsonStr.c_str()); jsonDoc.Parse<0>(jsonStr.c_str());
if (jsonDoc.HasParseError()) if (jsonDoc.HasParseError())
{ {
printf("GetParseError %s\n", jsonDoc.GetParseError()); LOG() << "GetParseError" << jsonDoc.GetParseError();
return false; return false;
} }
return true; return true;
......
...@@ -39,7 +39,19 @@ struct LinePrintArguments{ ...@@ -39,7 +39,19 @@ struct LinePrintArguments{
std::string text ; std::string text ;
// 打印类型 // 打印类型
std::string printType; std::string printType;
};
struct LabLinePrintArguments{
// x坐标
int x;
// y坐标
int y;
// 自动换行
int line ;
// 打印的数据
std::string text ;
// 打印类型
std::string printType;
}; };
struct MathArguments{ struct MathArguments{
...@@ -67,6 +79,8 @@ public: ...@@ -67,6 +79,8 @@ public:
//static void GetProcPath(std::string &str); //static void GetProcPath(std::string &str);
bool DoLabPrint(std::string prtStr, std::string orderStr, std::string xmlStr, std::string &error);
private: private:
//typedef bool ( *OpenPrint)( void); //typedef bool ( *OpenPrint)( void);
//typedef void ( *InitPrinter)( char str[]); //typedef void ( *InitPrinter)( char str[]);
...@@ -124,6 +138,11 @@ private: ...@@ -124,6 +138,11 @@ private:
* */ * */
bool _PrintOrder(rapidxml::xml_node<> * xmlNode,rapidjson::Document &doc); bool _PrintOrder(rapidxml::xml_node<> * xmlNode,rapidjson::Document &doc);
/* 功能:打印订单
* 参数:[1]打印模版,[2]打印内容json,[3]错误信息
* 返回:成功与否
* */
bool _LabPrintOrder(rapidxml::xml_node<> * xmlNode,rapidjson::Document& doc);
/* 功能:解析打印属性表 /* 功能:解析打印属性表
* 参数:[1]xml element * 参数:[1]xml element
...@@ -131,6 +150,12 @@ private: ...@@ -131,6 +150,12 @@ private:
* */ * */
LinePrintArguments _ParsePrintArgumnets(const rapidxml::xml_node<>* e); LinePrintArguments _ParsePrintArgumnets(const rapidxml::xml_node<>* e);
/* 功能:解析打印属性表
* 参数:[1]xml element
* 返回:LinePrintArguments 对象
* */
LabLinePrintArguments _LabParsePrintArgumnets(const rapidxml::xml_node<>* e);
/* 功能:解析math属性表 /* 功能:解析math属性表
* 参数:[1]xml element * 参数:[1]xml element
* 返回:MathArguments对象 * 返回:MathArguments对象
...@@ -143,6 +168,12 @@ private: ...@@ -143,6 +168,12 @@ private:
* */ * */
void _PrintData(const LinePrintArguments& arguments); void _PrintData(const LinePrintArguments& arguments);
/* 功能:打印数据
* 参数:[1]打印参数
* 返回:NULL
* */
void _LabPrintData(const LabLinePrintArguments& arguments);
/* 功能:从json中获取某一字段的信息 /* 功能:从json中获取某一字段的信息
* 参数:[1]jsonDoc,json document ,[2]toMatch,待匹配字符串,[3]childNode,json子串 * 参数:[1]jsonDoc,json document ,[2]toMatch,待匹配字符串,[3]childNode,json子串
* 返回:NULL * 返回:NULL
...@@ -165,6 +196,12 @@ private: ...@@ -165,6 +196,12 @@ private:
* 参数:[1]xml模板,[2]打印内容json[3]结点数据 * 参数:[1]xml模板,[2]打印内容json[3]结点数据
* 返回:NULL * 返回:NULL
* */ * */
void _LabPrintLine(rapidxml::xml_node<> *node,rapidjson::Document& jsonDoc, rapidjson::Value &childNode);
/* 功能:打印单行数据
* 参数:[1]xml模板,[2]打印内容json[3]结点数据
* 返回:NULL
* */
void _PrintMultLine(rapidxml::xml_node<> *node,rapidjson::Document& jsonDoc,rapidjson::Value &jsonValue); void _PrintMultLine(rapidxml::xml_node<> *node,rapidjson::Document& jsonDoc,rapidjson::Value &jsonValue);
/* 功能:获取json的value值 /* 功能:获取json的value值
* 参数:NULL * 参数:NULL
......
...@@ -18,3 +18,11 @@ extern "C" void __declspec(dllexport) DoPrintWithXml(char *prtStr, char *orderS ...@@ -18,3 +18,11 @@ extern "C" void __declspec(dllexport) DoPrintWithXml(char *prtStr, char *orderS
PrintLib::GetInstance().DoPrint(prt,order,xml,err); PrintLib::GetInstance().DoPrint(prt,order,xml,err);
} }
extern "C" void __declspec(dllexport) DoLabPrintWithXml(char *prtStr, char *orderStr, char*xmlStr, char* error)
{
std::string prt = prtStr;
std::string order = orderStr;
std::string xml = xmlStr;
std::string err = error;
PrintLib::GetInstance().DoLabPrint(prt,order,xml,err);
}
...@@ -1324,6 +1324,12 @@ int LabPrtLine(int x, int y, int line, const char *data) ...@@ -1324,6 +1324,12 @@ int LabPrtLine(int x, int y, int line, const char *data)
int TscPrintClose() int TscPrintClose()
{ {
if(g_info.dvname.empty())
{
LOG() << "标签打印机名为空";
return 0;
}
char name[128]={0}; char name[128]={0};
strcpy(name,g_info.dvname.c_str()); strcpy(name,g_info.dvname.c_str());
...@@ -1937,7 +1943,14 @@ bool f_OpenPrinter(char config[]) ...@@ -1937,7 +1943,14 @@ bool f_OpenPrinter(char config[])
return TRUE; return TRUE;
} }
void f_ClosePrinter() void f_ClosePrinter(int type)
{ {
if(type == 2)
{
LabPrtClose();
}
else
{
PrtClose(); PrtClose();
}
} }
\ No newline at end of file
...@@ -13,4 +13,5 @@ EXPORTS ...@@ -13,4 +13,5 @@ EXPORTS
LabPrtClose @11 LabPrtClose @11
DoPrintWithXml @12 DoPrintWithXml @12
DoPrtList @13 DoPrtList @13
DoLabPrintWithXml @14
...@@ -64,7 +64,7 @@ bool f_LaodAddress(); ...@@ -64,7 +64,7 @@ bool f_LaodAddress();
bool f_OpenPrinter(char config[]); bool f_OpenPrinter(char config[]);
void f_ClosePrinter(); void f_ClosePrinter(int type);
#endif #endif
\ No newline at end of file
...@@ -151,6 +151,15 @@ void testPrt() ...@@ -151,6 +151,15 @@ void testPrt()
#include "PrintLib.h" #include "PrintLib.h"
void DoLabPrintWithXmls(char *prtStr, char *orderStr, char*xmlStr, char* error)
{
std::string prt = prtStr;
std::string order = orderStr;
std::string xml = xmlStr;
std::string err = error;
PrintLib::GetInstance().DoLabPrint(prt,order,xml,err);
}
void DoPrintWithXmls(char *prtStr, char *orderStr, char*xmlStr, char* error) void DoPrintWithXmls(char *prtStr, char *orderStr, char*xmlStr, char* error)
{ {
string prt = prtStr; string prt = prtStr;
...@@ -198,7 +207,8 @@ int main() ...@@ -198,7 +207,8 @@ int main()
//Unloaddll(); //Unloaddll();
//char xmlstr[] = "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n<ticket type=\"OrderPrint\" typeCode=\"1\">\n<line printType=\"Text\" text=\"0001非码测试单\" codeType=\"1\" fontType=\"1\" fontStyle=\"1\" width=\"2\" height=\"2\" leftMargin=\"0\" AlignMode=\"1\"/>\n</ticket>"; //char xmlstr[] = "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n<ticket type=\"OrderPrint\" typeCode=\"1\">\n<line printType=\"Text\" text=\"0001非码测试单\" codeType=\"1\" fontType=\"1\" fontStyle=\"1\" width=\"2\" height=\"2\" leftMargin=\"0\" AlignMode=\"1\"/>\n</ticket>";
//char xmlstr[] = "<?xml version=\"1.0\" encoding=\"utf-8\"?><ticket type = \"OrderPrint\" typeCode = \"1\"><line printType=\"Text\" text=\"%1Bulabula%2\" fontType=\"1\" fontStyle=\"1\" width=\"1\" height=\"1\" leftMargin=\"0\" AlignMode=\"0\"><math index=\"1\" content=\"order_title\" /><math index=\"2\" content=\"order_index\" /></line></ticket>"; //char xmlstr[] = "<?xml version=\"1.0\" encoding=\"utf-8\"?><ticket type = \"OrderPrint\" typeCode = \"1\"><line printType=\"Text\" text=\"%1Bulabula%2\" fontType=\"1\" fontStyle=\"1\" width=\"1\" height=\"1\" leftMargin=\"0\" AlignMode=\"0\"><math index=\"1\" content=\"order_title\" /><math index=\"2\" content=\"order_index\" /></line></ticket>";
char conf[] = "{\"ip\":\"172.16.1.252\",\"PaperWidth\":\"56\",\"type\":3}"; //char conf[] = "{\"name\":\"Xprinter\",\"paperwidth\":\"56\",\"type\":3}";
char conf[] = "{\"name\":\"Xprinter\",\"high\":\"25\",\"labelprttype\":0,\"type\": 4,\"width\":\"40\"}";
//char order[] = "{\"order_title\":\"AAAA\",\"order_index\":\"0001\"}"; //char order[] = "{\"order_title\":\"AAAA\",\"order_index\":\"0001\"}";
char err[1024] = {}; char err[1024] = {};
...@@ -211,7 +221,7 @@ int main() ...@@ -211,7 +221,7 @@ int main()
GetFileBuf(xmlstr, xmlfilename); GetFileBuf(xmlstr, xmlfilename);
GetFileBuf(order, jsonfilename); GetFileBuf(order, jsonfilename);
DoPrintWithXmls(conf, order, xmlstr, err); DoLabPrintWithXmls(conf, order, xmlstr, err);
system("pause"); system("pause");
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment