print.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. package api
  2. import (
  3. "fmt"
  4. "os"
  5. "os/exec"
  6. "path"
  7. "runtime"
  8. "strings"
  9. "testing"
  10. "github.com/gin-gonic/gin"
  11. )
  12. func Printr(c *gin.Context, apictx *ApiSession) (interface{}, error) {
  13. return nil, nil
  14. }
  15. func TestToPdf(t *testing.T) {
  16. fileSrcPath := "/Users/xing/Desktop/123/test2.docx" //自己机器上的文件地址
  17. outPath := "/Users/xing/Desktop/123/pdf" //转出文件的路径
  18. fileType := "pdf"
  19. osName := runtime.GOOS //获取系统类型
  20. switch osName {
  21. case "darwin": //mac系统
  22. command := "/Applications/LibreOffice.app/Contents/MacOS/soffice"
  23. pdfFile, err := FuncDocs2Pdf(command, fileSrcPath, outPath, fileType)
  24. if err != nil {
  25. println("转化异常:", err.Error())
  26. }
  27. fmt.Println("转化后的文件:", pdfFile)
  28. case "linux":
  29. command := "libreoffice7.3"
  30. pdfFile, err := FuncDocs2Pdf(command, fileSrcPath, outPath, fileType)
  31. if err != nil {
  32. println("转化异常:", err.Error())
  33. }
  34. fmt.Println("转化后的文件:", pdfFile)
  35. case "windows":
  36. command := "soffice libreoffice" // 因为没有windows机器需要自己测试下这个命令行
  37. pdfFile, err := FuncDocs2Pdf(command, fileSrcPath, outPath, fileType)
  38. if err != nil {
  39. println("转化异常:", err.Error())
  40. }
  41. fmt.Println("转化后的文件:", pdfFile)
  42. default:
  43. fmt.Println("暂时不支持的系统转化:" + runtime.GOOS)
  44. }
  45. }
  46. /**
  47. *@tips libreoffice 转换指令:
  48. * libreoffice6.2 invisible --convert-to pdf csDoc.doc --outdir /home/[转出目录]
  49. *
  50. * @function 实现文档类型转换为pdf或html
  51. * @param command:libreofficed的命令(具体以版本为准);win:soffice; linux:libreoffice6.2
  52. * fileSrcPath:转换文件的路径
  53. * fileOutDir:转换后文件存储目录
  54. * converterType:转换的类型pdf/html
  55. * @return fileOutPath 转换成功生成的文件的路径 error 转换错误
  56. */
  57. func FuncDocs2Pdf(command string, fileSrcPath string, fileOutDir string, converterType string) (fileOutPath string, error error) {
  58. //校验fileSrcPath
  59. srcFile, erByOpenSrcFile := os.Open(fileSrcPath)
  60. if erByOpenSrcFile != nil && os.IsNotExist(erByOpenSrcFile) {
  61. return "", erByOpenSrcFile
  62. }
  63. //如文件输出目录fileOutDir不存在则自动创建
  64. outFileDir, erByOpenFileOutDir := os.Open(fileOutDir)
  65. if erByOpenFileOutDir != nil && os.IsNotExist(erByOpenFileOutDir) {
  66. erByCreateFileOutDir := os.MkdirAll(fileOutDir, os.ModePerm)
  67. if erByCreateFileOutDir != nil {
  68. fmt.Println("File ouput dir create error.....", erByCreateFileOutDir.Error())
  69. return "", erByCreateFileOutDir
  70. }
  71. }
  72. //关闭流
  73. defer func() {
  74. _ = srcFile.Close()
  75. _ = outFileDir.Close()
  76. }()
  77. //convert
  78. cmd := exec.Command(command, "--invisible", "--language=zh-CN", "--convert-to", converterType,
  79. fileSrcPath, "--outdir", fileOutDir)
  80. byteByStat, errByCmdStart := cmd.Output()
  81. //命令调用转换失败
  82. if errByCmdStart != nil {
  83. return "", errByCmdStart
  84. }
  85. //success
  86. fileOutPath = fileOutDir + "/" + strings.Split(path.Base(fileSrcPath), ".")[0]
  87. if converterType == "html" {
  88. fileOutPath += ".html"
  89. } else {
  90. fileOutPath += ".pdf"
  91. }
  92. fmt.Println("文件转换成功...", string(byteByStat))
  93. return fileOutPath, nil
  94. }