Last updated on October 18, 2023

WP Forms dynamic PDF generation

Don’t know where to add this snippet? Read our guide: How to add code snippets.

Generate PDFs based on form input with WP Forms.

To generate a dynamic PDF from a WP Forms submission, you can use the following code snippet:

/**
 * Generate a dynamic PDF from WP Forms submission.
 *
 * @param array $fields The form fields.
 * @param int   $entry_id The form entry ID.
 */
function wpsnippets_generate_pdf_from_wpforms_submission( $fields, $entry_id ) {
    // Load the TCPDF library
    require_once( 'tcpdf/tcpdf.php' );

    // Create a new PDF instance
    $pdf = new TCPDF();

    // Set PDF metadata
    $pdf->SetCreator( 'Your Name' );
    $pdf->SetAuthor( 'Your Name' );
    $pdf->SetTitle( 'WP Forms Submission PDF' );
    $pdf->SetSubject( 'WP Forms Submission' );

    // Add a new page
    $pdf->AddPage();

    // Loop through the form fields and add them to the PDF
    foreach ( $fields as $field ) {
        $pdf->Cell( 0, 10, $field['label'] . ': ' . $field['value'], 0, 1 );
    }

    // Output the PDF
    $pdf->Output( 'wpforms_submission_' . $entry_id . '.pdf', 'D' );
}

// Hook into WP Forms submission process
add_action( 'wpforms_process_complete', 'wpsnippets_generate_pdf_from_wpforms_submission', 10, 2 );

This code snippet demonstrates how to generate a dynamic PDF from a WP Forms submission using the TCPDF library. It defines a custom function wpsnippets_generate_pdf_from_wpforms_submission that takes the form fields and entry ID as parameters. Inside the function, it loads the TCPDF library, creates a new PDF instance, sets the PDF metadata, adds a new page, and loops through the form fields to add them to the PDF. Finally, it outputs the PDF with a filename based on the entry ID.

To use this code snippet, you need to have the TCPDF library installed and activated in your WordPress project. You can customize the PDF metadata and layout by modifying the code within the function. Additionally, make sure to update the require_once statement to the correct path of the TCPDF library file.

Examples

Example 1: Generating a PDF from WP Forms submission

This use case demonstrates how to generate a dynamic PDF document from a WP Forms submission. The code example uses the wpsnippets_generate_pdf() function to generate the PDF and the wpsnippets_wpforms_submission_handler() function to handle the form submission.

function wpsnippets_generate_pdf( $form_data ) {
    // Generate the PDF content based on the form data
    // ...

    // Return the PDF content
    return $pdf_content;
}

function wpsnippets_wpforms_submission_handler( $fields, $entry, $form_data ) {
    // Generate the PDF from the form submission
    $pdf_content = wpsnippets_generate_pdf( $form_data );

    // Send the PDF as an email attachment
    wp_mail( 'recipient@example.com', 'Form Submission PDF', '', '', array( $pdf_content ) );
}
add_action( 'wpforms_process_complete', 'wpsnippets_wpforms_submission_handler', 10, 3 );

In this code example, the wpsnippets_generate_pdf() function is responsible for generating the PDF content based on the form data. The wpsnippets_wpforms_submission_handler() function is hooked into the wpforms_process_complete action and is triggered when a form submission is completed. It calls the wpsnippets_generate_pdf() function to generate the PDF and then sends it as an email attachment using the wp_mail() function.

Example 2: Saving a PDF to the server

This use case demonstrates how to generate a dynamic PDF document from a WP Forms submission and save it to the server. The code example uses the wpsnippets_generate_pdf() function to generate the PDF and the wpsnippets_wpforms_submission_handler() function to handle the form submission.

function wpsnippets_generate_pdf( $form_data ) {
    // Generate the PDF content based on the form data
    // ...

    // Save the PDF to the server
    $pdf_path = '/path/to/save/pdf.pdf';
    file_put_contents( $pdf_path, $pdf_content );

    // Return the PDF path
    return $pdf_path;
}

function wpsnippets_wpforms_submission_handler( $fields, $entry, $form_data ) {
    // Generate the PDF from the form submission
    $pdf_path = wpsnippets_generate_pdf( $form_data );

    // Do something with the PDF path, e.g. store it in the database
}
add_action( 'wpforms_process_complete', 'wpsnippets_wpforms_submission_handler', 10, 3 );

In this code example, the wpsnippets_generate_pdf() function is responsible for generating the PDF content based on the form data. It then saves the PDF to the server using the file_put_contents() function. The wpsnippets_wpforms_submission_handler() function is hooked into the wpforms_process_complete action and is triggered when a form submission is completed. It calls the wpsnippets_generate_pdf() function to generate the PDF and then performs some action with the PDF path, such as storing it in the database.

Example 3: Customizing the PDF layout

This use case demonstrates how to customize the layout of the generated PDF document. The code example uses the wpsnippets_generate_pdf() function to generate the PDF and includes a custom template file to define the layout.

function wpsnippets_generate_pdf( $form_data ) {
    // Load the PDF template file
    $template_path = '/path/to/pdf-template.php';
    ob_start();
    include $template_path;
    $pdf_content = ob_get_clean();

    // Return the PDF content
    return $pdf_content;
}

function wpsnippets_wpforms_submission_handler( $fields, $entry, $form_data ) {
    // Generate the PDF from the form submission
    $pdf_content = wpsnippets_generate_pdf( $form_data );

    // Send the PDF as an email attachment
    wp_mail( 'recipient@example.com', 'Form Submission PDF', '', '', array( $pdf_content ) );
}
add_action( 'wpforms_process_complete', 'wpsnippets_wpforms_submission_handler', 10, 3 );

In this code example, the wpsnippets_generate_pdf() function loads a custom PDF template file using the include statement. The template file can contain HTML and PHP code to define the layout of the PDF document. The content of the template file is captured using output buffering and then returned as the PDF content. The wpsnippets_wpforms_submission_handler() function is hooked into the wpforms_process_complete action and is triggered when a form submission is completed. It calls the wpsnippets_generate_pdf() function to generate the PDF and then sends it as an email attachment using the wp_mail() function.

Last updated on October 18, 2023. Originally posted on November 3, 2023.

Leave a Reply

Your email address will not be published. Required fields are marked *