语音转录
This commit is contained in:
@@ -4,40 +4,56 @@ 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) {
|
||||
public boolean htmlStringConvertToPDF(String pdfFilePath, String htmlcontent) {
|
||||
Document document = new Document();
|
||||
PdfWriter writer = null;
|
||||
try {
|
||||
if(!new File(pdfFilePath).exists()){
|
||||
new File(pdfFilePath).createNewFile();
|
||||
}
|
||||
// if(!new File(pdfFilePath).exists()){
|
||||
// new File(pdfFilePath).createNewFile();
|
||||
// }
|
||||
writer = PdfWriter.getInstance(document, new FileOutputStream(pdfFilePath));
|
||||
// 设置底部距离60,解决重叠问题
|
||||
|
||||
document.setPageSize(PageSize.A4);
|
||||
document.setMargins(50, 45, 50, 60);
|
||||
document.setMarginMirroring(false);
|
||||
document.open();
|
||||
XMLWorkerHelper.getInstance().parseXHtml(writer, document, new ByteArrayInputStream(htmlcontent.getBytes("UTF-8")), null, Charset.forName("UTF-8"));
|
||||
// 解决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) {
|
||||
@@ -131,18 +147,4 @@ public class PdfUtils {
|
||||
return true;
|
||||
}
|
||||
|
||||
public static void main(String [] args) {
|
||||
try {
|
||||
|
||||
String pdfFile = "D:\\test13.pdf";
|
||||
String htmlContent1 = "<h1>夫士大夫士大夫发</h1><p>会见韩国机构行家</p ><h2>地方鬼地方鬼地方三个广泛的</h2>";
|
||||
String htmlContent = "<html><body style=\"font-size:12.0pt; font-family:宋体\">" + "<h1>夫士大夫士大夫发</h1><p>会见韩国机构行家</p ><h2>地方鬼地方鬼地方三个广泛的</h2></body></html>";
|
||||
String htmlContent2 = "<html><body style=\\\"font-size:12.0pt; font-family:宋体\\\"><h1>夫士大夫士大夫发</h1><p>会见韩国机构行家</p ><h2>地方鬼地方鬼地方三个广泛的</h2></body></html>";
|
||||
|
||||
htmlStringConvertToPDF(pdfFile,htmlContent2);
|
||||
// parseHtml2PdfByFilePath(pdfFile,htmlFile);
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user