Microsoft Office 2007 Word does this.
Convert image files and docs to PDF
Hello!
I was wondering how can I convert image files(JPEG,png, etc.) and docs (.doc, .docx, .xls, .txt, etc.) to a PDF file?
@isaackearl @jimmck I wanted to keep you updated, I've actually find a fine solution for my issue.
Convert Images to PDF
Before we can use the PHP library, we will need to install few dependencies:
sudo apt-get install libmagickwand-dev imagemagick php5-imagick
sudo service nginx restart
sudo service php5-fpm restart
Now we can simply use it like so:
$file = storage_path('test.jpg');
$image = new Imagick($file);
$image->setImageFormat('pdf');
$image->writeImage(storage_path('test.pdf'));
return "Done";
Convert doc, docx, xls, txt etc. to PDF
In order to do that, we will use libreoffice which can do that for us with no effort at all. We will install the most basic installation of libreoffice as we only need the command line:
sudo apt-get install libreoffice --no-install-recommends
sudo apt-get install libreoffice-calc # <- this one if for .xls files
It will install around 400MB of data on your server just so you know.
Now, when we want to convert a file to PDF:
libreoffice --headless --invisible --convert-to pdf output.pdf input.docx
We can combine the command above with Symfony Process object or just call it with exec method. I would recommend put all converting in a queue so the user won't need to wait until it's done.
Please or to participate in this conversation.