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

如何在OctoberCMS中使用DynamicPDF插件从后端列表导出PDF

Hey there! Let me walk you through how to hook up the DynamicPDF plugin with your backend list's export button in OctoberCMS. I’ve tackled this exact setup a few times, so here’s a straightforward, step-by-step breakdown that should get you up and running:

1. First, Build Your DynamicPDF Template

Head over to the DynamicPDF section in your OctoberCMS backend and create a new PDF template. Design it to display the data from your list—use Twig syntax to loop through records and pull in fields, like:

{% for item in records %}
    <div class="record-item">
        <h3>{{ item.name }}</h3>
        <p>Email: {{ item.email }}</p>
        <p>Created: {{ item.created_at|date('Y-m-d') }}</p>
    </div>
{% endfor %}

Save the template and make a note of its alias/code (you’ll need this to link it to your export action later).

2. Add a Custom Export Action to Your Controller

Open your model’s backend controller (usually located at plugins/yourvendor/yourplugin/controllers/YourModel.php) and add a new method to handle the PDF export:

public function exportPdf()
{
    // Get selected records from the list
    $selectedIds = post('checked');
    
    // Fetch records: use selected ones, or all visible if none are picked
    if ($selectedIds) {
        $records = YourModel::whereIn('id', $selectedIds)->get();
    } else {
        $records = $this->listGetRecords();
    }

    // Generate PDF using your DynamicPDF template
    $pdf = \Backend\Facades\Backend::pdf()->loadTemplate(
        'yourvendor.yourplugin::pdf.your_template_alias', // Replace with your template alias
        ['records' => $records] // Pass data to the template
    );

    // Force download the PDF with a timestamped filename
    return $pdf->download('list-export-' . date('Y-m-d-His') . '.pdf');
}

Make sure to replace YourModel with your actual model class name, and yourvendor.yourplugin::pdf.your_template_alias with the correct alias from your PDF template.

3. Add the Export Button to Your List

You have two options here—add it as a bulk action or a top toolbar button:

Option A: Bulk Action (for selected records)

Edit your model’s list configuration file (list_columns.yaml) and add a new bulk action:

bulkActions:
    exportPdf:
        label: Export to PDF
        icon: icon-file-pdf-o

Option B: Top Toolbar Button (next to default exports)

Add this method to your controller to override the list toolbar:

public function listOverrideButtons($toolbar)
{
    $toolbar->addButton('exportPdf', [
        'label' => 'Export to PDF',
        'icon' => 'icon-file-pdf-o',
        'action' => 'exportPdf',
        'context' => 'button',
    ]);
}
4. Test and Tweak
  • Go to your backend list, select some records (or leave none selected if you set up the fallback), click your new "Export to PDF" button, and verify the PDF loads with your data.
  • If you hit snags:
    • Double-check your template alias is correct in the controller method.
    • Use {{ dump(records) }} in your PDF template to confirm the data is being passed properly.
    • Ensure your plugin permissions include access to the exportPdf action (add it to your permissions.yaml if needed).
    • For styling issues: Stick to inline CSS in your PDF template—most PDF renderers don’t play nice with external stylesheets.

内容的提问来源于stack exchange,提问作者L.Benrong

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.25 06:46:21