【问题标题】:How to export PDF from back-end edit/update view using DynamicPDF plugin in OctoberCMS如何在 OctoberCMS 中使用 DynamicPDF 插件从后端编辑/更新视图中导出 PDF
【发布时间】:2020-02-23 04:21:45
【问题描述】:

我将使用 DynamicPDF 插件在 OctoberCMS 中更新/编辑我的插件视图时从后端导出一些字段到 pdf,有人可以帮我吗?

在插件控制器上我有这个电话:

<?php namespace Vimagem\Pacientes\Controllers;

use Backend\Classes\Controller;
use BackendMenu;
use Renatio\DynamicPDF\Classes\PDF;
use Renatio\DynamicPDF\Classes\PDFWrapper;


class Pacientes extends Controller
{
    public $implement = [        'Backend\Behaviors\ListController',        'Backend\Behaviors\FormController',        'Backend\Behaviors\ReorderController'    ];

    public $listConfig = 'config_list.yaml';
    public $formConfig = 'config_form.yaml';
    public $reorderConfig = 'config_reorder.yaml';

    public function __construct()
    {
        parent::__construct();
        BackendMenu::setContext('Vimagem.Pacientes', 'main-menu-item');
    }


    /** PDF **/




public function pdf($id)
{

    return PDF::loadTemplate('export-data-pdf')->stream('download.pdf');
}



}

在 PDF 模板 (export-data-pdf) 上,我需要从一个客户端调用一些表单字段:

{{ name }}
{{ address }}
{{ phone }}
etc...

但我无法显示这些字段,这是怎么回事?

谢谢你, 维托

【问题讨论】:

  • 是的,但我们需要有关您尝试做什么以及遇到什么问题的更具体信息。
  • 您好,我已经创建了一个 from on builder 插件。现在需要获取要在 PDF 上输出的字段值。我将更新我在问题中使用的代码,谢谢约瑟夫

标签: pdf plugins octobercms octobercms-plugins dynamicpdf


【解决方案1】:

这已经完成,解决了这个问题。

这是控制器应用程序:

<?php namespace Vimagem\Pacientes\Controllers;

use Backend\Classes\Controller;
use BackendMenu;
use Renatio\DynamicPDF\Classes\PDFWrapper;
use Vimagem\Pacientes\Models\Paciente;
use \October\Rain\Database\Traits\Validation;
use Str;

class Pacientes extends Controller
{
    public $implement = [        'Backend\Behaviors\ListController',        'Backend\Behaviors\FormController',        'Backend\Behaviors\ReorderController'    ];

    public $listConfig = 'config_list.yaml';
    public $formConfig = 'config_form.yaml';
    public $reorderConfig = 'config_reorder.yaml';

    public function __construct()
    {
        parent::__construct();
        BackendMenu::setContext('Vimagem.Pacientes', 'main-menu-item');
    }




/**** PDF Export ***/

public function pdf($id)
    {
        $paciente = Paciente::find($id);
        if ($paciente === null) {
            throw new ApplicationException('User not found.');
        }


        $filename = Str::slug($paciente->nome) . '.pdf';

        try {
            /** @var PDFWrapper $pdf */
            $pdf = app('dynamicpdf');

            $options = [
                'logOutputFile' => storage_path('temp/log.htm'),
            ];

            return $pdf
                ->loadTemplate('export-data-pdf', compact('paciente'))
                ->setOptions($options)
                ->stream($filename);

        } catch (Exception $e) {
            throw new ApplicationException($e->getMessage());
        }
    }   


}


现在我可以像这样在模板上使用部分:

<p>{{ paciente.nome }}</p>

<p>{{ paciente.morada }}</p>


etc...

感谢所有试图帮助我的人。

维托

【讨论】:

    【解决方案2】:

    此代码在插件documents 中找到。

    use Renatio\DynamicPDF\Classes\PDF; // import facade
    
    ...
    
    public function pdf()
    {
        $templateCode = 'renatio::invoice'; // unique code of the template
        $data = ['name' => 'John Doe']; // optional data used in template
    
        return PDF::loadTemplate($templateCode, $data)->stream('download.pdf');
    }
    

    我用过这个插件,效果很好。您需要将数据传递给 PDF 流。

    【讨论】:

    • 谢谢,我已经使用此代码,但不能满足我的需要,我需要从客户端的记录 ID 中提取数据,动态数据。
    • 是的,但这就是您将数据注入 pdf 的方式。例如Patient::where('id', $id')-&gt;first() 那么您可以访问您想要的用户记录。轻松豌豆。
    猜你喜欢
    • 1970-01-01
    • 2017-06-17
    • 1970-01-01
    • 1970-01-01
    • 2019-04-20
    • 1970-01-01
    • 2015-10-31
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多