Skip to main content

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 pid URL 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. Default false.
    • print (boolean): Open the print dialog when the PDF is viewed. Default false.
    • signed (boolean): Sign the URL so it opens without logging in. Default false.
    • expires (string): How long a signed URL lasts, as a relative date accepted by PHP's strtotime(), like 1 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 isn't escaped, so escape it for where you output it, like esc_url() in HTML.

The error code will be:

  • invalid_entry when the entry ID doesn't exist
  • invalid_pdf_setting when the PDF ID can't be found on the entry's form
  • inactive when the PDF is turned off
  • conditional_logic when 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.