思路:其他文档转成配pdf然后通过流发送到前台,前台支持pdf
1.jodconverter-spring-boot-starter方法
1.pom文件
org.jodconverter
jodconverter-core
4.2.2
org.jodconverter
jodconverter-spring-boot-starter
4.2.2
org.jodconverter
jodconverter-local
4.2.2
2.application.xml
jodconverter:
local:
enabled: true
max-tasks-per-process: 10
port-numbers: 8100
3.调用
@RestController
public class OnlinePreviewController {
// 第一步:转换器直接注入
@Autowired
DocumentConverter documentConverter;
@GetMapping(“/toPdfFile”)
public String toPdfFile(FileMessage fileMessage) {
// 获取HttpServletResponse
HttpServletResponse response =
((ServletRequestAttributes)RequestContextHolder.getRequestAttributes()).getResponse();
// 需要转换的文件
File file = new File(fileMessage.getFileDownloadUri());
try {
// 转换之后文件生成的地址
File newFile = new File(“D:/common_files/pdf”);
if (!newFile.exists()) {
newFile.mkdirs();
}
String converterPdf = “D:/common_files/pdf” + “/” + fileMessage.getFileName() + “-pdf.pdf”;
// 文件转化
documentConverter.convert(file).to(new File(converterPdf)).execute();
// 使用response,将pdf文件以流的方式发送的前段
ServletOutputStream outputStream = response.getOutputStream();
// 读取文件
InputStream in = new FileInputStream(new File(converterPdf));
// copy文件
int i = IOUtils.copy(in, outputStream);
System.out.println(i);
in.close();
outputStream.close();
} catch (Exception e) {
e.printStackTrace();
}
return “This is to pdf”;
}
}
但是这种会发生excle过长穿行的情况;
2.使用com.artofsolving
1.pom文件
com.artofsolving
jodconverter
2.2.2
org.openoffice
jurt
3.0.1
org.openoffice
ridl
3.0.1
org.openoffice
juh
3.0.1
org.openoffice
unoil
3.0.1
注意2.2.2是在maven服务器上没有 所以需要单独下载处理 直接上网盘地址
https://pan.baidu.com/s/1dwfLHhUUg9nXf1aRAYfJ8Q
提取码:57nm
2.Controller
import com.artofsolving.jodconverter.DocumentConverter;
import com.artofsolving.jodconverter.openoffice.connection.OpenOfficeConnection;
import com.artofsolving.jodconverter.openoffice.connection.SocketOpenOfficeConnection;
import org.apache.commons.io.IOUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
@RestController
public class OnlinePreviewController {
@GetMapping(“/toPdfFile”)
public void toPdfFile(FileMessage fileMessage) throws IOException {
// 获取HttpServletResponse
HttpServletResponse response =
((ServletRequestAttributes)RequestContextHolder.getRequestAttributes()).getResponse();
// 源文件目录
File inputFile = new File(fileMes
sage.getFileDownloadUri());
if (!inputFile.exists()) {
System.out.println(“源文件不存在!”);
return;
}
// 转换之后文件生成的地址
File newFile = new File(“D:/common_files/pdf”);
if (!newFile.exists()) {
newFile.mkdirs();
}
String converterPdf = “D:/common_files/pdf” + “/” + fileMessage.getFileName() + “-pdf.pdf”;
// 输出文件目录
File outputFile = new File(converterPdf);
if (!outputFile.getParentFile().exists()) {
outputFile.getParentFile().exists();
}
// 调用openoffice服务线程
String command =
“C:/Program Files (x86)/OpenOffice 4/program/soffice.exe -headless -accept=“socket,host=127.0.0.1,port=8100;urp;””;
Process p = Runtime.getRuntime().exec(command);
// 连接openoffice服务
OpenOfficeConnection connection = new SocketOpenOfficeConnection(“127.0.0.1”, 8100);
connection.connect();
// 转换
DocumentConverter converter = new ConverterDocument(connection);
converter.convert(inputFile, outputFile);
ServletOutputStream outputStream = response.getOutputStream();
// 读取文件
InputStream in = new FileInputStream(new File(converterPdf));
// copy文件
int i = IOUtils.copy(in, outputStream);
in.close();
outputStream.close();
// 关闭连接
connection.disconnect();
// 关闭进程
p.destroy();
System.out.println(“转换完成!”);
}
}
转换方法为
DocumentConverter converter = new ConverterDocument(connection);
converter.convert(inputFile, outputFile);
因为excel折行问题 所以我们把纸张改变 重写了ConverterDocument
3.ConverterDocument
import com.artofsolving.jodconverter.openoffice.connection.OpenOfficeConnection;
import com.artofsolving.jodconverter.openoffice.converter.StreamOpenOfficeDocumentConverter;
import com.sun.star.awt.Size;
import com.sun.star.beans.PropertyValue;
import com.sun.star.lang.XComponent;
import com.sun.star.uno.UnoRuntime;
import com.sun.star.view.PaperFormat;
import com.sun.star.view.XPrintable;
/**
-
应用模块名称:
-
代码描述:
-
Copyright: Copyright © 2020, Inc. All rights reserved.
-
Company: 万朋测绘科技有限公司
-
@author baocl
-
@since 2020/5/27 14:49
*/
public class ConverterDocument extends StreamOpenOfficeDocumentConverter {
public ConverterDocument(OpenOfficeConnection connection) {
super(connection);
}
public final static Size A5, A4, A3;
public final static Size B4, B5, B6;
public final static Size paperSize;
static {
A5 = new Size(14800, 21000);
A4 = new Size(21000, 29700);
A3 = new Size(29700, 42000);
B4 = new Size(25000, 35300);
B5 = new Size(17600, 25000);
B6 = new Size(12500, 17600);
// 最大限度 宽 1600000
paperSize = new Size(29700, 27940);
}
@Override
protected void refreshDocument(XComponent document) {
super.refreshDocument(document);
XPrintable xPrintable = (XPrintable)UnoRuntime.queryInterface(XPrintable.class, document);
PropertyValue[] printerDesc = new PropertyValue[2];
// 转换
printerDesc[0] = new PropertyValue();
printerDesc[0].Name = “PaperFormat”;
printerDesc[0].Value = PaperFormat.USER;
// 纸张大小
printerDesc[1] = new PropertyValue();
printerDesc[1].Name = “PaperSize”;
printerDesc[1].Value = paperSize;
try {
xPrintable.setPrinter(printerDesc);
} catch (Exception e) {
e.printStackTrace();
}
}
}
但是这里有一个问题 多sheet页 只有第一页改变了 所以解决办法想了两个
1:将多个sheet合成一个
2:将多sheet也分开打印
(暂时没实现)
然后发现了kkFileView 请参考:https://blog.csdn.net/qq_20143059/article/details/106427297
emmmmmm真香
[
新人创作打卡挑战赛
发博客就能抽奖!定制产品红包拿不停!
](https://blogdev.blog.csdn.net/article/details/124124117?utm_campaign=marketingcard&utm_source=qq_20143059&utm_content=106379651)
最后
小编这些年深知大多数初中级工程师,想要提升自己,往往是自己摸索成长,自己不成体系的自学效果低效漫长且无助。
因此我收集整理了一份《2024年Java全套学习资料》,初衷也很简单,就是希望能够帮助到想自学提升又不知道该从何学起的朋友,同时减轻大家的负担。
由于文件比较大,这里只是将部分目录截图出来,每个节点里面都包含大厂面经、学习笔记、源码讲义、实战项目、讲解视频,并且会持续更新!
如果你需要这些资料,⬅专栏获取
ng)
新人创作打卡挑战赛 [外链图片转存中…(img-d8bpDA4A-1719528479668)]
发博客就能抽奖!定制产品红包拿不停!
](https://blogdev.blog.csdn.net/article/details/124124117?utm_campaign=marketingcard&utm_source=qq_20143059&utm_content=106379651)
最后
小编这些年深知大多数初中级工程师,想要提升自己,往往是自己摸索成长,自己不成体系的自学效果低效漫长且无助。
因此我收集整理了一份《2024年Java全套学习资料》,初衷也很简单,就是希望能够帮助到想自学提升又不知道该从何学起的朋友,同时减轻大家的负担。
[外链图片转存中…(img-IOXILxhr-1719528479669)]
[外链图片转存中…(img-sq4uD8gl-1719528479670)]
[外链图片转存中…(img-rUCNxzfA-1719528479671)]
由于文件比较大,这里只是将部分目录截图出来,每个节点里面都包含大厂面经、学习笔记、源码讲义、实战项目、讲解视频,并且会持续更新!
如果你需要这些资料,⬅专栏获取