1

I thought I would be able to find a plugin for this but there's doesn't seem to be one.

This is the required process:

The user fills in a form on the site (preferably cforms!) The data from the form populates the empty cells of a pdf on the sever

Then, well once I get that far then I'll worry about the next steps!

Generating the pdf is not an option as it's a legal document.

Thanks.

daveyb
  • 88
  • 1
  • 9
  • I guess, it will be useful to read http://stackoverflow.com/questions/7364/pdf-editing-in-php – Igor Dec 18 '12 at 12:07
  • "Generating the pdf is not an option as it's a legal document." please explain, what would would the difference between "populating" and "generating" be? The `cforms` API can certainly be used to provide the data that is used to built the PDF you're after, look at the docs. – soulseekah Dec 18 '12 at 12:14
  • Thanks for your comments! When I say "populating" I mean taking an existing pdf and using form data to fill in the blanks; when I say "generating" I mean creating a template using html/css/php then converting that into a pdf (which *is* possible using this in wordpress: http://crosstec.de/en/markets/breezingforms-form-apps/details/30/16/community-market-type-form-apps-pdf-download-on-thank-you-page.html ) – daveyb Dec 18 '12 at 12:20
  • Multi-post: http://wordpress.stackexchange.com/q/76516/12615 – brasofilo Dec 18 '12 at 18:35

1 Answers1

1

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'

daveyb
  • 88
  • 1
  • 9