You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何通过WordPress表单生成含自定义字段的模板化PDF信件

Got it, let's break down how to build this WordPress workflow—collecting user info via a form and generating a customized PDF letter from a fixed template. Here are the most practical approaches depending on your needs:


Approach 1: Paid Plugin Stack (Easiest for Non-Developers)

If you want a no-code, reliable solution, go with Gravity Forms + Gravity PDF:

  • First, set up your form with Gravity Forms. Add a Single Line Text field for the user’s name, another for nationality (or a Dropdown if you want predefined options). Note each field’s ID (found in field settings)—you’ll need this for the template.
  • Install the Gravity PDF add-on. Once activated, head to Forms > PDF Templates. You can tweak a pre-built template or create a custom HTML/CSS one. Use placeholders like {Name:1} (where 1 is your name field’s ID) and {Nationality:2} for nationality. Example template snippet:
    <!DOCTYPE html>
    <html>
    <head>
        <style>
            body { font-family: Arial, sans-serif; line-height: 1.6; padding: 20px; }
            .letter-header { text-align: center; margin-bottom: 30px; border-bottom: 1px solid #ddd; padding-bottom: 10px; }
            .signature { margin-top: 40px; text-align: right; }
        </style>
    </head>
    <body>
        <div class="letter-header">
            <h2>Official Confirmation Letter</h2>
        </div>
        <p>Dear {Name:1},</p>
        <p>We have successfully received your submission. Your stated nationality is <strong>{Nationality:2}</strong>.</p>
        <p>Thank you for providing this information.</p>
        <div class="signature">
            <p>Sincerely,</p>
            <p>Your Team</p>
        </div>
    </body>
    </html>
    
  • Go back to your form settings, open the PDFs tab, and enable your template. Choose to attach the PDF to confirmation emails (user/admin) or save it to your media library. That’s it—submissions will auto-generate the customized PDF.

Approach 2: Free Plugin Combination (Budget-Friendly)

For a zero-cost setup, use Contact Form 7 + CF7 to PDF Extension:

  • Install Contact Form 7 and create your form with fields like:
    [text your-name placeholder="Full Name"]
    [text your-nationality placeholder="Nationality"]
    [submit "Submit"]
    
  • Install the CF7 to PDF Extension. Navigate to CF7 to PDF > Add Template to build your letter. Use the form field names as placeholders (e.g., [your-name], [your-nationality]) in your template content.
  • In your Contact Form 7 settings, go to the Mail tab. Under the PDF section, enable your template, then choose to attach the PDF to the outgoing email or save it to your server. Test a submission to confirm everything works.

Approach 3: Custom Code (Full Control for Developers)

If you need full customization, use PHP with a PDF library like TCPDF:

  1. Upload the TCPDF library to your theme folder (download it from the official repo and extract it).
  2. Add this code to your theme’s functions.php file to hook into form submissions (example for Contact Form 7):
    add_action('wpcf7_submit', 'generate_custom_pdf', 10, 2);
    function generate_custom_pdf($contact_form, $abort) {
        if ($abort) return;
    
        // Get submitted data
        $submission = WPCF7_Submission::get_instance();
        if (!$submission) return;
        $data = $submission->get_posted_data();
        $name = sanitize_text_field($data['your-name']);
        $nationality = sanitize_text_field($data['your-nationality']);
    
        // Load your HTML template (create this file in your theme folder)
        $template_path = get_stylesheet_directory() . '/pdf-letter-template.html';
        $template_content = file_get_contents($template_path);
    
        // Replace placeholders
        $template_content = str_replace('{{NAME}}', $name, $template_content);
        $template_content = str_replace('{{NATIONALITY}}', $nationality, $template_content);
    
        // Generate PDF
        require_once(get_stylesheet_directory() . '/tcpdf/tcpdf.php');
        $pdf = new TCPDF();
        $pdf->AddPage();
        $pdf->SetFont('dejavusans', '', 12); // Use a font that supports your language
        $pdf->writeHTML($template_content, true, false, true, false, '');
    
        // Save PDF to uploads folder
        $upload_dir = wp_upload_dir();
        $pdf_file_path = $upload_dir['path'] . '/letter-' . time() . '.pdf';
        $pdf->Output($pdf_file_path, 'F');
    
        // Optional: Attach PDF to confirmation email
        $mail_settings = $contact_form->prop('mail');
        $mail_settings['attachments'] .= "\n" . $pdf_file_path;
        $contact_form->set_prop('mail', $mail_settings);
    }
    
  3. Create your pdf-letter-template.html file in the theme folder with placeholders like {{NAME}} and {{NATIONALITY}}.

Pro Tips

  • For non-English characters (e.g., Chinese, Arabic), ensure your PDF plugin/code uses a font that supports those languages (TCPDF includes several multi-language fonts).
  • Always test submissions to verify placeholders are replaced correctly and formatting stays consistent.
  • Restrict access to generated PDFs if they contain sensitive data—adjust server file permissions or store them in a private directory.

内容的提问来源于stack exchange,提问作者Nima Afraz

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.20 07:24:58