golang+libreffice6.2实现word,excel,pptx转pdf,html


本文摘自网络,作者,侵删。

<center><font size="5">golang + libreoffice6.2 实现 word,excel,pptx 转pdf/html</font></center>

  • 方法[^本地需安装libreoffice]

    /**
    *@tips libreoffice 转换指令:
    * libreoffice6.2 invisible --convert-to pdf csDoc.doc --outdir /home/[转出目录]
    *
    * @function 实现文档类型转换为pdf或html
    * @param command:libreofficed的命令(具体以版本为准);win:soffice; linux:libreoffice6.2
    *     fileSrcPath:转换文件的路径
    *         fileOutDir:转换后文件存储目录
    *       converterType:转换的类型pdf/html
    * @return fileOutPath 转换成功生成的文件的路径 error 转换错误
     */
    func FuncDocs2Pdf(command string, fileSrcPath string, fileOutDir string, converterType string) (fileOutPath string, error error) {
      //校验fileSrcPath
      srcFile, erByOpenSrcFile := os.Open(fileSrcPath)
      if erByOpenSrcFile != nil && os.IsNotExist(erByOpenSrcFile) {
          return "", erByOpenSrcFile
      }
      //如文件输出目录fileOutDir不存在则自动创建
      outFileDir, erByOpenFileOutDir := os.Open(fileOutDir)
      if erByOpenFileOutDir != nil && os.IsNotExist(erByOpenFileOutDir) {
          erByCreateFileOutDir := os.MkdirAll(fileOutDir, os.ModePerm)
          if erByCreateFileOutDir != nil {
              logs.Error("File ouput dir create error.....", erByCreateFileOutDir.Error())
              return "", erByCreateFileOutDir
          }
      }
      //关闭流
      defer func() {
          _ = srcFile.Close()
          _ = outFileDir.Close()
      }()
      //convert
      cmd := exec.Command(command, "--invisible", "--convert-to", converterType,
          fileSrcPath, "--outdir", fileOutDir)
      byteByStat, errByCmdStart := cmd.Output()
      //命令调用转换失败
      if errByCmdStart != nil {
          return "", errByCmdStart
      }
      //success
      fileOutPath = fileOutDir + "/" + strings.Split(path.Base(fileSrcPath), ".")[0]
      if converterType == "html" {
          fileOutPath += ".html"
      } else {
          fileOutPath += ".pdf"
      }
      logs.Info("文件转换成功...", string(byteByStat))
      return fileOutPath, nil
    }
    
    

本文来自:简书

感谢作者:我是不会赢的

查看原文:golang+libreffice6.2实现word,excel,pptx转pdf,html

相关阅读 >>

Go-zero 是如何追踪你的请求链路?

详解 Go 语言中的方法

Golang如何删除数组中的元素

手撸Golang spring ioc/aop 之1

Golang浮点数精度丢失问题扩展包怎么解决

手撸Golang 架构设计原则 依赖倒置原则

Golang和Go是什么关系

聊聊dubbo-Go-proxy的client

Golang 怎么调用c代码

Golang中协程图文详解

更多相关阅读请进入《Go》频道 >>




打赏

取消

感谢您的支持,我会继续努力的!

扫码支持
扫码打赏,您说多少就多少

打开支付宝扫一扫,即可进行扫码打赏哦

分享从这里开始,精彩与您同在

评论

管理员已关闭评论功能...