### Install Gravity PDF Extensions with Composer
Source: https://docs.gravitypdf.com/v6/extensions/installing-upgrading-extensions
These are example Composer commands to install various Gravity PDF extensions. After configuring the repository and authentication, run these commands in your project's CLI to add the desired extensions. Ensure you have the correct license keys for each extension if installing multiple individually.
```bash
composer require gravitypdf/gravity-pdf-bulk-generator
composer require gravitypdf/gravity-pdf-core-booster
composer require gravitypdf/gravity-pdf-enhanced-download
composer require gravitypdf/gravity-pdf-previewer
composer require gravitypdf/gravity-pdf-reports-for-gfchart
composer require gravitypdf/gravity-pdf-watermark
composer require gravitypdf/pdf-for-gravityview
```
--------------------------------
### PHP: Set up and tear down Gravity PDF template files
Source: https://docs.gravitypdf.com/v6/developers/template-configuration-and-image
The setUp() method is used to install custom fonts when a template is installed, and the tearDown() method cleans up additional template-related files when the template is deleted. This example demonstrates adding a custom font and removing an installation directory.
```php
'Fira Sans',
'regular' => __DIR__ . '/../install/Hello_World/font-fira-sans/FiraSans-Regular.ttf',
'italics' => __DIR__ . '/../install/Hello_World/font-fira-sans/FiraSans-Italic.ttf',
'bold' => __DIR__ . '/../install/Hello_World/font-fira-sans/FiraSans-SemiBold.ttf',
'bolditalics' => __DIR__ . '/../install/Hello_World/font-fira-sans/FiraSans-SemiBoldItalic.ttf',
];
\GPDFAPI::add_pdf_font( $font_data );
}
/**
* Runs when the template is deleted via the PDF Template Manager
*
* @Internal Great for cleaning up any additional directories
*/
public function tearDown() {
$misc = \GPDFAPI::get_misc_class();
/* Cleanup files */
$misc->rmdir( __DIR__ . '/../install/Hello_World' );
}
/**
* Return the templates configuration structure which controls what extra fields will be shown in the "Template" section when configuring a form's PDF.
*
* @return array The array, split into core components and custom fields
*/
public function configuration() {
return [
/* Enable core fields */
'core' => [
],
/* Create custom fields to control the look and feel of a template */
'fields' => [
],
];
}
}
```
--------------------------------
### Diagnose Composer 401 Errors with cURL
Source: https://docs.gravitypdf.com/v6/extensions/installing-upgrading-extensions
This snippet demonstrates how to use cURL to test Composer authentication. Replace the example credentials with your actual username and password from your auth.json file to get a detailed error message from the API, which can help identify license activation problems.
```shell
# Replace the username and password with the values in your auth.json file
curl --user "https://example.com:NpMmzRwVpkudnXQRfuEdk7wEi2W88Fz6" https://composer.gravitypdf.com/gravity-pdf-bulk-generator-2.0.0.zip
```
--------------------------------
### Example composer.json for WordPress Project with Gravity PDF
Source: https://docs.gravitypdf.com/v6/users/installation
This example demonstrates a complete composer.json file for a WordPress project that includes the Gravity PDF plugin. It configures repositories (including wpackagist and Gravity PDF's own), allows plugins like composer/installers, and specifies PHP and plugin requirements.
```json
{
"name": "acme/wordpress-website",
"type": "project",
"config": {
"allow-plugins": {
"composer/installers": true
}
},
"repositories": [
{
"type": "composer",
"url": "https://wpackagist.org",
"only": [
"wpackagist-plugin/*",
"wpackagist-theme/*"
]
},
{
"type": "composer",
"url": "https://composer.gravitypdf.com",
"only": [
"gravitypdf/*"
]
}
],
"require": {
"php": "^8.3",
"wpackagist-plugin/two-factor": "^0.9",
"gravitypdf/gravity-pdf": "^6.12"
}
}
```
--------------------------------
### Hook into Gravity PDF Plugin Installation (PHP)
Source: https://docs.gravitypdf.com/v6/developers/actions/gfpdf_plugin_installed
This action hook executes only when the Gravity PDF plugin is activated for the first time. It's suitable for running one-time setup routines or checks that should only occur upon initial installation. No specific input parameters are provided.
```php
add_action( 'gfpdf_plugin_installed', function() {
/* Run any one-time checks you need to */
} );
```
--------------------------------
### Example of Invoice Due Date Calculation
Source: https://docs.gravitypdf.com/v6/templates/invoice-vertex
Illustrates how the 'Invoice Due Date' setting calculates the due date based on the form submission date and a specified number of days. This is a conceptual example.
```text
If form submission date is `2024-11-29` and due date setting is `7` days,
the invoice would be due on `2024-12-06`.
```
--------------------------------
### Get WordPress Installation Path (ABSPATH)
Source: https://docs.gravitypdf.com/v6/developers/helper-parameters
The `ABSPATH` WordPress constant represents the root directory of your WordPress installation. While less commonly used for template assets compared to other methods, it can be utilized to reference files located in the WordPress root.
```php
```
--------------------------------
### PHP PDF Template Header Example
Source: https://docs.gravitypdf.com/v6/developers/first-custom-pdf
This is a valid PDF template header example for Gravity PDF. It includes essential metadata such as Template Name, Version, Description, Author, Group, License, and Required PDF Version, along with optional Tags for better searchability within the PDF Template Manager.
```php
/**
* Template Name: Zadani
* Version: 1.2
* Description: A minimalist business-style template that will generate a well-spaced document great for printing. Through the Template tab, you can control the PDF header and footer, change the background color or image, and show or hide the form title, page names, HTML fields and the Section Break descriptions.
* Author: Gravity PDF
* Author URI: https://gravitypdf.com
* Group: Core
* License: GPLv2
* Required PDF Version: 4.0
* Tags: Header, Footer, Background, Optional HTML Fields, Optional Page Fields, Container Background Color
*/
```
--------------------------------
### Ansible Deployment Hook for Gravity PDF Core Fonts
Source: https://docs.gravitypdf.com/v6/users/core-pdf-fonts
This Ansible task automates the download and synchronization of Gravity PDF's Core Fonts during a Trellis deployment. It clones the font repository and uses rsync to move the files to the appropriate directory within the web application. Ensure the `dest` path is correctly configured for your specific project setup.
```yaml
---
# Download and sync core Gravity PDF fonts
- git:
repo: 'https://github.com/GravityPDF/mpdf-core-fonts'
dest: "{{ deploy_helper.shared_path }}/mpdf-core-fonts"
version: master
force: yes
- synchronize:
src: "{{ deploy_helper.shared_path }}/mpdf-core-fonts/"
dest: "{{ deploy_helper.new_release_path }}/web/app/PDF-Templates/fonts"
rsync_opts:
- "--exclude=.git"
delegate_to: "{{ inventory_hostname }}"
```
--------------------------------
### Gravity PDF Shortcode Examples
Source: https://docs.gravitypdf.com/v6/users/shortcodes-and-mergetags
Examples of using the Gravity PDF shortcode with and without an entry ID. The entry ID can be specified directly or via a URL parameter. If no entry ID is provided, it can be inferred from URL parameters like 'entry' or 'lid'.
```shortcode
[gravitypdf id="560f2ef799945" entry="250"]
[gravitypdf id="560f2ef799945"]
```
--------------------------------
### Install Gravity PDF using WP-CLI
Source: https://docs.gravitypdf.com/v6/users/installation
This WP-CLI command installs and activates the Gravity PDF plugin directly from a provided URL. It's a convenient method for developers who manage their WordPress sites using the command line interface.
```bash
wp plugin install https://gravitypdf.com/download-gravity-pdf --activate
```
--------------------------------
### Install Gravity PDF Plugin using Composer
Source: https://docs.gravitypdf.com/v6/users/installation
This command installs the Gravity PDF plugin into your project using Composer. It assumes the Composer repository has already been added to your composer.json file. This method is recommended for developers for streamlined dependency management.
```bash
composer require gravitypdf/gravity-pdf
```
--------------------------------
### Trellis: Override deploy_build_after Hook
Source: https://docs.gravitypdf.com/v6/users/core-pdf-fonts
This configuration snippet for Trellis shows how to override the default `deploy_build_after` hook to include a custom deployment script. This is necessary for integrating custom deployment tasks, such as installing Gravity PDF Core Fonts, into the deployment process. Add the custom hook file to the `deploy_build_after` list.
```yaml
# Overriding a hook that Trellis already uses by default
default_deploy_build_after:
- "{{ playbook_dir }}/roles/deploy/hooks/build-after.yml"
- "{{ playbook_dir }}/deploy-hooks/build-after.yml" # add this line
```
--------------------------------
### Configure Composer Authentication for Gravity PDF
Source: https://docs.gravitypdf.com/v6/extensions/installing-upgrading-extensions
This configuration is used to set up authentication for the Gravity PDF Composer repository. It requires a username (your site URL) and a password (your license key) to ensure only valid customers can install extensions. Replace placeholders with your actual site URL and license key.
```json
{
"http-basic": {
"composer.gravitypdf.com": {
"username": "https://{WORDPRESS_HOME_URL}",
"password": "{LICENSE_KEY}"
}
}
}
```
```json
{
"http-basic": {
"composer.gravitypdf.com": {
"username": "https://example.com",
"password": "gravity-pdf-core-booster@NpMmzRwVpkudnXQRfuEdk7wEi2W88Fz6, gravity-pdf-previewer@6NZMdPGmwUTgzfUh6qmfVgGp4kZA7jQ4"
}
}
}
```
--------------------------------
### HTML Examples for CSS Border Radius
Source: https://docs.gravitypdf.com/v6/developers/pdf-features/rounded-corners
Provides various HTML examples illustrating the application of CSS border-radius for styling PDF templates. These examples showcase standard rounding, larger corners, single corner rounding, creating circles, and controlling horizontal/vertical radii independently.
```html
`
#### `PDF_TEMPLATE_LOCATION`
* **Description**: A constant containing the path to the PDF working directory (or multisite working directory). `__DIR__` is generally recommended over this.
* **Usage**: ``
#### `PDF_TEMPLATE_URL_LOCATION`
* **Description**: A constant containing the URL to the PDF working directory (or multisite working directory). Useful for providing links to files on the host machine.
* **Usage**: `View Sample`
#### `wp_upload_dir()`
* **Description**: A WordPress function that returns an array with path and URL information about the WordPress uploads directory.
* **Usage**: `
`
#### `ABSPATH`
* **Description**: A WordPress constant referencing the root directory where WordPress is installed. Less commonly used for storing template assets.
* **Usage**: `
`
```
--------------------------------
### Access Gravity PDF Misc Class and Check Admin Page (PHP)
Source: https://docs.gravitypdf.com/v6/developers/api/get_misc_class
This snippet demonstrates how to get the Gravity PDF misc class object using GPDFAPI::get_misc_class() and check if the current page is a Gravity PDF admin page with the is_gfpdf_page() method. It requires the Gravity PDF plugin to be installed and active.
```php
add_action( 'init', function() {
if ( class_exists( 'GPDFAPI' ) ) {
/* Get Gravity PDF misc class */
$misc = GPDFAPI::get_misc_class();
/* Check if we are on a Gravity PDF admin page */
if ( $misc->is_gfpdf_page() ) {
//load scripts or styles on the Gravity PDF admin page here
}
}
} );
```
--------------------------------
### Get All Gravity Forms PDFs (PHP)
Source: https://docs.gravitypdf.com/v6/developers/api/get_form_pdfs
Retrieves all configured PDFs for a specific Gravity Form and iterates through them to display their names. This example demonstrates checking for the existence of the GPDFAPI class and handling potential errors from the API call. It's useful for dynamically listing available PDF forms associated with a Gravity Form.
```php
add_action( 'init', function() {
if ( class_exists( 'GPDFAPI' ) ) {
/* Get a list of PDFs assigned to form #2 */
$pdfs = GPDFAPI::get_form_pdfs( 2 );
if ( ! is_wp_error( $pdfs ) && sizeof( $pdfs ) > 0 ) {
echo 'A paragraph of text to be included at the start of every PDF Invoice for a specific form.
` tag, get its file path, URL, and dimensions. The example also includes a check to ensure the image file exists before attempting to display it, preventing broken image icons. ```php /* * 39 is the ID of our field * The signature details isn't in the 'field' sub-key */ echo wp_kses_post( $form_data['signature_details_id'][39]['img'] ); /* a