aspose jar下载不下来,需要修改下载jar包的来源
<repositories>
<repository>
<id>aspose-maven-repository</id>
<url>https://releases.aspose.com/java/repo</url>
<snapshots>
<enabled>false</enabled>
</snapshots>
</repository>
</repositories>
1.word(doc和docx)转pdf
<dependency>
<groupId>com.aspose</groupId>
<artifactId>aspose-words</artifactId>
<version>20.12</version>
<type>pom</type>
</dependency>
package com.ruoyi.web.controller.common; import com.aspose.words.Document; import com.aspose.words.License; import com.aspose.words.SaveFormat; import com.ruoyi.common.exception.ServiceException; import org.apache.xmlbeans.ResourceLoader; import org.springframework.web.multipart.MultipartFile; import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.io.InputStream; public class WordToPdf { public static InputStream wordToPdf(MultipartFile file) {try(ByteArrayOutputStream baos = new ByteArrayOutputStream()) { Document doc = new Document(file.getInputStream()); //sourceFile是将要被转化的word文档 doc.save(baos, SaveFormat.PDF);//全面支持DOC, DOCX, OOXML, RTF HTML, OpenDocument, PDF, EPUB, XPS, SWF 相互转换 byte[] pdfBytes = baos.toByteArray(); InputStream pdfInputStream = new ByteArrayInputStream(pdfBytes); return pdfInputStream; } catch (Exception e) { System.out.println("convert failed"); e.printStackTrace(); throw new ServiceException("convert word to pdf failed"); } } }
2.Excel(xls和xlsx)转pdf
<dependency>
<groupId>com.aspose</groupId>
<artifactId>aspose-cells</artifactId>
<version>20.12</version>
</dependency>
package com.ruoyi.web.controller.common; import com.aspose.cells.PdfSaveOptions; import com.aspose.cells.Workbook; import com.aspose.words.License; import com.ruoyi.common.exception.ServiceException; import org.apache.xmlbeans.ResourceLoader; import org.springframework.web.multipart.MultipartFile; import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.io.InputStream; public class ExcelToPdf { public static InputStream excelToPdf(MultipartFile file) {try(ByteArrayOutputStream baos = new ByteArrayOutputStream()) { Workbook wb = new Workbook(file.getInputStream()); PdfSaveOptions pdfSaveOptions = new PdfSaveOptions(); pdfSaveOptions.setOnePagePerSheet(true); wb.save(baos, pdfSaveOptions); baos.flush(); baos.close(); byte[] pdfBytes = baos.toByteArray(); InputStream pdfInputStream = new ByteArrayInputStream(pdfBytes); return pdfInputStream; } catch (Exception e) { e.printStackTrace(); throw new ServiceException("convert excel to pdf failed"); } } }
3.ppt(ppt和pptx)转pdf
<dependency>
<groupId>com.aspose</groupId>
<artifactId>aspose-slides</artifactId>
<version>20.12</version>
<classifier>jdk16</classifier>
</dependency>
package com.ruoyi.web.controller.common; import com.aspose.slides.Presentation; import com.aspose.slides.SaveFormat; import com.ruoyi.common.exception.ServiceException; import org.springframework.web.multipart.MultipartFile; import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.io.InputStream; public class PptToPdf { public static InputStream pptToPdf(MultipartFile file) { try(ByteArrayOutputStream baos = new ByteArrayOutputStream()) { Presentation pres = new Presentation(file.getInputStream()); pres.save(baos, SaveFormat.Pdf); byte[] pdfBytes = baos.toByteArray(); InputStream pdfInputStream = new ByteArrayInputStream(pdfBytes); return pdfInputStream; }catch (Exception e) { e.printStackTrace(); throw new ServiceException("convert ppt/pptx to pdf failed"); } } }
注:免费版生成出来是有水印的,如果购买正版,会有license,把文件放到resource目录下,读取并验签
private static boolean getLicense() { boolean result = false; try { ClassLoader classLoader = ResourceLoader.class.getClassLoader(); InputStream is = classLoader.getResourceAsStream("license.xml"); // license.xml应放在resource路径下 License aposeLic = new License(); aposeLic.setLicense(is); result = true; } catch (Exception e) { e.printStackTrace(); } return result; }