gravityflowpdf_content

Introduction

The gravityflowpdf_content filter is used to modify the contents the PDF Generator add-on defines via the step settings template before generating the PDF. 

Parameters

ParameterTypeDefinition
$bodyArrayThe markup the template from step settings produced with current entry values.
$file_pathArrayWhere the PDF will be generated upon successful completion of the step.
$entryArrayThe current entry array.
$stepStepThe current step.

Examples

Add a custom heading above the PDF template content

1
2
3
4
5
add_filter( 'gravityflowpdf_content', 'sh_pdf_content_modify', 10, 4 );
function sh_pdf_content_modify( $body, $file_path, $entry, $step ) {
    $body = '<h1>Inserting a custom title before template content</h1>' . $body;
    return $body;
}

Prevent the {all_fields} merge tag from shrinking all the text in the table for long tables.

1
2
3
4
5
6
7
8
add_filter( 'gravityflowpdf_content', 'sh_pdf_content_modify', 10, 4 );
function sh_pdf_content_modify( $body, $file_path, $entry, $step ) {
    $body = str_replace( '<table width="99%" border="0" cellpadding="1" cellspacing="0" bgcolor="#EAEAEA"><tr><td>', '', $body );
        $body = str_replace( "</td>\r\n                   </tr>\r\n               </table>", '', $body );
 
        return $body;
    return $body;
}

Use error handling and try/catch block for complex custom markup in PDF.

The gravityflowpdf_content filter lets you put whatever you need into the PDF content using basic html markup. Perhaps you have a use case where:

  • A single entries’ PDF is a review/aggregate report that needs to display multiple Gravity Charts
  • A single entry has a Nested Forms field and wants to display certain fields in a table
  • Complex SLA calculations or workflow status comparisons would be using multiple GFAPI::get_entries searches to show a larger business context.

In any of these cases and more, the following example shows how you can ensure that a PHP error during the PDF generation would not cause the entire step to fail. It also lets you decide if/what details are added to the workflow timeline to help admin staff troubleshoot what the root cause was.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
add_filter( 'gravityflowpdf_content', array( $this, 'flowpdf_dynamic_program_review_content'), 10, 4 );
function flowpdf_dynamic_program_review_content( $body, $file_path, $current_entry, $current_step ) {
     
    if ( ! in_array( $current_step->get_id(), array'1000', '1002' ) ) ) {
        return $body;
    }
 
    $pdf_content = '';
 
    $pdf_generate_errors = [];
 
    // Custom error handler to capture only relevant errors
    $custom_error_handler = function ($errno, $errstr, $errfile, $errline) use (&$pdf_generate_errors) {
        $pdf_generate_errors[] = sprintf(
            'Error: [%d] %s in %s on line %d',
            $errno,
            $errstr,
            $errfile,
            $errline
        );
        return true; // Prevents PHP's default error handler from running
    };
 
    set_error_handler( $custom_error_handler );
 
    try {
     
        //Do complex logic here.
        //You would obviously check values for as many edge cases to avoid causing failures.
        //But the try/catch block ensures if you happen to miss one,
        //or entry data is in an unexpected format that causes a PHP failure the step will still proceed.
 
    } catch (Exception $e) {
        // Handle exceptions if necessary
        $pdf_generate_errors[] = 'Exception: ' . $e->getMessage();
    } finally {
        // Restore the previous error handler
        restore_error_handler();
    }
 
 
    if ( ! empty( $pdf_generate_errors ) ) {
            $body = GFCommon::replace_variables( $body, $form, $entry, false, false, false, 'html' );
            $body .= '<h2>PDF Generation Errors</h2><p>Please contact IT Support with the following details:</p><pre>' . print_r( $pdf_generate_errors, true ) . '</pre>';
    } else {
            // Proceed with normal processing
            $pdf_content = GFCommon::replace_variables( $pdf_content, $form, $entry, false, false, false, 'html');
            $body .= $pdf_content;
    }
    return $body;
 
}

Placement

This code can be used in the functions.php file of the active theme, a custom functions plugin, a custom add-on, or with a code snippets plugin.

See also the PHP section in this article: Where Do I Put This Code?