How to create PDF documents from MS word doc using Ruby

T

Talib Hussain

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 Hussain
 
H

Heesob Park

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,009
Latest member
GidgetGamb

Latest Threads

Top