@@ -132,38 +132,43 @@ def checksum(self, document_id: int) -> int:
132132def convert_office_to_pdf (self , document_id : int ) -> int :
133133 document = Document .objects .get (pk = document_id )
134134
135- try :
135+ if settings . DEBUG :
136136 # Check if unoserver is running
137137 ping_result = subprocess .run (
138138 ["unoping" ], capture_output = True , timeout = 5 , check = False
139139 )
140140
141141 if ping_result .returncode != 0 :
142142 # Server not running, start it as a daemon
143- # Note: --daemon causes the process to fork, so we use Popen and don't wait
144- subprocess .Popen (
145- ["unoserver" , "--daemon" , "--conversion-timeout" , "300" ],
146- stdout = subprocess .DEVNULL ,
147- stderr = subprocess .DEVNULL ,
148- )
143+ # Here we want to use the system unoserver, as it needs access to LibreOffice
144+ try :
145+ subprocess .Popen (
146+ [
147+ f"{ os .environ ['HOME' ]} /.local/bin/unoserver" ,
148+ "--daemon" ,
149+ "--conversion-timeout" ,
150+ "300" ,
151+ ],
152+ stdout = subprocess .DEVNULL ,
153+ stderr = subprocess .DEVNULL ,
154+ )
155+ except FileNotFoundError as e :
156+ raise MissingBinary ("unoserver" ) from e
149157 # Give the server time to start up and be ready
150158 time .sleep (2 )
151159
152- try :
153- result = subprocess .run (
154- ["unoconvert" , "-" , "-" , "--convert-to" , "pdf" ],
155- input = document .original .read (),
156- capture_output = True ,
157- check = True ,
158- )
159- sub = result .stdout
160- except subprocess .CalledProcessError as e :
161- raise DocumentProcessingError (
162- document , exc = e , message = '"unoconvert" has failed: %s' % e .output [:800 ]
163- ) from e
164-
165- except FileNotFoundError as e :
166- raise MissingBinary ("unoserver" ) from e
160+ try :
161+ result = subprocess .run (
162+ ["unoconvert" , "-" , "-" , "--convert-to" , "pdf" ],
163+ input = document .original .read (),
164+ capture_output = True ,
165+ check = True ,
166+ )
167+ sub = result .stdout
168+ except subprocess .CalledProcessError as e :
169+ raise DocumentProcessingError (
170+ document , exc = e , message = '"unoconvert" has failed: %s' % e .stderr [:800 ]
171+ ) from e
167172
168173 document .pdf .save (str (uuid .uuid4 ()) + ".pdf" , ContentFile (sub ))
169174
0 commit comments