调解系统初始化版本
This commit is contained in:
@@ -0,0 +1,147 @@
|
||||
package com.ruoyi.common.utils;
|
||||
|
||||
import com.documents4j.api.DocumentType;
|
||||
import com.documents4j.api.IConverter;
|
||||
import com.documents4j.job.LocalConverter;
|
||||
import com.itextpdf.text.Document;
|
||||
import com.itextpdf.text.Font;
|
||||
import com.itextpdf.text.PageSize;
|
||||
import com.itextpdf.text.pdf.BaseFont;
|
||||
import com.itextpdf.text.pdf.PdfWriter;
|
||||
import com.itextpdf.tool.xml.XMLWorkerFontProvider;
|
||||
import com.itextpdf.tool.xml.XMLWorkerHelper;
|
||||
import com.ruoyi.common.config.RuoYiConfig;
|
||||
import com.tencentcloudapi.teo.v20220901.models.CC;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
|
||||
import java.io.*;
|
||||
import java.nio.charset.Charset;
|
||||
import java.nio.file.Files;
|
||||
|
||||
import static cn.hutool.core.util.ClassLoaderUtil.getClassLoader;
|
||||
|
||||
/**
|
||||
* @author wangqiong
|
||||
* @description pdf转换工具类
|
||||
* @date 2023-11-28 15:20
|
||||
*/
|
||||
@Slf4j
|
||||
public class PdfUtils {
|
||||
/**
|
||||
* 将html字符串转为pdf
|
||||
* @param pdfFilePath 保存的路径
|
||||
* @param htmlcontent:必须是完整的html格式,比如<html><body>123</body></html>
|
||||
*/
|
||||
public static boolean htmlStringConvertToPDF(String pdfFilePath, String htmlcontent) {
|
||||
Document document = new Document();
|
||||
PdfWriter writer = null;
|
||||
try {
|
||||
writer = PdfWriter.getInstance(document, new FileOutputStream(pdfFilePath));
|
||||
// 设置底部距离60,解决重叠问题
|
||||
|
||||
document.setPageSize(PageSize.A4);
|
||||
document.setMargins(50, 45, 50, 60);
|
||||
document.setMarginMirroring(false);
|
||||
document.open();
|
||||
// 解决PDF中文不显示
|
||||
String fontPath = "/D:/simsun.ttf"; //字体文件路径
|
||||
XMLWorkerFontProvider provider = new XMLWorkerFontProvider(XMLWorkerFontProvider.DONTLOOKFORFONTS);
|
||||
provider.register(fontPath);//注册字体
|
||||
log.error("注册字体");
|
||||
XMLWorkerHelper.getInstance().parseXHtml(writer, document, new ByteArrayInputStream(htmlcontent.getBytes("UTF-8")), Charset.forName("UTF-8"), provider);
|
||||
} catch (Exception e) {
|
||||
log.error("htmlStringConvertToPDF error", e.getMessage());
|
||||
return false;
|
||||
} finally {
|
||||
if (null != document) {
|
||||
document.close();
|
||||
}
|
||||
if (null != writer) {
|
||||
writer.close();
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
/**
|
||||
* 根据html文件生成pdf
|
||||
* @param pdfFilePath pdf文件生成路径
|
||||
* @param htmlFilePath html文件路径
|
||||
*/
|
||||
public static boolean htmlFileConvertToPDF(String pdfFilePath, String htmlFilePath) {
|
||||
Document document = new Document();
|
||||
PdfWriter writer = null;
|
||||
FileOutputStream fileOutputStream = null;
|
||||
FileInputStream fileInputStream = null;
|
||||
try {
|
||||
fileOutputStream = new FileOutputStream(pdfFilePath);
|
||||
writer = PdfWriter.getInstance(document, fileOutputStream);
|
||||
// 设置底部距离60,解决重叠问题
|
||||
document.setPageSize(PageSize.A4);
|
||||
document.setMargins(50, 45, 50, 60);
|
||||
document.setMarginMirroring(false);
|
||||
document.open();
|
||||
StringBuffer sb = new StringBuffer();
|
||||
fileInputStream = new FileInputStream(htmlFilePath);
|
||||
BufferedReader br = new BufferedReader(new InputStreamReader(fileInputStream, "UTF-8"));
|
||||
String readStr = "";
|
||||
while ((readStr = br.readLine()) != null) {
|
||||
sb.append(readStr);
|
||||
}
|
||||
XMLWorkerHelper.getInstance().parseXHtml(writer, document, new ByteArrayInputStream(sb.toString().getBytes("Utf-8")), null, Charset.forName("UTF-8"));
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
} finally {
|
||||
if (null != document) {
|
||||
document.close();
|
||||
}
|
||||
if (null != writer) {
|
||||
writer.close();
|
||||
}
|
||||
if (null != fileInputStream) {
|
||||
try {
|
||||
fileInputStream.close();
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
if (null != fileOutputStream) {
|
||||
try {
|
||||
fileOutputStream.close();
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* docx转pdf
|
||||
* @param pdfSaveDirectory:文件保存目录,例:D:\\pdf\\
|
||||
* @param fileName:保存的文件名称,例:test.pdf
|
||||
* @param docxFile:需要转换的docx文件
|
||||
* @return
|
||||
*/
|
||||
private static boolean docxConvertToPDF(String pdfSaveDirectory,String fileName,File docxFile) {
|
||||
// 不存在则新建
|
||||
File directory = new File(pdfSaveDirectory);
|
||||
if (!directory.exists()) {
|
||||
directory.mkdirs();
|
||||
}
|
||||
String pdfFilePath = pdfSaveDirectory + fileName;
|
||||
File outputFile = new File(pdfFilePath);
|
||||
try {
|
||||
InputStream docxInputStream = Files.newInputStream(docxFile.toPath());
|
||||
OutputStream outputStream = Files.newOutputStream(outputFile.toPath());
|
||||
IConverter converter = LocalConverter.builder().build();
|
||||
converter.convert(docxInputStream).as(DocumentType.DOCX).to(outputStream).as(DocumentType.PDF).execute();
|
||||
docxInputStream.close();
|
||||
outputStream.close();
|
||||
|
||||
} catch (Exception e) {
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user