2024年10月8日 更改转换pdf方法

This commit is contained in:
bai 2024-10-08 21:25:32 +08:00
parent ac9d8c1e36
commit 390e10e3bc
21 changed files with 1078 additions and 154 deletions

View File

@ -257,6 +257,22 @@
<artifactId>jsch</artifactId>
<version>0.1.55</version>
</dependency>
<!-- 引入libreoffice依赖-->
<dependency>
<groupId>org.jodconverter</groupId>
<artifactId>jodconverter-local</artifactId>
<version>${libreoffice.jodconverter-local.version}</version>
</dependency>
<dependency>
<groupId>org.libreoffice</groupId>
<artifactId>ridl</artifactId>
<version>${libreoffice.ridl.version}</version>
</dependency>
<dependency>
<groupId>org.jodconverter</groupId>
<artifactId>jodconverter-spring-boot-starter</artifactId>
<version>${libreoffice.jodconverter-spring-boot-starter.version}</version>
</dependency>
</dependencies>
</project>

View File

@ -61,20 +61,6 @@
<artifactId>logging-interceptor</artifactId>
<version>2.7.5</version>
</dependency>
<!-- 连接libreOffice驱动包 -->
<!-- 连接libreOffice驱动包 -->
<dependency>
<groupId>com.artofsolving</groupId>
<artifactId>jodconverter</artifactId>
<version>2.2.1</version>
</dependency>
<dependency>
<groupId>org.openoffice</groupId>
<artifactId>bootstrap-connector</artifactId>
<version>0.1.1</version>
</dependency>
<!-- 连接libreOffice驱动包END -->
<dependency>
<groupId>cn.hutool</groupId>
<artifactId>hutool-http</artifactId>

View File

@ -0,0 +1,56 @@
package org.jeecg.modules.httpinterface.controller;
import cn.hutool.core.convert.Convert;
import cn.hutool.core.net.url.UrlBuilder;
import cn.hutool.core.util.CharsetUtil;
import cn.hutool.crypto.SecureUtil;
import cn.hutool.http.HttpUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.google.common.collect.Maps;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.compress.utils.Lists;
import org.apache.commons.lang.StringUtils;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.aspect.annotation.AutoLog;
import org.jeecg.common.system.base.controller.JeecgController;
import org.jeecg.modules.kc.grab.exports.entity.TBks;
import org.jeecg.modules.kc.grab.exports.service.ITBksService;
import org.jeecg.modules.kc.jiaoshi.entity.KcZhihuijiaoshi;
import org.jeecg.modules.kc.jiaoshi.entity.KcZhihuijiaoshiMonitorLog;
import org.jeecg.modules.kc.jiaoshi.entity.KcZhihuijiaoshiOperateLog;
import org.jeecg.modules.kc.jiaoshi.service.IKcZhihuijiaoshiMonitorLogService;
import org.jeecg.modules.kc.jiaoshi.service.IKcZhihuijiaoshiOperateLogService;
import org.jeecg.modules.kc.jiaoshi.service.IKcZhihuijiaoshiService;
import org.jeecg.modules.tools.office.pdf.PDFUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
/**
* @Description: 对外部访问接口
* @Author: jeecg-boot
* @Date: 2023-04-10
* @Version: V1.0
*/
@Slf4j
@Api(tags="对外部访问接口")
//@RestController
@RequestMapping("/temp")
public class testController extends JeecgController<TBks, ITBksService> {
@AutoLog(value = "对外部访问接口-通过IDS访问奥威亚接口")
@ApiOperation(value="T_BKS-抓取-通过id查询", notes="T_BKS-抓取-通过id查询")
@GetMapping(value = "/testPDF")
public Result<?> runAvyApiByIds(String ids,String type){
PDFUtil.office2PDF("F:\\temp\\test\\a.docx","F:\\temp\\test\\a.pdf");
return Result.OK();
}
}

View File

