Commits

Jason R. Coombs committed 2da75bd

Added a CherryPy web server to convert doc files to PDF

Comments (0)

Files changed (2)

jaraco/office/convert.py

 import os
 from win32com.client import Dispatch, constants
+import pythoncom
+import threading
 
 from jaraco.util.filesystem import save_to_file, replace_extension
 
 	target_format = getattr(constants, 'wdFormatPDF', 17)
 
 	def __init__(self):
+		if threading.current_thread().getName() != 'MainThread':
+			pythoncom.CoInitialize()
 		self.word = Dispatch('Word.Application')
 
 	def convert(self, docfile_string):
 
 	def __del__(self):
 		self.word.Quit()
+
+class ConvertServer(object):
+	def default(self, filename):
+		cherrypy.response.headers['Content-Type'] = 'application/pdf'
+		return Converter().convert(cherrypy.request.body.fp.read())
+	default.exposed = True
+
+	@staticmethod
+	def start_server():
+		global cherrypy
+		import cherrypy
+		cherrypy.quickstart(ConvertServer())
 	entry_points = dict(
 		console_scripts = [
 			'doc-to-pdf = jaraco.office.word:doc_to_pdf_cmd',
+			'doc-to-pdf-server = jaraco.office.convert:ConvertServer.start_server',
 		],
 	)
 	)