English 中文(简体)
采用不开放的办事处的方式,将货物运至pdf,过于缓慢
原标题:Docx to pdf using openoffice headless way too slow

I ve been using PHPWord for docx files generation. And it s been working great. But now I have the need to also make available some of those files on a pdf version.

After a few research I found PyODConverter which use OOo. Seemed quite a good option since I don t want to depend on third party web services. I tried it out on my machine and it works fined, so I ve applied it on my server as well. It took a little longer but I ve managed to get it working on there too.

There is however an (bad) issue. On the server this takes about 21 seconds to get it done, while on my machine it doesn t take longer than 2. :( This is way too much time for my needs so I ve been trying to spot what might be causing this delay. Starting openoffice in healess mode with socket creation is okay. So I ve been looking at the python script trying to find out which instruction might be causing to slow down. I ve narrowed it down to this line:

context = resolver.resolve("uno:socket,host=127.0.0.1,port=8100;urp;StarOffice.ComponentContext")

This is the action that s taking about 20secs to execute. The code where it is inserted:

localContext = uno.getComponentContext()
resolver = localContext.ServiceManager.createInstanceWithContext("com.sun.star.bridge.UnoUrlResolver", localContext)
try:
    context = resolver.resolve("uno:socket,host=127.0.0.1,port=8100;urp;StarOffice.ComponentContext")
except NoConnectException:
    raise DocumentConversionException, "failed to connect to OpenOffice.org on port %s" % port
self.desktop = context.ServiceManager.createInstanceWithContext("com.sun.star.frame.Desktop", context)

Any clues on what might be causing this delay? I ve ruled out the document that I m trying to convert since this operations occur before that. Could it be a problem with uno ? Or maybe another missing library that might be causing useless testing on during the resolve() operation?

欢迎任何想法。

最佳做法 无

最佳回答

我设法通过使用管道而不是连接的垫款来消除拖延。

context = resolver.resolve("uno:pipe,name=myuser_OOffice;urp;StarOffice.ComponentContext")

I still have one problem though... the user executing the python script must be the same that starts OOo for everything to work okay. Usually it would not be much of an issue, but I m trying to execute python from my web application and I still didn t manage to get it working. I m trying with something like this:

exec( sudo -u#1000 -s python path/to/DocumentConverter.py filename.docx filename.pdf );

我没有这样做。 是否许可使用人(www-data)执行 su?

问题回答

或许服务器上的名称解决器有t 查询 localhost。 (这将是非常奇怪的,但20秒钟与英国航天中心预告一样。) 您可尝试将其替换为127.0.0.1

或者,它也许会做一些微调,将IPv6和IPv4重新贴在 当地东道<>/code>上,试图通过IPv6和失败(即该部分可能不支持IPv6,或默认该接口约束)连接起来,然后又回到IPv4。 在这种情形下,补救办法相同:将 localhost改为127.0.0.1

开放的办公室如此之大。 我也在考虑这一问题,但当时我找到了更轻松的解决办法。

我不得不从上载文件中得出头4页的预览。 这是我做的:

abiword document.doc --to=ps --exp-props="pages:1-4"
gs -q -dNOPAUSE -dBATCH -dTextAlphaBits=4  -dGraphicsAlphaBits=4 -r72 -sDEVICE=pnggray -sOutputFile=preview%d.png document.ps

因此,你可以听到最近的措辞,并尝试这样的东西:

abiword document.docx --to=pdf




相关问题
Can Django models use MySQL functions?

Is there a way to force Django models to pass a field to a MySQL function every time the model data is read or loaded? To clarify what I mean in SQL, I want the Django model to produce something like ...

An enterprise scheduler for python (like quartz)

I am looking for an enterprise tasks scheduler for python, like quartz is for Java. Requirements: Persistent: if the process restarts or the machine restarts, then all the jobs must stay there and ...

How to remove unique, then duplicate dictionaries in a list?

Given the following list that contains some duplicate and some unique dictionaries, what is the best method to remove unique dictionaries first, then reduce the duplicate dictionaries to single ...

What is suggested seed value to use with random.seed()?

Simple enough question: I m using python random module to generate random integers. I want to know what is the suggested value to use with the random.seed() function? Currently I am letting this ...

How can I make the PyDev editor selectively ignore errors?

I m using PyDev under Eclipse to write some Jython code. I ve got numerous instances where I need to do something like this: import com.work.project.component.client.Interface.ISubInterface as ...

How do I profile `paster serve` s startup time?

Python s paster serve app.ini is taking longer than I would like to be ready for the first request. I know how to profile requests with middleware, but how do I profile the initialization time? I ...

Pragmatically adding give-aways/freebies to an online store

Our business currently has an online store and recently we ve been offering free specials to our customers. Right now, we simply display the special and give the buyer a notice stating we will add the ...

Converting Dictionary to List? [duplicate]

I m trying to convert a Python dictionary into a Python list, in order to perform some calculations. #My dictionary dict = {} dict[ Capital ]="London" dict[ Food ]="Fish&Chips" dict[ 2012 ]="...