@ -1,54 +1,28 @@
package org.jeecg.modules.kc.teachingunitcontent.controller;
import java.io.File;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import cn.hutool.core.io.FileUtil;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.common.util.DateUtils;
import org.jeecg.common.util.oConvertUtils;
import org.jeecg.modules.kc.teachingunitcontent.entity.KcTeachingUnitContentOne;
import org.jeecg.modules.kc.teachingunitcontent.entity.KcTeachingUnitContentThree;
import org.jeecg.modules.kc.teachingunitcontent.service.IKcTeachingUnitContentThreeService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.aspect.annotation.AutoLog;
import org.jeecg.common.system.base.controller.JeecgController;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.modules.kc.teachingunitcontent.entity.KcTeachingUnitContentThree;
import org.jeecg.modules.kc.teachingunitcontent.service.IKcTeachingUnitContentThreeService;
import org.jeecg.modules.tools.Global;
import org.jeecg.modules.tools.pdf.PDFUtil;
import org.jeecg.modules.zyk.entity.ZykInfo;
import org.jeecg.modules.zyk.service.IZykService;
import org.jeecg.modules.zyk.utils.WjlxEnum;
import org.jeecgframework.poi.excel.ExcelImportUtil;
import org.jeecgframework.poi.excel.def.NormalExcelConstants;
import org.jeecgframework.poi.excel.entity.ExportParams;
import org.jeecgframework.poi.excel.entity.ImportParams;
import org.jeecgframework.poi.excel.view.JeecgEntityExcelView;
import org.jeecg.common.system.base.controller.JeecgController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.servlet.ModelAndView;
import com.alibaba.fastjson.JSON;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.jeecg.common.aspect.annotation.AutoLog;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import javax.servlet.http.HttpServletRequest;
import java.util.Map;
/**
* @Description: 教学单元内容-第三层

View File

@ -1,22 +1,19 @@
package org.jeecg.modules.kc.zyInfoStudent.controller;
import java.io.File;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import cn.hutool.core.io.FileUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.aspect.annotation.AutoLog;
import org.jeecg.common.system.base.controller.JeecgController;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.common.system.vo.LoginUser;
import org.jeecg.common.util.*;
@ -36,12 +33,6 @@ import org.jeecg.modules.kc.zyInfoScjl.service.IZyInfoScjlService;
import org.jeecg.modules.kc.zyInfoStudent.entity.ZyInfoStudent;
import org.jeecg.modules.kc.zyInfoStudent.entity.ZyInfoStudentSys;
import org.jeecg.modules.kc.zyInfoStudent.service.IZyInfoStudentService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.modules.kc.zyInfoStudentHp.entity.ZyInfoStudentHp;
import org.jeecg.modules.kc.zyInfoStudentHp.service.IZyInfoStudentHpService;
import org.jeecg.modules.kc.zyInfoStudentPcz.service.IZyInfoStudentPczService;
@ -50,28 +41,25 @@ import org.jeecg.modules.system.service.ISysUserService;
import org.jeecg.modules.system.service.impl.SysBaseApiImpl;
import org.jeecg.modules.tools.DictUtils;
import org.jeecg.modules.tools.Global;
import org.jeecg.modules.tools.pdf.PDFUtil;
import org.jeecg.modules.tools.office.pdf.PDFUtil;
import org.jeecg.modules.zyk.entity.ZykInfo;
import org.jeecg.modules.zyk.service.IZykService;
import org.jeecg.modules.zyk.utils.WjlxEnum;
import org.jeecgframework.poi.excel.ExcelImportUtil;
import org.jeecgframework.poi.excel.def.NormalExcelConstants;
import org.jeecgframework.poi.excel.entity.ExportParams;
import org.jeecgframework.poi.excel.entity.ImportParams;
import org.jeecgframework.poi.excel.view.JeecgEntityExcelView;
import org.jeecg.common.system.base.controller.JeecgController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.mock.web.MockMultipartFile;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.servlet.ModelAndView;
import com.alibaba.fastjson.JSON;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.jeecg.common.aspect.annotation.AutoLog;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.Map;
/**
* @Description: 学生提交作业

View File

@ -15,12 +15,8 @@ import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.aspect.annotation.AutoLog;
import org.jeecg.common.system.base.controller.JeecgController;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.common.system.vo.LoginUser;
import org.jeecg.common.util.DateUtils;
import org.jeecg.common.util.SpringContextHolder;
import org.jeecg.common.util.SpringContextUtils;
import org.jeecg.common.util.oConvertUtils;
import org.jeecg.modules.kc.config.entity.KcExportConfigTpkwcqkjzglx;
import org.jeecg.modules.kc.config.service.IKcExportConfigTpkwcqkjzglxService;
import org.jeecg.modules.kc.kcSysConfig.entity.KcSysConfig;
@ -32,7 +28,7 @@ import org.jeecg.modules.kc.zyJxdg.entity.ZyJxdgSys;
import org.jeecg.modules.kc.zyJxdg.service.IZyJxdgService;
import org.jeecg.modules.system.service.impl.SysBaseApiImpl;
import org.jeecg.modules.tools.Global;
import org.jeecg.modules.tools.pdf.PDFUtil;
import org.jeecg.modules.tools.office.pdf.PDFUtil;
import org.jeecg.modules.zyk.entity.ZykInfo;
import org.jeecg.modules.zyk.service.IZykService;
import org.jeecg.modules.zyk.utils.WjlxEnum;

View File

@ -0,0 +1,130 @@
package org.jeecg.modules.tools.office.convert;
import org.apache.poi.poifs.filesystem.FileMagic;
import org.apache.poi.ss.usermodel.PrintSetup;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.ss.usermodel.WorkbookFactory;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
public final class MyOfficeUtils {
private MyOfficeUtils () {}
/**
* 获取文档类型
* @param file 待转换的文件
* @return 文件转换器
* @throws IOException io异常
*/
public static FileMagic getFileMagic(File file) throws IOException {
try (FileInputStream inp = new FileInputStream(file)) {
return getFileMagic(inp);
}
}
/**
* 获取文档类型
* @param inp 待转换的文件流
* @return 文件转换器
* @throws IOException io异常
*/
public static FileMagic getFileMagic(InputStream inp) throws IOException {
InputStream is = FileMagic.prepareToCheckMagic(inp);
return FileMagic.valueOf(is);
}
/**
* 判断是否excel03版本
* @param file 待检测的文件
* @return true是03版本 false不是03版本
*/
public static boolean isExcel03(File file) {
try {
boolean ole2 = FileMagic.OLE2 == getFileMagic(file);
if (!ole2) {
return false;
}
try (Workbook workbook = WorkbookFactory.create(file)) {
if(workbook != null) return true;
}
return false;
} catch (Exception e) {
return false;
}
}
/**
* 判断是否excel07版本
* @param file 待检测的文件
* @return true是07版本 false不是07版本
*/
public static boolean isExcel07(File file) {
try {
boolean ole2 = FileMagic.OOXML == getFileMagic(file);
if (!ole2) {
return false;
}
try (Workbook workbook = WorkbookFactory.create(file)) {
if(workbook != null) return true;
}
return false;
} catch (Exception e) {
return false;
}
}
/**
* excel默认打印配置
* @param workbook 文档对象
*/
public static void setDefaultExcelPrintScale (Workbook workbook) {
int numberOfSheets = workbook.getNumberOfSheets();
for (int i = 0; i < numberOfSheets; i++) {
workbook.removePrintArea(i);
setSheetPrintScale(workbook.getSheetAt(i), null);
}
}
static void setExcelPrintScale (Workbook workbook, OfficeInputExcelPrintSetup inputExcelPrintSetup) {
int numberOfSheets = workbook.getNumberOfSheets();
for (int i = 0; i < numberOfSheets; i++) {
workbook.removePrintArea(i);
setSheetPrintScale(workbook.getSheetAt(i), inputExcelPrintSetup);
}
}
private static void setSheetPrintScale(Sheet sheet, OfficeInputExcelPrintSetup inputExcelPrintSetup) {
PrintSetup setup = sheet.getPrintSetup();
if (inputExcelPrintSetup == null) {
setup.setLandscape(true);
setup.setFitHeight((short) 0);
setup.setFitWidth((short) 1);
setup.setPaperSize(PrintSetup.A4_PAPERSIZE);
sheet.setFitToPage(true);
sheet.setPrintRowAndColumnHeadings(false);
sheet.setHorizontallyCenter(true);
sheet.setVerticallyCenter(false);
} else {
setup.setLandscape(inputExcelPrintSetup.isLandscape());
setup.setFitHeight(inputExcelPrintSetup.getFitHeight());
setup.setFitWidth(inputExcelPrintSetup.getFitWidth());
setup.setPaperSize(inputExcelPrintSetup.getPaperSize());
sheet.setFitToPage(inputExcelPrintSetup.isFitToPage());
sheet.setPrintRowAndColumnHeadings(inputExcelPrintSetup.isPrintRowAndColumnHeadings());
sheet.setHorizontallyCenter(inputExcelPrintSetup.isHorizontallyCenter());
sheet.setVerticallyCenter(inputExcelPrintSetup.isVerticallyCenter());
}
sheet.setMargin(Sheet.TopMargin, 0.75);
sheet.setMargin(Sheet.RightMargin, 0.70);
sheet.setMargin(Sheet.BottomMargin, 0.75);
sheet.setMargin(Sheet.LeftMargin, 0.70);
sheet.setMargin(Sheet.HeaderMargin, 0.30);
sheet.setMargin(Sheet.FooterMargin, 0.30);
}
}

