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.
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>';
}
Create a Signed PDF URL From Another Form
A submitter can change any field's value, including Hidden, Administrative, and pre-populated fields. Unless a hard-to-guess value proves the original entry is theirs, they can download anyone's PDF.
This example creates a signed PDF URL for Form A when Form B is submitted. The URL is saved to a field in Form B, which can then be used in Form B's Confirmation and Notification messages. To link from Form A's own notification or confirmation, use the signed merge tag or [gravitypdf] shortcode instead.
- Form A (ID
456) has the PDF, and saves a secret code in Hidden field2. - Form B (ID
123) collects the entry ID in field6and the secret code in field4, then saves the signed URL in Hidden field8.
1. Generate the secret code in Form A
Add a Hidden field to Form A, then set its value when the form is submitted. This runs on the server, so the code isn't in the page, can't be shared by a page cache, and can't be chosen by the submitter. Entries submitted before you add this have no code, so they won't get a link.
// Change 456 to Form A's ID
add_action( 'gform_pre_submission_456', function() {
// Change input_2 to match the Hidden field's ID
$_POST['input_2'] = wp_generate_password( 32, false );
} );
2. Send the entry's owner to Form B
Enable Allow field to be populated dynamically on Form B's fields, using the parameter names entry_id and code. Then add a link to Form B in Form A's confirmation or notification:
<a href="https://example.com/form-b/?entry_id={entry_id}&code={Secret Code:2}">Get your PDF</a>
3. Save the signed URL in Form B
Add a Hidden field to Form B for the URL. When Form B is submitted, this loads the entry from field 6. If that entry belongs to Form A and the code in field 4 matches the one saved on it, the signed URL is saved to field 8 and the code is deleted. Otherwise, field 8 is left blank.
// Change 123 to Form B's ID
add_filter( 'gform_entry_post_save_123', function( $entry ) {
// Form A: the form with the PDF
$form_a_id = 456;
$form_a_pdf_id = '560f2ef799945';
$form_a_code_field = 2; // The field saving the secret code
// Form B: the form saving the signed URL
$form_b_entry_id_field = 6; // The field holding the entry ID
$form_b_code_field = 4; // The field holding the secret code
$form_b_url_field = 8; // The Hidden field saving the signed URL
$link_expires = '7 days'; // How long the signed URL lasts
$url = '';
$target = GFAPI::get_entry( (int) rgar( $entry, $form_b_entry_id_field ) );
$code = rgar( $entry, $form_b_code_field );
if (
method_exists( 'GPDFAPI', 'get_pdf_url' )
&& ! is_wp_error( $target )
&& (int) rgar( $target, 'form_id' ) === $form_a_id
&& $code !== ''
&& hash_equals( (string) rgar( $target, $form_a_code_field ), $code )
) {
$url = GPDFAPI::get_pdf_url(
$target['id'],
$form_a_pdf_id,
[
'signed' => true,
'expires' => $link_expires,
]
);
// get_pdf_url() returns a WP_Error if the PDF is inactive or its conditional logic fails
if ( is_wp_error( $url ) ) {
$url = '';
} else {
// Each code only works once
GFAPI::update_entry_field( $target['id'], $form_a_code_field, '' );
}
}
// Always overwrite the field, so a submitted value is never used
GFAPI::update_entry_field( $entry['id'], $form_b_url_field, $url );
$entry[ $form_b_url_field ] = $url;
return $entry;
} );
Use the field's merge tag in Form B's confirmation or notification:
<a href="{Signed URL:8}">View your PDF</a>
The link from step 2 only works once, so the owner can't get a new URL after this one expires.
Source Code
This method is located in api.php.