2008/12/15 Talib Hussain said:
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 (
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 Heesob