View File

@ -0,0 +1,239 @@
package org.jeecg.modules.tools.office.convert;
import lombok.extern.slf4j.Slf4j;
import org.apache.poi.poifs.filesystem.FileMagic;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.ss.usermodel.WorkbookFactory;
import org.apache.poi.util.IOUtils;
import org.jeecg.common.util.SpringContextUtils;
import org.jeecg.modules.tools.Global;
import org.jodconverter.core.DocumentConverter;
import org.jodconverter.core.document.DefaultDocumentFormatRegistry;
import org.jodconverter.core.document.DocumentFormat;
import org.jodconverter.core.job.ConversionJobWithOptionalSourceFormatUnspecified;
import org.jodconverter.core.job.ConversionJobWithSourceSpecified;
import org.jodconverter.core.office.OfficeException;
import java.io.*;
import java.nio.file.Files;
import java.util.UUID;
/**
* 文档转化工具统一入口
*/
@Slf4j
public final class OfficeConvertHelper {
private static Global global = SpringContextUtils.getBean(Global.class);
public void setGlobal(Global g){
global = g;
}
private static String TMP_DIRECTORY = global.getTmpDickPath();
private OfficeConvertHelper () {}
public static OfficeConverterBox converter(DocumentConverter documentConverter) {
return new OfficeConverterBox(documentConverter);
}
/**
* 转换
* @param outputHelper 输出对象
* @throws IOException io异常
* @throws OfficeException office异常
*/
static void execute(OfficeOutputBox outputHelper) throws IOException, OfficeException {
OfficeInputBox officeInputBox = outputHelper.getOfficeInputBox();
OfficeConverterBox officeConverterBox = officeInputBox.getOfficeConverterBox();
DocumentConverter documentConverter = officeConverterBox.getDocumentConverter();
File inpFile = officeInputBox.getInpFile();
InputStream inp = officeInputBox.getInp();
boolean inpCloseStream = officeInputBox.isCloseStream();
DocumentFormat inpDocumentFormat = officeInputBox.getInpDocumentFormat();
OfficeInputExcelPrintSetup inputExcelPrintSetup = officeInputBox.getOfficeInputExcelPrintSetup();
File outFile = outputHelper.getOutFile();
OutputStream outp = outputHelper.getOutp();
boolean outpCloseStream = outputHelper.isCloseStream();
DocumentFormat outDocumentFormat = outputHelper.getOutDocumentFormat();
if (inpFile != null) {
DocumentFormat isDocFormat = inpDocumentFormat;
if (MyOfficeUtils.isExcel03(inpFile)) {
isDocFormat = DefaultDocumentFormatRegistry.XLS;
} else if (MyOfficeUtils.isExcel07(inpFile)) {
isDocFormat = DefaultDocumentFormatRegistry.XLSX;
}
if (DefaultDocumentFormatRegistry.XLS == isDocFormat || DefaultDocumentFormatRegistry.XLSX == isDocFormat) {
File file = new File(TMP_DIRECTORY, "xls" + UUID.randomUUID().toString().replace("-", "") + ".tmp");
try {
try (
OutputStream tmpOs = toBufferedOutputStream(Files.newOutputStream(file.toPath()));
Workbook workbook = WorkbookFactory.create(inpFile);
) {
MyOfficeUtils.setExcelPrintScale(workbook, inputExcelPrintSetup);
workbook.write(tmpOs);
}
ConversionJobWithSourceSpecified as = documentConverter.convert(file).as(isDocFormat);
if (outFile != null) {
if (outDocumentFormat != null) {
as.to(outFile).as(outDocumentFormat).execute();
} else {
as.to(outFile).execute();
}
} else {
OutputStream bufferedOs = toBufferedOutputStream(outp);
as.to(bufferedOs, outpCloseStream).as(outDocumentFormat).execute();
}
} finally {
try {
Files.delete(file.toPath());
} catch (Exception e) {
log.error("文档转换出现问题:" + e.getMessage(),e);
}
}
} else {
if (inpDocumentFormat != null) {
ConversionJobWithSourceSpecified as = documentConverter.convert(inpFile).as(inpDocumentFormat);
if (outFile != null) {
if (outDocumentFormat != null) {
as.to(outFile).as(outDocumentFormat).execute();
} else {
as.to(outFile).execute();
}
} else {
OutputStream bufferedOs = toBufferedOutputStream(outp);
as.to(bufferedOs, outpCloseStream).as(outDocumentFormat).execute();
}
} else {
ConversionJobWithOptionalSourceFormatUnspecified as = documentConverter.convert(inpFile);
if (outFile != null) {
if (outDocumentFormat != null) {
as.to(outFile).as(outDocumentFormat).execute();
} else {
as.to(outFile).execute();
}
} else {
OutputStream bufferedOs = toBufferedOutputStream(outp);
as.to(bufferedOs, outpCloseStream).as(outDocumentFormat).execute();
}
}
}
} else {
File unknownFile = new File(TMP_DIRECTORY, "unknown" + UUID.randomUUID().toString().replace("-", "") + ".tmp");
try {
InputStream bufferedIs = null;
try {
bufferedIs = FileMagic.prepareToCheckMagic(inp);
IOUtils.copy(bufferedIs, unknownFile);
} finally {
if (inpCloseStream) {
if (bufferedIs != null) {
try {
bufferedIs.close();
bufferedIs = null;
} catch (Exception e) {
log.error("文档转换出现问题:" + e.getMessage(),e);
}
} else {
try {
inp.close();
} catch (Exception e) {
log.error("文档转换出现问题:" + e.getMessage(),e);
}
}
} else {
if (bufferedIs != null) {
try {
bufferedIs.reset();
} catch (Exception e) {
log.error("文档转换出现问题:" + e.getMessage(),e);
}
}
}
}
DocumentFormat isDocFormat = inpDocumentFormat;
if (MyOfficeUtils.isExcel03(unknownFile)) {
isDocFormat = DefaultDocumentFormatRegistry.XLS;
} else if (MyOfficeUtils.isExcel07(unknownFile)) {
isDocFormat = DefaultDocumentFormatRegistry.XLSX;
}
if (DefaultDocumentFormatRegistry.XLS == isDocFormat || DefaultDocumentFormatRegistry.XLSX == isDocFormat) {
File file = new File(TMP_DIRECTORY, "xls" + UUID.randomUUID().toString().replace("-", "") + ".tmp");
try {
try (
OutputStream tmpOs = toBufferedOutputStream(Files.newOutputStream(file.toPath()));
Workbook workbook = WorkbookFactory.create(unknownFile);
) {
MyOfficeUtils.setExcelPrintScale(workbook, inputExcelPrintSetup);
workbook.write(tmpOs);
}
ConversionJobWithSourceSpecified as = documentConverter.convert(file).as(isDocFormat);
if (outFile != null) {
if (outDocumentFormat != null) {
as.to(outFile).as(outDocumentFormat).execute();
} else {
as.to(outFile).execute();
}
} else {
OutputStream bufferedOs = toBufferedOutputStream(outp);
as.to(bufferedOs, outpCloseStream).as(outDocumentFormat).execute();
}
} finally {
try {
Files.delete(file.toPath());
} catch (Exception e) {
log.error("文档转换出现问题:" + e.getMessage(),e);
}
}
} else {
if (inpDocumentFormat != null) {
ConversionJobWithSourceSpecified as = documentConverter.convert(unknownFile).as(inpDocumentFormat);
if (outFile != null) {
if (outDocumentFormat != null) {
as.to(outFile).as(outDocumentFormat).execute();
} else {
as.to(outFile).execute();
}
} else {
OutputStream bufferedOs = toBufferedOutputStream(outp);
as.to(bufferedOs, outpCloseStream).as(outDocumentFormat).execute();
}
} else {
ConversionJobWithOptionalSourceFormatUnspecified as = documentConverter.convert(unknownFile);
if (outFile != null) {
if (outDocumentFormat != null) {
as.to(outFile).as(outDocumentFormat).execute();
} else {
as.to(outFile).execute();
}
} else {
OutputStream bufferedOs = toBufferedOutputStream(outp);
as.to(bufferedOs, outpCloseStream).as(outDocumentFormat).execute();
}
}
}
} finally {
try {
Files.delete(unknownFile.toPath());
} catch (Exception e) {
log.error("文档转换出现问题:" + e.getMessage(),e);
}
}
}
}
private static OutputStream toBufferedOutputStream(OutputStream stream) {
if (stream instanceof BufferedOutputStream) {
return stream;
}
return new BufferedOutputStream(stream);
}
}

