Purpose

PDF Templates in Wonderful Relations provide a structured way to generate PDFs using predefined static templates and dynamic data. Unlike static PDFs, these templates allow for a flexible combination of fixed content, database-driven elements, and structured formatting to meet various business needs.

Generating PDFs directly from database queries can be cumbersome and inefficient. To address this, Wonderful Relations utilizes a Container Approach, where PDF templates are composed of multiple PDF Template Container . Each container acts as a building block, enabling precise structuring of content across multiple pages.

Visual Example

Example: Workshop Participant List with Letterhead

Example: Rehearsal Plan with AGBs / Terms and Conditions attached

Parameters

A PDF Template serves as the top-level structure defining a complete PDF document. It consists of the following parameters:

Title

The display name of the template.

Identifier

A unique key for accessing the template programmatically.

Project

Associates the template with a specific Wonderful Relations project.

Description

A brief explanation of the template’s purpose.

How PDF Templates Iterate

To illustrate how PDF Templates function, consider the example of generating a workshop report:

  1. Cover Page (Static Content)
    • The first PDF Template Container is a static cover page with the event name, date, and organizer details.
  2. Dynamic Participant List (Variable Content)
    • The second PDF Template Container dynamically retrieves an attendee list from the database.
    • Depending on the number of participants, this section can span one or multiple pages.
  3. Terms & Conditions (Static or Conditional Content)
    • The final PDF Template Container includes standard terms and conditions.
    • If additional disclaimers or special instructions are needed, extra pages can be appended dynamically.

This modular approach ensures that each document is generated efficiently while maintaining a structured format.

Code Examples

Using PDFRequest to Merge Two PDF Files

In this example, an empty PDF with a stamp (“Amount Received”) is placed on an existing invoice PDF.

 
$filename = $id . "_confirmed.pdf";  
 
$options['identifier'] = 'yourproject_amount_received';  
$selector              = array( "id" => "dummy", "filename" => $filename );  
$executor              = new Executor();  
$option_dto_factory    = new OptionDTOFactory( new DTOFactoryManager() );  
$DPI_container         = new DPIContainerPDFController( $executor, $option_dto_factory );  
$controller            = new PDFController( $DPI_container );  
$request               = new PDFRequest( $options, $selector );  
  
$controller->process( $request );  
  
file_put_contents( wp_get_upload_dir()["basedir"] . "/" . $filename, $controller->present() );  
  
return $filename;
 

Creating a PDF Attachment Programmatically

In this example, a PDF is created programmatically, and a function returns a PDF attachment for an email function.

public static function create_pdf_attachment( $selector_id, string $identifier): self {  
 
    $pdf_instance = new GetPDF();
    $attachment_settings = $pdf_instance->build_pdf([
        "id"      => $selector_id,
        "options" => ["identifier" => $identifier]
    ])[1];  
 
    $filename = $attachment_settings['filename'];  
 
    return new self( $filename, $attachment_settings['data'] );  
}

Conclusion

PDF Templates in Wonderful Relations offer a modular and scalable approach to generating structured PDF documents. By utilizing containers, templates, and database-driven content, businesses can create tailored PDF outputs without excessive complexity.

With features like dynamic content loading, reusable templates, and programmatic access, PDF generation becomes as flexible as your application requires. 🚀