简体   繁体   中英

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.

The Page (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. 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?

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.

        $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. When the the render button on index.blade.php is clicked, generate the pdf from the pdf.blade.php.

PDF Page (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();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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