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

相关阅读 >>

rust 内存管理

Go2 到底长啥样?

手撸Golang etcd raft协议之9

关于Golang中无法获取地址问题的解析

Go-grpc-rest环境搭建

详解Go 中方法与函数的区别

Golang 协程(Goroutine) 运行过程 与 并发

Go:测量函数执行时间的方法

Golang语言社区投稿】Golang高并发基于协程,通道的任务池

vim--Golang开发配置

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




打赏

取消

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

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

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

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

评论

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