package com.ruoyi.web.controller.tool;
|
|
import cn.hutool.core.util.StrUtil;
|
import cn.hutool.http.HttpUtil;
|
import com.documents4j.api.DocumentType;
|
import com.documents4j.api.IConverter;
|
import com.documents4j.job.LocalConverter;
|
import lombok.extern.slf4j.Slf4j;
|
import org.springframework.mock.web.MockMultipartFile;
|
import org.springframework.web.multipart.MultipartFile;
|
|
import java.io.*;
|
import java.net.URL;
|
import java.nio.file.Files;
|
import java.util.HashMap;
|
|
@Slf4j
|
public class PdfUtils {
|
|
/**
|
* word 转 pdf
|
*
|
* @param url
|
*/
|
public static String wordToPdf(String url,String filePath, String fileName) {
|
try {
|
DocumentType documentType = DocumentType.DOC;
|
if(url.contains(".docx")){
|
documentType = DocumentType.DOCX;
|
}
|
if(url.contains(".doc")){
|
documentType = DocumentType.DOC;
|
}
|
if(url.contains(".xlsx")){
|
documentType = DocumentType.XLSX;
|
}else {
|
if(url.contains(".xls")){
|
documentType = DocumentType.XLS;
|
}
|
}
|
InputStream inputStream = new URL(url).openStream();
|
ByteArrayOutputStream stream = new ByteArrayOutputStream();
|
IConverter converter = LocalConverter.builder().build();
|
converter.convert(inputStream)
|
.as(documentType)
|
.to(stream)
|
.as(DocumentType.PDF).execute();
|
|
//上传图片
|
byte2File(stream.toByteArray(),filePath + "/pdf",fileName.substring(0,fileName.lastIndexOf(".")) + ".pdf");
|
MultipartFile multipartFile = convertToMultipartFile(stream,fileName.substring(0,fileName.lastIndexOf(".")) );
|
String s = ObsUploadUtil.obsUpload(multipartFile);
|
|
stream.close();
|
inputStream.close();
|
return s;
|
} catch (Exception e) {
|
e.printStackTrace();
|
}
|
return null;
|
}
|
|
|
|
public static MultipartFile convertToMultipartFile(ByteArrayOutputStream baos, String fileName) throws IOException {
|
// 创建一个临时文件
|
File tempFile = File.createTempFile(fileName, ".pdf");
|
|
// 将ByteArrayOutputStream中的数据写入临时文件
|
try (FileOutputStream fos = new FileOutputStream(tempFile)) {
|
baos.writeTo(fos);
|
} catch (IOException e) {
|
e.printStackTrace();
|
}
|
|
// 创建一个MultipartFile对象
|
return new MockMultipartFile(
|
fileName + ".pdf", // 参数名称
|
fileName + ".pdf", // 文件名
|
"application/pdf", // 内容类型
|
Files.readAllBytes(tempFile.toPath()) // 文件内容
|
);
|
}
|
|
/**
|
* file转byte
|
*/
|
public static byte[] file2byte(File file){
|
byte[] buffer = null;
|
try{
|
FileInputStream fis = new FileInputStream(file);
|
ByteArrayOutputStream bos = new ByteArrayOutputStream();
|
byte[] b = new byte[1024];
|
int n;
|
while ((n = fis.read(b)) != -1)
|
{
|
bos.write(b, 0, n);
|
}
|
fis.close();
|
bos.close();
|
buffer = bos.toByteArray();
|
}catch (FileNotFoundException e){
|
e.printStackTrace();
|
}
|
catch (IOException e){
|
e.printStackTrace();
|
}
|
return buffer;
|
}
|
|
/**
|
* byte 转file
|
*/
|
public static File byte2File(byte[] buf, String filePath, String fileName){
|
BufferedOutputStream bos = null;
|
FileOutputStream fos = null;
|
OutputStreamWriter osw = null;
|
File file = null;
|
try{
|
File dir = new File(filePath+"/");
|
if (!dir.exists()){
|
dir.mkdirs();
|
}
|
file = new File(filePath +File.separator + fileName);
|
fos = new FileOutputStream(file);
|
bos = new BufferedOutputStream(fos);
|
// osw = new OutputStreamWriter(fos, StandardCharsets.UTF_8);
|
bos.write(buf);
|
}catch (Exception e){
|
e.printStackTrace();
|
}
|
finally{
|
if (bos != null){
|
try{
|
bos.close();
|
}catch (IOException e){
|
e.printStackTrace();
|
}
|
}
|
if (fos != null){
|
try{
|
fos.close();
|
}catch (IOException e){
|
e.printStackTrace();
|
}
|
}
|
}
|
return file;
|
}
|
/**
|
* multipartFile转File
|
**/
|
public static File multipartFile2File(MultipartFile multipartFile){
|
File file = null;
|
if (multipartFile != null){
|
try {
|
file= File.createTempFile("tmp", null);
|
multipartFile.transferTo(file);
|
System.gc();
|
file.deleteOnExit();
|
}catch (Exception e){
|
e.printStackTrace();
|
log.warn("multipartFile转File发生异常:"+e);
|
}
|
}
|
return file;
|
}
|
|
|
public static void main(String[] args) {
|
// String url = "file:///E:\\qiyeweixin\\WXWork\\1688855207501340\\Cache\\File\\2024-09\\专业技术工作总结.docx";
|
// String filePath = "E:\\qiyeweixin\\WXWork\\1688855207501340\\Cache\\File\\2024-09";
|
// String fileName = "专业技术工作总结.docx";
|
// String s = wordToPdf(url, filePath, fileName);
|
// System.err.println(s);
|
|
// String url = "file:///F:\\测试动态列表Word.doc";
|
//// String filePath = "E:\\qiyeweixin\\WXWork\\1688855207501340\\Cache\\File\\2024-09";
|
//// String fileName = "专业技术工作总结.docx";4
|
// String filePath = "F:\\";
|
//
|
// String s = wordToPdf(url, filePath, "测试动态列表Word.doc");
|
// System.err.println(s);
|
|
|
|
// TODO Auto-generated method stub
|
|
|
HashMap<String, Object> paramMap = new HashMap<>();
|
paramMap.put("CorpID", "SCZT006959");
|
paramMap.put("Pwd", "123456");
|
paramMap.put("Mobile", "19522115070");
|
paramMap.put("Cell", "");
|
paramMap.put("SendTime", "");
|
paramMap.put("Content", java.net.URLEncoder.encode("你好,这是测试短信发送。【职评网】"));
|
String result3 = HttpUtil.post("http://sdk2.028lk.com/sdk2/BatchSend2.aspx", paramMap);
|
if (result3 == null) {
|
result3 = "";
|
}
|
result3 = StrUtil.trim(result3);
|
log.info("返回结果:" + result3);
|
|
if (result3.matches("^\\d+$") && !result3.equals("0")) {
|
return;
|
}
|
|
}
|
|
public static String test(String fileName){
|
String url = "file:///F:\\"+fileName;
|
// String filePath = "E:\\qiyeweixin\\WXWork\\1688855207501340\\Cache\\File\\2024-09";
|
// String fileName = "专业技术工作总结.docx";4
|
String filePath = "F:\\";
|
|
String s = wordToPdf(url, filePath, fileName);
|
System.err.println(s);
|
|
return s;
|
}
|
|
}
|