How to convert documents into pdf using PDFCreator and PHP

Introduction

In this tutorial I’m going to show you how you can convert document files such as word (doc, docx), powerpoint (ppt, pptx), excel (xls, xlsx) and image files into pdf format using pdfcreator.
Pdfcreator is a free software used to create pdf files from different file types.
You may want to use it to convert the files which are uploaded into your website into a single format.

Configure PDF Creator

After downloading and installing pdfCreator you may want to have the files auto-save into a default location so that there will be no user intervention that will be needed when converting files into pdf. To do that open up pdfCreator, press ctrl + O in your keyboard to launch the options. Under the Program Menu you will see the auto-save option. Check the use auto-save checkbox and select PDF as the auto-save format. For the filename I used the original title of the file plus the date and time in which it was converted so as to avoid duplicate filenames. Finally select the directory in which you want to save the pdf file that will be generated. It has also a send email feature but we won’t be delving into that in this article.

image

Batch File

Create a batch file which will be used to call pdfCreator to do its job. A batch file has an extension of .bat so be sure to save the text file with this extension so that it will be executable. The batch file will contain the call for pdfcreator.exe and then the parameters (command plus the location of the file that you want to convert)

pdfcreator.exe /PF"D:\files\asset_flow.xlsx"

Next we need to create another batch file that will clear the contents of the batch file that we created above. This is to prevent the files from being converted more than once. Note that this must only be executed at a time wherein you’re certain that files has all been converted.

@echo on > launcher.bat

If your application is running 24/7 then you might want to think of another solution as this might not be possible if there’s no idle time for your application.

Upload Script

I assume that you already have an upload script which will upload the files in a certain location so I won’t delve into that. What were gonna do here is to add code into your upload script that will put contents into the batch file that we created earlier.

<?php
$filename = $_POST['filename'];

//file location + filename
$command = "pdfcreator.exe /PF";
$contents = file_get_contents('launcher.bat');
file_put_contents('launcher', $contents . "\n" . $command . $filename);

What the code above does is to append the command, filename and file location to the current contents of the batch file.

Task Scheduler

Finally you have to decide which time you’re going to execute the batch file that mass converts the files that were uploaded and the batch file that clears the contents of the first batch file. You can do this using the task scheduler on Windows or setup a cron job if its on Linux.

Conclusion

PDFCreator is a quick and easy way to create PDF Files from other document files such as Word, Powerpoint and Excel. If you ever need the functionality to convert documents to PDF Files automatically then use PDFCreator.