Is there any command line tool to convert DOC and DOCX files to PDF? If no, can it be automated by some Automator script (open-print to PDF-close)?
If you have Office:Mac 2008 Business Edition or Office:Mac 2011 Home/Business Edition, Automator actions are included with those editions. One of the Automator actions included with those versions of Office:Mac is "Convert Format of Word Documents", and one of the options in that Automator action is PDF. This page has great information about Automator and Office:Mac.
If you have Home/Student Edition instead of Business Edition, or don't have Office at all, you can accomplish it via AppleScript. Mac OS X Hints has an article about bulk converting text files to PDF via AppleScript, and the comments to that article give some options to convert DOC/DOCX to PDF via RTF. That might result in a loss of formatting or linking if you've got very complex DOC/DOCX files, but might be sufficient for files that aren't terribly complex.
You can use the
pip install docx2pdf # single file docx2pdf myfile.docx # entire folder docx2pdf myfolder/
Disclaimer: I wrote
docx2pdf after getting frustrated at the lack of cross-platform tools to convert docx to pdf directly using Microsoft Word as I needed a perfect replica with zero formatting issues. https://github.com/AlJohri/docx2pdf
In Office 2016 the Automator approach will run into issues due to security sandboxing. (The symptom: Word stays open, and you get an "Error while printing" dialog.)
A workaround is to install LibreOffice, which can be used to convert files from the command line. On MacOS, the command is:
/Applications/LibreOffice.app/Contents/MacOS/soffice \ --headless \ --convert-to pdf \ myfile.docx
The PDF will only be as good as LibreOffice's conversion from MS Office, of course, but it's adequate for many purposes.
Another approach, if you really don't care about formatting, is to use
pandoc and LaTeX:
pandoc -t latex myfile.docx -o myfile.pdf
You'll need to install pandoc and LaTeX as described in this answer, though, and your PDF will come out looking like a LaTex document -- basic formatting, headers, lists, etc. will generally be preserved, but things like fonts and margins won't.
For my application the LibreOffice solution posted by @David Moles is a great solution. I need to use this from within an application since my version of Word does not run on MacOS 12.3.1 (well, it is 32-bit so it stopped after 10.14.6. I had been using AppleScript to tell Word to convert to pdf). I learned that each space-delimited part of the arguments needs to be an object in the array of arguments passed to NSTask. eg
NSTASK *task = [[NSTask alloc] init]; task.executableURL = [NSURL fileURLWithPath:@"/Applications/LibreOffice.app/Contents/MacOS/soffice"]; NSString *filename = ...; task.currentDirectoryURL = [NSURL fileURLWithPath: ...]; task.arguments = @[@"--headless" , @"--convert-to", @"pdf", filename];
1How does this code snipped answer the question asked, how can an end-user use this on the command line to convert DOC to PDF?– nohillside ♦May 8, 2022 at 9:05
It is a comment on the answer which gave me a solution to my issue but I haven't worked out how to format the code snippet within a comment; if I could then I would put it into a comment. I hope the info provided in the last sentence will help save someone with little NSTask experience a lot of time: it took me ages to understand the mistake I was making.– ghrMay 9, 2022 at 7:48