|
|
@@ -1,497 +0,0 @@
|
|
|
-package org.springblade.evisa.controller;
|
|
|
-
|
|
|
-import io.swagger.annotations.Api;
|
|
|
-import lombok.AllArgsConstructor;
|
|
|
-import lombok.Synchronized;
|
|
|
-import lombok.extern.slf4j.Slf4j;
|
|
|
-import org.apache.pdfbox.pdmodel.PDDocument;
|
|
|
-import org.apache.pdfbox.pdmodel.PDPage;
|
|
|
-import org.jsoup.Jsoup;
|
|
|
-import org.jsoup.nodes.Document;
|
|
|
-import org.jsoup.nodes.Element;
|
|
|
-import org.jsoup.select.Elements;
|
|
|
-import org.springblade.common.utils.CommonUtil;
|
|
|
-import org.springblade.common.utils.SafeURLEncoder;
|
|
|
-import org.springblade.common.utils.SnowFlakeUtil;
|
|
|
-import org.springblade.core.oss.model.BladeFile;
|
|
|
-import org.springblade.core.tool.utils.Func;
|
|
|
-import org.springblade.core.tool.utils.IoUtil;
|
|
|
-import org.springblade.evisa.utils.FileUtils;
|
|
|
-import org.springblade.evisa.vo.ArchivesSplitInfoVO;
|
|
|
-import org.springblade.evisa.vo.TaskArchiveSplitVO;
|
|
|
-import org.springblade.resource.feign.NewIOSSClient;
|
|
|
-import org.springframework.beans.factory.annotation.Autowired;
|
|
|
-import org.springframework.data.redis.core.StringRedisTemplate;
|
|
|
-import org.springframework.jdbc.core.BeanPropertyRowMapper;
|
|
|
-import org.springframework.jdbc.core.JdbcTemplate;
|
|
|
-import org.springframework.scheduling.annotation.Scheduled;
|
|
|
-import org.springframework.transaction.annotation.Transactional;
|
|
|
-import org.springframework.web.bind.annotation.RestController;
|
|
|
-
|
|
|
-import javax.annotation.Resource;
|
|
|
-import java.io.*;
|
|
|
-import java.util.ArrayList;
|
|
|
-import java.util.List;
|
|
|
-import java.util.Map;
|
|
|
-import java.util.concurrent.CompletableFuture;
|
|
|
-import java.util.concurrent.ThreadPoolExecutor;
|
|
|
-import java.util.concurrent.TimeUnit;
|
|
|
-
|
|
|
-import org.apache.pdfbox.rendering.PDFRenderer;
|
|
|
-
|
|
|
-import java.awt.image.BufferedImage;
|
|
|
-import javax.imageio.ImageIO;
|
|
|
-
|
|
|
-
|
|
|
-/**
|
|
|
- * 清表基础数据表 控制器
|
|
|
- *
|
|
|
- * @author BladeX
|
|
|
- * @since 2022-05-18
|
|
|
- */
|
|
|
-@RestController
|
|
|
-@AllArgsConstructor
|
|
|
-@Api(value = "电签类", tags = "电签类接口")
|
|
|
-@Slf4j
|
|
|
-public class ArchiveController {
|
|
|
-
|
|
|
- private final StringRedisTemplate RedisTemplate;
|
|
|
-
|
|
|
- private final JdbcTemplate jdbcTemplate;
|
|
|
-
|
|
|
- private final NewIOSSClient newIOSSClient;
|
|
|
-
|
|
|
- // 线程池
|
|
|
- @Resource(name = "archivePoolExecutor")
|
|
|
- private ThreadPoolExecutor archExecutor;
|
|
|
-
|
|
|
- // @Scheduled(cron = "0/30 * * * * ?")
|
|
|
- public void SignTaskBatchPng() {
|
|
|
- //执行代码
|
|
|
- log.info("分解pdf专图片");
|
|
|
- // String sql = "SELECT distinct b.id,b.archive_id as archiveId ,REPLACE(b.file_url,'https://xinan1.zos.ctyun.cn','http://100.86.2.1:80') as fileUrl,c.id as taskId from u_archives_auto a , u_archive_file b ,u_task_split c where a.id=b.archive_id and ((FIND_IN_SET(a.id,c.ids) and c.type=3) or (a.contract_id=c.contract_id and c.type=2)) and a.is_deleted=0 and b.is_deleted=0 and a.split_status not in(1,2) LIMIT 20";
|
|
|
- String sql = "SELECT distinct b.id,b.archive_id as archiveId ,b.file_url as fileUrl,c.id as taskId from u_archives_auto a , u_archive_file b ,u_task_split c where a.id=b.archive_id and ((FIND_IN_SET(a.id,c.ids) and c.type=3) or (a.contract_id=c.contract_id and c.type=2)) and a.is_deleted=0 and b.is_deleted=0 and a.split_status not in(1,2) LIMIT 20";
|
|
|
- List<TaskArchiveSplitVO> query = jdbcTemplate.query(sql, new BeanPropertyRowMapper<>(TaskArchiveSplitVO.class));
|
|
|
-
|
|
|
- if (query != null && query.size() >= 1) {
|
|
|
- for (TaskArchiveSplitVO dataInfo : query) {
|
|
|
- if (archExecutor.getQueue().size() <= 20) {
|
|
|
- Boolean aBoolean = RedisTemplate.hasKey("splitpng-" + dataInfo.getArchiveId());
|
|
|
- if (!aBoolean) {
|
|
|
- RedisTemplate.opsForValue().setIfAbsent("splitpng-" + dataInfo.getArchiveId(), "1", 600, TimeUnit.SECONDS);
|
|
|
- CompletableFuture<Void> runAsync = CompletableFuture.runAsync(() -> {
|
|
|
- try {
|
|
|
- signTaskBatchpngToHtml(dataInfo);
|
|
|
- } catch (Exception e) {
|
|
|
- e.printStackTrace();
|
|
|
- }
|
|
|
- }, archExecutor);
|
|
|
- }
|
|
|
- }
|
|
|
- }
|
|
|
- }
|
|
|
- System.out.println("队列数量_img" + archExecutor.getQueue().size());
|
|
|
- System.out.println("活跃数量_img" + archExecutor.getActiveCount());
|
|
|
- System.out.println("总共数量_img" + archExecutor.getTaskCount());
|
|
|
- System.out.println("完成数量_img" + archExecutor.getCompletedTaskCount());
|
|
|
- }
|
|
|
-
|
|
|
- // 分解第一页的任务
|
|
|
-
|
|
|
- public void signTaskBatchpngToHtml(TaskArchiveSplitVO taskSign) {
|
|
|
- try {
|
|
|
- String fileUrl = taskSign.getFileUrl();
|
|
|
- String archiveId = taskSign.getArchiveId();
|
|
|
- String id = taskSign.getId();
|
|
|
- String taskId = taskSign.getTaskId();
|
|
|
- List<String> listPdf = new ArrayList<>();
|
|
|
- int startPage = 0;
|
|
|
- for (int i = 2; i <= 10; i++) {
|
|
|
- // 获取pdf第二页的数据
|
|
|
- String firstUrl = FileUtils.getSysLocalFileUrl() + "archiveSplit/" + archiveId + "first__" + i + "__.pdf";
|
|
|
- File file = new File(firstUrl);
|
|
|
- if (!file.exists()) {
|
|
|
- int pdfByPage = getPdfByPage(i, i, fileUrl, firstUrl);
|
|
|
- if(pdfByPage==1){
|
|
|
- String sql22 = "INSERT INTO u_archives_split_info(id,status,file_url,first_file_url,task_id,archive_id,create_time) VALUES(?,?,?,?,?,?,SYSDATE())";
|
|
|
- int addCount = jdbcTemplate.update(sql22, id, 4, fileUrl, "获取数据源失败", taskId, archiveId);
|
|
|
- String updateSql = "UPDATE u_archives_auto SET split_status=? WHERE id=?";
|
|
|
- int delCount = jdbcTemplate.update(updateSql, 1, archiveId);
|
|
|
- return;
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- // 保存第一页为300DPI图片
|
|
|
- String imagePath = FileUtils.getSysLocalFileUrl() + "archiveSplit/" + archiveId + "first__" + i + "__.png";
|
|
|
- File imgfile = new File(imagePath);
|
|
|
- if (!imgfile.exists()) {
|
|
|
- int dataNum = savePdfAsImage(1, firstUrl, imagePath);
|
|
|
- }
|
|
|
- // 删除pdf
|
|
|
- file.delete();
|
|
|
- String state = OcrTitle(imagePath, "1");
|
|
|
- if (state.equals("1")) {
|
|
|
- if (startPage < 2) {
|
|
|
- startPage = i;
|
|
|
- }
|
|
|
- listPdf.add(imagePath);
|
|
|
- } else {
|
|
|
- imgfile.delete();
|
|
|
- break;
|
|
|
- }
|
|
|
- }
|
|
|
- System.out.println(listPdf.size());
|
|
|
- String filePath = startPage + "--" + (listPdf.size() + 1);
|
|
|
- //判断
|
|
|
- List<Map<String, Object>> mapList = jdbcTemplate.queryForList("select * from u_archives_split_info where id=" + id + "");
|
|
|
- if (mapList != null && Func.isNotEmpty(mapList) && mapList.size() >= 1) {
|
|
|
- String status = mapList.get(0).get("status") + "";
|
|
|
- // if (status.equals("3")) {
|
|
|
- String updateSql = "update u_archives_split_info set status=2 ,file_url='"+fileUrl+"',first_file_url ='"+filePath+"' where id=" + id;
|
|
|
- jdbcTemplate.update(updateSql);
|
|
|
- String uateSql = "UPDATE u_archives_auto SET split_status=? WHERE id=?";
|
|
|
- int delCount = jdbcTemplate.update(uateSql, 2, archiveId);
|
|
|
- // }
|
|
|
-
|
|
|
- } else {
|
|
|
- // 所有SQL都改为参数化查询
|
|
|
- String sql22 = "INSERT INTO u_archives_split_info(id,status,file_url,first_file_url,task_id,archive_id,create_time) VALUES(?,?,?,?,?,?,SYSDATE())";
|
|
|
- int addCount = jdbcTemplate.update(sql22, id, 2, fileUrl, filePath, taskId, archiveId);
|
|
|
-
|
|
|
- String updateSql = "UPDATE u_archives_auto SET split_status=? WHERE id=?";
|
|
|
- int delCount = jdbcTemplate.update(updateSql, 2, archiveId);
|
|
|
- }
|
|
|
- String delSql = "DELETE FROM u_archive_file WHERE id<>? AND archive_id=?";
|
|
|
- jdbcTemplate.update(delSql, id, archiveId);
|
|
|
- RedisTemplate.delete("splitpng-" + archiveId);
|
|
|
-
|
|
|
- } catch (Exception e) {
|
|
|
- System.out.println("12321312");
|
|
|
- e.printStackTrace();
|
|
|
- }
|
|
|
- }
|
|
|
- // @Scheduled(cron = "0/30 * * * * ?")
|
|
|
- public void SplitPdfInfo() {
|
|
|
- //执行代码
|
|
|
- log.info("分解html开始");
|
|
|
- String sql = "select * from u_archives_split_info where status =2 LIMIT 20 "; // and TIMESTAMPDIFF(MINUTE, create_time, NOW()) >=3";
|
|
|
- List<ArchivesSplitInfoVO> query = jdbcTemplate.query(sql, new BeanPropertyRowMapper<>(ArchivesSplitInfoVO.class));
|
|
|
- log.info("分解html开始===" + query.size());
|
|
|
- if (query != null && query.size() >= 1) {
|
|
|
- for (ArchivesSplitInfoVO dataInfo : query) {
|
|
|
- if (archExecutor.getQueue().size() <= 10) {
|
|
|
- Boolean aBoolean = RedisTemplate.hasKey("splithtml-" + dataInfo.getArchiveId());
|
|
|
- if (!aBoolean) {
|
|
|
-
|
|
|
- if (!aBoolean) {
|
|
|
- RedisTemplate.opsForValue().setIfAbsent("splithtml-" + dataInfo.getArchiveId(), "1", 1200, TimeUnit.SECONDS);
|
|
|
- CompletableFuture<Void> runAsync = CompletableFuture.runAsync(() -> {
|
|
|
- try {
|
|
|
- /*===============执行批量任务===============*/
|
|
|
- signTaskBatchpng(dataInfo);
|
|
|
- } finally {
|
|
|
- RedisTemplate.delete("splithtml-" + dataInfo.getArchiveId());
|
|
|
- }
|
|
|
- }, archExecutor);
|
|
|
- }
|
|
|
- }
|
|
|
- }
|
|
|
- }
|
|
|
- }
|
|
|
- System.out.println("队列数量_html" + archExecutor.getQueue().size());
|
|
|
- System.out.println("活跃数量_html" + archExecutor.getActiveCount());
|
|
|
- System.out.println("总共数量_html" + archExecutor.getTaskCount());
|
|
|
- System.out.println("完成数量_html" + archExecutor.getCompletedTaskCount());
|
|
|
- }
|
|
|
-
|
|
|
- public void signTaskBatchpng(ArchivesSplitInfoVO taskSign) {
|
|
|
- try {
|
|
|
- System.out.println("分解001");
|
|
|
- String archiveId = taskSign.getArchiveId();
|
|
|
- String fileUlr = taskSign.getFileUrl();
|
|
|
- String firstPage = FileUtils.getSysLocalFileUrl() + "archiveSplit/";
|
|
|
- String firstFileUrl = taskSign.getFirstFileUrl();
|
|
|
- String firstUrl[] = firstFileUrl.split("--");
|
|
|
- int basePage = Integer.parseInt(firstUrl[1]);
|
|
|
- int baseStart = Integer.parseInt(firstUrl[0]);
|
|
|
- String dutyUser = "";
|
|
|
- int bkb = 0;
|
|
|
- //将imagePath 的数据转成一个可解析的html
|
|
|
- String htmlUrl = pngToHtml(firstPage, archiveId, taskSign.getFirstFileUrl());
|
|
|
- System.out.println("分解002=" + htmlUrl);
|
|
|
-
|
|
|
- if (htmlUrl.indexOf("_001.html") >= 0 && htmlUrl.indexOf("archiveSplit") >= 0) {
|
|
|
- String htmlString = IoUtil.readToString(new FileInputStream(htmlUrl));
|
|
|
- Document doc = Jsoup.parse(htmlString);
|
|
|
- Element table = doc.select("table").first();
|
|
|
- Elements trs = table.select("tr");
|
|
|
- //由于解析已经成功,可能数据已经分解过,需要删除
|
|
|
- if (trs != null && trs.size() >= 1) {
|
|
|
- String sql = "delete from u_archive_file where id<>'" + taskSign.getId() + "' and archive_id='" + archiveId + "'";
|
|
|
- jdbcTemplate.execute(sql);
|
|
|
- }
|
|
|
-
|
|
|
- for (int i = 0; i <= trs.size() - 1; i++) {
|
|
|
- Element tr = trs.get(i);
|
|
|
- String zrz = tr.select("td").get(0).text();
|
|
|
- String wjtm = tr.select("td").get(1).text();
|
|
|
- String rq = tr.select("td").get(2).text();
|
|
|
- String ym = tr.select("td").get(3).text();
|
|
|
- if (zrz.equals("责任者") && wjtm.equals("文件题名") && rq.equals("日期")) {
|
|
|
- continue;
|
|
|
- }
|
|
|
- int startYm = 0;
|
|
|
- int endYm = 0;
|
|
|
- if (i < trs.size() - 1) {
|
|
|
- startYm = Func.toInt(ym);
|
|
|
- String enData = trs.get(i + 1).select("td").get(3).text();
|
|
|
- if (enData.indexOf("页") >= 0) {
|
|
|
- enData = trs.get(i + 2).select("td").get(3).text();
|
|
|
- }
|
|
|
-
|
|
|
- String[] parts = enData.split("(?<=\\D)(?=\\d)|(?<=\\d)(?=\\D)");
|
|
|
- if (parts != null && parts.length >= 1) {
|
|
|
- endYm = Func.toInt(parts[0]);
|
|
|
- }
|
|
|
- } else {
|
|
|
- String[] split = ym.split("(?<=\\D)(?=\\d)|(?<=\\d)(?=\\D)");
|
|
|
- if (split != null && split.length >= 3) {
|
|
|
- startYm = Func.toInt(split[0]);
|
|
|
- endYm = Func.toInt(split[2]);
|
|
|
- } else {
|
|
|
- startYm = Func.toInt(split[0]);
|
|
|
- endYm = Func.toInt(split[0]);
|
|
|
- }
|
|
|
- }
|
|
|
- startYm = basePage + startYm;
|
|
|
- endYm = basePage + endYm;
|
|
|
- dutyUser = zrz;
|
|
|
- System.out.println("序号=" + i + "--文件提名:" + wjtm + "--开始(" + startYm + "-" + endYm + ")---页数" + (endYm - startYm + 1));
|
|
|
- // 分解文件
|
|
|
- String fmlUrl = FileUtils.getSysLocalFileUrl() + "archiveSplit/" + archiveId + "_cf_00" + i + ".pdf";
|
|
|
- getPdfByPage(startYm, endYm, fileUlr, fmlUrl);
|
|
|
- saveDataToMysql(fmlUrl, wjtm, taskSign.getId(), endYm - startYm + 1, i, zrz, rq);
|
|
|
- bkb = endYm;
|
|
|
- }
|
|
|
- } else {
|
|
|
- return;
|
|
|
- }
|
|
|
-
|
|
|
- // 添加封面信息
|
|
|
- String fmlUrl = FileUtils.getSysLocalFileUrl() + "archiveSplit/" + archiveId + "_fm_001.pdf";
|
|
|
- getPdfByPage(1, 1, fileUlr, fmlUrl);
|
|
|
- saveDataToMysql(fmlUrl, "封面", taskSign.getId(), 1, -4, dutyUser, "");
|
|
|
-
|
|
|
- // 卷内目录
|
|
|
- String jnmuUrl = FileUtils.getSysLocalFileUrl() + "archiveSplit/" + archiveId + "_jnml_001.pdf";
|
|
|
- getPdfByPage(baseStart, basePage, fileUlr, jnmuUrl);
|
|
|
- saveDataToMysql(jnmuUrl, "卷内目录", taskSign.getId(), 1, -3, dutyUser, "");
|
|
|
-
|
|
|
- // 卷内备考表
|
|
|
- String jnbkbUrl = FileUtils.getSysLocalFileUrl() + "archiveSplit/" + archiveId + "_jnbkb_001.pdf";
|
|
|
- getPdfByPage(bkb + 1, bkb + 1, fileUlr, jnbkbUrl);
|
|
|
-
|
|
|
- File jlPdfFile = new File(jnbkbUrl);
|
|
|
- if (jlPdfFile.exists()) {
|
|
|
- saveDataToMysql(jnbkbUrl, "卷内备考表", taskSign.getId(), 1, 100, dutyUser, "");
|
|
|
- }
|
|
|
-
|
|
|
- // 背脊表
|
|
|
- String bjbUrl = FileUtils.getSysLocalFileUrl() + "archiveSplit/" + archiveId + "_beiji_001.pdf";
|
|
|
- String bjbUrlPng = FileUtils.getSysLocalFileUrl() + "archiveSplit/" + archiveId + "_beiji_001.png";
|
|
|
-
|
|
|
- int pdfByPage = getPdfByPage(bkb + 2, bkb + 2, fileUlr, bjbUrl);
|
|
|
- if(pdfByPage==0){
|
|
|
- File bgImgFile = new File(bjbUrlPng);
|
|
|
- if (!bgImgFile.exists()) {
|
|
|
- int dataNum = savePdfAsImage(1, bjbUrl, bjbUrlPng);
|
|
|
- }
|
|
|
- String state = OcrTitle(bjbUrlPng, "3");
|
|
|
- if (state.equals("1")) {
|
|
|
- saveDataToMysql(bjbUrl, "背脊表", taskSign.getId(), 1, 101, dutyUser, "");
|
|
|
- }
|
|
|
- bgImgFile.delete();
|
|
|
- }
|
|
|
- // 修改任务状态
|
|
|
- String updateSql = "update u_archives_split_info set status=3 where id=" + taskSign.getId();
|
|
|
- jdbcTemplate.execute(updateSql);
|
|
|
- // 修改 u_archives_auto 为 已经分解
|
|
|
- String updateSqlAuto = "update u_archives_auto set split_status=1 where id=" + taskSign.getArchiveId();
|
|
|
- jdbcTemplate.execute(updateSqlAuto);
|
|
|
-
|
|
|
- // 统计各个任务的结果
|
|
|
- String taxkSql = "UPDATE u_task_split a set finished = (SELECT count(1) from u_archives_auto b where FIND_IN_SET(b.id,a.ids) and b.split_status=1) where FIND_IN_SET(" + taskSign.getArchiveId() + ",a.ids) and a.type=3";
|
|
|
- String taxkSql2 = "UPDATE u_task_split a set finished = (SELECT count(1) from u_archives_auto b where a.contract_id=b.contract_id) where a.id=" + taskSign.getTaskId() + " and a.type=2";
|
|
|
- jdbcTemplate.execute(taxkSql);
|
|
|
- jdbcTemplate.execute(taxkSql2);
|
|
|
-
|
|
|
- // 删除html
|
|
|
- File fileHtml = new File(fmlUrl);
|
|
|
- fileHtml.delete();
|
|
|
-
|
|
|
- // 修改完成情况
|
|
|
- RedisTemplate.delete("splithtml-" + archiveId);
|
|
|
- } catch (Exception e) {
|
|
|
- throw new RuntimeException(e);
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- public static String pngToHtml(String fileUrl, String pKeyId, String pageNum) {
|
|
|
- String lasHhtmlUrl = "";
|
|
|
- try {
|
|
|
- // 定义Python解释器路径和脚本路径
|
|
|
- String pythonScript = "/Users/hongchuangyanfa/Desktop/PycharmProjects/splitPngToHtml.py";
|
|
|
- // 构建命令
|
|
|
- ProcessBuilder pb = new ProcessBuilder("python3", pythonScript, fileUrl, pKeyId, pageNum);
|
|
|
- Process process = pb.start();
|
|
|
-
|
|
|
- // 读取Python脚本输出
|
|
|
- BufferedReader reader = new BufferedReader(
|
|
|
- new InputStreamReader(process.getInputStream()));
|
|
|
- String htmlUrl;
|
|
|
- while ((htmlUrl = reader.readLine()) != null) {
|
|
|
- System.out.println(htmlUrl);
|
|
|
- if (htmlUrl.indexOf("html文件路径") >= 0 && htmlUrl.indexOf("_001.html") >= 0 && htmlUrl.indexOf("archiveSplit") >= 0) {
|
|
|
- lasHhtmlUrl = htmlUrl.replace("html文件路径", "");
|
|
|
- }
|
|
|
- }
|
|
|
- // 等待进程结束
|
|
|
- int exitCode = process.waitFor();
|
|
|
- if (exitCode == 0) {
|
|
|
- return lasHhtmlUrl;
|
|
|
- } else {
|
|
|
- return "1";
|
|
|
- }
|
|
|
- } catch (Exception e) {
|
|
|
- e.printStackTrace();
|
|
|
- return "1";
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- public static String OcrTitle(String fileUrl, String type) {
|
|
|
- String lasHhtmlUrl = "";
|
|
|
- try {
|
|
|
- // 定义Python解释器路径和脚本路径
|
|
|
- String pythonScript = "/Users/hongchuangyanfa/Desktop/PycharmProjects/splitPngByTitle.py";
|
|
|
- // 构建命令
|
|
|
- ProcessBuilder pb = new ProcessBuilder("python3", pythonScript, fileUrl, type);
|
|
|
- Process process = pb.start();
|
|
|
-
|
|
|
- // 读取Python脚本输出
|
|
|
- BufferedReader reader = new BufferedReader(
|
|
|
- new InputStreamReader(process.getInputStream()));
|
|
|
- String htmlUrl;
|
|
|
- while ((htmlUrl = reader.readLine()) != null) {
|
|
|
- System.out.println("222" + htmlUrl);
|
|
|
- if (htmlUrl.indexOf("图片中是否有卷内目录") >= 0 && htmlUrl.indexOf("True") >= 0) {
|
|
|
- return "1";
|
|
|
- }
|
|
|
- }
|
|
|
- // 等待进程结束
|
|
|
- int exitCode = process.waitFor();
|
|
|
- if (exitCode == 0) {
|
|
|
- return lasHhtmlUrl;
|
|
|
- } else {
|
|
|
- return "1";
|
|
|
- }
|
|
|
- } catch (Exception e) {
|
|
|
- e.printStackTrace();
|
|
|
- return "1";
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
-
|
|
|
- public static int getPdfByPage(int startPage, int endPage, String filePath, String savePath) {
|
|
|
- try {
|
|
|
- InputStream inputStreamByUrl = CommonUtil.getOSSInputStream3(filePath);
|
|
|
- // 加载PDF文件
|
|
|
- PDDocument document = PDDocument.load(inputStreamByUrl);
|
|
|
- // 创建新文档
|
|
|
- PDDocument newDocument = new PDDocument();
|
|
|
-
|
|
|
- // 注意:PDFBox中的页面索引从0开始
|
|
|
- int actualStart = Math.max(0, startPage - 1); // 将用户输入的1转换为0
|
|
|
- int actualEnd = Math.min(document.getNumberOfPages() - 1, endPage - 1); // 将用户输入的10转换为9
|
|
|
-
|
|
|
- // 添加指定范围的页面
|
|
|
- for (int i = actualStart; i <= actualEnd; i++) {
|
|
|
- PDPage page = document.getPage(i);
|
|
|
- newDocument.addPage(page);
|
|
|
- }
|
|
|
-
|
|
|
- // 保存为新文件
|
|
|
- newDocument.save(savePath);
|
|
|
- newDocument.close();
|
|
|
- document.close();
|
|
|
- return 0;
|
|
|
- } catch (Exception e) {
|
|
|
- return 1;
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- public static int savePdfAsImage(int pageNum, String filePath, String outputPath) {
|
|
|
- try (InputStream inputStream = FileUtils.getInputStreamByUrl(filePath);
|
|
|
- PDDocument document = PDDocument.load(inputStream)) {
|
|
|
-
|
|
|
- // 验证页码范围
|
|
|
- if (pageNum < 1 || pageNum > document.getNumberOfPages()) {
|
|
|
- return 1;
|
|
|
- }
|
|
|
-
|
|
|
- PDFRenderer renderer = new PDFRenderer(document);
|
|
|
-
|
|
|
- // 设置DPI为300
|
|
|
- final int DPI = 300;
|
|
|
-
|
|
|
- // 渲染指定页面(注意PDFBox使用0-based索引)
|
|
|
- //BufferedImage image = renderer.renderImage(pageNum - 1, DPI / 72f);
|
|
|
- BufferedImage image = renderer.renderImageWithDPI(0, DPI); // 0 表示第一页
|
|
|
- // 确保输出目录存在
|
|
|
- File outputFile = new File(outputPath);
|
|
|
- outputFile.getParentFile().mkdirs();
|
|
|
-
|
|
|
- // 保存为PNG格式(可改为JPG等)
|
|
|
- ImageIO.write(image, "PNG", outputFile);
|
|
|
-
|
|
|
- log.info("PDF页面已成功保存为图片: {}", outputPath);
|
|
|
- inputStream.close();
|
|
|
- document.close();
|
|
|
- return 0;
|
|
|
- } catch (Exception e) {
|
|
|
- log.error("PDF转图片失败", e);
|
|
|
- return 1;
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- public int saveDataToMysql(String upFileUrl, String fileName, String fileId, int filePage, int sort, String dutyUser, String fileTime) {
|
|
|
- // 获取封面信息
|
|
|
- long newPkId = SnowFlakeUtil.getId(); //主键Id
|
|
|
- File fmfile = new File(upFileUrl);
|
|
|
- if (fmfile.exists()) {
|
|
|
- BladeFile bladeFile = this.newIOSSClient.uploadFile(fileName + ".pdf", upFileUrl);
|
|
|
- if (bladeFile != null && Func.isNotEmpty(bladeFile.getLink())) {
|
|
|
- String FmPdfUrl = bladeFile.getLink();
|
|
|
- String sql = " insert into u_archive_file( " +
|
|
|
- " id,project_id,contract_id,node_id,file_number,file_name,file_time,file_url,pdf_file_url,file_page,is_approval,is_certification,is_need_certification,duty_user,create_user,create_dept,create_time,update_user,update_time,status,is_deleted,sheet_type,sheet_source, " +
|
|
|
- " drawing_no,cite_change_number,certification_time,e_visa_file,node_ext_id,file_type,archive_id,origin_id,filming_time,filmingor_time,tag_id,pic_code,refer_code,film_code,width,height,ftime,utime,del_time,sort,box_name,box_number,is_auto_file,is_archive,page_num, " +
|
|
|
- " file_size,source_type,is_element,pdf_page_url,fid,rectification,classify,m_wbs_tree_contract_p_key_id,u_image_classification_file_id,archive_file_storage_type,node_tree_structure,date_name,archive_file_stroage_type,out_id,sort_num " +
|
|
|
- " ) " +
|
|
|
- " SELECT " + newPkId + ",project_id,contract_id,node_id,file_number,'" + fileName + "','" + fileTime + "','" + FmPdfUrl + "','" + FmPdfUrl + "'," + filePage + ",is_approval,is_certification,is_need_certification,'" + dutyUser + "',create_user,create_dept,create_time,update_user,update_time,status,is_deleted,sheet_type,sheet_source, " +
|
|
|
- " drawing_no,cite_change_number,certification_time,e_visa_file,node_ext_id,file_type,archive_id,origin_id,filming_time,filmingor_time,tag_id,pic_code,refer_code,film_code,width,height,ftime,utime,del_time," + sort + ",box_name,box_number,is_auto_file,is_archive,page_num, " +
|
|
|
- " file_size,source_type,is_element,pdf_page_url,fid,rectification,classify,m_wbs_tree_contract_p_key_id,u_image_classification_file_id,archive_file_storage_type,node_tree_structure,date_name,archive_file_stroage_type,out_id,sort_num " +
|
|
|
- " from u_archive_file where id=" + fileId;
|
|
|
- System.out.println(fileName + "----" + sql);
|
|
|
- jdbcTemplate.execute(sql);
|
|
|
-
|
|
|
- } else {
|
|
|
- // 检查一下oss是否启动
|
|
|
- System.out.println("oss服务未启动,无法上传文件到oss");
|
|
|
- return 500;
|
|
|
- }
|
|
|
- } else {
|
|
|
- return 404;
|
|
|
- }
|
|
|
- fmfile.delete();
|
|
|
- return 200;
|
|
|
- }
|
|
|
-}
|