View File

@ -0,0 +1,31 @@
package org.jeecg.modules.tools.office.convert;
import org.jodconverter.core.DocumentConverter;
import java.io.File;
import java.io.InputStream;
public class OfficeConverterBox {
private final DocumentConverter documentConverter;
OfficeConverterBox(DocumentConverter documentConverter) {
this.documentConverter = documentConverter;
}
public OfficeInputBox from(File inpFile) {
return new OfficeInputBox(this, inpFile);
}
public OfficeInputBox from(InputStream inp) {
return new OfficeInputBox(this, inp, true);
}
public OfficeInputBox from(InputStream inp, boolean closeStream) {
return new OfficeInputBox(this, inp, closeStream);
}
DocumentConverter getDocumentConverter() {
return documentConverter;
}
}

View File

@ -0,0 +1,114 @@
package org.jeecg.modules.tools.office.convert;
import org.jodconverter.core.document.DefaultDocumentFormatRegistry;
import org.jodconverter.core.document.DocumentFormat;
import java.io.File;
import java.io.InputStream;
import java.io.OutputStream;
public class OfficeInputBox {
private final OfficeConverterBox officeConverterBox;
private File inpFile;
private InputStream inp;
private boolean closeStream = true;
private DocumentFormat inpDocumentFormat;
private OfficeInputExcelPrintSetup officeInputExcelPrintSetup;
OfficeInputBox(OfficeConverterBox officeConverterBox, File inpFile) {
this.officeConverterBox = officeConverterBox;
this.inpFile = inpFile;
}
/**
*
* @param officeConverterBox 转换对象
* @param inp 输入流
* @param closeStream 是否关闭输入流默认true
*/
OfficeInputBox(OfficeConverterBox officeConverterBox, InputStream inp, boolean closeStream) {
this.officeConverterBox = officeConverterBox;
this.inp = inp;
this.closeStream = closeStream;
}
/**
*
* @param documentFormat @see {org.jodconverter.core.document.DefaultDocumentFormatRegistry}
* @return this
*/
public OfficeInputBox as(DocumentFormat documentFormat) {
this.inpDocumentFormat = documentFormat;
return this;
}
/**
*
* @param extension @see {org.jodconverter.core.document.DefaultDocumentFormatRegistry}
* @return this
*/
public OfficeInputBox asExtension(String extension) {
this.inpDocumentFormat = DefaultDocumentFormatRegistry.getFormatByExtension(extension);
return this;
}
/**
*
* @param mediaType @see {org.jodconverter.core.document.DefaultDocumentFormatRegistry}
* @return this
*/
public OfficeInputBox asMediaType(String mediaType) {
this.inpDocumentFormat = DefaultDocumentFormatRegistry.getFormatByMediaType(mediaType);
return this;
}
public OfficeInputExcelPrintSetup excel() {
if (this.officeInputExcelPrintSetup == null) {
this.officeInputExcelPrintSetup = new OfficeInputExcelPrintSetup(this);
}
return this.officeInputExcelPrintSetup;
}
public OfficeOutputBox to(File outFile) {
return new OfficeOutputBox(this, outFile);
}
public OfficeOutputStreamBox to(OutputStream outp) {
return new OfficeOutputStreamBox(this, outp, true);
}
public OfficeOutputStreamBox to(OutputStream outp, boolean closeStream) {
return new OfficeOutputStreamBox(this, outp, closeStream);
}
OfficeConverterBox getOfficeConverterBox() {
return officeConverterBox;
}
File getInpFile() {
return inpFile;
}
InputStream getInp() {
return inp;
}
boolean isCloseStream() {
return closeStream;
}
DocumentFormat getInpDocumentFormat() {
return inpDocumentFormat;
}
OfficeInputExcelPrintSetup getOfficeInputExcelPrintSetup() {
return this.officeInputExcelPrintSetup;
}
}

