Skip to main content
Version: v6

Helper Parameters to Assist PDF Template Development

Introduction

We've included a number of techniques to assist you when creating custom PDF templates. From viewing the $form_data array in an easy-to-read format, to direct paths to the PDF working directory.

URL Parameters

When viewing a Gravity PDF in your browser, there's a couple of URL parameters that will assist you. A URL parameter is simply a value added to your URL which Gravity PDF can read. It might look like this:

https://gravitypdf.com/pdf/60b71344af2fd/235/?name=value

The URL parameter is the name=value pair after the question mark.

data=1

caution

From Gravity PDF 6.4+ this helper parameter is only available when Debug Mode is enabled or the WordPress environment isn't set to production (WP_ENVIRONMENT_TYPE !== 'production').

Preview of the 'data' attribute

  • The data URL parameter is used to display the full output of the $form_data array in your browser. This is very useful when creating your PDF template using PHP only.
  • Usage: https://gravitypdf.com/pdf/60b71344af2fd/235/?data=1

html=1

caution

From Gravity PDF 6.4+ this helper parameter is only available when Debug Mode is enabled or the WordPress environment isn't set to production (WP_ENVIRONMENT_TYPE !== 'production').

Preview of the 'html' attribute

  • The HTML URL parameter is used to display the generated PDF template's HTML mark-up. What you'll see is the HTML that gets sent to the PDF software with all the PHP and merge tag formatting complete. This is very useful when debugging layout issues in your custom templates.
  • Usage: https://gravitypdf.com/pdf/60b71344af2fd/235/?html=1

template={name}

caution

From Gravity PDF 6.4+ this helper parameter can only contain the following characters: A-Z, a-z, 0-9, _, and -.

Preview of the 'template' attribute

  • The template URL parameter allows you to quickly swap between different PDF template files. The {name} value needs to be substituted for the PHP template name (with the .php extension removed).
  • Usage: https://gravitypdf.com/pdf/60b71344af2fd/235/?template=hello-world

Useful Paths and URLs

When creating custom PDF templates, it's often useful to include external CSS files and images, however you don't want to use a fixed path or URL. Instead, you should use a PHP constant or function that contains the correct path or URL.

__DIR__

  • This is a PHP magic constant that references the current absolute path to the current file. We use this when referencing images or other PHP files in the PDF template.
  • We recommend using absolute paths for images and stylesheets located on your web server.
  • Usage: <img src="<?php echo esc_attr( __DIR__ ); ?>/images/hello-world.png" width="400" />

PDF_TEMPLATE_LOCATION

PDF_TEMPLATE_URL_LOCATION

  • This is a constant that contains the URL to the PDF working directory or, for multisite installations, the URL to the individual multisite working directory. This is useful if you want to provide a link to a file on the host machine.
  • If you want to include images or stylesheets that are located on your web server, we recommend using __DIR__ where possible
  • Usage: <a href="<?php echo esc_url( PDF_TEMPLATE_URL_LOCATION ); ?>images/hello-world.png">View Sample</a>

wp_upload_dir()

  • A WordPress function that returns path and URL information about the uploads directory.
  • Usage: <?php $upload_dir = wp_upload_dir(); ?> <img src="<?php echo esc_attr( $upload_dir['basedir'] . '2015/04/hello-world.png' ); ?>" width="400" />

ABSPATH

  • A WordPress constant that references the directory in which WordPress is installed. This is less useful than the other methods (usually you don't store files in the root directory), but it's good to know about.
  • Usage: <img src="<?php echo esc_attr( ABSPATH ); ?>logo.png" width="400" />