pp2img.go 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. package ppt2img
  2. import (
  3. "bytes"
  4. "fmt"
  5. "os"
  6. "os/exec"
  7. "path"
  8. "strings"
  9. )
  10. //FuncDocs2Pdf
  11. /**
  12. *@tips libreoffice 转换指令:
  13. * libreoffice6.2 invisible --convert-to pdf csDoc.doc --outdir /home/[转出目录]
  14. *
  15. * @function 实现文档类型转换为pdf或html
  16. * @param command:libreofficed的命令(具体以版本为准);win:soffice; linux:libreoffice6.2
  17. * fileSrcPath:转换文件的路径
  18. * fileOutDir:转换后文件存储目录
  19. * converterType:转换的类型pdf/html
  20. * @return fileOutPath 转换成功生成的文件的路径 error 转换错误
  21. */
  22. func FuncDocs2Pdf(command string, fileSrcPath string, fileOutDir string, converterType string) (fileOutPath string, error error) {
  23. //校验fileSrcPath
  24. srcFile, erByOpenSrcFile := os.Open(fileSrcPath)
  25. if erByOpenSrcFile != nil && os.IsNotExist(erByOpenSrcFile) {
  26. return "", erByOpenSrcFile
  27. }
  28. //如文件输出目录fileOutDir不存在则自动创建
  29. outFileDir, erByOpenFileOutDir := os.Open(fileOutDir)
  30. if erByOpenFileOutDir != nil && os.IsNotExist(erByOpenFileOutDir) {
  31. erByCreateFileOutDir := os.MkdirAll(fileOutDir, os.ModePerm)
  32. if erByCreateFileOutDir != nil {
  33. fmt.Println("File ouput dir create error.....", erByCreateFileOutDir.Error())
  34. return "", erByCreateFileOutDir
  35. }
  36. }
  37. //关闭流
  38. defer func() {
  39. _ = srcFile.Close()
  40. _ = outFileDir.Close()
  41. }()
  42. //convert
  43. cmd := exec.Command(command, "--invisible", "--convert-to", converterType,
  44. fileSrcPath, "--outdir", fileOutDir)
  45. _, errByCmdStart := cmd.Output()
  46. //命令调用转换失败
  47. if errByCmdStart != nil {
  48. return "", errByCmdStart
  49. }
  50. //success
  51. fileOutPath = fileOutDir + "/" + strings.Split(path.Base(fileSrcPath), ".")[0]
  52. if converterType == "html" {
  53. fileOutPath += ".html"
  54. } else {
  55. fileOutPath += ".pdf"
  56. }
  57. //fmt.Println("文件转换成功...", string(byteByStat))
  58. return fileOutPath, nil
  59. }
  60. //Pdf2Img
  61. /**
  62. *@tips libreoffice 转换指令:
  63. * libreoffice6.2 invisible --convert-to pdf csDoc.doc --outdir /home/[转出目录]
  64. *
  65. * @function 实现文档类型转换为pdf或html
  66. * @param command:libreofficed的命令(具体以版本为准);win:soffice; linux:libreoffice6.2
  67. * fileSrcPath:转换文件的路径
  68. * fileOutDir:转换后文件存储目录
  69. * converterType:转换的类型pdf/html
  70. * @return fileOutPath 转换成功生成的文件的路径 error 转换错误
  71. */
  72. func Pdf2Img(fileSrcPath string, fileOutDir string) (fileOutPath string, error error) {
  73. fmt.Println("fileSrcPath:",fileSrcPath)
  74. fmt.Println("fileOutDir:",fileOutDir)
  75. //convert
  76. //convert -resize 1100x -density 200 -quality 200 双重挤压下的能化产品何去何从?.pdf 双重挤压下的能化产品何去何从?.png
  77. convertCmd:="convert -resize 1100x -density 200 -quality 200 "+fileSrcPath+" "+fileOutDir
  78. cmd := exec.Command("/bin/bash", "-c", convertCmd)
  79. var out bytes.Buffer
  80. cmd.Stdout = &out
  81. err := cmd.Run()
  82. if err!=nil {
  83. fmt.Println("cmd.Run Err:",err.Error())
  84. }
  85. fmt.Println(convertCmd)
  86. fmt.Println("文件转换成功...", out.String())
  87. return fileOutPath, nil
  88. }