Django / Python converts PDF using shell (os.system / Popen) not working in production

I use libreoffice in the CLI to convert some documents to PDF files in Django.

This is in my opinion (and also tried in the save method on the model):

outdir = '/'.join([settings.MEDIA_ROOT, 'pdf']) command = ['libreoffice', '--headless', '--convert-to', 'pdf', '-outdir', outdir, form.instance.upload.path] stdout, stderr = subprocess.Popen(command).communicate() 

I also tried the following instead of subprocess.Popen :

 os.system("libreoffice --headless --convert-to pdf --outdir %s %s" % (outdir, instance.upload.path)) 

Then I check:

 # quick check new file exists with open("%s/%s.pdf" % (outdir, os.path.splitext(instance.upload.name)[0])): pass 

When I run them through the website, it just doesn't work - the PDF file is not created.

The thing is: both of these methods work both in my local environment and when the functions in which they are located are called from $ python manage.py shell

The only problem is production.

  • Django 1.4
  • Python 2.7.3
  • Server Version: Apache / 2.2.22 (Ubuntu) Server Built: February 13, 2012 01:51:50 (Rackspace)

Notice that I thought I might be experiencing this error: Popen no longer works with apache / wsgi and python 2.7.2? ...

Although Graham works as follows (I added this to my apache2.conf ), it seems nothing changes.

 WSGIApplicationGroup %{GLOBAL} 

My sys administration skills are small, although I may have done it wrong and I'm completely at a standstill, any advice or directions about other things to try or think about will be appreciated.

+4
source share
2 answers

mod_wsgi , when the one generated from the apache2 process will not have the $PATH defined in your shell, so you must pass subprocess.Popen path absolute to libreoffice binary, i.e. /usr/bin/libreoffice , not just its name.

+1
source

You can import the LibreOffice / OpenOffice uno module in Python and avoid using the command line. I wrote more complete documentation, which you can download at http://documenthacker.wordpress.com/ (or maybe www.documenthacker.com soon).

The cookbook section has an example for converting a document to PDF.

0
source

Source: https://habr.com/ru/post/1444592/


All Articles