# How to create PDF documents from MS word doc using Ruby

**URL:** <https://www.ruby-forum.com/t/how-to-create-pdf-documents-from-ms-word-doc-using-ruby/155662>\
**Category:** Ruby\
**Created:** [December 15, 2008, 12:09pm UTC](https://www.ruby-forum.com/t/how-to-create-pdf-documents-from-ms-word-doc-using-ruby/155662 "2008-12-15T12:09:16Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![talib](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/bc8723/32.png) [@talib](https://www.ruby-forum.com/u/talib)\
**Post date:** [December 15, 2008, 12:09pm UTC](https://www.ruby-forum.com/t/how-to-create-pdf-documents-from-ms-word-doc-using-ruby/155662/1 "2008-12-15T12:09:16Z")

</div>

Hi Experts,

Could you please tell me how to create PDF documents from MS word doc  
using Ruby?

I have no. of documents (Word Files) which I need to convert to .PDF.

TIA,  
Talib H.

---

<div class="post-metadata">

**Author:** ![talib](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/bc8723/32.png) [@talib](https://www.ruby-forum.com/u/talib)\
**Post date:** [December 15, 2008, 2:57pm UTC](https://www.ruby-forum.com/t/how-to-create-pdf-documents-from-ms-word-doc-using-ruby/155662/2 "2008-12-15T14:57:05Z")

</div>

2008/12/15 Talib H. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> Hi Experts,
> 
> Could you please tell me how to create PDF documents from MS word doc  
> using Ruby?
> 
> I have no. of documents (Word Files) which I need to convert to .PDF.

1. Download and install PDFCreator  
([PDFCreator: Download our free PDF converter here - pdfforge](http://www.pdfforge.org/products/pdfcreator))

2. Save the following code as word2pdf.rb and run “ruby word2pdf.rb  
\*.doc”

# adopted from PDFCreator COM Ruby sample code by Frank Heind?fer

require ‘win32ole’

pdfcreator = WIN32OLE.new(‘PDFCreator.clsPDFCreator’)  
event = WIN32OLE\_EVENT.new(pdfcreator)  
event.on\_event(‘eReady’) do  
$readyState = 1  
end

pdfcreator.cStart(‘/NoProcessingAtStartup’)  
pdfcreator.setproperty(‘cOption’, ‘UseAutosave’, 1)  
pdfcreator.setproperty(‘cOption’, ‘UseAutosaveDirectory’, 1)  
pdfcreator.setproperty(‘cOption’, ‘AutosaveFormat’, 0) # 0 = PDF  
pdfcreator.cClearCache()  
pdfcreator.setproperty(‘cPrinterStop’, false)

word = WIN32OLE.new(‘word.application’)  
word.activeprinter = ‘PDFCreator’

ARGV.each do |file|

pdfcreator.setproperty(‘cOption’, ‘AutosaveDirectory’,  
File.dirname(file))  
pdfcreator.setproperty(‘cOption’, ‘AutosaveFilename’,  
File.basename(file, File.extname(file)))  
file = file.gsub(‘/’,‘\’)  
if !FileTest.exist?(file) then  
print ‘Can’'t find the file: ', file  
break  
end

doc = word.documents.open(file,‘ReadOnly’ =\> true)  
$readyState = 0  
word.printout  
while $readyState==0  
pdfcreator.cOption(‘UseAutosave’)  
sleep 1  
end  
word.activedocument.close(false)  
end

word.quit  
pdfcreator.cClearCache()  
pdfcreator.cClose()

HTH,

Park H.

---

<div class="post-metadata">

**Author:** ![talib](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/bc8723/32.png) [@talib](https://www.ruby-forum.com/u/talib)\
**Post date:** [December 16, 2008, 7:43am UTC](https://www.ruby-forum.com/t/how-to-create-pdf-documents-from-ms-word-doc-using-ruby/155662/3 "2008-12-16T07:43:46Z")

</div>

Heesob P. wrote:

> 2008/12/15 Talib H. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):
> 
> > Hi Experts,
> > 
> > Could you please tell me how to create PDF documents from MS word doc  
> > using Ruby?
> > 
> > I have no. of documents (Word Files) which I need to convert to .PDF.  
> > end

> HTH,
> 
> Park H.

Thanks a lot
