Skip to main content

PDF for GravityView Hooks – gfpdf_gv_template_styles

Description​

Adds your own CSS to a PDF. Echo the CSS from your function, without a <style> tag, and it's included in the PDF's stylesheet.

The action fires for both the Single Entry PDF and the Multiple Entries PDF. The third argument, $context, says which one is being generated (single or directory). Check it and return early to style only one.

Two aliases target a single View or View layout:

Alias

  • gfpdf_gv_template_styles_{$view_id}: replace {$view_id} with a View ID.
  • gfpdf_gv_template_styles_{$layout}: replace {$layout} with table, list, map, diy or layout_builder.

Your CSS goes near the start of the PDF's stylesheet. PDF for GravityView adds its shared field styles on this action at priority 10 and the View's theme at priority 20 (see gfpdf_gv_pdf_themes). Use a priority above 20 to come after the theme. The two aliases always run after it. The layout's own CSS follows, and the View's Additional CSS setting is last, so it always wins.

info

In 1.x this action ran at the end of the stylesheet, after the Additional CSS. It now runs at the start. If a rule stopped working, the layout's own CSS is overriding it. Add the tag name to your selector (td.label rather than .label), or move the rule to the View's Additional CSS setting.

See the CSS section for the classes you can target.

Version​

This hook was introduced in PDF for GravityView 0.1. The {$view_id} alias was added in 0.6.

In 2.0 it began firing for the Multiple Entries PDF, and the $context argument was added.

Arguments​

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

$view | \GV\View​

  • The View the PDF is being generated for.

$view_entry | \GV\Entry or \GV\Entry_Collection​

  • A \GV\Entry for a Single Entry PDF.
  • A \GV\Entry_Collection for a Multiple Entries PDF.
  • Check $context before treating it as one or the other.

$context | string​

  • Which PDF is being generated: single for the Single Entry PDF, or directory for the Multiple Entries PDF.
  • Code written before 2.0 that accepts two arguments keeps working, and runs for both PDFs.
  • A 1.x template override in the PDF Working Directory only passes $view and $view_entry. Give $context a default of 'single', as in the examples below, or PHP throws an error when that override runs.

Usage​

Add a large header and footer margin to all PDFs:

add_action( 'gfpdf_gv_template_styles', function( $view, $view_entry ) {
?>
@page {
margin-header: 2in;
margin-footer: 2in;
}
<?php
}, 10, 2 );

Shrink the table text when a Multiple Entries PDF holds more than 50 entries, to fit more on each page:

add_action( 'gfpdf_gv_template_styles', function( $view, $view_entry, $context = 'single' ) {
if ( $context !== 'directory' || $view_entry->count() <= 50 ) {
return;
}
?>
table.gv-table th,
table.gv-table td {
font-size: 8pt;
}
<?php
}, 10, 3 );

Add CSS to the Single Entry PDF of any Table Layout View, but not its Multiple Entries PDF:

add_action( 'gfpdf_gv_template_styles_table', function( $view, $view_entry, $context = 'single' ) {
if ( $context !== 'single' ) {
return;
}
?>
td.label,
div.label {
color: #555555;
}
<?php
}, 10, 3 );

Source Code​

This action is located in src/Pdf/Layout/TemplateStyles.php of the PDF for GravityView extension, and is run by every template in the /src/Pdf/Templates/ directory.