springboot jodconverter openoffice achieve office online file preview

This has been a good few months ago do not remember a lot of details have been there clearly. Deploy today to the environment also encountered a lot of problems. Summarize.

1, the general idea of ​​office for online preview file is the file transfer pdf, in achieving online browsing through pdf.js. Among the various tools turn pdf conversion are some differences, there is aliasing problems.

2, another idea is to call pdf conversion function office through a script, this effect is best, high-fidelity. But that will be some trouble, rely office tools.

3, if you also want to deploy on Linux using which you can only use the open-source office software openoffice.

4, jodconverter can be a good tool to convert calls openoffice configuration is relatively simple.

Renderings:

 

 

 

springboot default integrated jodconverter all applications it is easy to use

yml add it follows

jodconverter: 
Enabled: to true # Note that this switch, you encounter this problem when deploying today, only to find investigation for a long time.
officeHome: C: \ Program Files (the x86) \ OpenOffice. 4
portNumbers: 8101,8102,8103,8104,8105
maxTasksPerProcess: . 5

 

Core code:

  public boolean office2pdf(FileEntity fileEntity, String suffix) {
    LOG.info("call office to pdf ,file info:" + fileEntity.toString());
    File file = new File(fileEntity.getAbsolute_path());
    if (file.exists() && file.isFile()) {
      LOG.info("call office to pdf ,find file info:" + fileEntity.toString());
      try {
        File pdfFile = new File(file.getParentFile(), fileEntity.getPrn() + suffix);
        if (!pdfFile.exists()) {
          DocumentConverter documentConverter = szAppConfig.getBeanByClass(DocumentConverter.class);
          documentConverter.convert(file).to(pdfFile).execute();
          if (".html".equalsIgnoreCase(suffix)) {
            try {
              String htmlFormant =
                  "<script src=\"" + sz_ng_root_path + "/commons/js/333614537928.js\"></script>\n" +
                      "    <STYLE>\n" +
                      "        \n" +
                      "        BODY, DIV, TABLE, THEAD, TBODY, TFOOT, TR, TH, TD, P {\n" +
                      "            font-family: \"微软雅黑\";\n" +
                      "            font-size: x-small\n" +
                      "        }\n" +
                      "\n" +
                      "        TABLE {\n" +
                      "            margin: 0 auto;\n" +
                      "            border-collapse: inherit;\n" +
                      "        }\n" +
                      "\n" +
                      "        .tabBox {\n" +
                      "            border-bottom: 1px solid #ccc;\n" +
                      "            padding-bottom: 1px;\n" +
                      "            height: 30px;\n" +
                      "            line-height: 30px;\n" +
                      "            color: #696969;\n" +
                      "        }\n" +
                      "\n" +
                      "        .tabBox a {\n" +
                      "            float: left;\n" +
                      "            font-family: \"微软雅黑\";\n" +
                      "            cursor: pointer;\n" +
                      "            padding: 0px 25px 0px;\n" +
                      "            font-size: 13px;\n" +
                      "            height: 30px;\n" +
                      "            line-height: 30px;\n" +
                      "            background: #F4F5F9;\n" +
                      "            border-top: 1px solid #C5D0DC;\n" +
                      "            border-left: 1px solid #C5D0DC;\n" +
                      "            border-bottom: 1px solid #C5D0DC;\n" +
                      "        }\n" +
                      "\n" +
                      "        .tabBox a.current {\n" +
                      "            border-bottom: 0px;\n" +
                      "            border-top: 2px solid #7599DE;\n" +
                      "            font-size: 13px;\n" +
                      "            color: #343434;\n" +
                      "            background: #FFFFFF;\n" +
                      "\n" +
                      "        }\n" +
                      "\n" +
                      "        .tabBox a:last-child {\n" +
                      "            border-right: 1px solid #C5D0DC;\n" +
                      "        }\n" +
                      "\n" +
                      "        .tabDetail {\n" +
                      "            display: none;\n" +
                      "        }\n" +
                      "    </STYLE>\n" +
                      "\n" +
                      "\n" +
                      "<script type=\"text/javascript\">\n" +
                      "    $(function () {\n" +
                      "        var tabs = $(\"[href^='#table']\");\n" +
                      "        $(\"H1:contains('摘要')\").remove();\n" +
                      "        tabs.wrapAll(\"<p class=\\\"tabBox\\\"></p>\");\n" +
                      "        tabs.removeAttr(\"href\");\n" +
                      "        var tabContents = $(\"a[NAME]\");\n" +
                      "        tabContents.each(function () {\n" +
                      "            var tcid = $(this).attr(\"name\");\n" +
                      "            var tc = $(this).next();\n" +
                      "            var tcl = $(this).prev();\n" +
                      "            $(this).wrap(\"<div class=\\\"tabDetail\\\"></div>\")\n" +
                      "            tc.appendTo($(this).parent());\n" +
                      "            tcl.appendTo($(this).parent());\n" +
                      "        });\n" +
                      "    });\n" +
                      "    $(function () {\n" +
                      "        $($('.tabBox > a')[0]).attr(\"class\",\"current\");\n" +
                      "        $($('.tabDetail')[0]).show();\n" +
                      "        $('.tabBox > a').on(\"click\",function(){\n" +
                      "            var num=$(this).index();\n" +
                      "            console.log(num)\n" +
                      "            $('.tabBox > a').each(function(){\n" +
                      "                if($(this).index()==num){\n" +
                      "                    $(this).attr(\"class\",\"current\");\n" +
                      "                    $('.tabDetail').hide();\n" +
                      "                    $($('.tabDetail')[num]).show();\n" +
                      "                }else{\n" +
                      "                    $(this).attr(\"class\",\"\");\n" +
                      "                }\n" +
                      "            });\n" +
                      "        });\n" +
                      "    });\n" +
                      "</script>\n" +
                      "</HTML>";
              RandomAccessFile raf = new RandomAccessFile(pdfFile.getAbsolutePath(), "rw");
              long lastPoint = 0; //记住上一次的偏移量
              String line = null;
              while ((line = raf.readLine()) != null) {
                final long ponit = raf.getFilePointer();
                if (line.toUpperCase().contains("</HTML>")) {
                  String str = line.toUpperCase().replace("</HTML>", htmlFormant);
                  raf.seek(lastPoint);
                  raf.write(str.getBytes("gb2312"));
                }
                lastPoint = ponit;
              }
              raf.close();
            } catch (IOException e) {
              e.printStackTrace();
            }
          }
          LOG.info("call office to pdf ,convert over. file info:" + fileEntity.toString());
        }
        return true;
      } catch (OfficeException e) {
        e.printStackTrace();
        LOG.info(
            "call office to pdf ,thrown error info:" + e.toString() + ", file info:" + fileEntity
                .toString());
        return false;
      }
    }
    return false;
  }

 

