How to convert Word (doc) to PDF from Windows command line ...

How to Convert Word (DOC/DOCX) to PDF from the Windows Command Line

Converting Word documents to PDF format from the command line in Windows can be a valuable skill, especially when automating tasks or dealing with batch conversions. This article explores various methods to achieve this, ranging from using Microsoft Word directly to leveraging free and open-source tools.

Why Use the Command Line for DOC to PDF Conversion?

  • Automation: Automate document conversion as part of a larger workflow.
  • Batch Processing: Convert multiple files at once without manual intervention.
  • Scripting: Integrate the conversion process into scripts for customized solutions.
  • Server-Side Conversion: Perform conversions on a server without a graphical user interface.

Methods for Converting Word to PDF via Command Line

Here are several approaches you can take, depending on your specific needs and available software:

1. Using Microsoft Word Directly

If you have Microsoft Word installed, you can utilize its command-line capabilities for conversion:

"winword.exe" "C:\My Files\doc1.doc" /mFilePrintDefault

This command relies on a "quiet PDF printer" being set as your default printer. This method essentially tells Word to print the document to the default PDF printer.

Pros:

  • Simple, if you already have Word installed.
  • Maintains formatting accurately, as it uses Word's native conversion engine.

Cons:

  • Requires Microsoft Word.
  • Relies on a default PDF printer being configured.
  • Might not be entirely "quiet" and could briefly display the Word interface.

2. Using DocTo

DocTo is a command-line tool specifically designed for document conversion:

docto -f "C:\Dir with Spaces\FilesToConvert\" -O "C:\DirToOutput" -T wdFormatPDF -OX .pdf

This command converts all files in the specified directory to PDF format. You can download DocTo from its GitHub repository.

Pros:

  • Supports batch conversion.
  • Relatively easy to use.

Cons:

  • Requires downloading and installing an external tool.
  • As of 2024, some AV engines flag docto 1.15 with warnings.

3. Using a PowerShell Script

PowerShell offers a scripting solution to convert Word documents to PDF. Here's an example:

#Requires -Version 2.0
#requires -Component NETFramework
#-----------------------------------------------------------------------------------
#Get the arguments passed into the script
Param([string]$WordFilePath = $(throw "Please supply a valid Word file path for the conversion."))
#-----------------------------------------------------------------------------------
#Add the Word Object so that we can manipulate documents from the command line
Add-Type -AssemblyName Microsoft.Office.Interop.Word

#Create a variable for the missing value
$Missing = [System.Type]::Missing

#Create an instance of Word
$Word = New-Object -ComObject Word.Application

#Open the Word document
$Document = $Word.Documents.Open($WordFilePath, $Missing, $False)

#Check to see if the file exists and that it is a word document
If (($Document) -and ($WordFilePath.Contains(".doc")))
{
    #Set the output file path
    $OutputFilePath = $WordFilePath.Replace(".doc", ".pdf")
    Write-Host "Converting the Word document to PDF"

    #Convert the Word document to PDF
    $Document.SaveAs([ref]$OutputFilePath, [ref]17)

    #Close the Word document
    $Document.Close()

    #Exit Word
    $Word.Quit()

    #Remove the object references
    [System.Runtime.Interopservices.Marshal]::ReleaseComObject($Word)
    [System.Runtime.Interopservices.Marshal]::ReleaseComObject($Document)
    Write-Host "The Word document has been successfully converted to PDF"
}
ElseIf (($Document) -and ($WordFilePath.Contains(".docx")))
{
    #Set the output file path
    $OutputFilePath = $WordFilePath.Replace(".docx", ".pdf")
    Write-Host "Converting the Word document to PDF"

    #Convert the Word document to PDF
    $Document.SaveAs([ref]$OutputFilePath, [ref]17)

    #Close the Word document
    $Document.Close()

    #Exit Word
    $Word.Quit()

    #Remove the object references
    [System.Runtime.Interopservices.Marshal]::ReleaseComObject($Word)
    [System.Runtime.Interopservices.Marshal]::ReleaseComObject($Document)
    Write-Host "The Word document has been successfully converted to PDF"
}
Else
{
    #Write the error message to the console
    Write-Host "The file path is either invalid or the file is not a Word document."
}

You can save this script as a .ps1 file and execute it from the command line, passing the Word file path as an argument.

Pros:

  • Flexible and customizable.
  • Leverages the power of PowerShell scripting.

Cons:

  • Requires some scripting knowledge.
  • Relies on Microsoft Word being installed.

4. Using docx2pdf

docx2pdf is a Python-based command-line tool that directly uses Microsoft Word for conversion, ensuring accurate formatting.

Install it using pip:

pip install docx2pdf

Then, run it from the command line:

docx2pdf myfolder/

This command converts all DOCX files in the "myfolder" directory to PDF.

Pros:

  • Simple to install and use (requires Python and pip).
  • Maintains formatting by leveraging Microsoft Word.
  • Supports batch conversion.

Cons:

  • Requires Python and the pip package installer.
  • Requires Microsoft Word to be installed.

5. Using LibreOffice

LibreOffice, a free and open-source office suite, can also be used for command-line PDF conversion.

libreoffice --headless -convert-to pdf <file_name>.docx -outdir output/path/for/pdf

This command converts the specified DOCX file to PDF and saves it in the specified output directory. The --headless option ensures that LibreOffice runs without a graphical interface.

Pros:

  • Free and open-source.
  • Cross-platform (works on Windows, macOS, and Linux).

Cons:

  • May not perfectly preserve the original formatting of complex documents.
  • Requires LibreOffice to be installed.

6. Using OfficeToPDF

OfficeToPDF is another command-line tool that leverages Microsoft Office for conversions. It's available as open-source on GitHub.

OfficeToPDF "c:\help.doc" "c:\output\help.pdf"

Pros:

  • Open-source.
  • Uses Microsoft Office for accurate conversion.

Cons:

  • Requires Microsoft Office.
  • Might require some configuration.

Choosing the Right Method

The best method for converting Word documents to PDF from the command line depends on your specific requirements:

  • For simple conversions with Microsoft Word installed: Use the winword.exe method or docx2pdf.
  • For batch conversions with good formatting accuracy and Microsoft Word installed: Use docx2pdf or DocTo.
  • For a free and cross-platform solution: Use LibreOffice.
  • For scripting and automation: Use the PowerShell script.

Remember to consider factors like cost, ease of use, formatting accuracy, and the need for batch processing when making your decision.