|
|
@@ -0,0 +1,62 @@
|
|
|
+package com.common.workflow.service.util;
|
|
|
+
|
|
|
+import com.aspose.words.Document;
|
|
|
+import com.aspose.words.License;
|
|
|
+import com.aspose.words.SaveFormat;
|
|
|
+
|
|
|
+
|
|
|
+import javax.servlet.http.HttpServletResponse;
|
|
|
+import java.io.ByteArrayInputStream;
|
|
|
+import java.io.InputStream;
|
|
|
+import java.io.OutputStream;
|
|
|
+import java.net.HttpURLConnection;
|
|
|
+import java.net.URL;
|
|
|
+
|
|
|
+/**
|
|
|
+ * Created by gyue on 2019-03-25.
|
|
|
+ */
|
|
|
+public class Word2Pdf {
|
|
|
+ public static boolean getLicense() {
|
|
|
+ boolean result = false;
|
|
|
+ try {
|
|
|
+ InputStream is = Word2Pdf.class.getClassLoader().getResourceAsStream("license-apose-word.xml"); // license.xml应放在..\WebRoot\WEB-INF\classes路径下
|
|
|
+ License aposeLic = new License();
|
|
|
+ aposeLic.setLicense(is);
|
|
|
+ result = true;
|
|
|
+ }catch (Exception e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+ return result;
|
|
|
+ }
|
|
|
+
|
|
|
+ public static void word2Pdf(String Address, HttpServletResponse response) {
|
|
|
+ if (!getLicense()) { // 验证License 若不验证则转化出的pdf文档会有水印产生
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ try {
|
|
|
+ URL url = new URL(Address);
|
|
|
+ HttpURLConnection conn = (HttpURLConnection)url.openConnection();
|
|
|
+ //得到输入流
|
|
|
+ InputStream inputStream = conn.getInputStream();
|
|
|
+
|
|
|
+ //TxtLoadOptions lo = new TxtLoadOptions();
|
|
|
+ //lo.setEncoding(Encoding.getUTF8());
|
|
|
+ Document doc = new Document(inputStream);// 原始excel路径
|
|
|
+
|
|
|
+ /*PdfSaveOptions pdfSaveOptions = new PdfSaveOptions(SaveFormat.PDF);
|
|
|
+ pdfSaveOptions.setOnePagePerSheet(true);
|
|
|
+ pdfSaveOptions.setAllColumnsInOnePagePerSheet(true);
|
|
|
+ pdfSaveOptions.setDefaultFont("SimSun");
|
|
|
+ pdfSaveOptions.setEmbedStandardWindowsFonts(true);
|
|
|
+ response.setContentType("application/pdf");
|
|
|
+ response.setCharacterEncoding("UTF-8");*/
|
|
|
+ OutputStream output = response.getOutputStream();
|
|
|
+ doc.save(output, SaveFormat.PDF);
|
|
|
+ output.flush();
|
|
|
+ inputStream.close();
|
|
|
+ output.close();
|
|
|
+ }catch (Exception e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+ }
|
|
|
+}
|