Skip to content

Instantly share code, notes, and snippets.

@grafikchaos
Last active December 14, 2015 01:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save grafikchaos/5009740 to your computer and use it in GitHub Desktop.
Save grafikchaos/5009740 to your computer and use it in GitHub Desktop.
Make unoconv apache's bitch

How to make unoconv run inside an apache instance: CHEAT

First, you should read this stackoverflow post

The instructions below are slightly modified to a specific project where we're converting every document to PDF


Create the /usr/local/bin/unoconv.sh file

  1. Create the /usr/local/bin/unoconv.sh file by doing: sudo touch /usr/local/bin/unoconv.sh
  2. Give the file executable permissions: sudo chmod +x /usr/local/bin/unoconv.sh
  3. Copy and paste this code into your unoconv.sh file
#!/bin/bash

if [ -z "$1" ]; then
    echo "Must pass source file to convert to pdf";
    exit 10;
fi

# This assumes your unoconv executable is located at /usr/bin/unoconv
# If that's not true try a `which unoconv` to get the path and update 
# the path below to your path
/usr/bin/unoconv -f pdf $1

Edit the sudoers file

Run sudo visudo to edit the /etc/sudoers file and add the following two lines to the bottom

# /etc/sudoers

# ... stuff ...

# Give apache sudo privileges to run /usr/local/bin/unoconv.sh script
www-data ALL=NOPASSWD: /usr/local/bin/unoconv.sh

Test it out in a php script

In your web root, create a file called test_unoconv.php

<?php
    # Provide the full filepath to a document you want converted into a PDF
    $file = "/path/to/your/xlsfile.xls";

    echo "Running: 'sudo /usr/local/bin/unoconv.sh {$file}'";
    shell_exec('sudo /usr/local/bin/unoconv.sh ' . $file);

In your browser, go to your test script (i.e., http://example.com/test_unoconv.php) and watch the original directory of the file to see if a new PDF was created. If everything worked, you should have a PDF with the same filename as the original source document

@hungmavuong66
Copy link

thanks!!! i already fix my problem from this post!!!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment