Gravity PDF Shortcode

Below, you’ll find a detailed guide on how to add the Gravity PDF Shortcode to your WordPress website, including its parameters, examples, and PHP function code. Additionally, we’ll assist you with common issues that might cause the Gravity PDF Plugin shortcode not to show or not to work correctly.

Before starting, here is an overview of the Gravity PDF Plugin and the shortcodes it provides:

Plugin Icon
Gravity PDF

"Gravity PDF is a unique plugin that seamlessly integrates with Gravity Forms to generate, email and download PDF documents. It is best for creating digital receipts, invoices, and more."

★★★★☆ (307) Active Installs: 50000+ Tested with: 6.3.2 PHP Version: 7.3
Included Shortcodes:
  • [gravitypdf]

Gravity PDF [gravitypdf] Shortcode

The Gravity Forms PDF Extended shortcode executes various actions based on the attributes passed. It validates the PDF, generates a URL for it, signs the URL for direct access until expiry, and generates shortcode markup.

Shortcode: [gravitypdf]

Parameters

Here is a list of all possible gravitypdf shortcode parameters and attributes:

  • id – Unique identifier of the PDF
  • text – Text displayed on the download button
  • type – Set to ‘download’ to trigger a PDF download
  • signed – If set, signs the URL for secure access
  • expires – Defines when the signed URL expires
  • class – Custom class for the download link
  • classes – Additional custom classes for the link
  • entry – Defines the entry ID for the PDF
  • print – If set, triggers the print dialogue
  • raw – If set, returns the raw PDF URL

Examples and Usage

Basic example – Display a PDF download link with the ID of the PDF and the entry ID.

[gravitypdf id="5" entry="3" /]

Advanced examples

Display a PDF download link with the ID of the PDF and the entry ID, with custom download text and a specific CSS class.

[gravitypdf id="5" entry="3" text="Get your PDF" class="custom-download-link" /]

Display a PDF download link with the ID of the PDF and the entry ID, with the type set to ‘view’ so the PDF opens in the browser instead of downloading, and the URL signed for security.

[gravitypdf id="5" entry="3" type="view" signed="true" /]

Display a raw URL of the PDF with the ID of the PDF and the entry ID, with the URL signed for security and set to expire in 2 days.

[gravitypdf id="5" entry="3" raw="true" signed="true" expires="2 days" /]

PHP Function Code

In case you have difficulties debugging what causing issues with [gravitypdf] shortcode, check below the related PHP functions code.

Shortcode line:

add_shortcode( 'gravitypdf', [ $this->model, 'process' ] );

Shortcode PHP function:

function process( $attributes ) {
		$controller = $this->getController();

		$shortcode_error_messages_enabled = $this->options->get_option( 'debug_mode', 'No' ) === 'Yes';
		$has_view_permissions             = $shortcode_error_messages_enabled && $this->gform->has_capability( 'gravityforms_view_entries' );

		/* merge in any missing defaults */
		$attributes = shortcode_atts(
			[
				'id'      => '',
				'text'    => 'Download PDF',
				'type'    => 'download',
				'signed'  => '',
				'expires' => '',
				'class'   => 'gravitypdf-download-link',
				'classes' => '',
				'entry'   => '',
				'print'   => '',
				'raw'     => '',
			],
			$attributes,
			static::SHORTCODE
		);

		/* See https://docs.gravitypdf.com/v6/developers/filters/gfpdf_gravityforms_shortcode_attributes/ for more information about this filter */
		$attributes = apply_filters( 'gfpdf_gravityforms_shortcode_attributes', $attributes );

		try {
			$attributes['entry'] = $this->get_entry_id_if_empty( $attributes['entry'] );

			/* Do PDF validation */
			$this->get_pdf_config( $attributes['entry'], $attributes['id'] );

			$pdf               = GPDFAPI::get_mvc_class( 'Model_PDF' );
			$download          = $attributes['type'] === 'download';
			$print             = ! empty( $attributes['print'] );
			$raw               = ! empty( $attributes['raw'] );
			$attributes['url'] = $pdf->get_pdf_url( $attributes['id'], $attributes['entry'], $download, $print );

			/* Sign the URL to allow direct access to the PDF until it expires */
			if ( ! empty( $attributes['signed'] ) ) {
				$attributes['url'] = $this->url_signer->sign( $attributes['url'], $attributes['expires'] );
			}

			$this->log->notice( 'Generating Shortcode Markup', [ 'attr' => $attributes ] );

			if ( $raw ) {
				return $attributes['url'];
			}

			return $controller->view->display_gravitypdf_shortcode( $attributes );

		} catch ( GravityPdfShortcodeEntryIdException $e ) {
			return $has_view_permissions ? $controller->view->no_entry_id() : '';
		} catch ( GravityPdfShortcodePdfConfigNotFoundException $e ) {
			return $has_view_permissions ? $controller->view->invalid_pdf_config() : '';
		} catch ( GravityPdfShortcodePdfInactiveException $e ) {
			return $has_view_permissions ? $controller->view->pdf_not_active() : '';
		} catch ( GravityPdfShortcodePdfConditionalLogicFailedException $e ) {
			return $has_view_permissions ? $controller->view->conditional_logic_not_met() : '';
		} catch ( Exception $e ) {
			return $has_view_permissions ? $e->getMessage() : '';
		}
	}

Code file location:

gravity-forms-pdf-extended/gravity-forms-pdf-extended/src/Controller/Controller_Shortcodes.php

Conclusion

Now that you’ve learned how to embed the Gravity PDF Plugin shortcode, understood the parameters, and seen code examples, it’s easy to use and debug any issue that might cause it to ‘not work’. If you still have difficulties with it, don’t hesitate to leave a comment below.

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *