之前的方法 对于大段文字的line-height不生效 换一种方式
<dependency>
<groupId>gui.ava</groupId>
<artifactId>html2image</artifactId>
<version>2.0.1</version>
</dependency>
这种方式的 大段文字出现不能自动换行的问题,解决方法是每个文字加span标签
public static String dealRows(String text,boolean isAll){
System.out.println(text.length());
System.out.println(text);
StringBuffer content=new StringBuffer();
for (char c : text.toCharArray()) {
if(isAll){
content.append("<span style='padding-right:3px'>"+c+"</span>");
}else{
if(isChineseChar(c)){
content.append("<span style='padding-right:3px'>"+c+"</span>");
}else{
content.append(c);
}
}
}
return content.toString();
}
public static boolean isChineseChar(char c) {
return String.valueOf(c).matches("[\u4e00-\u9fa5]");
}
@RequestMapping(value = "/fa/applet/sportsCertificate", method = RequestMethod.GET)
public void sportsCertificate(HttpServletRequest request, HttpServletResponse response) {
// 将图像输出到Servlet输出流中。
ServletOutputStream sos = null;
try {
PageData pd = this.getPageData();
String path = request.getContextPath();
String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + path + "/";
sos = response.getOutputStream();
int height=(int)(pd.getFloat("h")*700);
Html2Image image1=Html2Image.fromURL(new URL(basePath+"f/sports-certificate?h="+height+"&id="+pd.get("id")+"&temp=" + new Date().getTime()));
image1.getImageRenderer().setWidth(800);
image1.getImageRenderer().setHeight(height);
image1.getImageRenderer().setAutoHeight(false);
BufferedImage image = image1.getImageRenderer().getBufferedImage();
BufferedImage bufferedImageToWrite = new BufferedImage(image.getWidth(), image.getHeight(), BufferedImage.TYPE_INT_RGB);
bufferedImageToWrite.createGraphics().drawImage(image, 0, 0, Color.WHITE, null);
ImageIO.write(bufferedImageToWrite, "png", sos);
} catch (Exception e) {
e.printStackTrace();
}
}
@RequestMapping(value = "/f/sports-certificate")
public ModelAndView sportsCertificate(HttpServletRequest request) {
ModelAndView mv = this.getModelAndView();
PageData pd = this.getPageData();
String path = request.getContextPath();
String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + path + "/";
PageData data=tSportsDetailsService.getSportsDetails(pd);
StringBuffer content=new StringBuffer();
content.append("感谢您参加xxxxxxxxxxxxxxxxxxxxxxxxxxxx“");
data.put("content",dealRows(content.toString(),true));
data.put("nickName",dealRows(data.getString("nickName"),false));
data.put("time",DateUtil.date2String(data.getDate("insert_time"),"yyyy年MM月dd日"));
data.put("actuatorName",data.get("actuatorName"));
data.put("sealPicture",basePath+data.get("seal_picture"));
data.put("erweima2",basePath+data.get("erweima2"));
data.put("bg",basePath+"assets/img/bg.jpg");
if(pd.containsKey("h")){
data.put("h",pd.get("h")+"px");
data.put("h2",pd.getInt("h")-200+"px");
data.put("h3",pd.getInt("h")-400+"px");
}
mv.addObject("pd",data);
mv.setViewName("certificate");
return mv;
}
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt"%>
<%@ page session="false"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + path + "/";
%>
<!DOCTYPE html>
<html>
<head>
<base href="<%=basePath%>">
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<%-- <script src="assets/js/jquery-1.11.2.min.js"></script>--%>
<%-- <script src="assets/js/html2canvas.js"></script>--%>
</head>
<body style="margin:0">
<div id="box" style="width:800px;background-color: #e35f00;font-family: 黑体;height: ${pd.h};">
<div style="background: url(${pd.bg}) no-repeat;background-size:100%; height:${pd.h2};">
<div style="padding-left:100px;padding-right:100px;height:${pd.h3};">
<div style="text-align: center;padding-top: 120px;font-size: 85px;color:#e35f00;font-weight: bold ">捐赠证书</div>
<div style="text-align: center;padding-top: 10px;font-size:22px; ">编号:${pd.certificate_no}</div>
<div style="text-align: center;padding-top: 50px;padding-bottom: 20px;font-size: 36px;
border-bottom-style: dashed;border-bottom-color:#e35f00;border-bottom-width: 1px ">${pd.nickName}</div>
<div style="text-indent: 50px;font-size: 32px;letter-spacing: 1px;line-height:55px;padding-top: 20px; word-break: break-all;word-wrap : break-word;">
${pd.content}
</div>
</div>
<div style="height:200px;padding-left: 50px;padding-right: 50px;text-align: center;background: url(${pd.sealPicture}) no-repeat center center;">
<div style="text-align: center;padding-top: 50px;font-size: 27px; ">${pd.actuatorName}</div>
<div style="text-align: center;padding-top: 10px;font-size: 27px; ">${pd.time}</div>
</div>
</div>
<table style="height: 200px;padding:15px 20px 15px 15px;width: 100%;">
<tr>
<td>
<div style="height:150px;width: 150px;border-radius: 10px;background: url(${pd.erweima2}) no-repeat center center ">
</div>
</td>
<td >
<div style="color: white;font-size: 25px;font-weight: bold">
<div style="line-height: 78px;">扫扫小程序码</div>
<div style="line-height: 20px;">查看项目详情</div>
</div>
</td>
</tr>
</table>
</div>
</body>
<script>
// new html2canvas(document.getElementById('box')).then(canvas => {
// // canvas为转换后的Canvas对象
// let oImg = new Image();
// oImg.src = canvas.toDataURL(); // 导出图片
// document.body.appendChild(oImg); // 将生成的图片添加到body
// $("body").html(oImg);
// });
</script>
</html>