我使用Windows Web Server 2008與ruby,PDFCreator,Microsoft Office和OpenOffice自動將文件轉換爲PDF。該設置適用於Microsoft Office文件,但我無法使用OpenOffice文件自動工作(例如.sxw)。當我手動執行時,PDFCreator能夠毫無問題地轉換.sxw文件,但當我使用下面的ruby腳本嘗試時,會引發以下錯誤。PDFCreator與OpenOffice文件
錯誤:1描述:ActiveX服務器尚未啓動!請使用function \「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