go实现word,excel,pptx转pdf
因工作需要实现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相关推荐
- php word excel转pdf文件怎么打开,php office文件(word/excel/ppt)转pdf文件,pptpdf
php office文件(word/excel/ppt)转pdf文件,pptpdf 把代码放到了github上,点击进入 前阶段有个项目用到了线上预览功能, 关于预览office文件实现核心就是,把o ...
- vue预览word,excel,pptx,pdf文件
vue预览word,excel,pptx,pdf文件 1.做word,excel,pptx的预览,要先确定文件路径访问是通过域名的url来预览,不可以通过IP的url来访问 先把文件路径的url进行u ...
- php word/excel/ppt 转pdf
转载至:https://blog.csdn.net/sangjinchao/article/details/78053545 把代码放到了github上,点击进入 前阶段有个项目用到了线上预览功能, ...
- springBootopenoffice将word/excel转换成pdf或者html用于预览
openoffice安装参见 https://blog.csdn.net/u010692825/article/details/106159178 此外网上也有很多类似的文章,都介绍得比较详细 现在简 ...
- ASP.net中将Word/Excel转换成PDF
关键字 : Asp.net, word, Excel, PDF 参照网友的一篇文章( http://www.vuu.cn/vuu/vuu-a/4302.html) --- ASP.Net实现将Word ...
- java word,excel,ppt转pdf
准备工作 1.下载 jacob.jar 链接:https://pan.baidu.com/s/1TWIGyX9A3xQ6AG9Y3mVlVg 提取码:abcd 2.下载安装wps WPS Offi ...
- jacob电脑重启后word/excel转pdf是0KB问题解决方法
问题现象:使用jacob将word/excel转换为pdf时,服务器重启后,必须重新登录下桌面,才可转换pdf 成功,否则转换的pdf文件直接是0KB. 1.确保所用电脑的office本身可以将wor ...
- Java实现 word.excel等文档在线预览
实现的原理: 通过第三方工具openoffice,将word,excel,pppt,txt等文件转换为pdf文件流 第一步下载Apache OpenOffice 安装包安装运行 地址:https:// ...
- 使用永中文档实现java在线预览Word,Excel,Pptx,Pdf
使用永中文档实现java在线预览Word,Excel,Pptx,Pdf 永中文档提供了在线预览的功能 永中开发者文档 如果需要直接运行,请直接修改代码中的两个参数 转换类型在下方,根据传入以及输出类型 ...
最新文章
- 简练软考知识点整理-范围确认易混概念
- 学习ansible playbook之前先了解下YAML语法
- nginx虚拟主机(基于域名虚拟主机、基于IP地址虚拟主机、基于端口虚拟主机设置)
- ORA-12170:TNS:连接超时
- 图片识别出处_AI人脸识别
- 只能由中文、字母、数字、下划线组成的字符串
- Linux 线程控制
- CreateThread()使用实例
- 消费金融盈利分析及风控能力建设
- NVisionXR_iOS教程十一 —— 多场景切换
- Git命令:git常用命令
- 设为首页和加入收藏js
- C# Winform 实现Ajax效果自定义按钮
- CentOS 6.7 配置JSP运行环境之resin
- linux命令之grep(详解)
- LayoutInflater类的作用与用法
- 复合查询sql子查询操作
- paypal如何支付欧元_paypal海外支付流程是什么?paypal中国可以用吗?
- 计算机课报告书,计算机实训总结报告书范文3篇
- connecting to host: cephm resulted in errors: IOError cannot send (already closed?)[ceph-deploy部署报错]