Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I have created a form using Cforms from Delicious. The client asked me for an option to print this form as a pdf. I tried some plugins such as Print Friendly and PDF Button that make it possible to add a pdf or print button, but this button is subsequently added to all pages and most of the time the form does not appear on the print preview /printout. Does anyone know how I can do this?

share|improve this question
add comment

1 Answer

up vote 0 down vote accepted

With help from a dev at WordPress Questions I got this solved. In wp-content/plugins/cforms/cforms.php we added

require_once(dirname(__FILE__) . '/html2pdf/html2pdf.class.php');

which loaded all the necessary data from the folder html2pdf which has all the files based on code from html2pdf.fr

Further in the same file after $newcontent .= substr($content,$last); we have code to pdfy the code:

 $pdfContent = '<table width="560" border="0" cellspacing="1" cellpadding="2" bgcolor="#CCCCCC">';  
    for($i = 1; $i <= $field_count; $i++) {

        if ( !$custom )
            $field_stat = explode('$#$', $cformsSettings['form'.$no]['cforms'.$no.'_count_field_' . $i]);
        else
            $field_stat = explode('$#$', $customfields[$i-1]);

        $field_name       = $field_stat[0];
        $field_type       = $field_stat[1];
        $field_required   = $field_stat[2];
        $field_emailcheck = $field_stat[3];
        $field_clear      = $field_stat[4];
        $field_disabled   = $field_stat[5];
        $field_readonly   = $field_stat[6];


        if($field_type == 'fieldsetstart') {
        $pdfContent.='<tr><td colspan="2"></td>&nbsp;</tr><tr><td  align="left" colspan="2" valign="middle"><span style="width:100pxheight:30px;float:left">' . stripslashes(($field_name)) . '</span></td></tr><tr><td colspan="2"></td>&nbsp;</tr><tr><td colspan="2"></td>&nbsp;</tr><tr><td colspan="2"></td>&nbsp;</tr>' ;
        }

    else if($field_type =='textarea') {
    $pdfContent.='<tr><td  align="left" valign="middle"><span style="width:100pxheight:30px;float:left">' . stripslashes(($field_name)) . '</span></td><td valign="middle"  bgcolor="#ffffff" align="left"><span style="width:300px;height:30px;float:left"><textarea  cols="40" style="width:400px;height:100px;"></textarea></span></td></tr><tr><td colspan="2"></td>&nbsp;</tr>' ;
    }   

    else{
    $pdfContent.='<tr><td  align="left" valign="middle"><span style="width:100pxheight:30px;float:left">' . stripslashes(($field_name)) . '</span></td><td valign="middle"  bgcolor="#ffffff" align="left"><span style="width:300px;height:30px;float:left"><input type="text" style="width:350px;height:30px;"></span></td></tr><tr><td colspan="2"></td>&nbsp;</tr>' ;
    }   

and

$pdfContent.='</table>';
//echo $pdfContent;
 try
            {
                $html2pdf = new HTML2PDF('P', 'A4', 'fr');
                $html2pdf->writeHTML($pdfContent, isset($_GET['vuehtml']));
                $html2pdf->Output(dirname(__FILE__) . '/html2pdf/pdf/form.pdf','F');
            }
            catch(HTML2PDF_exception $e) {
                echo $e;
                exit;
            }
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.