java查询信息生成相关证书图片3

之前的方法 对于大段文字的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>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要在Java生成微信收款二维码,你可以使用第三方库,比如`com.github.wxpay:wxpay-sdk`。以下是一个简单的示例代码: 首先,确保你已经在项目的`pom.xml`文件中添加了`wxpay-sdk`库的依赖: ```xml <dependency> <groupId>com.github.wxpay</groupId> <artifactId>wxpay-sdk</artifactId> <version>xxxxx</version> </dependency> ``` 然后,你可以使用下面的代码生成微信收款二维码: ```java import com.github.wxpay.sdk.WXPay; import com.github.wxpay.sdk.WXPayConfig; import com.github.wxpay.sdk.WXPayConstants; import com.github.wxpay.sdk.WXPayUtil; import java.util.HashMap; import java.util.Map; public class WeChatPayment { public static void main(String[] args) throws Exception { // 创建WXPayConfig配置对象,实现相关配置 WXPayConfig config = new WXPayConfig() { @Override public String getAppID() { return "YourAppID"; } @Override public String getMchID() { return "YourMchID"; } @Override public String getKey() { return "YourKey"; } @Override public InputStream getCertStream() { // 返回证书文件的输入流,如果不需要证书,则返回null return null; } @Override public int getHttpConnectTimeoutMs() { return 8000; } @Override public int getHttpReadTimeoutMs() { return 10000; } }; // 创建WXPay对象 WXPay wxpay = new WXPay(config, WXPayConstants.SignType.MD5, true); // 设置生成二维码的参数 Map<String, String> data = new HashMap<>(); data.put("body", "商品描述"); data.put("out_trade_no", "订单号"); data.put("total_fee", "订单金额(单位:分)"); data.put("spbill_create_ip", "终端IP"); data.put("notify_url", "异步通知地址"); data.put("trade_type", "NATIVE"); // 调用统一下单API获取支付二维码链接 Map<String, String> result = wxpay.unifiedOrder(data); String qrCodeUrl = result.get("code_url"); System.out.println("微信收款二维码链接:" + qrCodeUrl); } } ``` 以上代码中,你需要将`YourAppID`、`YourMchID`和`YourKey`替换为你自己的微信开放平台的相关信息。另外,你还需要提供商品描述、订单号、订单金额、终端IP、异步通知地址等参数。执行该代码后,会输出生成的微信收款二维码链接。你可以使用该链接生成对应的二维码图片,并提供给用户进行支付。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值