feat:generateReceiptImage接口增加图片自定义保存路径
This commit is contained in:
parent
a8f13d72b9
commit
2986fa0c14
|
@ -23,13 +23,19 @@ public class TemplateController {
|
|||
|
||||
|
||||
@GetMapping("/template/{templateName}")
|
||||
public Result<String> testTemplate(@RequestParam Integer width, @RequestParam Integer height, String jsonData, @PathVariable String templateName) {
|
||||
this.templateService.generateReceiptImage(JSONObject.parseObject(jsonData), templateName, width, height);
|
||||
public Result<String> testTemplate(@RequestParam String jsonData,
|
||||
@RequestParam Integer width,
|
||||
@RequestParam Integer height,
|
||||
@PathVariable String templateName,
|
||||
@RequestParam(defaultValue = "E:\\images") String saveDir) {
|
||||
this.templateService.generateReceiptImage(JSONObject.parseObject(jsonData), templateName, width, height, saveDir);
|
||||
return Result.ok("模板生成成功");
|
||||
}
|
||||
|
||||
@GetMapping("/template")
|
||||
public Result<String> testTemplate(String jsonData, @RequestParam Integer width, @RequestParam Integer height) {
|
||||
public Result<String> testTemplate(@RequestParam String jsonData,
|
||||
@RequestParam Integer width,
|
||||
@RequestParam Integer height, @RequestParam(defaultValue = "E:\\images") String saveDir) {
|
||||
String html = "<!DOCTYPE html>\n" +
|
||||
"<html>\n" +
|
||||
"<head><title>Test</title></head>\n" +
|
||||
|
@ -38,7 +44,7 @@ public class TemplateController {
|
|||
"<h2><span th:text='${name}'> </span></h2>\n" +
|
||||
"</body>\n" +
|
||||
"</html>";
|
||||
this.templateService.generateReceiptImage(JSONObject.parseObject(jsonData), html, width, height);
|
||||
this.templateService.generateReceiptImage(JSONObject.parseObject(jsonData), html, width, height, saveDir);
|
||||
return Result.ok("模板生成成功");
|
||||
}
|
||||
|
||||
|
|
|
@ -56,13 +56,15 @@ public class TemplateService {
|
|||
|
||||
/**
|
||||
* 生成小票图片
|
||||
* @param data json数据,用来填充模板
|
||||
*
|
||||
* @param data json数据,用来填充模板
|
||||
* @param template 模板(html字符串或者模板名称)
|
||||
* @param width 图片宽度
|
||||
* @param height 图片高度
|
||||
* @param width 图片宽度
|
||||
* @param height 图片高度
|
||||
* @param saveDir 图片的保存路径,如果为空,那么不进行图片的保存
|
||||
* @return 图片字节数组
|
||||
*/
|
||||
public byte[] generateReceiptImage(JSONObject data, String template, int width, int height) {
|
||||
public byte[] generateReceiptImage(JSONObject data, String template, int width, int height, String saveDir) {
|
||||
try {
|
||||
// 获取模板上下文
|
||||
Context context = this.getContext(data);
|
||||
|
@ -86,7 +88,17 @@ public class TemplateService {
|
|||
// 渲染模板
|
||||
String html = templateEngine.process(template, context);
|
||||
|
||||
return this.generate(html, width, height);
|
||||
BufferedImage image = this.generate(html, width, height);
|
||||
|
||||
// 保存图片
|
||||
if (saveDir != null && !"".equals(saveDir)) {
|
||||
String outputPath = saveDir + "\\genera_image_" + System.currentTimeMillis() + ".png";
|
||||
ImageIO.write(image, "PNG", new File(outputPath));
|
||||
}
|
||||
|
||||
ByteArrayOutputStream byteOutputStream = new ByteArrayOutputStream();
|
||||
ImageIO.write(image, "PNG", byteOutputStream);
|
||||
return byteOutputStream.toByteArray();
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
throw new RRException("图片打印失败");
|
||||
|
@ -98,7 +110,7 @@ public class TemplateService {
|
|||
* 生成图片
|
||||
* @param html html内容
|
||||
*/
|
||||
private byte[] generate(String html, int width, int height){
|
||||
private BufferedImage generate(String html, int width, int height){
|
||||
try {
|
||||
// 转换为xhtml
|
||||
String xhtml = this.htmlToXhtml(html);
|
||||
|
@ -107,15 +119,7 @@ public class TemplateService {
|
|||
Document document = this.xhtmlToDocument(xhtml);
|
||||
|
||||
// 生成图片
|
||||
BufferedImage image = createImageToDocument(document, width, height);
|
||||
|
||||
// 保存图片
|
||||
String outputPath = "E:\\images\\test_" + System.currentTimeMillis() + ".png";
|
||||
ImageIO.write(image, "PNG", new File(outputPath));
|
||||
|
||||
ByteArrayOutputStream byteOutputStream = new ByteArrayOutputStream();
|
||||
ImageIO.write(image, "PNG", byteOutputStream);
|
||||
return byteOutputStream.toByteArray();
|
||||
return createImageToDocument(document, width, height);
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
throw new RRException("图片打印失败");
|
||||
|
|
Loading…
Reference in New Issue