html转pdf
This commit is contained in:
+14
@@ -118,6 +118,20 @@ public class VideoController extends BaseController {
|
||||
|
||||
return videoService.secretaryRoleByUserId(userId);
|
||||
}
|
||||
/**
|
||||
* 根据html字符串转pdf并和案件关联
|
||||
* @param reservedConferenceVO
|
||||
* @return
|
||||
*/
|
||||
@Anonymous
|
||||
@PostMapping("htmlToPDF")
|
||||
public AjaxResult secretaryRoleByUserId( @RequestBody ReservedConferenceVO reservedConferenceVO) {
|
||||
if( reservedConferenceVO.getCaseId()==null || StrUtil.isEmpty(reservedConferenceVO.getHtmlContent())){
|
||||
return success();
|
||||
}
|
||||
|
||||
return videoService.htmlToPDF(reservedConferenceVO);
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
@@ -181,6 +181,17 @@
|
||||
</dependency>
|
||||
|
||||
|
||||
<dependency>
|
||||
<groupId>com.itextpdf</groupId>
|
||||
<artifactId>itextpdf</artifactId>
|
||||
<version>5.5.13</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.itextpdf.tool</groupId>
|
||||
<artifactId>xmlworker</artifactId>
|
||||
<version>5.5.13</version>
|
||||
</dependency>
|
||||
|
||||
|
||||
<!-- 发送邮件-->
|
||||
<dependency>
|
||||
|
||||
@@ -140,5 +140,9 @@ public class RuoYiConfig
|
||||
{
|
||||
return getProfile() + "/video";
|
||||
}
|
||||
public static String getHtml2PDFPath()
|
||||
{
|
||||
return getProfile() + "/upload/html2PDF/";
|
||||
}
|
||||
// https://1304001529.vod-qcloud.com/b78823bbvodcq1304001529/3ce565bf3270835011486046286/f0.mp4
|
||||
}
|
||||
|
||||
@@ -0,0 +1,148 @@
|
||||
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.PageSize;
|
||||
import com.itextpdf.text.pdf.PdfWriter;
|
||||
import com.itextpdf.tool.xml.XMLWorkerHelper;
|
||||
|
||||
import java.io.*;
|
||||
import java.nio.charset.Charset;
|
||||
import java.nio.file.Files;
|
||||
|
||||
/**
|
||||
* @author wangqiong
|
||||
* @description pdf转换工具类
|
||||
* @date 2023-11-28 15:20
|
||||
*/
|
||||
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 {
|
||||
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"));
|
||||
} catch (Exception e) {
|
||||
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;
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
try {
|
||||
|
||||
String pdfFile = "D:\\test2.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 >" + "<h1>夫士大夫士大夫发</h1><p>会见韩国机构行家</p ><h2>地方鬼地方鬼地方三个广泛的</h2></body></html>";
|
||||
|
||||
htmlStringConvertToPDF(pdfFile,htmlContent);
|
||||
// parseHtml2PdfByFilePath(pdfFile,htmlFile);
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
||||
+14
-1
@@ -23,12 +23,25 @@ public class ReservedConferenceVO {
|
||||
* 房间号id
|
||||
*/
|
||||
private Long roomId;
|
||||
/**
|
||||
* 会议开始时间
|
||||
*/
|
||||
@NotNull(message = "会议开始时间不能为空")
|
||||
@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
|
||||
private Date scheduleStartTime;
|
||||
|
||||
/**
|
||||
* 会议结束时间
|
||||
*/
|
||||
@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
|
||||
private Date scheduleEndTime;
|
||||
/**
|
||||
* 案件id
|
||||
*/
|
||||
@NotNull(message = "案件id不能为空")
|
||||
private Long caseId;
|
||||
/**
|
||||
* html内容
|
||||
*/
|
||||
private String htmlContent;
|
||||
|
||||
}
|
||||
|
||||
@@ -2,6 +2,7 @@ package com.ruoyi.wisdomarbitrate.service;
|
||||
|
||||
import com.ruoyi.common.core.domain.AjaxResult;
|
||||
import com.ruoyi.wisdomarbitrate.domain.IdentityAuthentication;
|
||||
import com.ruoyi.wisdomarbitrate.domain.vo.ReservedConferenceVO;
|
||||
import com.ruoyi.wisdomarbitrate.domain.vo.WeChatUserVO;
|
||||
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
@@ -39,4 +40,11 @@ public interface VideoService {
|
||||
* @return
|
||||
*/
|
||||
AjaxResult secretaryRoleByUserId(Long userId);
|
||||
|
||||
/**
|
||||
* 根据html字符串转pdf并和案件关联
|
||||
* @param reservedConferenceVO
|
||||
* @return
|
||||
*/
|
||||
AjaxResult htmlToPDF(ReservedConferenceVO reservedConferenceVO);
|
||||
}
|
||||
|
||||
+35
-3
@@ -13,9 +13,11 @@ import com.ruoyi.common.core.domain.entity.SysRole;
|
||||
import com.ruoyi.common.core.domain.entity.SysUser;
|
||||
import com.ruoyi.common.core.domain.model.LoginUser;
|
||||
import com.ruoyi.common.core.redis.RedisCache;
|
||||
import com.ruoyi.common.utils.PdfUtils;
|
||||
import com.ruoyi.common.utils.SecurityUtils;
|
||||
import com.ruoyi.common.utils.SmsUtils;
|
||||
import com.ruoyi.common.utils.StringUtils;
|
||||
import com.ruoyi.common.utils.file.FileUploadUtils;
|
||||
import com.ruoyi.common.utils.spring.SpringUtils;
|
||||
import com.ruoyi.system.mapper.SysRoleMapper;
|
||||
import com.ruoyi.system.mapper.SysUserMapper;
|
||||
@@ -23,6 +25,7 @@ import com.ruoyi.system.mapper.SysUserRoleMapper;
|
||||
import com.ruoyi.wisdomarbitrate.domain.CaseApplication;
|
||||
import com.ruoyi.wisdomarbitrate.domain.CaseAttach;
|
||||
import com.ruoyi.wisdomarbitrate.domain.IdentityAuthentication;
|
||||
import com.ruoyi.wisdomarbitrate.domain.vo.ReservedConferenceVO;
|
||||
import com.ruoyi.wisdomarbitrate.domain.vo.WeChatUserVO;
|
||||
import com.ruoyi.wisdomarbitrate.mapper.CaseApplicationMapper;
|
||||
import com.ruoyi.wisdomarbitrate.mapper.CaseAttachMapper;
|
||||
@@ -312,6 +315,38 @@ public class VideoServiceImpl implements VideoService {
|
||||
return success(jsonObject);
|
||||
}
|
||||
|
||||
/**
|
||||
* 根据html字符串转pdf并和案件关联
|
||||
* @param reservedConferenceVO
|
||||
* @return
|
||||
*/
|
||||
@Override
|
||||
public AjaxResult htmlToPDF(ReservedConferenceVO reservedConferenceVO) {
|
||||
String currentFileName = System.currentTimeMillis() + ".pdf";
|
||||
String fileName = null;
|
||||
try {
|
||||
fileName = getPathFileName(RuoYiConfig.getHtml2PDFPath(), currentFileName);
|
||||
} catch (IOException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
|
||||
// html转pdf并上传到服务器
|
||||
boolean convertFlag = PdfUtils.htmlStringConvertToPDF(RuoYiConfig.getHtml2PDFPath() + currentFileName, reservedConferenceVO.getHtmlContent());
|
||||
|
||||
// 绑定案件
|
||||
if(convertFlag){
|
||||
CaseAttach caseAttach = CaseAttach.builder().caseAppliId(reservedConferenceVO.getCaseId())
|
||||
.annexName(fileName)
|
||||
.annexPath(RuoYiConfig.getHtml2PDFPath())
|
||||
.annexType(11)
|
||||
.build();
|
||||
caseAttachMapper.save(caseAttach);
|
||||
return AjaxResult.success();
|
||||
}else {
|
||||
return AjaxResult.error("pdf转换失败");
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 查询出音视频集合,并下载,在将云点播上面的音视频删除
|
||||
@@ -388,9 +423,7 @@ public class VideoServiceImpl implements VideoService {
|
||||
staticAndMksDir = Paths.get(absPath).toFile().toString();
|
||||
long downloadFile = HttpUtil.downloadFile(fileUrl, staticAndMksDir);
|
||||
if(downloadFile>0) {
|
||||
log.info("下载成功roomId"+roomId);
|
||||
Long caseId = caseApplicationMapper.selectCaseIdByRoomId(roomId);
|
||||
log.info("下载成功caseId"+caseId);
|
||||
String annexName = getPathFileName(RuoYiConfig.getVideoUploadPath(), fileName);
|
||||
// 存入数据库
|
||||
CaseAttach caseAttach = CaseAttach.builder().caseAppliId(caseId)
|
||||
@@ -399,7 +432,6 @@ public class VideoServiceImpl implements VideoService {
|
||||
.annexType(9)
|
||||
.build();
|
||||
caseAttachMapper.save(caseAttach);
|
||||
log.info("保存附件号成功");
|
||||
return annexName;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user