Interface PdfTemplate<T>

All Known Implementing Classes:
AbstractJacksonPdfTemplate, DefaultPdfTemplate, PdfInvoiceTemplate

public interface PdfTemplate<T>

This interface is implemented by classes that want to be added to the PDFfer registry as PDF templates. PDF template classes must also be annotated with the PdfTemplateComponent stereotype, which allows developers to provide the name of the template and to indicate the scope that the template should be configured for in the registry context. Unless specified otherwise, the scope will be "prototype".

The lifecycle of a template is meant to be as follows

  • created as a Spring bean in the PDFfer Registry context when needed to generate a PDF document
  • the PDF payload is set with the setPayload(Object) method
  • the payload is validated with the validate() method
  • the generate() method is called to process the payload and create the actual PDF document
  • the bytes of the PDF are retrieved with the getPdfContent() method by the requester

Mostly, you will probably want to use the AbstractJacksonPdfTemplate class for your templates, rather than implement this interface from scratch.

  • Method Summary

    Modifier and Type
    Method
    Description
    void
    Prepares the PDF document.
    Returns the payload used by this instance to generate a PDF document.
    Returns the payload class that this template is able to handle.
    byte[]
    Returns the PDF document that was generated by generate() as an array of bytes
    static String
    getTemplatePath​(String group, String name)
    Makes a template path string from group and name.
    static String
    getTemplatePath​(PdfTemplate<?> template)
    Returns the template path string for the given template.
    void
    setPayload​(T payload)
    Sets the payload that should be used by this instance to generate a PDF document.
    static String[]
    Returns the group and name components from a template path string.
    static String
    templateToString​(PdfTemplate<?> template)
    Returns a string representation of a PDF template.
    boolean
    Checks that the payload is consistent with the requirements of the template.
  • Method Details

    • getTemplatePath

      static String getTemplatePath(String group, String name)
      Makes a template path string from group and name. It uses the PdfTemplateComponent.GROUP_SEPARATOR string for that.
      Parameters:
      group - the name of the template group
      name - the name of the template
      Returns:
      the template path for the given group and name
    • getTemplatePath

      static String getTemplatePath(PdfTemplate<?> template)
      Returns the template path string for the given template. It uses the PdfTemplateComponent.GROUP_SEPARATOR string for that.
      Parameters:
      template - the template of which the path is required
      Returns:
      the template path for the given template
    • splitTemplatePath

      static String[] splitTemplatePath(String path)
      Returns the group and name components from a template path string.
      Parameters:
      path - the template path to be de-composed
      Returns:
      an array of strings with the group at element 0 and the name at element 1
    • templateToString

      static String templateToString(PdfTemplate<?> template)
      Returns a string representation of a PDF template.
      Parameters:
      template - the template to represent as a string
      Returns:
      the string representing the template
    • getPayloadClass

      Class<T> getPayloadClass()
      Returns the payload class that this template is able to handle. It takes instances of the class returned by this method and produces PDFs from them.
      Returns:
      the payload class
    • getPayload

      T getPayload()
      Returns the payload used by this instance to generate a PDF document. It must be of the type indicated by getPayloadClass().
      Returns:
      the payload
    • setPayload

      void setPayload(T payload)
      Sets the payload that should be used by this instance to generate a PDF document. It must be of the type indicated by getPayloadClass().
      Parameters:
      payload - the payload
    • validate

      boolean validate()

      Checks that the payload is consistent with the requirements of the template.

      The concept of "valid payload" is entirely up to each PdfTemplate instance.

      Returns:
      true if the payload is valid and can be safely used to generate PDFs, false otherwise
    • generate

      void generate()
      Prepares the PDF document. It uses the payload set with setPayload(Object) and assumes that the payload has already been checked with validate(). The generated PDF document can be accessed with getPdfContent() after this method returns.
    • getPdfContent

      byte[] getPdfContent()
      Returns the PDF document that was generated by generate() as an array of bytes
      Returns:
      the bytes of the generated PDF document