LibreOffice 命令可以直接从终端运行,但不能从代码运行

问题描述 投票:0回答:1

我目前的命令已经可以直接从终端运行,正如我在标题中提到的,但当我从代码中使用它时它不起作用。

lowriter --invisible --headless --convert-to pdf:writer_pdf_Export --outdir ./assets/public/conv_pdf/ ./assets/public/gen_docx/filename.docx

这是我的代码的样子:

err = srvHelpers.DocxToPdf(
    fmt.Sprintf(".%v", strings.Replace(docxOutputPath, "./", "/", -1)),
    fmt.Sprintf(".%v", strings.Replace(cmmHelpers.GetConfig("CONVERTED_PDF_OUTPUT_PATH"), "./", "/", -1)),
)

if err != nil {
    cmmHelpers.MLogger(fmt.Sprintf("docxToPdf err: %v", err))
}

func DocxToPdf(srcDocxPath string, outputPath string) error{
    arg0 := "lowriter"
    arg1 := "--invisible --headless"
    arg2 := "--convert-to"
    arg3 := "pdf:writer_pdf_Export"
    arg4 := fmt.Sprintf("--outdir %v", outputPath)
    command := fmt.Sprintf("%v %v %v %v %v %v", arg0, arg1, arg2, arg3, arg4, srcDocxPath)
    _, err := exec.Command(command).Output()

    if(err != nil){
        return err
    }

    return nil
}

当我检查服务器日志时,我可以看到以下消息:

docxToPdf 错误:fork/exec lowriter --invisible --headless --convert-to pdf:writer_pdf_Export --outdir ./assets/public/conv_pdf/ ./assets/public/gen_docx/vhc_75-71506.docx:没有这样的文件或目录

这是该命令有效的证据:

enter image description here

go libreoffice
1个回答
0
投票

错误告诉您找不到

lowriter --invisible --headless --convert-to pdf:writer_pdf_Export --outdir ./assets/public/conv_pdf/ ./assets/public/gen_docx/vhc_75-71506.docx
。您已将所有内容作为程序名称传递。

exec.Command 文档我们看到您传递了命令名称,然后是它的参数。

func Command(name string, arg ...string) *Cmd

你的代码应该类似于...

func DocxToPdf(srcDocxPath string, outputPath string) error{
    _, err := exec.Command(
      "lowriter",
      "--invisible", 
      "--headless", 
      "--convert-to pdf:writer_pdf_Export",
      fmt.Sprintf("--outdir %v", outputPath),
      srcDocxPath
    ).Output()

    if(err != nil){
        return err
    }

    return nil
}
© www.soinside.com 2019 - 2024. All rights reserved.