因工作需要实现word转pdf

实现原理:利用go命令行调用libreffice命令实现文件转化

**使用 libreffice 文件转pdf 并发数不能超2,只能一个文件转化成功后,在进行下一个

注意事项使用前本机或者服务器先安装开源软件 libreffice !!!

注意事项使用前本机或者服务器先安装开源软件 libreffice !!!

传送门:libreoffice

代码如下(粘贴后直接运行):

package Testimport ("fmt""os""os/exec""path""runtime""strings""testing"
)//TestToPdf golang+libreffice 实现word,excel,pptx转pdf,html
func TestToPdf(t *testing.T) {fileSrcPath := "/Users/xing/Desktop/123/test2.docx" //自己机器上的文件地址outPath := "/Users/xing/Desktop/123/pdf"            //转出文件的路径fileType := "pdf"osName := runtime.GOOS //获取系统类型switch osName {case "darwin": //mac系统command := "/Applications/LibreOffice.app/Contents/MacOS/soffice"pdfFile, err := FuncDocs2Pdf(command, fileSrcPath, outPath, fileType)if err != nil {println("转化异常:", err.Error())}fmt.Println("转化后的文件:", pdfFile)case "linux":command := "libreoffice7.3"pdfFile, err := FuncDocs2Pdf(command, fileSrcPath, outPath, fileType)if err != nil {println("转化异常:", err.Error())}fmt.Println("转化后的文件:", pdfFile)case "windows":command := "soffice libreoffice" // 因为没有windows机器需要自己测试下这个命令行pdfFile, err := FuncDocs2Pdf(command, fileSrcPath, outPath, fileType)if err != nil {println("转化异常:", err.Error())}fmt.Println("转化后的文件:", pdfFile)default:fmt.Println("暂时不支持的系统转化:" + runtime.GOOS)}
}/**
*@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) {//校验fileSrcPathsrcFile, 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 {fmt.Println("File ouput dir create error.....", erByCreateFileOutDir.Error())return "", erByCreateFileOutDir}}//关闭流defer func() {_ = srcFile.Close()_ = outFileDir.Close()}()//convertcmd := exec.Command(command, "--invisible", "--language=zh-CN", "--convert-to", converterType,fileSrcPath, "--outdir", fileOutDir)byteByStat, errByCmdStart := cmd.Output()//命令调用转换失败if errByCmdStart != nil {return "", errByCmdStart}//successfileOutPath = fileOutDir + "/" + strings.Split(path.Base(fileSrcPath), ".")[0]if converterType == "html" {fileOutPath += ".html"} else {fileOutPath += ".pdf"}fmt.Println("文件转换成功...", string(byteByStat))return fileOutPath, nil
}

参考文章:

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

go实现word,excel,pptx转pdf相关推荐

  1. php word excel转pdf文件怎么打开,php office文件(word/excel/ppt)转pdf文件,pptpdf

    php office文件(word/excel/ppt)转pdf文件,pptpdf 把代码放到了github上,点击进入 前阶段有个项目用到了线上预览功能, 关于预览office文件实现核心就是,把o ...

  2. vue预览word,excel,pptx,pdf文件

    vue预览word,excel,pptx,pdf文件 1.做word,excel,pptx的预览,要先确定文件路径访问是通过域名的url来预览,不可以通过IP的url来访问 先把文件路径的url进行u ...

  3. php word/excel/ppt 转pdf

    转载至:https://blog.csdn.net/sangjinchao/article/details/78053545 把代码放到了github上,点击进入 前阶段有个项目用到了线上预览功能, ...

  4. springBootopenoffice将word/excel转换成pdf或者html用于预览

    openoffice安装参见 https://blog.csdn.net/u010692825/article/details/106159178 此外网上也有很多类似的文章,都介绍得比较详细 现在简 ...

  5. ASP.net中将Word/Excel转换成PDF

    关键字 : Asp.net, word, Excel, PDF 参照网友的一篇文章( http://www.vuu.cn/vuu/vuu-a/4302.html) --- ASP.Net实现将Word ...

  6. java word,excel,ppt转pdf

    准备工作 1.下载 jacob.jar  链接:https://pan.baidu.com/s/1TWIGyX9A3xQ6AG9Y3mVlVg  提取码:abcd 2.下载安装wps WPS Offi ...

  7. jacob电脑重启后word/excel转pdf是0KB问题解决方法

    问题现象:使用jacob将word/excel转换为pdf时,服务器重启后,必须重新登录下桌面,才可转换pdf 成功,否则转换的pdf文件直接是0KB. 1.确保所用电脑的office本身可以将wor ...

  8. Java实现 word.excel等文档在线预览

    实现的原理: 通过第三方工具openoffice,将word,excel,pppt,txt等文件转换为pdf文件流 第一步下载Apache OpenOffice 安装包安装运行 地址:https:// ...

  9. 使用永中文档实现java在线预览Word,Excel,Pptx,Pdf

    使用永中文档实现java在线预览Word,Excel,Pptx,Pdf 永中文档提供了在线预览的功能 永中开发者文档 如果需要直接运行,请直接修改代码中的两个参数 转换类型在下方,根据传入以及输出类型 ...

最新文章

  1. 简练软考知识点整理-范围确认易混概念
  2. 学习ansible playbook之前先了解下YAML语法
  3. nginx虚拟主机(基于域名虚拟主机、基于IP地址虚拟主机、基于端口虚拟主机设置)
  4. ORA-12170:TNS:连接超时
  5. 图片识别出处_AI人脸识别
  6. 只能由中文、字母、数字、下划线组成的字符串
  7. Linux 线程控制
  8. CreateThread()使用实例
  9. 消费金融盈利分析及风控能力建设
  10. NVisionXR_iOS教程十一 —— 多场景切换
  11. Git命令:git常用命令
  12. 设为首页和加入收藏js
  13. C# Winform 实现Ajax效果自定义按钮
  14. CentOS 6.7 配置JSP运行环境之resin
  15. linux命令之grep(详解)
  16. LayoutInflater类的作用与用法
  17. 复合查询sql子查询操作
  18. paypal如何支付欧元_paypal海外支付流程是什么?paypal中国可以用吗?
  19. 计算机课报告书,计算机实训总结报告书范文3篇
  20. connecting to host: cephm resulted in errors: IOError cannot send (already closed?)[ceph-deploy部署报错]

热门文章

  1. B: Snowdrop修长廊
  2. 使用MySQL Workbench修改数据库名称
  3. Java面试中会遇到哪些常见问题?粤嵌支招
  4. 【入门】App测试基础
  5. task03 Pytorch模型定义
  6. 飞马D2000 激光雷达LVX数据文件解析
  7. ChinaNet路由政策(较老的文章)
  8. java接收键盘输入姓名_Java:用键盘输入个人信息并输出
  9. 写下几次坑爹的U盘装系统过程
  10. The books which are worth reading for me