Ok, I've worked out how to do this, it seems to be a bit messy, shame there is not a simpler way!
The two plug-ins I'm using are cforms (www.deliciousdays.com/cforms-plugin) and a plug-in which loads the Zend framework (h6e.net/wordpress/plugins/zend-framework) this is needed for it's pdf writing abilities.
With both plug-ins activated locate the file in the cforms plug-in directory called my-functions.php and download to your computer. Most of this file is commented out so you need to uncomment the function
function my_cforms_action($cformsdata) {
}
Anything in this function will run when the submit button is pressed (see the cforms API documentation for more details). You can test this is working but echoing something in the this function. You can also test the Zend framework has been loaded using
if (defined('WP_ZEND_FRAMEWORK') && constant('WP_ZEND_FRAMEWORK')) {
echo 'Zend is working!';
}
The form fields come in an array so we need to print that out first to work out the names of the fields (replacing "5" with whichever form you are using):
$formID = $cformsdata['id'];
$form = $cformsdata['data'];
if ( $formID == '5' ) {
print_r($form);
}
Once you have your field names this is the full code to write to the pdf
//Get the ID of the current form and all the data that's been submitted
$formID = $cformsdata['id'];
$form = $cformsdata['data'];
//run this code only if it's the form with this ID
if ( $formID == '5' ) {
//Loads the Zend pdf code
require_once 'Zend/Pdf.php';
//Set the path of the pdf (in the root of my wordpress installation)
$fileName = 'c100-eng2.pdf';
//loads the pdf
$pdf = Zend_Pdf::load($fileName);
//Selects the page to write to
$page = $pdf->pages[0];
//Selects which font to use
$font = Zend_Pdf_Font::fontWithName(Zend_Pdf_Font::FONT_HELVETICA);
$page->setFont($font, 12);
//Writes the text from the field 'Your name' 210 points from the left and 420 points from the bottom of the selected page
$page->drawText($form['cf_form5_Your name'], 210, 420);
//for some reason there is no way to wrap text using Zend_pdf so we have to do it ourselves for any paragraphs...
//starting 600 points from the bottom of the page
$startPos = 600;
//we're using the form field "About you"
$paragraph = $form['cf_form5_About you'];
//sets the width of the paragraph to 30 characters
$paragraph = wordwrap( $paragraph, 30, '\n');
//breaks paragraph into lines
$paragraphArray = explode('\n', $paragraph);
//writes out the lines starting 200 points from the left with a line height of 12 points
foreach ($paragraphArray as $line) {
$line = ltrim($line);
$page->drawText($line, 200, $startPos);
$startPos = $startPos - 12;
}
//saves the pdf
$pdf->save('new.pdf');
I had problems to start with as the pdf I was using wasn't the right type, you can check that your code is correct by creating a pdf using Zend pdf as that should always work (find out how to do that here: framework.zend.com/manual/1.12/en/zend.pdf.html).
I use Photoshop to work out the exact locations I want to write at by setting the ruler to 'points'