Tutorials
Posted on Sept 16, 2011 at 23:20
Files - Dynamic PDF generation
I originally did this setup on a Windows machine under Cygwin. I'm sure it could be modified for use on other operating systems, particularly on Linux.
1) Download odtphp - takes an OpenOffice template you create and replaces specified tags with your text.
- http://www.odtphp.com/
- after extracting, create the following directories:
odtphp-1.0.1/processing
odtphp-1.0.1/processing/templates
odtphp-1.0.1/processing/pdf_files
2) Download PyODConverter - converts an OpenOffice file to PDF (single script - DocumentConverter.py)
- http://artofsolving.com/opensource/pyodconverter
- copy DocumentConverter.py to odtphp-1.0.1/processing
3) Start OpenOffice as a service that runs in the background
/cygdrive/c/Program\ Files\ \(x86\)/OpenOffice.org\ 3/program/soffice -accept="socket,port=8100;urp;StarOffice.ServiceManager"
- Check out the cronjob script I wrote to do this very thing on a persistent basis:
start_ooffice.sh
4) Create your OpenOffice Writer template (odtphp-1.0.1/processing/templates/my_template.odt)
{DATE_MONTH} {DATE_DAY}, {DATE_YEAR}
5) Create the php page
<?php
# Pull in the required odtphp library
require_once('odtphp-1.0.1/library/odf.php');
# Put the OpenOffice directory in our $PATH
$newpath = "C:\Program Files (x86)\OpenOffice.org 3\program";
$current_path = getenv('PATH');
$newpath = $newpath.';'.$current_path;
putenv('PATH='.$newpath);
# Define all our files
$template_file = "odtphp-1.0.1/processing/templates/my_template.odt";
$tmp_odt_file = "odtphp-1.0.1/processing/pdf_files/tmp.odt";
$new_pdf_file = "odtphp-1.0.1/processing/pdf_files/new.pdf";
# Modify the template with our new data and save the new file
$odf = new odf("$template_file");
$date_month = date('F');
$date_day = date('jS');
$date_year = date('Y');
$odf->setVars('DATE_MONTH', $date_month);
$odf->setVars('DATE_DAY', $date_day);
$odf->setVars('DATE_YEAR', $date_year);
$odf->saveToDisk($tmp_odt_file);
# Convert the OpenOffice Writer file to PDF format
exec("python odtphp-1.0.1\processing\DocumentConverter.py $tmp_odt_file $new_pdf_file");
# Let's cleanup after ourselves
unlink("$tmp_odt_file") or die ("Could not remove temp file!");
?>