PDF for GravityView Hooks – gfpdf_gv_directory_widget_html
Description
Print a GravityView widget's content in the Multiple Entries PDF.
Most widgets are controls for a live page (search bars, pagination, sorting, page size), so the PDF prints nothing for a widget unless a listener on this filter returns HTML for it. That's why a widget in your View's header or footer can be missing from the PDF. The only widget printed by default is the GravityKit Maps Multiple Entries Map widget, drawn as a static map image (see Maps in PDFs).
The filter runs once for each widget in the View's header and footer widget areas. Header widgets print after the page title and before the entries. Footer widgets print after the last entry. Return an empty string to print nothing.
Your HTML is added to the PDF as is, so escape any values from entries or settings. If $html isn't empty, another listener (such as the Maps widget) has already drawn the widget, so return it unchanged.
Version
This hook was introduced in PDF for GravityView 2.0.
Arguments
The following arguments are passed to functions that use this hook:
$html | string
- The HTML to print for this widget. An empty string prints nothing.
- The default value is
''.
$widget | \GV\Widget
- The widget being rendered.
$widget->get_widget_id()is its type (such ascustom_content,page_infoormap), and$widget->configuration->get( 'setting' )reads its saved settings.
$view | \GV\View
- The View the PDF is being generated for.
$entries | \GV\Entry_Collection
- The entries included in the PDF.
$zone | string
- The widget area the widget is in:
headerorfooter.
Usage
Print GravityView's Custom Content widget in the PDF:
add_filter( 'gfpdf_gv_directory_widget_html', function( $html, $widget, $view, $entries, $zone ) {
if ( $html !== '' || $widget->get_widget_id() !== 'custom_content' ) {
return $html;
}
$content = (string) $widget->configuration->get( 'content' );
if ( $widget->configuration->get( 'wpautop' ) ) {
$content = wpautop( $content );
}
return '<div class="gv-widget-custom-content">' . wp_kses_post( do_shortcode( $content ) ) . '</div>';
}, 10, 5 );
Replace the Show Pagination Info widget with a line saying how many entries the PDF contains:
add_filter( 'gfpdf_gv_directory_widget_html', function( $html, $widget, $view, $entries, $zone ) {
if ( $html !== '' || $widget->get_widget_id() !== 'page_info' ) {
return $html;
}
return sprintf( '<p class="pdf-entry-count">%d entries</p>', $entries->count() );
}, 10, 5 );
Source Code
This filter is located in src/Pdf/DirectoryWriter.php of the PDF for GravityView extension.