ProjectFilePDF.php
4.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
<?php
namespace App\Console\Commands;
use App\Http\Controllers\File\FileController;
use App\Models\ProjectAssociation\ProjectAssociation;
use App\Models\File\DataFile;
use Dompdf\Dompdf;
use Dompdf\Options;
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;
// 最大支持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();
parent::__construct();
}
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;
}
$file_path = json_encode($file_path['data']);
$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->debug_echo('项目文件数据保存成功!');
return 0;
}
/**
* @param $html
* @param $filename
* @return array
*/
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;
}
// todo 生成中文有问题
# 实例化并使用dompdf类
// $options = new Options();
// $options->setDefaultFont('arial');
// $dompdf = new Dompdf($options);
$dompdf = new Dompdf();
$dompdf->loadHtml($html);
#(可选)设置纸张大小和方向
$dompdf->setPaper('A4', 'landscape');
# 将HTML渲染为PDF
$dompdf->render();
// 获取PDF内容
$pdfContent = $dompdf->output();
$this->fileController->path = '/V6/PDF';
// 将PDF内容保存到文件
@file_put_contents($savePath, $pdfContent);
// 创建一个文件实例
$file = new File($savePath);
return $this->fileController->api_upload_single($file);
}
/**
* 根据数据生成 Html
* @param $item
* @return string
*/
protected function html($item)
{
$html = '<html>';
$html .= '<body style="font-family:arial">';
$html .= '<h1>Hello, World!</h1>';
$html .= '<p>中文内容ffffff</p>';
$html .= '</body>';
$html .= '</html>';
return $html;
}
}