zoukankan      html  css  js  c++  java
  • 后端队列生成pdf,发送到邮箱

    最开始可以先将你想要的pdf模板写好先,如testPage

    <!DOCTYPE html>
    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>{{$data[['title']}}</title>
    <style type="text/css">
    
    * {
        font-family: 'Arial', sans-serif;
      }
    ul li{
        list-style:none;
        float:left;
       }
    div li{
      list-style:none;
      margin-left:50px
    }
      table {
        border:1px solid black;
      }
      table th{
        border:1px solid black;
      }
      table td{
        border:1px solid black;
        word-wrap: break-word;
        word-break: break-all;
      }
      .header{
        width:100%;
        height:800px;
        text-align:center
      }
    </style>
    
    </head>
    
    <body>
    
    <div class="header">
     <div style="text-align:center">
     </div>
      <table style="100%;" cellspacing="0">
      <tr>
        <th colspan="5"><h1>{{$data['testPager']['exam_page']['title']}}</h1></th>
      </tr>
      <tr>
        <th colspan="3" style="text-align:left"><h3>Shop:  {{$data['testPager']['pagerable']['name']}}</h3></th>
        <th colspan="2"><h3>Total Score</h3></th>
       </tr>
       <tr>
        <!-- <th>No</th> -->
        <th colspan="3" style="text-align:left"><h3>Date</h3></th>
        <th colspan="2" style="font-color:red"><font color="red">{{$data['score']}}</font></th>
       </tr>
       <tr>
        <th>No</th>
        <th>Category</th>
        <th>Details</th>
        <th>Point</th>
        <th>Score</th>
       </tr>
       @foreach($data['page_content'] as $key=>$value)        
       <tr>
        <td>{{$key+1}}</td>
        <td>{{$value['title']}}</td>
        <td style="text-align:left">{{$value['description']}}</td>
        <td>{{$value['point']}}</td>
        <td><font color="red">{{$value['score']}}</font></td>
       </tr>
       @endforeach
      </table>
     </div>
    </div>
    
    </body>
    <script language="JavaScript" type="text/javascript">
    </script>
    </html>

    1.后端控制器获取数据,分派队列任务,将获取的数据传到队列中去

    $user = User::all()->toArray();

    dispatch(new TestPdf($user));  //TestPdf()为创建的队列
    2.TestPdf()队列中生成pdf数据
    $pdf = PDF::loadView('pdfTemplate.testPage',compact('data'))->setPaper('a4'); //pdfTemplate.testPage为views目录下的视图文件,compact('data')是传到pdf中的数据,
    setPaper('a4')设置pdf的大小
    //将pdf发送到1093684305@qq.com的邮箱
    Mail::to('1093684305@qq.com')->send(new ReportPage($pdf,$user));
    3.创建Mail类
    php artisan make:mail ReportPage    //在根目录下的app下的Mail目录生成ReportPage.php文件
    class ReportPage extends Mailable
    {
        use Queueable, SerializesModels;
        // protected $data;
        protected $pdf;  //传递过来的pdf原数据
        protected $data;  //pdf的变量数据
        /**
         * Create a new message instance.
         *
         * @return void
         */
        public function __construct($pdf,$data)
        {
            $this->data = $data;
            $this->pdf = $pdf;
        }
    
        /**
         * Build the message.
         *
         * @return $this
         */
        public function build()
        {
        //发送邮件视图testPage时,data数据也要传到视图中去,output()是pdf原数据,testPage.pdf发送到邮箱的pdf文件名字
    return $this->view('pdfTemplate.testPage',['data'=>$this->data]) ->subject('testPage') //邮件主题,即收到邮件时邮件的名字 ->attachData($this->pdf->output(), 'testPage.pdf', [ 'mime' => 'application/pdf' ]); } }
    踩过这个坑,还有下一个坑等着你,这一路就是给自己填坑,坑填多了,也就习惯了,直到这一路平坦了,也就无怨无悔了。
  • 相关阅读:
    java(5)流程控制n阶乘各位和
    java(4)运算符
    java(3)
    java(2)
    java(1)
    语音识别,图片识别(1)
    java实现——005从尾到头打印链表
    java实现——004替换空格
    java实现——003二维数组中的查找
    java实现——035第一个只出现一次的字符
  • 原文地址:https://www.cnblogs.com/xiaofeilin/p/13572684.html
Copyright © 2011-2022 走看看