菜鸟电子面单打印组件,自定义打印区展示内容
客户一般要求自定义打印区里面,展示订单备货信息。
但是官方文档菜鸟打印交互协议里面介绍的data设置goodsInfo这种方法,经过试验是没法使用的。
后来发现由于templateURL可以自己定义。那就自己设置一个url,并定义好打印内容,内容为备货商品展示。
但要注意的是,这个URL必须为满足应答头为application/json,否则报错
排版的问题,比如长宽高的设置值怎么办,我一般先到菜鸟打印组件下的设计器先去排版。
再通过获取用户的打印模板的接口获取自定义模板的URL,访问连接
http://cloudprint.cainiao.com/template/customArea/721839获取layout里面的属性值
内容格式为菜鸟要求的格式,如下所示:
<?xml version='1.0'encoding='UTF-8'?>
<layout id='CUSTOM_AREA'xmlns='http://cloudprint.cainiao.com/print' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' xsi:schemaLocation='http://cloudprint.cainiao.com/printhttp://cloudprint-docs-resource.oss-cn-shanghai.aliyuncs.com/lpml_schema.xsd' xmlns:editor='http://cloudprint.cainiao.com/schema/editor'top='140' width='100' height='40'style='zIndex: 1; overflow: hidden;'>
<layout id='element_layout_9F7484A0D698A082' left='5' top='5'width='26' height='6'editor:_for_='element_text_B9D9F95086F51FEE' style='zIndex:3;overflow:visible;'>
<text value='11112测试规格:规格222 数量:1'style='wrap:true;direction:horizontal;letterSpacing:0;fontSize:9;lineHeight:5;fontItalic:false;fontFamily:宋体;fontUnderline:false;valign:top;align:left;fontWeight:false'/>
</layout>
</layout>
Java代码如下:
@RequestMapping(value = "/alipay/taobao/myPrintTemplate/{orderId}", method= RequestMethod.GET)
publicvoid myprinttemplate(
@PathVariable(value="orderId") Integer orderId,
HttpServletResponse response){
response.setCharacterEncoding("utf-8");
response.setContentType("application/json");
List<GoodsOrderDetail> dbDetailList = goodsOrderDetailMapper.selectGoodsOrderDetailByOrderId(orderId);
StringBuilder sb = new StringBuilder();
for (GoodsOrderDetail detail : dbDetailList) {
sb.append(detail.getGoodsName());
sb.append(" 规格:");
sb.append(detail.getSpecification());
sb.append(" 数量:");
sb.append(detail.getGoodsNum());
}
StringBuffer result = new StringBuffer("<?xmlversion='1.0' encoding='UTF-8'?>");
result.append("<layout id='CUSTOM_AREA'xmlns='http://cloudprint.cainiao.com/print'");
result.append(" xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'");
result.append(" xsi:schemaLocation='http://cloudprint.cainiao.com/printhttp://cloudprint-docs-resource.oss-cn-shanghai.aliyuncs.com/lpml_schema.xsd'");
result.append(" xmlns:editor='http://cloudprint.cainiao.com/schema/editor'top='140'");
result.append(" width='100' height='40' style='zIndex: 1;overflow: hidden;'>");
result.append(" <layoutid='element_layout_9F7484A0D698A082' left='5' top='5' width='26'");
result.append(" height='6' editor:_for_='element_text_B9D9F95086F51FEE'");
result.append(" style='zIndex:3;overflow:visible;'>");
result.append("<text value='");
result.append(sb.toString());
result.append("'style='wrap:true;direction:horizontal;letterSpacing:0;fontSize:9;lineHeight:5;fontItalic:false;fontFamily:宋体;fontUnderline:false;valign:top;align:left;fontWeight:false'/>");
result.append(" </layout>");
result.append("</layout>");
try {
response.getWriter().write(result.toString());
} catch (Exception e) {
e.printStackTrace();
}
}