View File

@ -0,0 +1,170 @@
package org.jeecg.modules.tools.office.convert;
import org.apache.poi.ss.usermodel.PrintSetup;
public class OfficeInputExcelPrintSetup {
private final OfficeInputBox officeInputBox;
/**
* 默认A4纸
*/
private short paperSize = PrintSetup.A4_PAPERSIZE;
/**
* 默认横版
*/
private boolean landscape = true;
/**
* 默认水平内容在一页上
*/
private short fitWidth = 1;
/**
* 默认垂直内容分页
*/
private short fitHeight = 0;
/**
* 默认铺满纸张
*/
private boolean fitToPage = true;
/**
* 默认内容水平居中
*/
private boolean horizontallyCenter = true;
/**
* 默认内容垂直与顶部对齐
*/
private boolean verticallyCenter = false;
/**
* 默认不打印excel的行号和列号
*/
private boolean printRowAndColumnHeadings = false;
OfficeInputExcelPrintSetup(OfficeInputBox officeInputBox) {
this.officeInputBox = officeInputBox;
}
public OfficeInputBox end() {
return this.officeInputBox;
}
/**
* 设置打印纸张大小
* @param paperSize @see {org.apache.poi.ss.usermodel.PrintSetup}
* @return this
*/
public OfficeInputExcelPrintSetup setPaperSize(short paperSize) {
this.paperSize = paperSize;
return this;
}
/**
* 设置打印方向默认true
* @param landscape 是否横版打印
* @return this
*/
public OfficeInputExcelPrintSetup setLandscape(boolean landscape) {
this.landscape = landscape;
return this;
}
/**
* 设置打印宽度
* @param fitWidth 是否打印宽度方向的内容在一页上
* @return this
*/
public OfficeInputExcelPrintSetup setFitWidth(boolean fitWidth) {
if (fitWidth) {
this.fitWidth = 1;
} else {
this.fitWidth = 0;
}
return this;
}
/**
* 设置打印高度
* @param fitHeight 是否打印高度方向的内容在一页上
* @return this
*/
public OfficeInputExcelPrintSetup setFitHeight(boolean fitHeight) {
if (fitHeight) {
this.fitHeight = 1;
} else {
this.fitHeight = 0;
}
return this;
}
/**
* 设置是否铺满
* @param fitToPage 内容是否铺满页面
* @return this
*/
public OfficeInputExcelPrintSetup setFitToPage(boolean fitToPage) {
this.fitToPage = fitToPage;
return this;
}
/**
* 设置水平居中
* @param horizontallyCenter 内容是否水平居中
* @return this
*/
public OfficeInputExcelPrintSetup setHorizontallyCenter(boolean horizontallyCenter) {
this.horizontallyCenter = horizontallyCenter;
return this;
}
/**
* 设置垂直居中
* @param verticallyCenter 内容是否垂直居中
* @return this
*/
public OfficeInputExcelPrintSetup setVerticallyCenter(boolean verticallyCenter) {
this.verticallyCenter = verticallyCenter;
return this;
}
/**
* 设置是否打印行号和列号默认false
* @param show 设置是否打印行号和列号
* @return this
*/
public OfficeInputExcelPrintSetup setPrintRowAndColumnHeadings(boolean show) {
this.printRowAndColumnHeadings = show;
return this;
}
short getPaperSize() {
return paperSize;
}
boolean isLandscape() {
return landscape;
}
short getFitWidth() {
return fitWidth;
}
short getFitHeight() {
return fitHeight;
}
boolean isFitToPage() {
return fitToPage;
}
boolean isHorizontallyCenter() {
return horizontallyCenter;
}
boolean isVerticallyCenter() {
return verticallyCenter;
}
boolean isPrintRowAndColumnHeadings() {
return printRowAndColumnHeadings;
}
}