上面有一个任意读写文件,是为了加入自定已的js 来控制excle 的展示样式。反正当时纠结了好久。

pom:

<!--open office-->
<dependency>
<groupId>org.jodconverter</groupId>
<artifactId>jodconverter-core</artifactId>
<version>4.1.0</version>
</dependency>
<dependency>
<groupId>org.jodconverter</groupId>
<artifactId>jodconverter-local</artifactId>
<version>4.1.0</version>
</dependency>
<dependency>
<groupId>org.jodconverter</groupId>
<artifactId>jodconverter-spring-boot-starter</artifactId>
<version>4.1.0</version>
</dependency>
<dependency>
<groupId>org.libreoffice</groupId>
<artifactId>juh</artifactId>
<version>5.4.2</version>
</dependency>
<dependency>
<groupId>org.libreoffice</groupId>
<artifactId>jurt</artifactId>
<version>5.4.2</version>
</dependency>
<dependency>
<groupId>org.libreoffice</groupId>
<artifactId>ridl</artifactId>
<version>5.4.2</version>
</dependency>
<dependency>
<groupId>org.libreoffice</groupId>
<artifactId>unoil</artifactId>
<version>5.4.2</version>
</dependency>
<dependency>
<groupId>org.libreoffice</groupId>
<artifactId>unoloader</artifactId>
<version>5.4.2</version>
</dependency>

代码就这么些着实有些简洁,却实现了一个看着不那么容易的功能。

Guess you like

Origin www.cnblogs.com/kunsyliu/p/11246168.html