简体   繁体   English

laravel-dompdf:如何让 dompdf 呈现与调用它相同的页面的 pdf?

[英]laravel-dompdf: How do I make dompdf render a pdf of the same page that it is called from?

What I'm trying to do is pass a page to dompdf in order to render it, but the button to render the page is located on the page itself.我想要做的是将页面传递给 dompdf 以呈现它,但呈现页面的按钮位于页面本身。

The Page (index.blade.php)页面(index.blade.php)

<div class="event-links">
        <div class="panel panel-default">
            <!-- <a href="{{    URL::to('/events/create')   }}"> -->
            <a href="{{ action('StrawdogController@pdfgen')}}">
                    <div class="panel-heading">Generate PDF</div>
            </a>                
        </div>
    </div>

    <div class="nametag-layout">
        <table class="table table-striped pagin-table table-bordered">
            <thead>
                <tr>
                    <td>Mod Start Time</td>
                    <td>Mod End Time</td>
                    <td>Mod Description</td>
                </tr>
            </thead>
            <tbody>
            @foreach($mods as $key => $value)
                <tr>
                    <td>{{ $value->mod_description }}</td>
                    <td>{{ $value->mod_start_time }}</td>
                    <td>{{ $value->mod_end_time }}</td>
                </tr>
            @endforeach
            </tbody>
        </table>
    </div>

and the Controller function (placeholder)和控制器功能(占位符)

public function pdfgen()
{
    $dompdf = new Dompdf();
    $dompdf->loadView('strawdogs.index');
    $dompdf->render();
    $dompdf->stream();
}

} }

There is a button on my page called 'Generate PDF' and I want the user to be able to press it and get a PDF of the table which is located on the same page.我的页面上有一个名为“生成 PDF”的按钮,我希望用户能够按下它并获取位于同一页面上的表格的 PDF。 At the moment, I've tried and I can see it passes along values if you call to another page, but it doesn't seem to do it for the same page.目前,我已经尝试过,如果您调用另一个页面,我可以看到它传递值,但它似乎没有为同一页面执行此操作。

Basically, is there a way to call dompdf on the same page, and if so, how do I?基本上,有没有办法在同一页面上调用 dompdf,如果是这样,我该怎么做?

On your controller you have to call view method and send variables with compact and then use loadHtml() and pass the view method as a parameter.在您的控制器上,您必须调用 view 方法并使用 compact 发送变量,然后使用 loadHtml() 并将 view 方法作为参数传递。

        $options = new Options();
        $options->set('defaultFont', 'DejaVu Sans');
        $dompdf = new Dompdf($options);
        // instantiate and use the dompdf class
        $html = view('pages.callsheet_view', compact(['request'],['number_of_no']));
        $dompdf->loadHtml($html,'UTF-8');


        $dompdf->render();
        $dompdf->stream();

You can create a new view(pdf.blade.php) without the render button.您可以在没有渲染按钮的情况下创建一个新视图(pdf.blade.php)。 When the the render button on index.blade.php is clicked, generate the pdf from the pdf.blade.php.当 index.blade.php 上的渲染按钮被点击时,从 pdf.blade.php 生成 pdf。

PDF Page (pdf.blade.php) PDF 页面 (pdf.blade.php)

<div class="nametag-layout">
    <table class="table table-striped pagin-table table-bordered">
        <thead>
            <tr>
                <td>Mod Start Time</td>
                <td>Mod End Time</td>
                <td>Mod Description</td>
            </tr>
        </thead>
        <tbody>
        @foreach($mods as $key => $value)
            <tr>
                <td>{{ $value->mod_description }}</td>
                <td>{{ $value->mod_start_time }}</td>
                <td>{{ $value->mod_end_time }}</td>
            </tr>
        @endforeach
        </tbody>
    </table>
</div>

and the Controller function (placeholder)和控制器功能(占位符)

public function pdfgen()
{
    $dompdf = new Dompdf();
    $dompdf->loadView('strawdogs.pdf');
    $dompdf->render();
    $dompdf->stream();
}
PDF::loadView('x.blade.php')->output();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM