使用x-easypdf生成PDF文件,使用了stsong.ttf字体,电脑打开没有问题,通过微信发送就有问题,显示中文乱码,有知道的大佬赐教下
一般后端在渲染pdf文件的时候,要对http请求头进行一些编码,包括根据浏览器的型号,进行相应规则的编码,例如:
response.setContentType("application/pdf,charset=utf-8");
//文件名处理,防止乱码问题
String userAgent = request.getHeader("User-Agent");
// 针对IE或者以IE为内核的浏览器:
if (userAgent.contains("MSIE") || userAgent.contains("Trident") || userAgent.contains("Edge")) {
response.setHeader("Content-Disposition", (type == 1 ? "inline;filename=" : "attachment;filename=")
+ java.net.URLEncoder.encode(BUSINESS_FILE_NAME, "UTF-8"));
return;
}
// 非IE浏览器的处理:
response.setHeader("Content-Disposition", (type == 1 ? "inline;filename=" : "attachment;filename=")
+ new String(BUSINESS_FILE_NAME.getBytes("UTF-8"), "ISO-8859-1"));