This commit is contained in:
Paidax
2024-12-18 15:52:35 +08:00
parent b6ed7cc0ce
commit 3ea351001e
20 changed files with 3402 additions and 17 deletions
@@ -0,0 +1,98 @@
package io.v.nutz.base.utils;
import cn.hutool.core.io.FileUtil;
import com.aspose.cells.PdfSaveOptions;
import com.aspose.cells.Workbook;
import com.aspose.words.Document;
import com.aspose.words.SaveFormat;
import lombok.extern.slf4j.Slf4j;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
/**
* 文档转换
*/
@Slf4j
public class OfficePlusUtil {
/**
* 转换
*
* @param sourcePath 源文件路径
* @param targetPath 目标文件路径
*/
public static void convert(String sourcePath, String targetPath) {
String suffix = FileUtil.getSuffix(sourcePath);
if (suffix.equals("doc") || suffix.equals("docx")) {
wordConvertPdf(sourcePath, targetPath);
} else if (suffix.equals("xls") || suffix.equals("xlsx")) {
excelConvertPdf(sourcePath, targetPath);
} else {
throw new RuntimeException("不支持的文档格式");
}
}
/**
* excel转pdf
*
* @param sourcePath 源文件路径
* @param targetPath 目标文件路径
*/
public static void excelConvertPdf(String sourcePath, String targetPath) {
FileOutputStream fileOS = null;
try {
Workbook wb = new Workbook(sourcePath);
fileOS = new FileOutputStream(targetPath);
PdfSaveOptions pdfSaveOptions = new PdfSaveOptions();
pdfSaveOptions.setOnePagePerSheet(true);
// for (int i = 0; i < 3; i++) {
// wb.getWorksheets().get(i).getHorizontalPageBreaks().clear();
// wb.getWorksheets().get(i).getVerticalPageBreaks().clear();
// }
// for (int i = 1; i < wb.getWorksheets().getCount(); i++) {
// wb.getWorksheets().get(i).setVisible(false);
// }
wb.getWorksheets().get(0).setVisible(true);
wb.save(fileOS, pdfSaveOptions);
fileOS.flush();
} catch (Exception e) {
log.info("转换pdf报错", e);
} finally {
if (fileOS != null) {
try {
fileOS.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
/**
* word转pdf
*
* @param sourcePath 源文件路径
* @param targetPath 目标文件路径
*/
public static void wordConvertPdf(String sourcePath, String targetPath) {
FileOutputStream os = null;
try {
File file = new File(targetPath);
os = new FileOutputStream(file);
Document doc = new Document(sourcePath);
doc.save(os, SaveFormat.PDF);
} catch (Exception e) {
log.info("转换pdf报错", e);
} finally {
try {
os.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
@@ -2,8 +2,10 @@ package io.v.nutz.sys.controllers.platform.sys;
import io.v.nutz.base.annontation.ViReturn;
import io.v.nutz.base.query.PageForm;
import io.v.nutz.sys.services.SysFileService;
import io.v.nutz.sys.services.SysUserService;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.authz.annotation.RequiresAuthentication;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.nutz.dao.Cnd;
import org.nutz.dao.Sqls;
@@ -13,6 +15,10 @@ import org.nutz.ioc.loader.annotation.IocBean;
import org.nutz.mvc.annotation.At;
import org.nutz.mvc.annotation.Ok;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
/**
* 文件管理
*
@@ -26,6 +32,8 @@ public class SysFileController {
@Inject
private SysUserService sysUserService;
@Inject
private SysFileService sysFileService;
@At("")
@Ok("beetl:/platform/sys/file/index.html")
@@ -51,4 +59,11 @@ public class SysFileController {
}
@At
@Ok("void")
@ViReturn
@RequiresAuthentication
public void convertPDF(String id, HttpServletRequest request, HttpServletResponse response) throws IOException {
sysFileService.convertPDF(id, request, response);
}
}
@@ -0,0 +1,22 @@
package io.v.nutz.sys.services;
import cn.wizzer.framework.base.service.BaseService;
import io.v.nutz.sys.models.Sys_file;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
/**
* @version 1.0
* @Author zzr
* @nameSysFileService
* @Date 2024/12/17 11:40
* @注释
*/
public interface SysFileService extends BaseService<Sys_file> {
/**
* 转换为PDF
*/
void convertPDF(String id, HttpServletRequest request, HttpServletResponse response);
}
@@ -0,0 +1,93 @@
package io.v.nutz.sys.services.impl;
import cn.hutool.core.io.FileUtil;
import cn.hutool.core.io.IoUtil;
import cn.hutool.core.util.CharsetUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.URLUtil;
import cn.hutool.http.ContentType;
import cn.wizzer.framework.base.Result;
import cn.wizzer.framework.base.service.BaseServiceImpl;
import io.v.nutz.base.utils.OfficePlusUtil;
import io.v.nutz.sys.models.Sys_file;
import io.v.nutz.sys.services.SysFileService;
import lombok.extern.slf4j.Slf4j;
import org.nutz.boot.starter.ftp.FtpService;
import org.nutz.dao.Cnd;
import org.nutz.dao.Dao;
import org.nutz.dao.util.cri.SqlExpressionGroup;
import org.nutz.ioc.loader.annotation.Inject;
import org.nutz.ioc.loader.annotation.IocBean;
import org.nutz.json.Json;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
/**
* @version 1.0
* @Author zzr
* @nameSysFileServiceImpl
* @Date 2024/12/17 11:40
* @注释
*/
@Slf4j
@IocBean(args = {"refer:dao"})
public class SysFileServiceImpl extends BaseServiceImpl<Sys_file> implements SysFileService {
@Inject
private FtpService ftpService;
public SysFileServiceImpl(Dao dao) {
super(dao);
}
@Override
public void convertPDF(String id, HttpServletRequest request, HttpServletResponse response) {
try {
SqlExpressionGroup seg = new SqlExpressionGroup();
seg.or("id", "=", id);
seg.or("filepath", "=", id);
Sys_file sys_file = dao().fetch(Sys_file.class, Cnd.where(seg).desc("id"));
if (ObjectUtil.isEmpty(sys_file)) {
sendErrorResponse(response, "文件记录不存在");
return;
}
//文件扩展名
String extName = FileUtil.extName(sys_file.getFilename());
File localSourcePath = File.createTempFile("file_convert", "." + extName);
FileOutputStream localOutPutStream = new FileOutputStream(localSourcePath.getPath());
ftpService.download(sys_file.getFilepath(), localOutPutStream);
//要转换的pdf本地临时路径
File localPdfPath = File.createTempFile("file_convert", ".pdf");
OfficePlusUtil.convert(localSourcePath.getPath(), localPdfPath.getPath());
byte[] bytes = IoUtil.readBytes(FileUtil.getInputStream(localPdfPath));
response.setHeader("Content-Disposition", "attachment;filename=" + URLUtil.encode(sys_file.getFilename()));
response.addHeader("Content-Length", "" + bytes.length);
response.setHeader("Access-Control-Allow-Origin", "*");
response.setHeader("Access-Control-Expose-Headers", "Content-Disposition");
response.setContentType("application/octet-stream;charset=UTF-8");
IoUtil.write(response.getOutputStream(), true, bytes);
//删除临时文件
FileUtil.del(localPdfPath);
FileUtil.del(localSourcePath);
} catch (Exception e) {
log.error("文件转换异常", e);
}
}
private void sendErrorResponse(HttpServletResponse response, String message) throws IOException {
response.setCharacterEncoding(CharsetUtil.UTF_8);
response.setContentType(ContentType.JSON.toString());
response.getWriter().write(Json.toJson(Result.error(message)));
}
}