我使用带有ruby、PDFCreator、Microsoft Office和OpenOffice的Windows Web Server2008自动将文件转换为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有什么想法吗?
谢谢,Peder
发布于 2010-07-21 05:27:42
真奇怪。现在它起作用了!
发布于 2013-07-09 21:48:43
此脚本调用cIsPrintable,并在Windows Registry中搜索名为"Print“的命令。
你一定是安装了什么东西,导致这个命令在windows注册表中被创建,这就是它现在工作的原因。
https://stackoverflow.com/questions/3293954
复制相似问题