PDF for GravityView Hooks – gfpdf_gv_pdf_themes
Description
The list of PDF stylesheets used for GravityView's Theme setting. Add to it to style PDFs for a GravityView theme you've registered, or replace the Vantage entry with a subclass that adjusts it.
GravityView's front-end theme CSS uses features the PDF engine doesn't support (CSS custom properties, flexbox and grid), so PDF for GravityView keeps a separate PDF stylesheet for each theme it supports. It ships one, for Vantage. A View set to the Legacy theme gets the plain PDF styling. See View Themes for examples.
Each theme is a PHP class that implements GFPDF\Plugins\PdfForGravityView\Pdf\Theme\ThemeInterface, which has two methods:
slug()returns the Theme setting value the stylesheet is for, such asvantage.css( $view )returns the CSS to add to the PDF.
Most themes should extend GFPDF\Plugins\PdfForGravityView\Pdf\Theme\AbstractTheme instead. It implements css() for you: it reads the colors saved in the View's appearance settings (or GravityView's defaults) and passes them to styles( array $palette ), which is the only other method you write. The palette has these roles, each a hex color: text, link, surface, border, divider, label, muted, header_bg, header_text and header_rule. Override tokens() to add your own roles.
The first theme whose slug() matches the View's Theme setting is used. Its CSS is added on gfpdf_gv_template_styles at priority 20, before the View's Additional CSS.
A theme applies the same CSS to the Single Entry and Multiple Entries PDFs. To style them differently, use gfpdf_gv_template_styles and its $context argument.
Version
This hook was introduced in PDF for GravityView 2.0.
Arguments
The following arguments are passed to functions that use this hook:
$themes | array
- The theme objects, each implementing
ThemeInterface. Entries that don't implement it are skipped. - The default value is
[ new \GFPDF\Plugins\PdfForGravityView\Pdf\Theme\Vantage() ].
Usage
Add a PDF stylesheet for a my-brand theme registered with GravityView. Only slug() and styles() are needed:
use GFPDF\Plugins\PdfForGravityView\Pdf\Theme\AbstractTheme;
add_action( 'init', function() {
if ( ! class_exists( AbstractTheme::class ) ) {
return;
}
class My_Brand_Pdf_Theme extends AbstractTheme {
public function slug() {
return 'my-brand';
}
protected function styles( array $palette ) {
return "
a { color: {$palette['link']}; }
.gv-field-label, td.label, div.label { color: {$palette['label']}; font-weight: bold; }
table.row-separator, .row-separator.even, .row-separator.odd { border-bottom: 1px solid {$palette['divider']}; }
table.gv-table th { background-color: {$palette['header_bg']}; color: {$palette['header_text']}; }
";
}
}
add_filter( 'gfpdf_gv_pdf_themes', function( $themes ) {
$themes[] = new My_Brand_Pdf_Theme();
return $themes;
} );
} );
Extend the Vantage styling. The first matching theme wins, so add the subclass to the front of the list:
use GFPDF\Plugins\PdfForGravityView\Pdf\Theme\Vantage;
add_action( 'init', function() {
if ( ! class_exists( Vantage::class ) ) {
return;
}
class My_Vantage_Pdf_Theme extends Vantage {
protected function styles( array $palette ) {
return parent::styles( $palette ) . "
.gv-list-view, .gv-map-view { border-radius: 0; box-shadow: none; }
";
}
}
add_filter( 'gfpdf_gv_pdf_themes', function( $themes ) {
array_unshift( $themes, new My_Vantage_Pdf_Theme() );
return $themes;
} );
} );
Source Code
This filter is located in src/Pdf/Theme/ThemeStyles.php of the PDF for GravityView extension. ThemeInterface, AbstractTheme and Vantage are in the same src/Pdf/Theme/ directory.