2

Assume a set of .docx files are in a directory to be converted to .pdf format. Is there a clever way to list the files and pipe it into a command that would 'print' the pdf files to a target directory? For example the resulting output of

ls -l *.docx

would be fed into the pdf-command (how this is done is at your discretion).

Assume MS-Word is not installed on the PC (Office Libre installed).

Preference is for solutions requiring minimal installation of Ubuntu utilities / packages and are well documented. Thank you

5
  • Is the listing to the stdout or to a file? Apr 10, 2019 at 16:32
  • @GeorgeUdosen Good Question. The idea is that the list of files would be processed by the command that pdfs the document and result in a new group of pdf files in the target directory. Hope this clarifies.
    – gatorback
    Apr 10, 2019 at 17:47
  • so you read from a file and process each file name... but note that again does each file name have their location indicated in the file list? Apr 10, 2019 at 18:09
  • @GeorgeUdosen I update the post to attempt to clarify. Not necessarily reading from a file, however that is a good idea.
    – gatorback
    Apr 10, 2019 at 18:23
  • I have added another way to get it done using the example you gave! Apr 11, 2019 at 10:45

2 Answers 2

4

This simple command should help:

soffice --headless --convert-to pdf ./* --outdir /path/to/target

Or:

soffice --headless --convert-to pdf /path/to/files/folder --outdir /path/to/target

The format is:

soffice --headless --convert-to <TargetFileExtension>[:NameOfFilter] file_to_convert.xxx

Where:

  • filter: specific format of file to convert
  • headless: Starts in "headless mode" which allows using the
    application without GUI. This special mode can be used
    when the application is controlled by external clients
    via the API.

Then to "output" the converted files I used this command:

ls -p | grep -v / | xargs -d "\n" soffice --headless --convert-to pdf --outdir /tmp/convert/target 2> /dev/null | grep -Eo "/.*\.doc[x]?"

Info:

  • ls -p: Append indicators to folders.
  • grep -v /: Only return files.
  • xargs -d "\n": Used to pass the output to the soffice command, takes care of white spaces in filenames.
  • 2> /dev/null: Hide errors.
  • grep -Eo "/.*\.doc[x]?": Return only affected files. And the [x]? catches both .docx and .doc files as used on my end and can be left out.

Let me add an anwser using the example OP gave:

ls *.docx | xargs -d "\n" soffice --headless --convert-to pdf --outdir /tmp/container/target

see: https://ask.libreoffice.org/en/question/2641/convert-to-command-line-parameter/

User soffice --help to see more options to that command.

1
  • This is an example of how a superior explanation presented: plenty of brilliant detail and a reference at the end to cap-off the explanation .
    – gatorback
    Apr 15, 2019 at 11:33
1

unoconv was a fast and simple, though for some reason on the first try it returned an error.

Install:

sudo apt-get install unoconv

Usage:

unoconv -f pdf -o proper_format *.docx

The command to converts all .docx documents in the current directory (whether two or a hundred) to pdf format and place them in the “proper_format” directory beneath the current directory. The directory "proper_format" is created if it does not exist.

For some reason the first attempt returned an error:

mkdir ./pdftarget
unoconv -f pdf -o pdftarget *.docx

Error: Unable to connect or start own listener. Aborting.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.