Forum: Ruby How to create PDF documents from MS word doc using Ruby

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
358a4fdb8939d8fc7ee6a439174c0446?d=identicon&s=25 Talib Hussain (talib)
on 2008-12-15 12:09
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
666b4e17b4bb0e2d999037a25f65a7cb?d=identicon&s=25 Heesob Park (phasis)
on 2008-12-15 14:57
(Received via mailing list)
2008/12/15 Talib Hussain <talibhn@gmail.com>:
> 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
358a4fdb8939d8fc7ee6a439174c0446?d=identicon&s=25 Talib Hussain (talib)
on 2008-12-16 07:43
Heesob Park wrote:
> 2008/12/15 Talib Hussain <talibhn@gmail.com>:
>> 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 Heesob

Thanks a lot
This topic is locked and can not be replied to.