0

我使用带有 ruby​​、PDFCreator、Microsoft Office 和 OpenOffice 的 Windows Web Server 2008 将文件自动转换为 PDF。该设置适用于 Microsoft Office 文件,但我无法让它自动处理 OpenOffice 文件(例如 .sxw)。当我手动执行此操作时,PDFCreator 能够毫无问题地转换 .sxw 文件,但是当我使用下面的 ruby​​ 脚本尝试它时会引发以下错误。

错误:1 说明:ActiveX-Server 尚未启动!请使用函数\"cStart()\" 来启动ActiveX-Server!

def convert( filename, data )
  require 'win32ole'

  dirpath = File.join( '/', 'files' )
  filepath =  File.join( dirpath, filename )
  puts filepath
  filepath_out = File.join( dirpath, 'output.pdf' )
  begin
     File.open( filepath, 'wb+' ) { |f| f.write( data ) }
     puts File.exists?( filepath ).inspect
     pdfcreator = WIN32OLE.new( 'PDFCreator.clsPDFCreator' )
     event = WIN32OLE_EVENT.new( pdfcreator )
     event.on_event( 'eReady' ) do
        File.open( filepath_out, 'rb' ) { |f| update_attribute( :data_converted, f.read ) }
        $printed = true
     end
     event.on_event( 'eError' ) do
        pdfcreator.cClose()
        raise 'error'
     end
     if !pdfcreator.cIsPrintable( filepath )
        raise 'error'
     end
     pdfcreator.cStart( '/NoProcessingAtStartup' )
     pdfcreator.setproperty( 'cOption', 'UseAutosave', 1 )
     pdfcreator.setproperty( 'cOption', 'UseAutosaveDirectory', 1 )
     pdfcreator.setproperty( 'cOption', 'AutosaveFormat', 0 )
     pdfcreator.setproperty( 'cDefaultprinter', 'PDFCreator' )
     pdfcreator.cClearCache()
     pdfcreator.setproperty( 'cPrinterStop', false )
     pdfcreator.setproperty( 'cOption', 'AutosaveDirectory', File.dirname( filepath_out ) )
     pdfcreator.setproperty( 'cOption', 'AutosaveFilename', File.basename( filepath_out ) )
     $printed = false
     pdfcreator.cPrintfile( "C:\\files" + File.basename( filepath ) )
     started_at = Time.new
     loop {
        pdfcreator.cOption( 'UseAutosave' ) # noop to get ready event
        break if $printed
        if ( Time.new - started_at )>TIMEOUT
           raise 'timeout'
        end
        sleep 0.5
     }
  rescue => e
     raise e
  ensure
     begin
        pdfcreator.cClearCache()
        pdfcreator.cClose()
     rescue
     end
     begin
        File.delete( filepath ) if File.exists?( filepath )
        File.delete( filepath_out ) if File.exists?( filepath_out )
     rescue
     end

  end

有任何想法吗?

谢谢,佩德

4

2 回答 2

1

此脚本调用cIsPrintable并在 Windows 注册表中搜索名为“打印”的命令。

您必须安装了一些东西,导致在 Windows 注册表中创建了这个命令,这就是它现在可以工作的原因。

于 2013-07-09T13:48:43.350 回答
0

奇怪的。现在它正在工作!

于 2010-07-20T21:27:41.630 回答