ProjectFilePDF.php 5.1 KB
<?php

namespace App\Console\Commands;

use App\Http\Controllers\File\FileController;
use App\Models\File\DataFile;
use App\Models\ProjectAssociation\ProjectAssociation;
use App\Services\CosService;
use Barryvdh\DomPDF\Facade\Pdf;
use Illuminate\Console\Command;
use Illuminate\Http\File;

class ProjectFilePDF extends Command
{
    use CmdSignal;

    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'project_file_pdf';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = '网站项目数据,生成PDF文件';

    protected $ProjectAssociation;

    protected $DataFile;

    protected $time;

    protected $fileController;

    protected $CosService;

    // 最大支持5个进程
    public $maxRunNumber = 50;

    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        $this->ProjectAssociation = new ProjectAssociation();
        $this->DataFile           = new DataFile();
        $this->time               = date("Y-m");
        $this->fileController     = new FileController();
        $this->CosService         = new CosService();
        parent::__construct();
    }

//    public function handle()
//    {
//        $project_data = [];
//        $html         = $this->html($project_data);
//        $filename     = hash('md5', $this->time . '-' . '$project_id' . '-' . '$friend_id' . '-' . '$user_id');
//        $this->savePDF($html, $filename);
////        $this->testStreamPdf($html, $filename);
////        $file_path = $this->savePDF2($html, $filename);
//        return 0;
//    }

    public function start(): int
    {
        # 0 - 未生成
        # 1 - 已生成
        # 2 - 其它问题
        $is_pdf = 0;
        $lists  = $this->ProjectAssociation::query()->where('is_pdf', $is_pdf)
                                           ->where('project_id', '!=', 0)
                                           ->where('friend_id', '!=', 0)
                                           ->where('user_id', '!=', 0)
                                           ->where('created_at', 'like', $this->time . '%')->first();

        if (is_null($lists)) {
            $this->debug_echo('没有任务,等待中');
            sleep(30);
            return 0;
        }
        $key   = $this->signature . '-' . $lists->id;
        $count = redis_get($key);

        $project_id = $lists->project_id;
        $user_id    = $lists->user_id;
        $friend_id  = $lists->friend_id;
        // todo 根据项目查询数据
        $project_data = [];
        $html         = $this->html($project_data);
        $filename     = hash('md5', $this->time . '-' . $project_id . '-' . $friend_id . '-' . $user_id);

        $file_path = $this->savePDF($html, $filename);
        if (empty($file_path)) {
            $this->debug_echo('pdf生成失败!');
            return 0;
        }
        $isRes = $this->DataFile->saveData(compact('project_id', 'user_id', 'friend_id', 'file_path') + ['time' => $this->time]);
        if (!$isRes) {
            if ($count == 2) {
                $lists->is_pdf = 2;
                $lists->save();
                $this->debug_echo('项目文件数据保存失败! - 其他原因 - ' . $key);
            } else {
                redis_set($key, $count + 1);
                $this->debug_echo('项目文件数据保存失败! - ' . $key);
            }
        }
        $lists->is_pdf = 1;
        $lists->save();
        $this->info('项目文件数据保存成功!');
        return 0;
    }


    /**
     * @param $html
     * @param $filename
     * @return string
     */
    public function savePDF($html, $filename)
    {
        $pdf_path = public_path('PDF/');
        if (!file_exists($pdf_path)) {
            mkdir($pdf_path, 0777, true);
        }
        // 指定保存路径和文件名
        $savePath = $pdf_path . $filename . '.pdf';
        if (file_exists($savePath)) {
            echo '文件已经存在';
            return 0;
        }

        $pdf = PDF::loadHTML($html);
        $pdf->save($savePath);

        $path = '/V6/PDF/' . $this->time;
        // 创建一个文件实例
        $file = new File($savePath);
        return $this->CosService->uploadFile($file, $path, $filename . '.pdf');
    }

    /**
     * 根据数据生成 Html
     * @param $item
     * @return string
     */
    protected function html($item)
    {
        $font_path = storage_path('fonts\msyh.ttf');
        return '<html><head><title>Laravel</title><meta http-equiv=\'Content-Type\' content=\'text/html; charset=utf-8\'/><style>body{  font-family: \'msyh\';  }  @font-face {  font-family: \'msyh\';  font-style: normal;  font-weight: normal;  src: url(' . $font_path . ') format(\'truetype\');  }</style></head><body><div class=\'container\'><div class=\'content\'><p style=\'font-family: msyh, DejaVu Sans,sans-serif;\'>献给母亲的爱</p><div style=\'font-family: msyh, DejaVu Sans,sans-serif;\' class=\'title\'>Laravel 5中文测试</div><div  class=\'title\'>测试三askjdhfkjasdhf</div></div></div></body></html>';
    }
}