Gravity PDF API – get_pdf_url()
Description
This method returns the URL to view or download an entry's PDF. Pass signed to get a signed URL that opens the PDF without logging in.
A URL is only returned when the PDF is active and its conditional logic passes for the entry. It doesn't check whether the current user is allowed to view the PDF.
warning
Anyone with a signed URL can open the PDF until it expires. Only share a signed URL with someone who should see the entry.
Version
This method was introduced in Gravity PDF 6.18.0.
Parameters
$entry_id | integer
- The Gravity Forms entry ID
$pdf_id | string
- The PDF internal identifier (found in the
pidURL parameter when viewing individual PDF settings in the admin area)
$args | array
- Optional. Any of:
download(boolean): Download the PDF instead of viewing it in the browser. Defaultfalse.print(boolean): Open the print dialog when the PDF is viewed. Defaultfalse.signed(boolean): Sign the URL so it opens without logging in. Defaultfalse.expires(string): How long a signed URL lasts, as a relative date accepted by PHP's strtotime(), like1 week. Default is the Logged Out Timeout. It can't go above the signed URL cap, 1 year by default.
Return
string | WP_Error
- The URL on success, otherwise a WP_Error object on failure
The URL isn't escaped, so escape it for where you output it, like esc_url() in HTML.
The error code will be:
invalid_entrywhen the entry ID doesn't existinvalid_pdf_settingwhen the PDF ID can't be found on the entry's forminactivewhen the PDF is turned offconditional_logicwhen the PDF's conditional logic doesn't pass for the entry
Usage
Get the URL to view a PDF:
add_action( 'init', function() {
if ( class_exists( 'GPDFAPI' ) ) {
$url = GPDFAPI::get_pdf_url( 25, '56c14c955d989' );
if ( is_wp_error( $url ) ) {
return;
}
/* Safe to use $url */
}
} );
Get a signed download URL that lasts 1 week:
$url = GPDFAPI::get_pdf_url(
25,
'56c14c955d989',
[
'download' => true,
'signed' => true,
'expires' => '1 week',
]
);
if ( ! is_wp_error( $url ) ) {
echo '<a href="' . esc_url( $url ) . '">Download your PDF</a>';
}
Source Code
This method is located in api.php.