PDF for GravityView API – create_multiple_entry_pdf()
Description
Generates a View's Multiple Entries PDF (the same PDF the Multiple Entry PDF widget links to). It either saves the PDF to disk and returns the path, or streams it to the current user's browser.
There's no entry ID parameter. The View selects its entries the same way it does on the front end. Use $args to search, sort and page them.
Call this method after the WordPress init hook has run. Called earlier, it returns a WP_Error with the code called_too_early_in_hook_cycle.
This method doesn't check permissions. A saved PDF is deleted from the temporary directory once it's 12 hours old, so move it somewhere permanent to keep it. See API Rate Limits, Caching and Permissions.
Version
This method was introduced in PDF for GravityView 2.0.
Parameters
$view_id | integer
- The GravityView ID (the View's Post ID).
- The View must have a Multiple Entries layout and a form.
$output_type | string
- Valid values:
downloaddisplaysave savewrites the PDF to disk and returns its path.downloadsends it to the browser as a file download, anddisplayopens it in the browser. Both end the current PHP request.- Defaults to
save.
$args | array | null
- The search, sort and paging to apply. The keys match the query arguments a View's own URL uses:
sort: an array of field ID =>ASC,DESCorRAND. Add more keys to sort by several fields. Only applied when the View has Enable sorting by column turned on (DataTables Views always accept it). Otherwise the View's configured sort is used.dir: the sort direction for the older single-field form ('sort' => '3', 'dir' => 'DESC'). Prefer the array form ofsort.pagenum: the page of entries to include.page_size: the number of entries per page. Takes priority overentries.mode:allorany. How multiple search fields combine.gv_search: the Search Bar's keyword box.gv_startandgv_end: the Search Bar's date range.gv_by: the Search Bar's "Submitted by" field.gv_id: an entry ID.filter_*: a Search Bar field, such asfilter_5for field 5 orfilter_2_3for input 2.3.letter: the A-Z Filters extension's selected letter.lat,long,unit,accuracy,is_currentandaddress_search: the Maps add-on's radius search.gv_search_view: the View ID a search belongs to.entries: how many entries the PDF includes.all,default(the View's own page size) or a number. Overrides the widget's "Number of Entries to Display" setting.
- Any other key returns a
WP_Errorwith the codeinvalid_request_args. - An array (even an empty one) is the complete search, sort and paging for the PDF. Nothing is taken from the current request.
nulluses the current request's query string, so a call made while a visitor is on a View page reflects their search. From WP-CLI, cron or a REST handler,nullgives you the View's configured sort and filters.- Defaults to
null.
If you don't pass page_size or entries, the View sets the count. The entry limit applies either way. See How Many Entries Are Included.
Return
string | WP_Error
- The full path to the PDF on success, or a
WP_Erroron failure. Error codes includeinvalid_view_id,view_has_no_multiple_entry_layout,invalid_view_form,invalid_request_argsandpdf_generation_error. - With
downloadordisplay, the PDF is streamed to the browser and PHP exits.
Usage
Email a PDF of every entry where field 5 is "Approved" from a scheduled task:
add_action( 'my_weekly_report', function() {
$temporary_pdf_path = \PDFGV_API::create_multiple_entry_pdf( 120, 'save', [
'entries' => 'all',
'filter_5' => 'Approved',
] );
if ( is_wp_error( $temporary_pdf_path ) ) {
return;
}
wp_mail( get_option( 'admin_email' ), 'Weekly report', 'The report is attached.', '', [ $temporary_pdf_path ] );
unlink( $temporary_pdf_path );
} );
Open the PDF in the browser when a user who can view entries adds ?export to a View page's URL. $args is null, so the PDF uses the search already applied on the page:
add_action( 'template_redirect', function() {
if ( ! isset( $_GET['export'] ) || ! current_user_can( 'gravityview_view_entries' ) ) {
return;
}
$result = \PDFGV_API::create_multiple_entry_pdf( 120, 'display' );
if ( is_wp_error( $result ) ) {
wp_die( esc_html( $result->get_error_message() ) );
}
} );
For a single entry's PDF, use create_single_entry_pdf().
Source Code
This method is located in api.php of the PDF for GravityView extension.