带有 OpenOffice 文件的 PDFCreator

发布于 2024-09-11 04:14:15 字数 2312 浏览 3 评论 0原文

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

错误:1 说明:ActiveX 服务器尚未启动!请使用函数“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

有什么想法吗?

谢谢, 佩德

I use a Windows Web Server 2008 with ruby, PDFCreator, Microsoft Office, and OpenOffice to convert files to PDF automatically. The setup works ok for Microsoft Office files but I have not been able to get it to work automatically with OpenOffice files (e.g. .sxw). PDFCreator is able to convert .sxw files without problem when I do it manually but throws the following error when I try it with my ruby script below.

Error: 1 Description: The ActiveX-Server has not been started! Please use function \"cStart()\" to start the 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

Any ideas?

Thanks,
Peder

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

挽容 2024-09-18 04:14:15

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

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

This script calls to cIsPrintable and that searches in Windows Registry for a command called "Print".

You must have installed something causing this command be created in windows registry and that's why now it works.

荭秂 2024-09-18 04:14:15

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

Strange. Now it is working!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文