laravel dompdf 导出不导出我视图的所有内容

问题描述 投票:0回答:2

我目前正在使用 Laravel 9,laravel dompdf 包来导出我的文件,这是我得到的 pdf 文件,根本没有内容。我传递的数据没有显示在 PDF 中。

但是当我将其作为视图打开时,数据正确传递并且 CSS 工作得很好。这是它的样子:

这是我在视图上的代码:

<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="utf-8">
        <meta name="viewport" content="width=device-width, initial-scale=1">
        <title>Bootstrap demo</title>
     
      </head>
    
      <style>
        * {
      box-sizing: border-box;
      margin:0;
      padding:0;
    }
    body {
      background:#DDD;
        
    }
    div.container {
      max-width: 1350px;
      margin: 0 auto;
      overflow: hidden
    }
    .upcoming {
      font-size: 45px;
      text-transform: uppercase;
      border-left: 14px solid rgba(255, 235, 59, 0.78);
      padding-left: 12px;
      margin: 18px 8px;
    }
    .container .item {
      width: 48%;
      float: left;
      padding: 0 20px;
      background: #fff;
      overflow: hidden;
      margin: 10px
    }
    .container .item-right, .container .item-left {
      float: left;
      padding: 20px 
    }
    .container .item-right {
      padding: 79px 0px;
      margin-right: 20px;
      width: 25%;
      position: relative;
      height: 286px;
      text-align: center;
    }
    .container .item-right .up-border, .container .item-right .down-border {
        padding: 14px 15px;
        background-color: #ddd;
        border-radius: 50%;
        position: absolute
    }
    .container .item-right .up-border {
      top: -8px;
      right: -35px;
    }
    .container .item-right .down-border {
      bottom: -13px;
      right: -35px;
    }
    .container .item-right .num {
      font-size: 50px;
      color: #111
    }
    .container .item-right .day, .container .item-left .event {
      color: #555;
      font-size: 15px;
      margin-bottom: 9px;
    }
    .container .item-right .day {
      text-align: center;
      font-size: 25px;
    }
    .container .item-left {
      width: 71%;
      padding: 34px 0px 19px 46px;
      border-left: 3px dotted #999;
    } 
    .container .item-left .title {
      color: #111;
      font-size: 34px;
      margin-bottom: 12px
    }
    .container .item-left .sce {
      margin-top: 5px;
      display: block
    }
    .container .item-left .sce .icon, .container .item-left .sce p,
    .container .item-left .loc .icon, .container .item-left .loc p{
        float: left;
        word-spacing: 5px;
        letter-spacing: 1px;
        color: #888;
        margin-bottom: 10px;
    }
    .container .item-left .sce .icon, .container .item-left .loc .icon {
      margin-right: 10px;
      font-size: 20px;
      color: #666
    }
    .container .item-left .loc {display: block}
    .fix {clear: both}
    .container .item .tickets, .booked, .cancel{
        color: #fff;
        padding: 6px 14px;
        float: right;
        margin-top: 10px;
        font-size: 18px;
        border: none;
        cursor: pointer
    }
    .container .item .tickets {background: #777}
    .container .item .booked {background: #3D71E9}
    .container .item .cancel {background: #DF5454}
    .linethrough {text-decoration: line-through}

      </style>

    <body>

        <div class="container">
            <h1 class="upcoming">Event Tickets</h1>
            @php ($i=1)

            @foreach ($tickets as $key=> $ticket)
            <div class="item">
              <div class="item-right">
                <h2 class="num"> {{ $ticket->ticket_no }}</h2>
                <p class="day">Ticket No.</p>
                <span class="up-border"></span>
                <span class="down-border"></span>
              </div> <!-- end item-right -->
              
              <div class="item-left">
                <p class="event">{{ $ticket->GiftGiving->name }}</p>
                <h2 class="title">{{ $ticket->name }}</h2>
                
                <div class="sce">
               
                  <p>{{Carbon\Carbon::parse($ticket->GiftGiving->start_at )->isoFormat('LL') }}<br/> {{   Carbon\Carbon::parse($ticket->GiftGiving->start_at )->isoFormat('h:mm A') }}</p>
                </div>
                <div class="fix"></div>
                <div class="loc">
           
                  <p>{{ $ticket->GiftGiving->venue }}</p>
                </div>
                <div class="fix"></div>
                <button class="booked">Batch No. {{ $ticket->GiftGiving->batch_no}}</button>
              </div> <!-- end item-right -->
            </div> <!-- end item -->

                 <!--Set limitation of printing only 5 ticket per page-->
                 @if ($i % 6 === 0)
                 <div style="page-break-after: always;"></div>
                 @endif
            
             @php ($i++)
         @endforeach
          </div>
        


    </body>
</html>
Controller 
public function GenerateTicket($code)
{
    # Retrieve the records using $code
    $GiftGiving = GiftGiving::where('code', $code)->firstOrFail();
    $tickets = GiftGivingBeneficiary::where('gift_giving_id', $GiftGiving->id)->get();

    # Users can only access their own charity's records
    if ($GiftGiving->charitable_organization_id == Auth::user()->charitable_organization_id) {

        # Must have at least one beneficiary before generating tickets
        if ($tickets->count() < 1) {
            $toastr = array(
                'message' => 'Gift Giving must have at least one (1) beneficiary first before generating tickets',
                'alert-type' => 'error'
            );

            return redirect()->back()->with($toastr);
        }

        # Retrieve the last batch no. from the gift giving.
        $batch_no = $GiftGiving->batch_no;

        # Increment Batch no. by +1
        $GiftGiving->update([
            'last_downloaded_by' => Auth::id(),
            'batch_no' => $batch_no + 1,
        ]);


        # Audit Logs Creation
        $log = new AuditLog;
        $log->user_id = Auth::user()->id;
        $log->action_type = 'GENERATE PDF';
        $log->charitable_organization_id = Auth::user()->charitable_organization_id;
        $log->table_name = 'Gift Giving';
        $log->record_id = $GiftGiving->code;
        $log->action = 'Charity Admin generated tickets for the Gift Giving [' . $GiftGiving->name . '] with batch no. ' . $GiftGiving->batch_no . '.';
        $log->performed_at = Carbon::now();
        $log->save();


        # Send Notification to each user in their Charitable Organizations
        $users = User::where('charitable_organization_id', Auth::user()->charitable_organization_id)->where('status', 'Active')->get();

        foreach ($users as $user) {
            $notif = new Notification;
            $notif->code = Str::uuid()->toString();
            $notif->user_id = $user->id;
            $notif->category = 'Gift Giving';
            $notif->subject = 'Generated Tickets';
            $notif->message = Auth::user()->role . ' ' . Auth::user()->info->first_name . ' ' .
                Auth::user()->info->last_name . ' has generated tickets for [' . $GiftGiving->name . '] with batch no. ' .
                $GiftGiving->batch_no . '.';
            $notif->icon = 'mdi mdi-ticket';
            $notif->color = 'info';
            $notif->created_at = Carbon::now();
            $notif->save();
        }
        
        // return view('charity.gifts.generate_ticket', compact('tickets'));
        $pdf = PDF::loadView('charity.gifts.generate_ticket', compact('tickets'));
        
        return $pdf->download($GiftGiving->name . ' - No. ' . $GiftGiving->batch_no . '.pdf');
    } else {

        $toastr = array(
            'message' => 'Users can only access their own charity records.',
            'alert-type' => 'error'
        );

        return redirect()->back()->with($toastr);
    }
}

希望有人能指出我错过的地方,提前谢谢。所有建议/答案都受到高度赞赏。

laravel pdf export dompdf
2个回答
0
投票

dompdf 对 CSS 和样式非常挑剔...但首先,您应该始终将

<style>
标签放在文档的
<head>
标签内。如果不把它放在那里,不同的浏览器呈现内容的方式就会不一致,包括无头系统和 domPDF。

因此,将您的

<style>
标签放入
<head>
标签中,看看这是否有助于解决问题,如果没有,请尝试将您的样式转换为带有样式的表格,因为 dompdf 似乎比 div 更适合处理表格。


0
投票

你应该关心语法:

@php $i = 1; @endphp 
@php $i++; @endphp
© www.soinside.com 2019 - 2024. All rights reserved.