SpringBoot实现Excel、Word转换为PDF
1、配置license
在resources
目录下创建license.xml
文件,代码如下:
<License> <Data> <Products> <Product>Aspose.Total for Java</Product> <Product>Aspose.Words for Java</Product> </Products> <EditionType>Enterprise</EditionType> <SubscriptionExpiry>20991231</SubscriptionExpiry> <LicenseExpiry>20991231</LicenseExpiry> <SerialNumber>8bfe198c-7f0c-4ef8-8ff0-acc3237bf0d7</SerialNumber> </Data> <Signature>sNLLKGMUdF0r8O1kKilWAGdgfs2BvJb/2Xp8p5iuDVfZXmhppo+d0Ran1P9TKdjV4ABwAgKXxJ3jcQTqE/2IRfqwnPf8itN8aFZlV3TJPYeD3yWE7IT55Gz6EijUpC7aKeoohTb4w2fpox58wWoF3SNp6sK6jDfiAUGEHYJ9pjU=</Signature></License>
引入jar包:
aspose没有相应的maven地址,所有手动引入jar包;
aspose-cells-20.4 - c.jar
aspose-words-18.10-jdk16.jar
下载地址:https://download.csdn.net/download/zhuocailing3390/76147206
2、excel转pdf
封装工具类:
import com.aspose.cells.*;import com.aspose.cells.License;import com.aspose.cells.LoadOptions;import com.aspose.cells.PdfSaveOptions;import java.io.*;/** * @Author: LiHuaZhi * @Date: 2021/7/13 14:21 * @Description: **/public class Excel2PdfUtil { /** * 加载配置文件 * @return */ private static boolean getLicense() { boolean result = false; try (InputStream in = Doc2PdfUtil.class.getClassLoader().getResourceAsStream("license.xml")) { License license = new License(); license.setLicense(in); result = true; } catch (Exception e) { e.printStackTrace(); } return result; } /** * @param inputStream * @param docPath * @param pdfPath * @description 如果流不为空则使用转pdf,反之则用docPath进行转pdf */ public static File excel2Pdf(InputStream inputStream, String docPath, String pdfPath) { System.out.println("pdf转换中..."); long old = System.currentTimeMillis(); File pdfFile = new File(pdfPath); try (FileOutputStream fos = new FileOutputStream(pdfFile)) { // 验证 if (!getLicense()) { throw new RuntimeException("文件转换失败!"); } // 设置字体包位置 - 不同环境可用从配置文件中获取 IndividualFontConfigs configs = new IndividualFontConfigs(); // configs.setFontFolder("/usr/share/fonts/chinese", true); configs.setFontFolder("C:\\Windows\\Fonts", true); LoadOptions loadOptions = new LoadOptions(); loadOptions.setFontConfigs(configs); Workbook workbook; if (inputStream != null) { workbook = new Workbook(inputStream, loadOptions); } else { workbook = new Workbook(docPath, loadOptions); } PdfSaveOptions opts = new PdfSaveOptions(); // 设置excel不换行在pdf显示 // opts.setAllColumnsInOnePagePerSheet(true); // 设置一个sheet在一页pdf opts.setOnePagePerSheet(true); workbook.save(fos, opts); long now = System.currentTimeMillis(); System.out.println("pdf转换成功,共耗时:" + ((now - old) / 1000.0) + "秒"); return pdfFile; } catch (Exception e) { e.printStackTrace(); throw new RuntimeException("文件转换失败!"); } finally { try { if (inputStream != null) { inputStream.close(); } } catch (IOException e) { e.printStackTrace(); } } }}
Controller实现:
@RequestMapping("/excel") @ResponseBody public void uploadExcel(@RequestParam("file") MultipartFile file) throws Exception { String docPath = "C:\\Users\\LiGezZ\\Desktop\\ad.xlsx"; String pdfPath = "C:\\Users\\LiGezZ\\Desktop\\test.pdf"; File pdf = Excel2PdfUtil.excel2Pdf(file.getInputStream(), docPath, pdfPath); }
3、word转pdf
封装工具类:
import java.io.*;import com.aspose.words.*;/** * @Author: LiHuaZhi * @Date: 2021/7/13 14:21 * @Description: **/public class Doc2PdfUtil { /** * 加载配置文件 * * @return */ private static boolean getLicense() { boolean result = false; try (InputStream in = Doc2PdfUtil.class.getClassLoader().getResourceAsStream("license.xml")) { License license = new License(); license.setLicense(in); result = true; } catch (Exception e) { e.printStackTrace(); } return result; } /** * doc转pdf * * @param docPath doc文件路径,包含.doc * @param pdfPath pdf文件路径,包含.pdf * @description 如果流不为空则使用转pdf,反正则用docPath进行转pdf */ public static File doc2Pdf(InputStream inputStream, String docPath, String pdfPath) { System.out.println("pdf转换中..."); long old = System.currentTimeMillis(); File pdfFile = new File(pdfPath); try (FileOutputStream fos = new FileOutputStream(pdfFile)) { // 验证 if (!getLicense()) { throw new RuntimeException("文件转换失败!"); } // FontSettings.getDefaultInstance().setFontsFolder("/usr/share/fonts/chinese", true); FontSettings.getDefaultInstance().setFontsFolder("C:\\Windows\\Fonts", true); Document document; if (inputStream != null) { document = new Document(inputStream); } else { document = new Document(docPath); } // DocumentBuilder builder = new DocumentBuilder(document); // 设置纸张大小 // builder.getPageSetup().setPaperSize(PaperSize.A3); // 设置横向 // builder.getPageSetup().setOrientation(Orientation.LANDSCAPE); document.save(fos, SaveFormat.PDF); long now = System.currentTimeMillis(); System.out.println("pdf转换成功,共耗时:" + ((now - old) / 1000.0) + "秒"); return pdfFile; } catch (Exception e) { e.printStackTrace(); throw new RuntimeException("文件转换失败!"); } finally { try { if (inputStream != null) { inputStream.close(); } } catch (IOException e) { e.printStackTrace(); } } }}
Controller实现:
@RequestMapping("/word") @ResponseBody public void uploadWord(@RequestParam("file") MultipartFile file) throws Exception { String docPath = "C:\\Users\\LiGezZ\\Desktop\\test.docx"; String pdfPath = "C:\\Users\\LiGezZ\\Desktop\\test.pdf"; File pdf = Doc2PdfUtil.doc2Pdf(file.getInputStream(), docPath, pdfPath); }