Skip to main content

PDF for GravityView Hooks – gfpdf_gv_pdf_settings

Description​

Change the settings a Single Entry PDF is built with. Use it when a particular entry, user or request needs different settings from the rest of the View, such as landscape for a wide entry or a header naming who downloaded the PDF. The filter receives the View's settings just before the PDF is generated. The saved View settings aren't changed.

It runs for every Single Entry PDF: the PDF link on a View, the Share PDF form, form notifications, the [gv_pdf_link] shortcode and the PDFGV_API. For the Multiple Entries PDF, use gfpdf_gv_pdf_settings_multiple.

The render cache accounts for the settings you return, so a PDF built with different settings is never served from another's cache entry.

Version​

This hook was introduced in PDF for GravityView 2.0.

Arguments​

The following arguments are passed to functions that use this hook:

$settings | array​

  • The PDF settings from the View's Single Entry PDF tab. The keys you're most likely to change are:
    • filename: the name without .pdf. Merge tags are processed after this filter runs. To change the final name, gfpdf_gv_pdf_filename is usually easier.
    • pdf_size: the paper size key, such as A4, A5 or LETTER.
    • orientation: portrait or landscape.
    • margin: CSS-style shorthand, such as 20mm or 1in 1.5in.
    • font: the font key, such as dejavusanscondensed.
    • font_size: a number, in points.
    • font_colour: a hex color, such as #222222.
    • header and footer: HTML. Merge tags are processed.
    • background_color: a hex color.
    • background_image: the image URL.
    • show_title: true to print the View's title at the top of the PDF.
    • show_labels: false to hide field labels.
    • additional_css: CSS added after the PDF's own styles.
    • template: the PDF template file name without .php, such as gravityview-single-entry-table-template.
  • Leave the other keys (id, label, format, security) unchanged.

$view | \GV\View​

  • The View the PDF is being generated for.

$entry | \GV\Entry​

  • The entry being rendered. If the View joins forms with GravityView's Multiple Forms extension, it's the joined entry.

Usage​

Print an entry's PDF in landscape when its field 7 (a "Page orientation" choice on the form) says so:

add_filter( 'gfpdf_gv_pdf_settings', function( $settings, $view, $entry ) {
if ( rgar( $entry->as_entry(), '7' ) === 'Landscape' ) {
$settings['orientation'] = 'landscape';
}

return $settings;
}, 10, 3 );

Add a line to the header saying who downloaded the PDF and when:

add_filter( 'gfpdf_gv_pdf_settings', function( $settings, $view, $entry ) {
$user = wp_get_current_user();

if ( ! $user->exists() ) {
return $settings;
}

$settings['header'] .= sprintf(
'<p style="font-size: 8pt; text-align: right;">Downloaded by %s on %s</p>',
esc_html( $user->display_name ),
esc_html( wp_date( get_option( 'date_format' ) ) )
);

return $settings;
}, 10, 3 );

Switch to Letter paper when the PDF link carries &paper=letter:

add_filter( 'gfpdf_gv_pdf_settings', function( $settings, $view, $entry ) {
if ( isset( $_GET['paper'] ) && $_GET['paper'] === 'letter' ) {
$settings['pdf_size'] = 'LETTER';
}

return $settings;
}, 10, 3 );

See Single Entry PDF Settings for what each setting does.

Source Code​

This filter is located in src/Pdf/Renderer.php of the PDF for GravityView extension.