laravel8 生成pdf

 private static function createPdfSendEmail($job_notes_id)
    {
        # 信息
        $recruit_job_notes_auth = DB::table('notes_auth')->select(["mobile", "job_id"])->find($job_notes_id);

        if (!$recruit_job_notes_auth->mobile) {
            return ["status" => Status::ERROR, "msg" => "投递信息有误"];
        }
        $recruit_job = DB::table('job')->select("city")->find($recruit_job_notes_auth->job_id);

        if (!$recruit_job->city) {
            return ["status" => Status::ERROR, "msg" => "所在城市信息有误"];
        }
        $field = [
            "id", "user_name", "head_img", "education", "work_st", "weChat", "email", "mobile",
            "birth", "work_date", "adv"
        ];
        $nt    = Note::where(
            [['mobile', "=", $recruit_job_notes_auth->mobile], ['status', "=", Status::REGULAR], ['is_deleted', '=', Status::NOT_DELETE]]
        )->select($field)->first();

        if (!$nt) {
            return ["status" => Status::ERROR, "msg" => "信息有误"];
        }
        $nt->work_exp;
        $nt->notes_intention;
        $nt->educational_back;

    
-------------------- 以上是你需要的数据 -----------------


        // 创建 TCPDF 实例
        $pdf = new TCPDF('P', 'mm', 'A4', true, 'UTF-8');

        // 设置文档信息
        $pdf->SetCreator('招聘');
        $pdf->SetAuthor('招聘');
        $pdf->SetTitle('招聘');
        $pdf->SetSubject('招聘');
        $pdf->SetKeywords('TCPDF, PDF, PHP');

        // 设置页眉和页脚信息
        //        $pdf->SetHeaderData('tcpdf_logo.jpg', 30, 'job.eeeknow.com/recruit/pc/', '学会偷懒,并懒出效率!', [0, 64, 255], [0, 64, 128]);
        //        $pdf->setFooterData([0, 64, 0], [0, 64, 128]);

        // 设置页眉和页脚字体
        $pdf->setHeaderFont(['stsongstdlight', '', '10']);
        $pdf->setFooterFont(['helvetica', '', '8']);

        // 设置默认等宽字体
        $pdf->SetDefaultMonospacedFont('courier');

        // 设置间距
        $pdf->SetMargins(15, 6, 15); //页面间隔
        $pdf->SetHeaderMargin(5);    //页眉top间隔
        $pdf->SetFooterMargin(10);   //页脚bottom间隔

        // 设置分页
        $pdf->SetAutoPageBreak(true, 10);

        // set default font subsetting mode
        $pdf->setFontSubsetting(true);

        //设置字体 stsongstdlight支持中文
        $pdf->SetFont('stsongstdlight', '', 10);

        $pdf->setPrintHeader(false);
        # 页尾
        //        $pdf->setPrintFooter(false);
        // 设置字体间距
        $pdf->SetFontSpacing(0.3);

        $b     = date('Y-m-d H:i:s', $nt->birth);
        $date1 = new \DateTime($b);
        $date2 = new \DateTime(date("y-m-d H:i:s"));

        $diff = $date1->diff($date2);

        $yearsDiff = ceil($diff->days / 365);
        $sex       = $nt->sex == 1 ? '男' : '女';

        $html = '<html><body>';
        $html .= '<div style="text-align:center;font-size: 22px;line-height: 1.5">' .
            $nt->user_name;
        $html .= '<div style=" font-size: 10px;">' . $sex .
            ' | 年龄:' .
            $yearsDiff . ' | ' . $nt->mobile . '</div>';

        $intention = '';
        foreach ($nt->notes_intention as $datum) {
            if ($datum->work_city == $recruit_job->city) {
                $city_name = DB::table('city')->select(["name"])->where("id", $datum->work_city)
                    ->first();
                $city_name = $city_name ? $city_name->name : '';

                $position_category      = DB::table('category')->select(["name"])->where("id", $datum->position_category_id)
                    ->first();
                $position_category_name = $position_category ? $position_category->name : '';

                $intention .= '<div style="font-size: 10px;line-height: 0.5;">' . $datum->work_year .
                    '年经验 | 意向:' .
                    $position_category_name . ' | ' . $nt->mobile . ' | 薪资:' . $datum->salary1 / 1000 . ' k -' .
                    $datum->salary2 / 1000 .
                    ' k |  城市:' . $city_name . '</div>';
            }
            continue;
        }


        $html .= $intention . '<div style="text-align: left;"><span style="font-size: 16px;">优势</span><hr>';
        $html .= '<div style="font-size: 12px;line-height: 1.5">' . $nt->adv . '</div>';
        $html .= '</div>';


        $expHl = '<div style="text-align: left;"><span style="font-size: 16px;">经验</span><hr>';
        foreach ($nt->work_exp as $k1 => $exp) {
            if ($exp->tp == 1) {
                $expHl .= '<div style="font-size: 12px;">';
                $expHl .= '<span>' . $exp->business_name . '         ' . $exp->position_name .
                    '               ' . date('Y.m.d', $exp->work_date_st) .
                    ' - ' . date('Y.m.d', $exp->work_date_ed) . '</span>';
                $expHl .= '<div>' . $exp->work_exp . '</div>';
                $expHl .= '</div>';
            }
        }
        $expHl .= '</div>';
        $html  .= $expHl;

        $expHl2 = '<div style="font-size: 16px;text-align: left;"><span>经历</span><hr>';
        foreach ($nt->work_exp as $expw) {
            if ($expw->tp == 2) {
                $expHl2 .= '<div style="font-size: 12px;">';
                $expHl2 .= '<span>' . $expw->business_name . '        ' . $expw->position_name .
                    '                 ' . date('Y.m.d', $expw->work_date_st) .
                    ' - ' . date('Y.m.d', $expw->work_date_ed) . '</span>';

                $expHl2 .= '<div>' . $expw->work_exp . '</div>';
                $expHl2 .= '</div>';
            }
        }
        $expHl2 .= '</div>';
        $html   .= $expHl2;


        $eduHl = '<div style="font-size: 16px;text-align: left;"><span>背景</span><hr>';
        foreach ($nt->educational_back as $edu) {
            $education = Status::EDUCATION[$edu->education];
            $dd        = $edu->university_name . '        ' . $education . '        ' . $edu->speciality;
            $eduHl     .= '<div style="font-size: 12px;">' . $dd . '</div>';
        }
        $eduHl .= '</div>';
        $html  .= $eduHl . '</div></body></html>';

        $pdf->AddPage();
        $pdf->writeHTML($html, true, false, true, false, '');

        $pdfName = rand(1000, 9999) . $nt->user_name;
        # 保存 PDF 到指定路径
        if (!is_dir(storage_path('file/save/pdf'))) {
            File::makeDirectory(storage_path('file/save/pdf'), 0777, true);
        }

        # PDF输出的方式。I,默认值,在浏览器中打开;D,点击下载按钮, PDF文件会被下载下来;F,文件会被保存在服务器中;S,PDF会以字符串形式输出;E:PDF以邮件的附件输出。
           // $path = storage_path('file/save/pdf/' . $pdfName . '.pdf');
           //  $pdf->Output($path, 'F'); 保存到指定目录
       
        $user = ['email' => 'gong92socl@163.com'];
        self::sendPdfEmail($path, $user);    # 发送邮件
        return ["status" => Status::SUCCESS, "msg" => ""];
    }

 

邮箱接收pdf 预览结果:

 

posted @ 2024-02-02 16:45  现世中的素人  阅读(48)  评论(0编辑  收藏  举报