kotlin 基于libreoffice的docx文件预览

本文主要记录了使用kotlin docx转化pdf的实现方式以及主流的方案的对比分析html

首先要实现docx文件在浏览器的预览,能够想到的有几种思路前端

1.转化docx文件到htmljava

2.转化docx到pdf而后前端用pdf.js之类的打开预览python

3.直接使用微软体系的东西 office web apps server (这个不作研究,由于没有windows server 全部的服务器都用linux)linux

而后经过百度和google找了不少方式,搜寻到如下几种可尝试的方案web

1.使用Apache POI 导出成html或者pdfbootstrap

2.使用python脚本模拟浏览器调用别的网站的pdf转化功能segmentfault

3.使用docx4jwindows

4.使用libreoffice 导出成pdf浏览器

最后本身上阵把全部的都试了一遍

1.POI简单易用简单的docx能够完美的转化出来,可是稍微复杂的格式直接文字丢失,图片丢失,最终没法使用

2.脚本调用,调用了一会,服务器就直接反馈错误多是对IP作了次数限制,这个也没法使用(若是能找到可靠的网站也能够一试,这个效果通常比较好转化出来的pdf质量高)

3. docx4j 和poi同样

4.libreoffice对于简单的docx能够完美转化,复杂格式稍微有变形,可是总体内容没有丢失,转化出来的东西能够接受,做为内部系统彻底够用

 

具体代码以下

gradle

compile 'org.libreoffice:unoil:5.4.2'
compile 'org.libreoffice:ridl:5.4.2'
compile 'org.libreoffice:jurt:5.4.2'
compile 'org.libreoffice:juh:5.4.2'
compile group: 'org.openoffice', name: 'bootstrap-connector', version: '0.1.1'

kotlin

package com.test.test01

import com.sun.star.beans.PropertyValue
import com.sun.star.frame.XDesktop
import com.sun.star.uno.UnoRuntime
import com.sun.star.frame.XComponentLoader
import java.io.File
import com.sun.star.frame.XStorable
import ooo.connector.BootstrapSocketConnector


fun main(args: Array<String>) {
    try {

        val xContext = BootstrapSocketConnector.bootstrap("D:\\Program Files\\LibreOffice 5\\program")

        val xMCF = xContext.getServiceManager()

        val oDesktop = xMCF.createInstanceWithContext(
                "com.sun.star.frame.Desktop", xContext)

        val xDesktop = UnoRuntime.queryInterface(
                XDesktop::class.java, oDesktop) as XDesktop

        val workingDir = "d:/upload/"
        val myTemplate = "aaaaaa.docx"

        if (!File(workingDir + myTemplate).canRead()) {
            throw RuntimeException("Cannot load template:" + File(workingDir + myTemplate))
        }

        val xCompLoader = UnoRuntime
                .queryInterface(com.sun.star.frame.XComponentLoader::class.java, xDesktop) as XComponentLoader

        val sUrl = "file:///" + workingDir + myTemplate

        var propertyValues = arrayOfNulls<PropertyValue>(0)

        propertyValues = arrayOfNulls<PropertyValue>(1)
        propertyValues[0] = PropertyValue()
        propertyValues[0]!!.Name = "Hidden"
        propertyValues[0]!!.Value = true

        val xComp = xCompLoader.loadComponentFromURL(
                sUrl, "_blank", 0, propertyValues)

        val xStorable = UnoRuntime
                .queryInterface(XStorable::class.java, xComp) as XStorable

        propertyValues = arrayOfNulls(2)
        propertyValues[0] = PropertyValue()
        propertyValues[0]!!.Name = "Overwrite"
        propertyValues[0]!!.Value = true
        propertyValues[1] = PropertyValue()
        propertyValues[1]!!.Name = "FilterName"
        propertyValues[1]!!.Value = "writer_pdf_Export"

// Appending the favoured extension to the origin document name
        val myResult = workingDir + "letterOutput.pdf"
        xStorable.storeToURL("file:///" + myResult, propertyValues)

        println("Saved " + myResult)

    } catch (e: Exception) {
        e.printStackTrace()
    }

}

最终代码来源于 https://segmentfault.com/a/1190000006789644 这里有java版本的代码

字体文件缺失形成的乱码问题能够参考 http://os.chinaunix.net/a2006/0831/1003/000001003262.shtml安装字体之后乱码问题就可修复

相关文章
相关标签/搜索