package com.xhsoft.thread;
import com.xhsoft.common.props.TeOssProperties;
import com.xhsoft.common.utils.MergeImageUtil;
import com.xhsoft.flange.entity.DrawingInfo;
import com.xhsoft.flange.service.IDrawingInfoService;
import com.xhsoft.modules.resource.builder.oss.OssBuilder;
import lombok.extern.slf4j.Slf4j;
import org.apache.pdfbox.Loader;
import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.rendering.PDFRenderer;
import org.springblade.core.oss.model.BladeFile;
import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
@Slf4j
public class ConvertDrawingToPicture implements Runnable {
private DrawingInfo drawing;
private IDrawingInfoService drawingInfoService;
private File file;
private OssBuilder ossBuilder;
private TeOssProperties teOssProperties;
public ConvertDrawingToPicture(DrawingInfo drawing, File file, IDrawingInfoService drawingInfoService, OssBuilder ossBuilder, TeOssProperties teOssProperties){
this.drawing = drawing;
this.drawingInfoService = drawingInfoService;
this.file = file;
this.ossBuilder = ossBuilder;
this.teOssProperties = teOssProperties;
}
@Override
public void run() {
String fileName = file.getName();
String name = fileName.substring(0, fileName.lastIndexOf("."));
log.info("{}-正在生成图片", fileName);
FileInputStream fis = null;
try {
PDDocument doc = Loader.loadPDF(file);
PDFRenderer renderer = new PDFRenderer(doc);
int pageCount = doc.getNumberOfPages();
List<BufferedImage> images = new ArrayList<>();
for(int i=0;i<pageCount;i++){
BufferedImage image = renderer.renderImageWithDPI(i, 296);
// BufferedImage image = renderer.renderImage(i, 2.5f);
// ImageIO.write(image, "PNG", new File(name+"-"+i+".png"));
images.add(image);
}
log.info("{}-正在整合图片", fileName);
File dir = file.getParentFile();
File pic = new File(dir, name+".png");
MergeImageUtil.mergeImage(images, pic);
log.info("{}-正在上传图片", fileName);
fis = new FileInputStream(pic);
BladeFile bladeFile = ossBuilder.template(teOssProperties.getPic()).putFile(pic.getName(), fis);
log.info("{}-正在保存图片地址", fileName);
String picUrl = bladeFile.getLink();
drawing.setPic(picUrl);
drawingInfoService.updateById(drawing);
} catch (IOException e) {
e.printStackTrace();
}finally{
if(fis != null) try{ fis.close(); }catch(Exception e){};
}
}
public static void main(String[] args){
// try {
// PDDocument doc = Loader.loadPDF(new File("files/drawing/HTP-ER-46-01-02-LO-01_ISO DRGS.pdf"));
// PDFRenderer renderer = new PDFRenderer(doc);
// int pageCount = doc.getNumberOfPages();
// for(int i=0;i<pageCount;i++){
// BufferedImage image = renderer.renderImageWithDPI(i, 296);
BufferedImage image = renderer.renderImage(i, 2.5f);
// ImageIO.write(image, "PNG", new File("files/pricture/"+i+".png"));
// }
// } catch (IOException e) {
// e.printStackTrace();
// }
}
}
然后用的时候:文章来源地址https://www.toymoban.com/news/detail-511605.html
/**
* 替换图纸文件
*/
@PostMapping("/replace")
@ApiOperationSupport(order = 13)
@ApiOperation(value = "替换图纸", notes = "图纸文件")
public R replaceFile(MultipartFile file, Long id) {
String fileName = file.getOriginalFilename();
DrawingInfo drawing = drawingInfoService.getById(id);
if(drawing == null){
log.error("图纸数据不存在,id={}", id);
return R.fail(StatusEnum.DRAWING_FILE_NOT_EXIST.getCode());
}
Long projectId = drawing.getProjectId();
//验证是否已存在该图纸
DrawingInfoTO condition = new DrawingInfoTO();
condition.setProjectId(projectId);
condition.setName(fileName);
condition.setIsDeleted(0);
List<DrawingInfo> drawings = drawingInfoService.list(Condition.getQueryWrapper(condition));
if (drawings.size() > 0) {
log.error(fileName + "图纸已存在");
return R.fail(fileName + StatusEnum.DRAWING_EXIST.getCode());
}
String fileUrl = null;
String date = CommonUtil.SDF_DATE.format(new Date());
Path dest = Paths.get(pathProperties.getDrawing(), date, fileName);
//保存文件在本地
try {
if(!dest.toFile().exists()){
Files.createDirectories(dest);
}
Files.copy(file.getInputStream(), dest, StandardCopyOption.REPLACE_EXISTING);
} catch (IOException e) {
log.error("上传文件异常,"+e.getMessage());
return R.fail(fileName + ":"+StatusEnum.SAVE_FILE_EXCEPTION.getCode());
}
File pdfFile = dest.toFile();
FileInputStream fis = null;
try{
fis = new FileInputStream(pdfFile);
}catch(Exception e){
e.printStackTrace();
}
try{
BladeFile bladeFile = ossBuilder.template(teOssProperties.getPdf()).putFile(fileName, fis);
fileUrl = bladeFile.getLink();
}catch(Exception e){
log.error("上传图纸文件异常,异常信息:{}", e.getMessage());
e.printStackTrace();
return R.fail(StatusEnum.UPLOAD_DRAWING_FILE_EXCEPTION.getCode());
}
DrawingInfoTO di = new DrawingInfoTO();
di.setId(drawing.getId());
// di.setCode(fileName);
// di.setName(fileName);
di.setUrl(fileUrl);
di.setPdfFile(pdfFile);
drawingInfoService.updateById(di);
String url = drawing.getUrl();
if(StringUtil.isNotBlank(url)){
String pdfName = url.substring(url.lastIndexOf('/')+1);
ossBuilder.template(teOssProperties.getPdf()).removeFile(pdfName);
}
String pic = drawing.getPic();
if(StringUtil.isNotBlank(pic)){
String picNme = pic.substring(pic.lastIndexOf('/')+1);
ossBuilder.template(teOssProperties.getPic()).removeFile(picNme);
}
ExecutorService fixedPool = Executors.newFixedThreadPool(1);
ConvertDrawingToPicture convert = new ConvertDrawingToPicture(
di,
di.getPdfFile(),
drawingInfoService,
ossBuilder,
teOssProperties
);
fixedPool.execute(convert);
fixedPool.shutdown();
logTool.insert(drawing.getProjectId(), SysEnum.FLANGE, ModuleEnum.DRAWING,
"替换: 图纸id="+id+",新图纸文件="+fileName );
return R.success(StatusEnum.REPLACE_DRAWING_FILE_SUCCESS.getCode());
}
}
文章来源:https://www.toymoban.com/news/detail-511605.html
到了这里,关于PDF转图片pdf转换图片,线程池的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!