-
-
Save uamv/73320db3a4f0f47559a27b211ee335d5 to your computer and use it in GitHub Desktop.
Allow generation of blank PDF in Gravity PDF
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// Utilize admin labels if defined on the field. | |
add_filter( 'gfpdf_field_label', function( $label, $field, $entry ) { | |
return ( strlen( $field->adminLabel ) > 0 ) ? $field->adminLabel : $label; | |
}, 10, 3 ); | |
// Get the URI | |
$request = $_SERVER['REQUEST_URI']; | |
// Check if we're generating a blank form | |
if ( preg_match( '/blank[\/]?$/', $request ) ) { | |
// Override empty and conditional settings | |
add_filter( 'gfpdf_current_pdf_configuration', function( $config, $entry, $form ) { | |
$config['meta']['empty'] = true; | |
$config['meta']['conditional'] = false; | |
return $config; | |
}, 10, 3 ); | |
// Handle empty display of various field types | |
add_action( 'gfpdf_field_value', function( $value, $field, $entry, $form, $class ) { | |
switch ( $field->type ) { | |
case 'text': | |
case 'textarea': | |
case 'uid': | |
case 'email': | |
case 'phone': | |
case 'date': | |
case 'signature': | |
case 'post_title': | |
case 'time': | |
case 'website': | |
case 'fileupload': | |
$value = ''; | |
break; | |
case 'name': | |
case 'address': | |
$value = array(); | |
break; | |
case 'checkbox': | |
case 'radio': | |
case 'multiselect': | |
$html = '<ul style="list-style-type:none;">'; | |
foreach ( $field->choices as $key => $option ) { | |
if ( 'checkbox' == $field->type || 'multiselect' == $field->type ) { | |
$html .= '<li>❐ ' . $option['text'] . '</li>'; | |
} elseif ( 'radio' == $field->type ) { | |
$html .= '<li>❍ ' . $option['text'] . '</li>'; | |
} | |
} | |
$html .= '</ul>'; | |
$value = $html; | |
break; | |
case 'select': // this one returns the raw HTML | |
if ( isset( $field->cssClass ) && strpos($field->cssClass, 'pdf-blank-empty') !== false ) { | |
$value = ''; | |
} else { | |
foreach ( $field->choices as $key => $option ) { | |
$html .= '❍ ' . $option['text'] . ' '; | |
} | |
$value = $html; | |
} | |
break; | |
case 'list': | |
if ( $field->enableColumns ) { | |
$value = typewheel_pdf_autofill_blank_list( $value, $field, $entry, $form, $class ); | |
$total_rows = $rows = (int) count( $value ); | |
if ( isset( $field->cssClass ) && preg_match( '/(?<=pdf-blank-rows-)[\d]+/', $field->cssClass, $desired_rows ) ) { | |
$total_rows = $desired_rows = (int) $desired_rows[0] - 1; | |
if ( $rows < $desired_rows ) { | |
while ( $desired_rows > $rows ) { | |
foreach ( $field->choices as $key => $column ) { | |
$value[ $desired_rows ][ $column['text'] ] = '▸'; | |
} | |
$desired_rows--; | |
} | |
} | |
} | |
foreach ( $field->choices as $key => $column ) { | |
$r = 0; | |
do { | |
if ( $value[ $r ][ $column['text'] ] == '' ) { | |
$value[ $r ][ $column['text'] ] = '▸'; | |
} | |
$r++; | |
} while ($r < $total_rows); | |
} | |
} else { | |
$value = array(); | |
} | |
break; | |
default: | |
// code... | |
break; | |
} | |
return $value; | |
}, 10, 5 ); | |
// Set height if pdf-blank-height-## class is defined | |
add_filter( 'gfpdf_field_html_value', function( $html, $value, $show_label, $label, $field, $form, $entry, $class ) { | |
// Check if the field has a defined blank height | |
if ( isset( $field->cssClass ) && preg_match( '/(?<=pdf-blank-height-)[\d\.]+\w+/', $field->cssClass, $match ) ) { | |
/* | |
* We're using the new `GFPDF\\Helper\\Helper_QueryPath` class. | |
* It acts like jQuery for PHP which makes it very handy | |
*/ | |
$qp = new GFPDF\Helper\Helper_QueryPath(); | |
$wrapper = $qp->html5( $html ); | |
$css = $wrapper->find( '.gfpdf-field .inner-container' )->css( 'height', $match[0] ); | |
/* | |
* Query Path likes to make valid HTML from fragments like we passed, which means the HTML we need is inside '<html></html>' tags | |
* To fix this we'll extract the innerHTML of the <html> tag | |
*/ | |
return $wrapper->top( 'html' )->innerHTML5(); | |
} | |
return $html; | |
}, 10, 8 ); | |
// A class to exclude fields only on blank PDFs | |
add_filter( 'gfpdf_pdf_html_output', function( $html, $form, $entry, $settings, $Helper_PDF ) { | |
$qp = new GFPDF\Helper\Helper_QueryPath(); | |
$wrapper = $qp->html5( $html ); | |
$css = $wrapper->find( '.pdf-blank-exclude' )->css('display','none'); | |
return $wrapper->top( 'html' )->innerHTML5(); | |
}, 20, 5 ); | |
} | |
// Allow a blank list to be filled with values via the `gfpdf_blank_list_values` filter | |
function typewheel_pdf_autofill_blank_list( $value, $field, $entry, $form, $class ) { | |
$fill = apply_filters( 'gfpdf_blank_list_values', array(), $value, $field, $entry, $form, $class ); | |
$value = maybe_unserialize( $value ); | |
$headers = array_keys( $value[0] ); | |
$filled = array(); | |
$row_count = count( $fill ); | |
$r = 0; | |
do { | |
foreach ( $fill as $row => $cols ) { | |
foreach ( $cols as $col => $data ) { | |
$filled[ $row ][ $headers[ $col ] ] = $data ? $data : '▸'; | |
} | |
} | |
$r++; | |
} while ( $r <= $row_count ); | |
return $filled; | |
} | |
// An extra page break class – not needed since pagebreak exists | |
add_filter( 'gfpdf_field_html_value', function( $html, $value, $show_label, $label, $field, $form, $entry, $class ) { | |
/* Check if the field has a page break */ | |
if ( isset( $field->cssClass ) && strpos($field->cssClass, 'pdf-break-before') !== false ) { | |
/* | |
* We're using the new `GFPDF\\Helper\\Helper_QueryPath` class. | |
* It acts like jQuery for PHP which makes it very handy | |
*/ | |
$qp = new GFPDF\Helper\Helper_QueryPath(); | |
$wrapper = $qp->html5( $html ); | |
$css = $wrapper->find( '.gfpdf-field' )->css( 'page-break-before', 'always' ); | |
/* | |
* Query Path likes to make valid HTML from fragments like we passed, which means the HTML we need is inside '<html></html>' tags | |
* To fix this we'll extract the innerHTML of the <html> tag | |
*/ | |
return $wrapper->top( 'html' )->innerHTML5(); | |
} | |
return $html; | |
}, 10, 8 ); | |
// Add HTML/CSS for PDF subsection class `pdf-subsection` | |
add_filter( 'gfpdf_field_section_break_html', function( $html, $title, $description, $value, $field, $form, $entry, $class ) { | |
/* | |
* We're using the new `GFPDF\\Helper\\Helper_QueryPath` class. | |
* It acts like jQuery for PHP which makes it very handy | |
*/ | |
$qp = new GFPDF\Helper\Helper_QueryPath(); | |
$wrapper = $qp->html5( $html ); | |
$section = $wrapper->find( '.gfpdf-section-title' ); | |
if ( $section->hasClass('pdf-break-before') ) { | |
// Add our pagebreak CSS class | |
$wrapper->find( '.gfpdf-section-title' )->css( 'page-break-before', 'always' ); | |
} | |
if ( $section->hasClass('pdf-subsection') ) { | |
$style = array( | |
'border-top' => 'none', | |
'font-size' => '75%', | |
'margin-top' => '0', | |
'padding-left' => '15px', | |
'padding-bottom'=> '5px', | |
'border-left' => '2px solid #65574A', | |
'line-height' => '1' | |
); | |
/* Insert our description after the field label */ | |
$wrapper->find( '.gfpdf-section-title' )->css( $style ); | |
} | |
/* Check if the field has a description */ | |
if ( isset( $description ) && 0 < strlen( $description ) && $section->hasClass('pdf-description') ) { | |
$wrapper->find( '.gfpdf-section-title h3' )->after( '<div id="field-' . $field->id . '-desc" class="gfpdf-section-description gfpdf-field">' . $description . '</div>' ); | |
} | |
/* | |
* Query Path likes to make valid HTML from fragments like we passed, which means the HTML we need is inside '<html></html>' tags | |
* To fix this we'll extract the innerHTML of the <html> tag | |
*/ | |
return $wrapper->top( 'html' )->innerHTML5(); | |
}, 10, 8 ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment