rgs = '/usr/lib/libreoffice/program/swriter -accept="socket,host=localhost,port=2002;urp;StarOffice.ServiceManager" -norestore -nofirstwizard -nologo -headless'
Here's a controller.
def do_documents(form):
import uno, os, socket, string ## prune this list
from com.sun.star.beans import PropertyValue
try:
os.mkdir(request.folder + 'temp_pdf')
except OSError:
pass
package_name = db.document_packages[request.args(0)].name
items = {}
# processing a hand made form
# get the doc id, make a list of fields for each
for k,v in form.vars.iteritems():
k_split = k.split('_')
if len(k_split) < 2 or k_split[0][:3] != 'id=':
continue
doc_id = k_split[0][3:]
if doc_id not in items:
items[doc_id] = []
items[doc_id].append((k_split[1], v))
# now attach the the running LibreOffice instance
# still need to implement a check if running and recovery if not
local = uno.getComponentContext()
resolver = local.ServiceManager.createInstanceWithContext("com.sun.star.bridge.UnoUrlResolver", local)
context = resolver.resolve("uno:socket,host=localhost,port=2002;urp;StarOffice.ComponentContext")
desktop = context.ServiceManager.createInstanceWithContext("com.sun.star.frame.Desktop", context)
for k, v in items.iteritems():
rcrd = db.document_templates[k]
path = request.folder + 'uploads/' + rcrd.body
tmplt = desktop.loadComponentFromURL("file:///"+path ,"_blank", 0, ())
print type(tmplt) # diagnostic
search = tmplt.createSearchDescriptor()
for val in v:
search.SearchString = '{{='+val[0]+'}}'
found = tmplt.findFirst(search)
while found:
found.String = string.replace(found.String, unicode('{{='+val[0]+'}}', 'utf-8'), unicode(val[1], 'utf-8'))
found = tmplt.findNext(found.End, search)
## next step is to implement the pdf conversion. I THINK this code will do it
## property = (PropertyValue("FilterName" , 0, "writer_pdf_Export" , 0 ),)
## newpath = request.folder + 'temp_pdf/' + os.path.split(path)[1]
## tmplt.storeToURL("file:///" + newpath,property)
## tmplt.dispose()
tmplt.storeAsURL("file:///home/cjk/wtf.odt",()) # not final code
tmplt.dispose()