PDF for GravityView API – create_single_entry_pdf()
Description
Generates a View's Single Entry PDF. It either saves the PDF to disk and returns the path, or streams it to the current user's browser.
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 0.8.
Parameters
$view_id | integer
- The GravityView ID (the View's Post ID).
$entry_id | string | integer
- A Gravity Forms entry ID from the form the View uses.
- For a View using GravityView's Multiple Forms extension, pass a comma-separated string of entry IDs. It must be the same set of IDs the View joins, not an arbitrary combination.
- Any other type, such as an array, returns a
WP_Errorwith the codeinvalid_entry_id.
$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.- Any other value returns a
WP_Errorwith the codeinvalid_output_type. Added in version 2.0.1. - Defaults to
save.
Return
string | WP_Error
- The full path to the PDF on success, or a
WP_Erroron failure. Error codes includeinvalid_view_id,invalid_entry_id,invalid_entry_id_for_view,invalid_output_typeandpdf_generation_error. - With
downloadordisplay, the PDF is streamed to the browser and PHP exits.
Usage
Keep a permanent copy of an entry's PDF when the entry is approved in GravityView. Files in the uploads folder can be downloaded by anyone who knows the URL, so use a folder outside your web root if the PDFs are private:
add_action( 'gravityview/approve_entries/approved', function( $entry_id ) {
$temporary_pdf_path = \PDFGV_API::create_single_entry_pdf( 120, $entry_id );
if ( is_wp_error( $temporary_pdf_path ) ) {
return;
}
$archive = wp_upload_dir()['basedir'] . '/approved-pdfs';
wp_mkdir_p( $archive );
rename( $temporary_pdf_path, $archive . '/entry-' . absint( $entry_id ) . '.pdf' );
} );
Stream the PDF to the current user with display or download. This admin_post_ handler serves /wp-admin/admin-post.php?action=my_entry_pdf&entry=20 to logged-in users who can view entries:
add_action( 'admin_post_my_entry_pdf', function() {
if ( ! current_user_can( 'gravityview_view_entries' ) ) {
wp_die( 'You are not allowed to view this PDF.' );
}
$result = \PDFGV_API::create_single_entry_pdf( 120, absint( $_GET['entry'] ?? 0 ), 'display' );
if ( is_wp_error( $result ) ) {
wp_die( esc_html( $result->get_error_message() ) );
}
} );
On a View that joins forms with the Multiple Forms extension, pass the joined entry IDs as a comma-separated string:
$temporary_pdf_path = \PDFGV_API::create_single_entry_pdf( 1025, '20,32,52' );
For a PDF of a View's Multiple Entries layout, use create_multiple_entry_pdf().
Source Code
This method is located in api.php of the PDF for GravityView extension.