Working …
This value you provided is not a number. Please try again.
This value you provided is not an integer. Please try again.
The value entered is not a valid Vanderbilt Medical Record Number (i.e. 4- to 9-digit number, excluding leading zeros). Please try again.
The value you provided must be within the suggested range
The value you provided is outside the suggested range
This value is admissible, but you may wish to double check it.
The value entered must be a time value in the following format HH:MM within the range 00:00-23:59 (e.g., 04:32 or 23:19).
This field must be a 5 or 9 digit U.S. ZIP Code (like 94043). Please re-enter it now.
This field must be a 10 digit U.S. phone number (like 415 555 1212). Please re-enter it now.
This field must be a valid email address (like joe@user.com). Please re-enter it now.
The value you provided could not be validated because it does not follow the expected format. Please try again.
Required format:
REDCap Logo
Plugins, Hooks, & External Modules
Developer methods for
Plugins, Hooks, & External Modules
Hook functions

REDCap Developer Tools:
Documentation for Plugins, Hooks, & External Modules

REDCap Version 14.9.1
redcap_pdf
(REDCap >= 9.5.0)
redcap_pdf — Allows custom actions to be performed before a PDF is output
Description
void redcap_pdf ( int $project_id, array $metadata, array $data, string $instrument = NULL, string $record = NULL, int $event_id = NULL, int $instance = 1 )
Allows for the interception of a PDF being generated or the manipulation of the $metadata or $data arrays that will be used to generate the PDF.
Location of Execution
The function is executed at the PdfController:index route/endpoint immediately before the PDF::renderPDF() method is called.
Parameters
project_id
The project ID number of the REDCap project.
metadata
The metadata array that will be passed to the PDF::renderPDF() method for building the content structure of the PDF.
data
The data array that will be passed to the PDF::renderPDF() method for injecting stored data values into the content structure of the PDF to display the data from one or more records in the project.
instrument
The unique form name of the instrument being exported as a PDF. Note: If instrument=NULL, this implies that ALL instruments in the project will be included in the PDF.
record
The name of the single record whose data will be included in the PDF. Note: If record=NULL, this implies a blank PDF is being exported (i.e., with no record data).
event_id
The current event_id for the record whose data will be included in the PDF.
instance
The repeating instance number of the current repeating instrument/event for the record whose data will be included in the PDF.
Return Values
Your function does not need to return anything unless you are manipulating $metadata and/or $data, in which case you must end your hook function with the following line of code:
return array('metadata'=>$metadata, 'data'=>$data);
Examples
Example #1:
An illustration of how to manipulate the metadata or data used to generate the PDF.     
function redcap_pdf($project_id, $metadata, $data, $instrument=null, $record=null, $event_id=null, $instance=1)
{
    // Loop through $metadata and alter a field attribute
    foreach ($metadata as &$attr) {
        // Replace all labels with static text (proof of concept)
        $attr['element_label'] = "This is a field label.";
    }

    // Remove a specific record from the $data array (record "101" in this example)
    unset( $data['101'] );

    // Return metadata and data in an associative array after being manipulated.
    // If not changed, then no need to return anything.
    return array('metadata'=>$metadata, 'data'=>$data);
}
Example #2:
A much more manageable way to perform project-specific operations for many projects at once is to create a directory structure where each project has its own subdirectory under the main Hooks directory (e.g., named "redcap/hooks/pid{$project_id}/redcap_survey_page.php"). This allows the code for each project to be sandboxed and separated and also makes it more manageable to utilize other files (e.g., PHP, HTML, CSS, and/or JavaScript files) that you can keep in the project's subdirectory (i.e., "pid{$project_id}") in the Hooks folder. Then the designated project handler PHP script can utilize any of the parameters passed in the function to perform actions specific to each project. NOTE: This example assumes that the "hooks" sub-directory is located in the same directory as the PHP file containing the Hook functions.
function redcap_pdf($project_id, $metadata, $data, $instrument=null, $record=null, $event_id=null, $instance=1)
{
    // Set the full path of the project handler PHP script located inside the
    // project-specific sub-folder, which itself exists in the main Hooks folder.
    $project_handler_script = dirname(__FILE__) . "/hooks/pid{$project_id}/".__FUNCTION__.".php";

    // Check if the project handler PHP script exists for this project, and if so,
    // then "include" the script to execute it. If not, do nothing.
    if (file_exists($project_handler_script)) include $project_handler_script;

    // Return metadata and data in an associative array after being manipulated.
    // If not changed, then no need to return anything.
    return array('metadata'=>$metadata, 'data'=>$data);
}
REDCap 14.9.1 - © 2024 Vanderbilt University