• Hey,

    We want to attach a pdf generated from the posted data to the email send via cf7 after hitting submit. To achieve this we decided to use fpdf. The code in our functions.php looks like this (shortened):

    add_action('wpcf7_before_send_mail', 'wpcf7_update_email_body');
    function wpcf7_update_email_body($contact_form) {
    
    	//import fpdf library
    	$submission = WPCF7_Submission::get_instance();
    	if ( $submission ) {
    		/* DEFINE CONSTANT AND GET FPDF CLASSES */
    		define ('FPDF_PATH',get_template_directory().'/inc/fpdf181/');
    		require(FPDF_PATH.'fpdf.php');
    
    		//get all the necessary data from the contact form
    		$posted_data = $submission->get_posted_data();
    		$firm = $posted_data["kufirm"];
    		$name = $posted_data["kuname"];
    		$image = $posted_data["file-864"];
    
    		//create new pdf and fill it with dynamic content
    		$pdf = new FPDF();
    		$pdf->AliasNbPages();
    		$pdf->AddPage('P','A4');
    		$pdf->SetFont('Arial','',12);
    
    		$pdf->Cell(50,10,'My PDF',0,1,'L');
    		$pdf->Cell(95,10,'firm',1,1,'C');
    		$pdf->Cell(95,10,'client name',1,1,'C');
    		$pdf->Image($image,10,10,-300);
    
    		$pdf->Output(FPDF_PATH.'test.pdf', 'F');
    	}
    }
    
    //add pdf created above to email attachments
    add_filter( 'wpcf7_mail_components', 'mycustom_wpcf7_mail_components' );
    function mycustom_wpcf7_mail_components($components){
    	if (empty($components['attachments'])) {
    		$components['attachments'] = array(FPDF_PATH .'test.pdf');
    	}
    	return $components;
    }

    It works perfectly fine as long as we comment $pdf->Image($image,10,10,-300); out. The image seems to create an issue that stops everything – the submit button keeps loading forever and no email is sent.

    Do you know why this is happening? Since we tried a lot we assume it’s a memory problem? How do you debug things like this in the first place?
    Or is there a better way to generate a PDF?

    https://www.ads-software.com/plugins/contact-form-7/

Viewing 8 replies - 1 through 8 (of 8 total)
  • Maybe this 4 year old thread could help:

    Takayuki Miyoshi: The $cf7->posted_data parameter only has file names. Use $cf7->uploaded_files instead, as it is an array of {field name => file path}. So when the field name is ‘file’, you can get the file path by $cf7->uploaded_files["file"].

    Thread Starter kcc24

    (@kcc24)

    Thanks for the fast reply.

    This attempt failed, same problem( although it could help us later). It also doesn’t work with local image files or remote images (via URL).

    This indicates that the problem is not related to Contact Form 7, would recommend a FPDF forum now.

    Maybe some php library for image processing or similar is missing on your server, would try to get FPDF working completely outside of WordPress first.

    Thread Starter kcc24

    (@kcc24)

    Ok, I’ll try it in an FPDF forum. Do you know how to get an error message from this or is this an FPDF task, too?

    Hello!

    We have got exactly the same problem here: the attempt to use an image in the pdf filled with dynamic content from Contact Form 7 results in a submit button that keeps loading forever and no email is sent.

    We did a lot ot testing and think FPDF is not to blame.

    Any Suggestions to solve this problem are greatly appreciated!

    Thread Starter kcc24

    (@kcc24)

    Hi,

    After several days I gave up on FPDF and used TCPDF instead. It works perfectly fine, although it’s also fully based on FPDF. I didn’t question it and was happy to find a solution for this.

    Hope I could help!

    KCC24

    Hi KCC24,

    thank you very much!

    I will give TCPDF a try.

    dadadesign

    Hi KCC24,
    for days now I’m trying to use TCPDF, but it doesn’t seem to work. Even without image the submit button loads forever, no email is sent and no pdf created. The code I’m using is originally the same as yours. My adaption for using it with TCPDF looks like this:

    add_action('wpcf7_before_send_mail', 'wpcf7_update_email_body');
    // add Contact Form 7 to PDF function
    
    	function wpcf7_update_email_body($contact_form) {
    
        $submission = WPCF7_Submission::get_instance();
        if ( $submission ) {
    
    // Include the main TCPDF library (search for installation path).
    require_once('tcpdf/examples/tcpdf_include.php');
    
    	$posted_data = $submission->get_posted_data();
        // SAVE FORM FIELD DATA AS VARIABLES
        $vorname = $posted_data["meta_guest-author"];
    	$nachname = $posted_data["nachname"];
    	$alter	 = $posted_data["meta_alter"];
        $strasse = $posted_data["strasse"];
    	$hausnummer = $posted_data["hausnummer"];
    	$plz = $posted_data["plz"];
        $ort = $posted_data["ort"];
    
    $pdf = new TCPDF(PDF_PAGE_ORIENTATION, PDF_UNIT, PDF_PAGE_FORMAT, true, 'UTF-8', false);
    
        $pdf->AddPage();
    
    	$pdf->Write(5,'Vorname: ');
    	$pdf->SetFont('Arial','',11);
        $pdf->Write(5,$vorname . "\n\n");
        $pdf->SetFont('Arial','B',11);
    	$pdf->Write(5,'Nachname: ');
        $pdf->SetFont('Arial','',11);
    	$pdf->Write(5,$nachname . "\n\n");
    	 $pdf->SetFont('Arial','B',11);
    	$pdf->Write(5,'Alter: ');
        $pdf->SetFont('Arial','',11);
    	$pdf->Write(5,$alter . "\n\n");
    	 $pdf->SetFont('Arial','B',11);
    	$pdf->Write(5,'Str.: ');
        $pdf->SetFont('Arial','',11);
    	$pdf->Write(5,$strasse . "\n\n");
    	 $pdf->SetFont('Arial','B',11);
    	$pdf->Write(5,'Hausnummer: ');
        $pdf->SetFont('Arial','',11);
    	$pdf->Write(5,$hausnummer . "\n\n");
    	 $pdf->SetFont('Arial','B',11);
    	$pdf->Write(5,'PLZ: ');
        $pdf->SetFont('Arial','',11);
    	$pdf->Write(5,$plz . "\n\n");
    	 $pdf->SetFont('Arial','B',11);
    	$pdf->Write(5,'Ort: ');
        $pdf->SetFont('Arial','',11);
    	$pdf->Write(5,$ort . "\n\n");
    	 $pdf->SetFont('Arial','B',11);
    
    	$pdf->Output('test.pdf', 'F');
        }
        }
    
        add_filter( 'wpcf7_mail_components', 'mycustom_wpcf7_mail_components' );
        function mycustom_wpcf7_mail_components($components){
        if (empty($components['attachments'])) {
        $components['attachments'] = array('test.pdf'); // ATTACH THE NEW PDF THAT WAS SAVED ABOVE
        }
        return $components;
        }

    Do you have a clue, why it’s not working?

    Thank you!

    dadadesign

Viewing 8 replies - 1 through 8 (of 8 total)
  • The topic ‘Image causing issues in FPDF’ is closed to new replies.