View File

@ -0,0 +1,88 @@
package org.jeecg.modules.tools.office.convert;
import org.jodconverter.core.document.DefaultDocumentFormatRegistry;
import org.jodconverter.core.document.DocumentFormat;
import org.jodconverter.core.office.OfficeException;
import java.io.File;
import java.io.IOException;
import java.io.OutputStream;
public class OfficeOutputBox {
private final OfficeInputBox officeInputBox;
private File outFile;
private OutputStream outp;
private boolean closeStream = true;
private DocumentFormat outDocumentFormat;
OfficeOutputBox(OfficeInputBox officeInputBox, File outFile) {
this.officeInputBox = officeInputBox;
this.outFile = outFile;
}
OfficeOutputBox(OfficeOutputStreamBox officeOutputStreamBox) {
this.officeInputBox = officeOutputStreamBox.getOfficeInputBox();
this.outp = officeOutputStreamBox.getOutp();
this.closeStream = officeOutputStreamBox.isCloseStream();
}
/**
*
* @param documentFormat @see {org.jodconverter.core.document.DefaultDocumentFormatRegistry}
* @return this
*/
public OfficeOutputBox as(DocumentFormat documentFormat) {
this.outDocumentFormat = documentFormat;
return this;
}
/**
*
* @param extension @see {org.jodconverter.core.document.DefaultDocumentFormatRegistry}
* @return this
*/
public OfficeOutputBox asExtension(String extension) {
this.outDocumentFormat = DefaultDocumentFormatRegistry.getFormatByExtension(extension);
return this;
}
/**
*
* @param mediaType @see {org.jodconverter.core.document.DefaultDocumentFormatRegistry}
* @return this
*/
public OfficeOutputBox asMediaType(String mediaType) {
this.outDocumentFormat = DefaultDocumentFormatRegistry.getFormatByMediaType(mediaType);
return this;
}
public void execute() throws IOException, OfficeException {
OfficeConvertHelper.execute(this);
}
public OfficeInputBox getOfficeInputBox() {
return officeInputBox;
}
public File getOutFile() {
return outFile;
}
public OutputStream getOutp() {
return outp;
}
public boolean isCloseStream() {
return closeStream;
}
public DocumentFormat getOutDocumentFormat() {
return outDocumentFormat;
}
}

View File

@ -0,0 +1,69 @@
package org.jeecg.modules.tools.office.convert;
import org.jodconverter.core.document.DocumentFormat;
import java.io.OutputStream;
public class OfficeOutputStreamBox {
private final OfficeInputBox officeInputBox;
private final OutputStream outp;
private final boolean closeStream;
OfficeOutputStreamBox(OfficeInputBox officeInputBox, OutputStream outp, boolean closeStream) {
this.officeInputBox = officeInputBox;
this.outp = outp;
this.closeStream = closeStream;
}
/**
*
* @param documentFormat @see {org.jodconverter.core.document.DefaultDocumentFormatRegistry}
* @return 文档输出类
*/
public OfficeOutputBox as(DocumentFormat documentFormat) {
OfficeOutputBox officeOutputBox = new OfficeOutputBox(this);
officeOutputBox.as(documentFormat);
return officeOutputBox;
}
/**
*
* @param extension @see {org.jodconverter.core.document.DefaultDocumentFormatRegistry}
* @return 文档输出类
*/
public OfficeOutputBox asExtension(String extension) {
OfficeOutputBox officeOutputBox = new OfficeOutputBox(this);
officeOutputBox.asExtension(extension);
return officeOutputBox;
}
/**
*
* @param mediaType @see {org.jodconverter.core.document.DefaultDocumentFormatRegistry}
* @return 文档输出类
*/
public OfficeOutputBox asMediaType(String mediaType) {
OfficeOutputBox officeOutputBox = new OfficeOutputBox(this);
officeOutputBox.asMediaType(mediaType);
return officeOutputBox;
}
OfficeInputBox getOfficeInputBox() {
return officeInputBox;
}
OutputStream getOutp() {
return outp;
}
boolean isCloseStream() {
return closeStream;
}
}

