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.
Here are several approaches you can take, depending on your specific needs and available software:
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:
Cons:
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:
Cons:
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:
Cons:
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:
Cons:
pip
package installer.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:
Cons:
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:
Cons:
The best method for converting Word documents to PDF from the command line depends on your specific requirements:
winword.exe
method or docx2pdf
.docx2pdf
or DocTo.Remember to consider factors like cost, ease of use, formatting accuracy, and the need for batch processing when making your decision.