1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192 |
- package ppt2img
- import (
- "bytes"
- "fmt"
- "os"
- "os/exec"
- "path"
- "strings"
- )
- //FuncDocs2Pdf
- /**
- *@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 {
- fmt.Println("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)
- _, 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"
- }
- //fmt.Println("文件转换成功...", string(byteByStat))
- return fileOutPath, nil
- }
- //Pdf2Img
- /**
- *@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 Pdf2Img(fileSrcPath string, fileOutDir string) (fileOutPath string, error error) {
- fmt.Println("fileSrcPath:",fileSrcPath)
- fmt.Println("fileOutDir:",fileOutDir)
- //convert
- //convert -resize 1100x -density 200 -quality 200 双重挤压下的能化产品何去何从?.pdf 双重挤压下的能化产品何去何从?.png
- convertCmd:="convert -resize 1100x -density 200 -quality 200 "+fileSrcPath+" "+fileOutDir
- cmd := exec.Command("/bin/bash", "-c", convertCmd)
- var out bytes.Buffer
- cmd.Stdout = &out
- err := cmd.Run()
- if err!=nil {
- fmt.Println("cmd.Run Err:",err.Error())
- }
- fmt.Println(convertCmd)
- fmt.Println("文件转换成功...", out.String())
- return fileOutPath, nil
- }
|