chenkailing
4 years ago
committed by
kl
24 changed files with 248 additions and 259 deletions
@ -1,50 +0,0 @@
|
||||
package cn.keking.utils; |
||||
|
||||
import com.aspose.cad.Color; |
||||
import com.aspose.cad.fileformats.cad.CadDrawTypeMode; |
||||
import com.aspose.cad.imageoptions.CadRasterizationOptions; |
||||
import com.aspose.cad.imageoptions.PdfOptions; |
||||
import org.slf4j.Logger; |
||||
import org.slf4j.LoggerFactory; |
||||
import org.springframework.stereotype.Component; |
||||
|
||||
import java.io.File; |
||||
import java.io.FileNotFoundException; |
||||
import java.io.FileOutputStream; |
||||
import java.io.OutputStream; |
||||
|
||||
/** |
||||
* @author chenjhc |
||||
* @since 2019/11/21 14:34 |
||||
*/ |
||||
@Component |
||||
public class CadUtils { |
||||
|
||||
private final Logger logger = LoggerFactory.getLogger(CadUtils.class); |
||||
|
||||
public boolean cadToPdf(String inputFilePath, String outputFilePath) { |
||||
com.aspose.cad.Image cadImage = com.aspose.cad.Image.load(inputFilePath); |
||||
CadRasterizationOptions cadRasterizationOptions = new CadRasterizationOptions(); |
||||
cadRasterizationOptions.setLayouts(new String[]{"Model"}); |
||||
cadRasterizationOptions.setNoScaling(true); |
||||
cadRasterizationOptions.setBackgroundColor(Color.getWhite()); |
||||
cadRasterizationOptions.setPageWidth(cadImage.getWidth()); |
||||
cadRasterizationOptions.setPageHeight(cadImage.getHeight()); |
||||
cadRasterizationOptions.setPdfProductLocation("center"); |
||||
cadRasterizationOptions.setAutomaticLayoutsScaling(true); |
||||
cadRasterizationOptions.setDrawType(CadDrawTypeMode.UseObjectColor); |
||||
PdfOptions pdfOptions = new PdfOptions(); |
||||
pdfOptions.setVectorRasterizationOptions(cadRasterizationOptions); |
||||
File outputFile = new File(outputFilePath); |
||||
OutputStream stream; |
||||
try { |
||||
stream = new FileOutputStream(outputFile); |
||||
cadImage.save(stream, pdfOptions); |
||||
cadImage.close(); |
||||
return true; |
||||
} catch (FileNotFoundException e) { |
||||
logger.error("PDFFileNotFoundException,inputFilePath:{}", inputFilePath, e); |
||||
return false; |
||||
} |
||||
} |
||||
} |
@ -1,79 +0,0 @@
|
||||
package cn.keking.utils; |
||||
|
||||
import cn.keking.service.FileHandlerService; |
||||
import org.apache.pdfbox.pdmodel.PDDocument; |
||||
import org.apache.pdfbox.rendering.ImageType; |
||||
import org.apache.pdfbox.rendering.PDFRenderer; |
||||
import org.apache.pdfbox.tools.imageio.ImageIOUtil; |
||||
import org.slf4j.Logger; |
||||
import org.slf4j.LoggerFactory; |
||||
import org.springframework.beans.factory.annotation.Value; |
||||
import org.springframework.stereotype.Component; |
||||
|
||||
import java.awt.image.BufferedImage; |
||||
import java.io.File; |
||||
import java.io.IOException; |
||||
import java.io.UnsupportedEncodingException; |
||||
import java.net.URLEncoder; |
||||
import java.util.ArrayList; |
||||
import java.util.List; |
||||
|
||||
@Component |
||||
public class PdfUtils { |
||||
|
||||
private final Logger logger = LoggerFactory.getLogger(PdfUtils.class); |
||||
|
||||
private final FileHandlerService fileHandlerService; |
||||
|
||||
@Value("${server.tomcat.uri-encoding:UTF-8}") |
||||
private String uriEncoding; |
||||
|
||||
public PdfUtils(FileHandlerService fileHandlerService) { |
||||
this.fileHandlerService = fileHandlerService; |
||||
} |
||||
|
||||
public List<String> pdf2jpg(String pdfFilePath, String pdfName, String baseUrl) { |
||||
List<String> imageUrls = new ArrayList<>(); |
||||
Integer imageCount = fileHandlerService.getConvertedPdfImage(pdfFilePath); |
||||
String imageFileSuffix = ".jpg"; |
||||
String pdfFolder = pdfName.substring(0, pdfName.length() - 4); |
||||
String urlPrefix = null; |
||||
try { |
||||
urlPrefix = baseUrl + URLEncoder.encode(URLEncoder.encode(pdfFolder, uriEncoding).replaceAll("\\+", "%20"), uriEncoding); |
||||
} catch (UnsupportedEncodingException e) { |
||||
logger.error("UnsupportedEncodingException", e); |
||||
urlPrefix = baseUrl + pdfFolder; |
||||
} |
||||
if (imageCount != null && imageCount > 0) { |
||||
for (int i = 0; i < imageCount ; i++) |
||||
imageUrls.add(urlPrefix + "/" + i + imageFileSuffix); |
||||
return imageUrls; |
||||
} |
||||
try { |
||||
File pdfFile = new File(pdfFilePath); |
||||
PDDocument doc = PDDocument.load(pdfFile); |
||||
int pageCount = doc.getNumberOfPages(); |
||||
PDFRenderer pdfRenderer = new PDFRenderer(doc); |
||||
|
||||
int index = pdfFilePath.lastIndexOf("."); |
||||
String folder = pdfFilePath.substring(0, index); |
||||
|
||||
File path = new File(folder); |
||||
if (!path.exists()) { |
||||
path.mkdirs(); |
||||
} |
||||
String imageFilePath; |
||||
for (int pageIndex = 0; pageIndex < pageCount; pageIndex++) { |
||||
imageFilePath = folder + File.separator + pageIndex + imageFileSuffix; |
||||
BufferedImage image = pdfRenderer.renderImageWithDPI(pageIndex, 105, ImageType.RGB); |
||||
ImageIOUtil.writeImage(image, imageFilePath, 105); |
||||
imageUrls.add(urlPrefix + "/" + pageIndex + imageFileSuffix); |
||||
} |
||||
doc.close(); |
||||
fileHandlerService.addConvertedPdfImage(pdfFilePath, pageCount); |
||||
} catch (IOException e) { |
||||
logger.error("Convert pdf to jpg exception, pdfFilePath:{}", pdfFilePath, e); |
||||
} |
||||
return imageUrls; |
||||
} |
||||
} |
Loading…
Reference in new issue