View File

@ -0,0 +1,65 @@
package org.jeecg.modules.tools.office.pdf;
import lombok.SneakyThrows;
import org.jeecg.common.util.SpringContextUtils;
import org.jeecg.modules.tools.office.convert.OfficeConvertHelper;
import org.jodconverter.core.DocumentConverter;
import java.io.File;
/**
* PFD工具类
*/
public class PDFUtil {
// private static Global global = SpringContextUtils.getBean(Global.class);
//
// public void setGlobal(Global g){
// global = g;
// }
private static DocumentConverter documentConverter = SpringContextUtils.getBean(DocumentConverter.class);
public void setDocumentConverter(DocumentConverter g){
documentConverter = g;
}
/**
* WORD转PDF方法
* @param sourceFile 要被转化的word文档路径如E:\\1.docx
* @param destFile 转成之后pdf文档存放路径如E:\\1.pdf
*/
@SneakyThrows
public static void office2PDF(String sourceFile, String destFile) {
OfficeConvertHelper.converter(documentConverter).from(new File(sourceFile)).to(new File(destFile)).execute();
// try {
// File inputFile = new File(sourceFile);
// File outputFile = new File(destFile);
// // 获得文件格式(2018年9月25日16:12:35添加不增加格式多个文档拼接后转换会报错)
// DefaultDocumentFormatRegistry formatReg = new DefaultDocumentFormatRegistry();
// DocumentFormat pdfFormat = formatReg.getFormatByFileExtension("pdf");
// DocumentFormat docFormat = formatReg.getFormatByFileExtension("doc");
//
// if (!outputFile.getParentFile().exists()) {
// outputFile.getParentFile().mkdirs();
// }
// OpenOfficeConnection connection = new SocketOpenOfficeConnection(global.libreOfficeUrl, global.libreOfficePort);
//// OpenOfficeConnection connection = new SocketOpenOfficeConnection("192.168.1.227", 8100);
// connection.connect();
// // 2018年7月19日修改 原方法生成pdf会报错 wy
//// DocumentConverter converter = new OpenOfficeDocumentConverter(connection);
// DocumentConverter converter = new StreamOpenOfficeDocumentConverter(connection);
// converter.convert(inputFile,docFormat, outputFile,pdfFormat);
// connection.disconnect();
// } catch (ConnectException e) {
// e.printStackTrace();
// }
}
public static void main(String[] args) {
PDFUtil.office2PDF("F:\\temp\\test\\a.docx","F:\\temp\\test\\a.pdf");
}
}

View File

