|
@@ -1,8 +1,5 @@
|
|
|
package com.steerinfo.ftp.uploadfile.utils;
|
|
|
|
|
|
-import com.itextpdf.text.*;
|
|
|
-import com.itextpdf.text.pdf.BaseFont;
|
|
|
-import com.itextpdf.text.pdf.PdfWriter;
|
|
|
import fr.opensagres.poi.xwpf.converter.xhtml.Base64EmbedImgManager;
|
|
|
import fr.opensagres.poi.xwpf.converter.xhtml.XHTMLConverter;
|
|
|
import fr.opensagres.poi.xwpf.converter.xhtml.XHTMLOptions;
|
|
@@ -322,131 +319,6 @@ public class POIWordToHtml {
|
|
|
return gridSpanNum;
|
|
|
}
|
|
|
|
|
|
- public static byte[] docxToPdf(InputStream src) {
|
|
|
- ByteArrayOutputStream baos = new ByteArrayOutputStream();
|
|
|
- byte[] resBytes = null;
|
|
|
- String result;
|
|
|
- try {
|
|
|
- // pdf文件的尺寸
|
|
|
- com.itextpdf.text.Document pdfDocument = new com.itextpdf.text.Document(PageSize.A3, 72, 72, 72, 72);
|
|
|
- PdfWriter pdfWriter = PdfWriter.getInstance(pdfDocument, baos);
|
|
|
- XWPFDocument doc = new XWPFDocument(src);
|
|
|
- pdfWriter.setInitialLeading(20);
|
|
|
- java.util.List<XWPFParagraph> plist = doc.getParagraphs();
|
|
|
- pdfWriter.open();
|
|
|
- pdfDocument.open();
|
|
|
- for (int i = 0; i < plist.size(); i++) {
|
|
|
- XWPFParagraph pa = plist.get(i);
|
|
|
- java.util.List<XWPFRun> runs = pa.getRuns();
|
|
|
- for (int j = 0; j < runs.size(); j++) {
|
|
|
- XWPFRun run = runs.get(j);
|
|
|
- java.util.List<XWPFPicture> piclist = run.getEmbeddedPictures();
|
|
|
- Iterator<XWPFPicture> iterator = piclist.iterator();
|
|
|
- while (iterator.hasNext()) {
|
|
|
- XWPFPicture pic = iterator.next();
|
|
|
- XWPFPictureData picdata = pic.getPictureData();
|
|
|
- byte[] bytepic = picdata.getData();
|
|
|
- Image imag = Image.getInstance(bytepic);
|
|
|
- pdfDocument.add(imag);
|
|
|
- }
|
|
|
- // 中文字体的解决
|
|
|
- BaseFont bf = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);
|
|
|
- Font font = new Font(bf, 11.0f, Font.NORMAL, BaseColor.BLACK);
|
|
|
- String text = run.getText(-1);
|
|
|
- byte[] bs;
|
|
|
- if (text != null) {
|
|
|
- bs = text.getBytes();
|
|
|
- String str = new String(bs);
|
|
|
- Chunk chObj1 = new Chunk(str, font);
|
|
|
- pdfDocument.add(chObj1);
|
|
|
- }
|
|
|
- }
|
|
|
- pdfDocument.add(new Chunk(Chunk.NEWLINE));
|
|
|
- }
|
|
|
- //需要关闭,不然无法获取到输出流
|
|
|
- pdfDocument.close();
|
|
|
- pdfWriter.close();
|
|
|
- resBytes = baos.toByteArray();
|
|
|
- } catch (Exception e) {
|
|
|
- }finally {
|
|
|
- try{
|
|
|
- if(baos != null){
|
|
|
- baos.close();
|
|
|
- }
|
|
|
- }catch (IOException e){
|
|
|
- }
|
|
|
- }
|
|
|
- return resBytes;
|
|
|
- }
|
|
|
|
|
|
|
|
|
- /**
|
|
|
- * PDF 转 HTML
|
|
|
- */
|
|
|
-
|
|
|
- /**
|
|
|
- * 把输入流里面的内容以UTF-8编码当文本取出。
|
|
|
- * 不考虑异常,直接抛出
|
|
|
- * @param ises
|
|
|
- * @return
|
|
|
- * @throws IOException
|
|
|
- */
|
|
|
- /**
|
|
|
- * 调用pdf2htmlEX将pdf文件转换为html文件
|
|
|
- *
|
|
|
- * @param exeFilePath
|
|
|
- * pdf2htmlEX.exe文件路径
|
|
|
- * @param pdfFile
|
|
|
- * pdf文件绝对路径
|
|
|
- * @param [destDir] 生成的html文件存放路径
|
|
|
- * @param htmlName
|
|
|
- * 生成的html文件名称
|
|
|
- * @return
|
|
|
- */
|
|
|
- public static boolean pdf2html(String exeFilePath, String pdfFile,
|
|
|
- String destDir, String htmlFileName) {
|
|
|
- if (!(exeFilePath != null && !"".equals(exeFilePath) && pdfFile != null
|
|
|
- && !"".equals(pdfFile) && htmlFileName != null && !""
|
|
|
- .equals(htmlFileName))) {
|
|
|
- System.out.println("传递的参数有误!");
|
|
|
- return false;
|
|
|
- }
|
|
|
- Runtime rt = Runtime.getRuntime();
|
|
|
- StringBuilder command = new StringBuilder();
|
|
|
- command.append(exeFilePath).append(" ");
|
|
|
- if (destDir != null && !"".equals(destDir.trim()))// 生成文件存放位置,需要替换文件路径中的空格
|
|
|
- command.append("--dest-dir ").append(destDir.replace(" ", "\" \""))
|
|
|
- .append(" ");
|
|
|
- command.append("--optimize-text 1 ");// 尽量减少用于文本的HTML元素的数目 (default: 0)
|
|
|
- command.append("--zoom 1.4 ");
|
|
|
- command.append("--process-outline 0 ");// html中显示链接:0——false,1——true
|
|
|
- command.append("--font-format woff ");// 嵌入html中的字体后缀(default ttf)
|
|
|
- // ttf,otf,woff,svg
|
|
|
- command.append(pdfFile.replace(" ", "\" \"")).append(" ");// 需要替换文件路径中的空格
|
|
|
- if (htmlFileName != null && !"".equals(htmlFileName.trim())) {
|
|
|
- command.append(htmlFileName);
|
|
|
- if (htmlFileName.indexOf(".html") == -1)
|
|
|
- command.append(".html");
|
|
|
- }
|
|
|
- try {
|
|
|
- Process p = rt.exec(command.toString());
|
|
|
- StreamGobbler errorGobbler = new StreamGobbler(p.getErrorStream(),
|
|
|
- "ERROR");
|
|
|
- // 开启屏幕标准错误流
|
|
|
- errorGobbler.start();
|
|
|
- StreamGobbler outGobbler = new StreamGobbler(p.getInputStream(),
|
|
|
- "STDOUT");
|
|
|
- // 开启屏幕标准输出流
|
|
|
- outGobbler.start();
|
|
|
- int w = p.waitFor();
|
|
|
- int v = p.exitValue();
|
|
|
- if (w == 0 && v == 0) {
|
|
|
- return true;
|
|
|
- }
|
|
|
- } catch (Exception e) {
|
|
|
- e.printStackTrace();
|
|
|
- }
|
|
|
- return false;
|
|
|
- }
|
|
|
-
|
|
|
}
|