0

I currently have this command that already works directly from terminal, as I mentioned in the title, but doesn't work it when I use it from my code.

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

Here's what my code looks like:

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
}

When I check my server's logs, I can see this message:

docxToPdf err: fork/exec lowriter --invisible --headless --convert-to pdf:writer_pdf_Export --outdir ./assets/public/conv_pdf/ ./assets/public/gen_docx/vhc_75-71506.docx: no such file or directory

Here's proof that the command works:

enter image description here

1
  • 2
    It is Command(name string, arg ...string), i.e. command and arguments are expected to be provided as separate arguments, not as as combined string. In your case it will assume that the first argument you have provided is the name of the executable, not a command line which should be interpreted by the shell. Hence no such file or directory. Commented Oct 10, 2024 at 4:32

2 Answers 2

2

The error is telling you it can't find lowriter --invisible --headless --convert-to pdf:writer_pdf_Export --outdir ./assets/public/conv_pdf/ ./assets/public/gen_docx/vhc_75-71506.docx. You've passed all that in as the program name.

From the exec.Command docs we see you pass in the command name, then its arguments.

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

Your code should be something like...

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

    if(err != nil){
        return err
    }

    return nil
}
Sign up to request clarification or add additional context in comments.

2 Comments

" ... --convert-to pdf:writer_pdf_Export", ..." - I would expect that this needs to be broken into two separate arguments, since the shell would do the same. Similar with --outdir %v
Seems like @SteffenUllrich was right. I had to break every part of the command for it to work. The fmt.Sprintf was unnecessary
1

Thanks to @Schwern and @Steffen Ullrich for the insight on how to fix my issue.

Basicallly, exec.Command wanted you to break your command by "character group". If it's between spaces, it should be a separate argument.

Here's what my updated code is:

func DocxToPdf(srcDocxPath string, outputPath string) error{
    cmd := exec.Command(
        "lowriter",
        "--invisible", 
        "--headless", 
        "--convert-to", 
        "pdf:writer_pdf_Export",
        "--outdir",
        outputPath,
        srcDocxPath,
    )

    output, err := cmd.CombinedOutput() // had to search for this to see the actual error while debugging

    if err != nil {
        fmt.Println(fmt.Sprint(err) + ": " + string(output))
        return err
    }

    return nil
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.