Download Log in

54. PDF Template Customization

Developer Guide

Customize PDF exports of form submissions using the Forge API PDF generation endpoint.

Generating a PDF from a Submission

php
function generate_submission_pdf( $submission_id ) {
    global $wpdb;
    $sub = $wpdb->get_row( $wpdb->prepare(
        "SELECT s.*, f.title as form_title
         FROM {$wpdb->prefix}formforge_submissions s
         JOIN {$wpdb->prefix}formforge_forms f ON f.id = s.form_id
         WHERE s.id = %d", $submission_id
    ) );

    if ( ! $sub ) return false;

    $data = json_decode( $sub->data, true );
    $html = '<h1>' . esc_html( $sub->form_title ) . '</h1>';
    $html .= '<p>Submission #' . $sub->id . ' | ' . $sub->created_at . '</p>';
    $html .= '<table border="1" cellpadding="8" cellspacing="0" width="100%">';
    foreach ( $data as $field ) {
        $html .= '<tr><td><strong>' . esc_html( $field['label'] ) . '</strong></td>';
        $html .= '<td>' . esc_html( $field['value'] ) . '</td></tr>';
    }
    $html .= '</table>';

    $result = FORMFORGE_Forge_API::instance()->generate_pdf( [
        'html'     => $html,
        'filename' => 'submission-' . $submission_id . '.pdf',
    ] );

    return $result['url'] ?? false;
}

If the Forge API PDF endpoint is unavailable or no remote PDF URL is returned, Form Forge generates a local %PDF-1.4 fallback with a blue Form Forge header, submission metadata, two-column field/value rows, and a footer. This keeps the Submissions screen returning an actual application/pdf response instead of an HTML print page.

Custom PDF Header and Footer

php
$html = '
<style>
    body { font-family: sans-serif; color: #1e293b; }
    .header { background: #3B82F6; color: white; padding: 20px; text-align: center; }
    .footer { text-align: center; font-size: 11px; color: #94a3b8; margin-top: 40px; }
    table { width: 100%; border-collapse: collapse; margin-top: 20px; }
    td { padding: 10px; border-bottom: 1px solid #e2e8f0; }
</style>
<div class="header">
    <h1>' . esc_html( $form_title ) . '</h1>
</div>
<!-- Field table here -->
<div class="footer">
    Generated by Form Forge | ' . date( 'Y-m-d H:i' ) . '
</div>';

Attaching PDF to Email Notification

php
add_filter( 'formforge_email_body', function( $body, $form, $data, $submission_id ) {
    $pdf_url = generate_submission_pdf( $submission_id );
    if ( $pdf_url ) {
        $body .= "nnPDF: " . $pdf_url;
    }
    return $body;
}, 10, 4 );

Forge AI Assistant Online

Hi! I'm the Form Forge AI assistant. Ask me anything about the plugin — setup, features, troubleshooting, or development.

Just now
Powered by Forge AI · Browse docs