@ -1,4 +1,4 @@
package org.jeecg.modules.tools.pdf;
package org.jeecg.modules.tools.office.pdf;
public class WordToPDf {

View File

@ -1,60 +0,0 @@
package org.jeecg.modules.tools.pdf;
import com.artofsolving.jodconverter.DefaultDocumentFormatRegistry;
import com.artofsolving.jodconverter.DocumentConverter;
import com.artofsolving.jodconverter.DocumentFormat;
import com.artofsolving.jodconverter.openoffice.connection.OpenOfficeConnection;
import com.artofsolving.jodconverter.openoffice.connection.SocketOpenOfficeConnection;
import com.artofsolving.jodconverter.openoffice.converter.StreamOpenOfficeDocumentConverter;
import org.jeecg.common.util.SpringContextUtils;
import org.jeecg.modules.tools.Global;
import java.io.File;
import java.net.ConnectException;
/**
* PFD工具类
*/
public class PDFUtil {
private static Global global = SpringContextUtils.getBean(Global.class);
public void setGlobal(Global g){
global = g;
}
/**
* WORD转PDF方法
* @param sourceFile 要被转化的word文档路径如E:\\1.docx
* @param destFile 转成之后pdf文档存放路径如E:\\1.pdf
*/
public static void office2PDF(String sourceFile, String destFile) {
try {
File inputFile = new File(sourceFile);
File outputFile = new File(destFile);
// 获得文件格式(2018年9月25日16:12:35添加不增加格式多个文档拼接后转换会报错)
DefaultDocumentFormatRegistry formatReg = new DefaultDocumentFormatRegistry();
DocumentFormat pdfFormat = formatReg.getFormatByFileExtension("pdf");
DocumentFormat docFormat = formatReg.getFormatByFileExtension("doc");
if (!outputFile.getParentFile().exists()) {
outputFile.getParentFile().mkdirs();
}
OpenOfficeConnection connection = new SocketOpenOfficeConnection(global.libreOfficeUrl, global.libreOfficePort);
// OpenOfficeConnection connection = new SocketOpenOfficeConnection("192.168.1.227", 8100);
connection.connect();
// 2018年7月19日修改 原方法生成pdf会报错 wy
// DocumentConverter converter = new OpenOfficeDocumentConverter(connection);
DocumentConverter converter = new StreamOpenOfficeDocumentConverter(connection);
converter.convert(inputFile,docFormat, outputFile,pdfFormat);
connection.disconnect();
} catch (ConnectException e) {
e.printStackTrace();
}
}
// public static void main(String[] args) {
// PDFUtil.office2PDF("G:\\work\\IDEAWork\\jeecg\\beadhouse\\beadhouse_customer_jeecg_java\\src\\main\\resources\\officetemplates\\jkpg\\HT.docx","F:\\ht4.pdf");
// }
}

View File

@ -10,7 +10,7 @@ import org.jeecg.common.util.SpringContextUtils;
import org.jeecg.modules.tools.FileUtils;
import org.jeecg.modules.tools.Global;
import org.jeecg.modules.tools.IdGen;
import org.jeecg.modules.tools.pdf.PDFUtil;
import org.jeecg.modules.tools.office.pdf.PDFUtil;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;

View File

@ -13,11 +13,10 @@ import org.jeecg.common.util.text.StringUtils;
import org.jeecg.modules.kc.ktgl.entity.KcKechengbiao;
import org.jeecg.modules.kc.ktgl.service.IKcKechengbiaoService;
import org.jeecg.modules.tools.Global;
import org.jeecg.modules.tools.pdf.PDFUtil;
import org.jeecg.modules.tools.office.pdf.PDFUtil;
import org.jeecg.modules.zyk.entity.ZykInfo;
import org.jeecg.modules.zyk.mapper.ZykMapper;
import org.jeecg.modules.zyk.service.IZykService;
import org.jeecg.modules.zyk.utils.FileHandleUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
@ -29,7 +28,10 @@ import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.*;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
* @Description: 资源库信息

View File

@ -297,10 +297,22 @@ third-app:
# appSecret
client-secret: ??
agent-id: ??
# libreOffice5
# libreOffice5(旧)弃用
libreOffice:
url: 127.0.0.1
port: 8100
# 文档转换(新)
jodconverter:
# 使用本机作为转换服务器如果使用其它主机作为转换服务器则需要配置remote
# 请参考 org.jodconverter.boot.autoconfigure.JodConverterRemoteProperties
local:
enabled: true
# 为了避免不必要的兼容问题需要配置本机的LibreOffice安装目录
office-home: D:\Program Files\LibreOffice
# 配置LibreOffice的服务端口以逗号分隔每个端口为一个独立的进程
port-numbers: 2001,2002,2003,2004,2005,2006,2007,2008,2009,2010
# 配置最大的任务队列
max-tasks-per-process: 100
# 媒体处理器
ffmpeg:
# 根路径

View File

@ -303,10 +303,22 @@ third-app:
# appSecret
client-secret: ??
agent-id: ??
# libreOffice5
# libreOffice5(旧)弃用
libreOffice:
url: 127.0.0.1
port: 8100
# 文档转换(新)
jodconverter:
# 使用本机作为转换服务器如果使用其它主机作为转换服务器则需要配置remote
# 请参考 org.jodconverter.boot.autoconfigure.JodConverterRemoteProperties
local:
enabled: true
# 为了避免不必要的兼容问题需要配置本机的LibreOffice安装目录
office-home: D:\Program Files\LibreOffice
# 配置LibreOffice的服务端口以逗号分隔每个端口为一个独立的进程
port-numbers: 2001,2002,2003,2004,2005,2006,2007,2008,2009,2010,2011,2012,2013,2014,2015,2016,2017,2018,2019,2020
# 配置最大的任务队列
max-tasks-per-process: 100
# 媒体处理器
ffmpeg:
# 根路径

36
pom.xml
View File

@ -72,6 +72,12 @@
<!-- Log4j2爆雷漏洞 -->
<log4j2.version>2.17.0</log4j2.version>
<logback.version>1.2.9</logback.version>
<!-- 引入libreoffice依赖 -->
<libreoffice.jodconverter-local.version>4.4.2</libreoffice.jodconverter-local.version>
<libreoffice.ridl.version>7.3.4</libreoffice.ridl.version>
<libreoffice.jodconverter-spring-boot-starter.version>4.4.2</libreoffice.jodconverter-spring-boot-starter.version>
</properties>
<modules>
@ -384,6 +390,36 @@
<artifactId>jimureport-nosql-starter</artifactId>
<version>${jimureport-spring-boot-starter.version}</version>
</dependency>
<!-- 连接libreOffice驱动包 -->
<!-- <dependency>-->
<!-- <groupId>com.artofsolving</groupId>-->
<!-- <artifactId>jodconverter</artifactId>-->
<!-- <version>2.2.1</version>-->
<!-- </dependency>-->
<!-- <dependency>-->
<!-- <groupId>org.openoffice</groupId>-->
<!-- <artifactId>bootstrap-connector</artifactId>-->
<!-- <version>0.1.1</version>-->
<!-- </dependency>-->
<dependency>
<groupId>org.jodconverter</groupId>
<artifactId>jodconverter-local</artifactId>
<version>${libreoffice.jodconverter-local.version}</version>
</dependency>
<dependency>
<groupId>org.libreoffice</groupId>
<artifactId>ridl</artifactId>
<version>${libreoffice.ridl.version}</version>
</dependency>
<dependency>
<groupId>org.jodconverter</groupId>
<artifactId>jodconverter-spring-boot-starter</artifactId>
<version>${libreoffice.jodconverter-spring-boot-starter.version}</version>
</dependency>
<!-- 连接libreOffice驱动包END -->
</dependencies>
</dependencyManagement>