先附上文件资源:https://download.csdn.net/download/m0_49605579/59212077
详细用法请看:https://blog.csdn.net/m0_49605579/article/details/122583318
1.把jar包和xml文件放在项目目录下并打开,本地只需要idea打开jar包即可,线上使用需导入本地依赖,这里是gradle方式导入,maven方式自行百度,如下图:
2.使用资源内的工具方法破解jar包生成后的水印(注:只能使用这里提供的15.8.0或者前后其他的老版本,具体范围没测,才能破解成功,使用新版依旧有水印)
public static boolean getLicense(InputStream inputStream) {
boolean result = false;
InputStream is = null;
try {
is=inputStream;
License aposeLic = new License();
aposeLic.setLicense(is);
result = true;
} catch (Exception e) {
e.printStackTrace();
}finally {
if (is != null) {
try {
is.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return result;
}
3.实现导出pdf功能
private final static String WORD = "word";
private final static String PDF = "pdf";
/**
* 导出文件(可选word/pdf)
*
* @param response
* @param fileName 文件名(带后缀)
* @param map (数据集合)
* @param type (导出类型:必须是word/pdf)
* @param config (策略配置:poi-tl插件使用)
*/
public static void exportFile(
HttpServletResponse response,
String fileName,
Map<String, Object> map,
String type,
Configure config) {
try {
// 获取Word模板,模板存放路径在项目的resources目录下
InputStream resourceAsStream = ImportOrExportUtils.class.getResourceAsStream("/template/" + fileName);
// 获取破解spiredoc水印xml文件
InputStream xmlIn = ImportOrExportUtils.class.getResourceAsStream("/xml/license.xml");
//破解spiredoc水印
Word2PdfAsposeUtil.getLicense(xmlIn);
//加载导出配置
XWPFTemplate template = XWPFTemplate.compile(resourceAsStream, config).render(map);
// 浏览器端下载
response.setContentType("application/octet-stream");
response.setCharacterEncoding("utf-8");
//word下载
if (WORD.equals(type)) {
response.setHeader("Content-disposition", "attachment;filename=\"" + fileName + "\"");
OutputStream out = response.getOutputStream();
BufferedOutputStream bos = new BufferedOutputStream(out);
template.write(bos);
bos.flush();
out.flush();
PoitlIOUtils.closeQuietlyMulti(template, bos, out);
//pdf下载
} else if (PDF.equals(type)) {
response.setHeader(
"Content-Disposition",
"attachment;filename=".concat(String.valueOf(URLEncoder.encode("export.pdf", "UTF-8"))));
OutputStream out = response.getOutputStream();
//获取相对路径url
//思路:由于aspose的转换pdf方法需要传入文件输入流,所以在此先得到生成的word格式文件到本地,然后得到输出流进行转换,转换完成输出到浏览器后删除本地的word文件
//此处需要在resource下创建template目录,如不想创建为空时添加makdir代码即可
URL url = Kit.class.getResource("/template/");
template.writeAndClose(new FileOutputStream(url.getPath() + "temporary.docx"));
InputStream in = Kit.class.getResourceAsStream("/template/temporary.docx");
Document document = new Document(in);
document.save(out, SaveFormat.PDF);
IOUtils.copy(in, out);
out.flush();
PoitlIOUtils.closeQuietlyMulti(template, resourceAsStream, in, out);
new File(url.getPath() + "temporary.docx").delete();
}
} catch (Exception e) {
e.printStackTrace();
}
}
4.注:需要前端代码配合(前端请求需要设置相应类型responseType: 'blob'
)
// 下载
export function download(data) {
return request({
url: '/admin/employee/export',
method: 'post',
data,
responseType: 'blob',
})
}
async downloadfn() {
const down = new FormData();
down.append("userId", this.UserRid);
down.append("type", "pdf");
let res = await download(down);
const fileName = "个人干审表.pdf";
const elink = document.createElement("a");
elink.download = fileName;
elink.style.display = "none";
elink.href = URL.createObjectURL(res.data);
document.body.appendChild(elink);
elink.click();
URL.revokeObjectURL(elink.href);
document.body.removeChild(elink);
},