21

Anyone know how to convert word files (.doc) to PDF using the command line in Windows?

We were using the service of OpenOffice, but in documents that have many tables and images, it ends up ruining much formatting.

  • hate to ask, but why cli specifically? scripting? – Journeyman Geek Feb 23 '12 at 11:40
  • what exactly you want to know – Xosler Feb 23 '12 at 12:12
  • Well, i can invoke printing off CLI, but it'll open windows for it, apparently – Journeyman Geek Feb 23 '12 at 12:22
7

"winword.exe" "C:\My Files\doc1.doc" /mFilePrintDefault Provided you have quiet PDF printer as default.

Also try libreOffice offspring od OpenOffice.org. It is based of go-ooo which had no problems with OLE metrics in office.

| improve this answer | |
  • I can do something like: winword.exe 'c :/ file.doc' '/ printer' 'c :/ arquivo_de_saida.pdf'? – Xosler Feb 23 '12 at 11:13
  • currently use the service openoffice / LibreOffice, but the conversion spoils the original formatting of the file, and if we can not lose any formatting. Part of the files are. Docx with many tables, headers and footers, we must keep the original formatting – Xosler Feb 23 '12 at 11:23
  • I am just wondering if "docx files" still have Word 6.0 compatibility attributes? Libre/OpenOffice parser does not have means to deal with them because "standard" from microsoft does not describe exact metrics. – ZaB Feb 23 '12 at 16:48
  • In W7 I get:'winword.exe' is not recognized as an internal or external command. Will see what directory it is in. – KAE Jun 22 '16 at 18:30
7
  1. if you have Microsoft Word installed, you can use DocTo:

    https://github.com/tobya/DocTo

    batch operation is supported, all files in directory can be converted from doc to pdf:

    docto -f "C:\Dir with Spaces\FilesToConvert\" -O "C:\DirToOutput" -T wdFormatPDF  -OX .pdf
    
  2. another option is to use the following powershell script: https://gallery.technet.microsoft.com/office/Script-to-convert-Word-f702844d

| improve this answer | |
3

I found this site, and by testing OfficeToPDF, it is apparently working well. Example:

OfficeToPDF "c​​:\help.doc" "c:\output\help.pdf"
| improve this answer | |
  • 3
    Under the hood, it use MS Office to convert the files (see requirement part). Don't pay for this and use open source solution : OfficeToPDF (it does exactly the same) – tigrou May 22 '17 at 14:11
  • Wondeful! The binaries (.exe) are there: github.com/cognidox/OfficeToPDF/releases – Basj Apr 4 '18 at 13:01
2

We use the BCL easyPDF SDK for that purpose in our application. It provides much more control over how the conversion looks (e.g. how to handle Track Changes annotations).

| improve this answer | |
  • wouldn't that mean actually having to write the application from scratch? – Journeyman Geek Feb 23 '12 at 11:40
  • In theory, although the OP mentioned this being used as part of a custom Java app. For our use it was convenient to have the conversion happen on the commandline so I wrapped more or less the sample code provided on the website in a tiny executable. – Kit Grose Feb 23 '12 at 12:14
2

I know the question is old, but the best solution to convert any kind of file to PDF is to use the PDFCreator. It is free, and has a command line EXE file you can use to convert any file to PDF. The application to read the file type has to be installed on the PC of course (you need office installed to convert an office document and Autocad to convert a CAD file). PDFCreator can run in the background and output files to a specified folder with no user interaction required. Sadly you cannot specify in the command line where to output the PDF. PDFCreator always output to the same folder specified in the settings. But knowing this, you can pick up the converted file in that folder and move/rename to wherever you want.

| improve this answer | |
  • In my attempts to use it, I was able to set the output directory, but it does not run quietly. It opens and closes an MS Word banner, but it STOPS on the PDFCreator GUI and you must then operate that manually to continue. One can however buy a license for their PDFCreator Server for just under $1K. – Thom Ives Oct 6 '19 at 1:30
1

Download the free PDFTOOFFICE FROM http://officetopdf.codeplex.com/releases/view/118190

  :: PURPOSE: convert word_file to pdf_file 
  :: DEFINE VARS 
  set WORD_FILE="C:\var\anc\issues\BDCTBA-12\docs\BDC-ISG\BDC-ISG.docx"
  set PDF_FILE="C:\var\anc\issues\BDCTBA-12\docs\BDC-ISG\BDC-ISG.pdf"

  :: REMOVE FIRST THE FILE 
  DEL /Q "%PDF_FILE%

  :: CONVERT THE WORD FILE TO A PDF FILE
  :: REMEMBER TO CTRL + S , while writing ... 
  :: NOTE IF THE FILE IS OPENED IN WORD YOU WOULD HAVE TO USE READ ONLY COPY
  :: ACTION !!!
  "OFFICETOPDF.EXE" "%WORD_FILE%" "%PDF_FILE%"

  :: REQUIREMENTS
  :: DOWNLOAD OFFICETOPDF FROM [http://officetopdf.codeplex.com/releases/view/118190][2]

  :: OTHER GOODIES - call from cygwin 
  :: PATH=/cygdrive/c/WINDOWS/system32:$PATH
  :: cmd /c convert-BDC-ISG.docx-to-BDC-ISG.cmd
| improve this answer | |
  • 1
    fyi, this project uses Word Interop. So Word is required to be installed. – jltrem Jul 22 '14 at 2:37
1

If you have Microsoft Word installed, you can use the docx2pdf command line utility to batch convert docx to pdf on windows or macos. https://github.com/AlJohri/docx2pdf

Install:

pip install docx2pdf

Run:

docx2pdf myfolder/

Disclaimer: I wrote this tool after struggling to find a cross-platform solution for batch converting docx to pdf with zero formatting issues since it directly uses Microsoft Word.

| improve this answer | |

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.