|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Console\Commands;
|
|
|
|
|
|
|
|
use App\Models\ProjectAssociation\ProjectAssociation;
|
|
|
|
use App\Models\File\DataFile;
|
|
|
|
use Dompdf\Dompdf;
|
|
|
|
use Dompdf\Options;
|
|
|
|
use Illuminate\Console\Command;
|
|
|
|
|
|
|
|
class ProjectFilePDF extends Command
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* 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 $AiccWechat;
|
|
|
|
|
|
|
|
protected $DataFile;
|
|
|
|
|
|
|
|
protected $time;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Create a new command instance.
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function __construct()
|
|
|
|
{
|
|
|
|
$this->AiccWechat = new ProjectAssociation();
|
|
|
|
$this->DataFile = new DataFile();
|
|
|
|
$this->time = date("Y-m-d");
|
|
|
|
parent::__construct();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Execute the console command.
|
|
|
|
*
|
|
|
|
* @return int
|
|
|
|
*/
|
|
|
|
public function handle()
|
|
|
|
{
|
|
|
|
$data = $this->get_data();
|
|
|
|
# 详细数据
|
|
|
|
$items = $data['items'];
|
|
|
|
# 总分页
|
|
|
|
$totalPage = $data['totalPage'];
|
|
|
|
$this->dataPush($items);
|
|
|
|
if ($totalPage > 1) {
|
|
|
|
for ($page = 2; $page <= $totalPage; $page++) {
|
|
|
|
$da = $this->get_data();
|
|
|
|
$this->dataPush($da['items']);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
$this->info('生成pdf完成');
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 数据生成并保存
|
|
|
|
* @param array $items
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function dataPush(array $items)
|
|
|
|
{
|
|
|
|
foreach ($items as $item) {
|
|
|
|
$project_id = $item->project_id;
|
|
|
|
$application_id = $item->wx_id;
|
|
|
|
$wx_user_id = $item->wx_user_id;
|
|
|
|
// todo 根据项目查询数据
|
|
|
|
$project_data = [];
|
|
|
|
$html = $this->html($project_data);
|
|
|
|
$filename = hash('md5', $this->time . '-' . $project_id . '-' . $application_id);
|
|
|
|
$file_path = $this->savePDF($html, $filename);
|
|
|
|
$this->DataFile->saveData(compact('project_id', 'application_id', 'file_path') + ['time' => $this->time]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public function get_data($page = 1, $perPage = 20)
|
|
|
|
{
|
|
|
|
$data = $this->AiccWechat->allData($page, $perPage);
|
|
|
|
# 总条数
|
|
|
|
$total = $data['total'];
|
|
|
|
if (empty($total)) {
|
|
|
|
$this->error('暂无绑定AICC微信数据');
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
# 详细数据
|
|
|
|
$items = $data['items'];
|
|
|
|
# 总分页
|
|
|
|
$totalPage = $data['totalPage'];
|
|
|
|
# 当前页
|
|
|
|
$currentPage = $data['currentPage'];
|
|
|
|
return compact('total', 'items', 'totalPage', 'currentPage');
|
|
|
|
}
|
|
|
|
|
|
|
|
public function savePDF($html, $filename)
|
|
|
|
{
|
|
|
|
// todo 生成中文有问题
|
|
|
|
# 实例化并使用dompdf类
|
|
|
|
$options = new Options();
|
|
|
|
$options->setDefaultFont('arial');
|
|
|
|
$dompdf = new Dompdf($options);
|
|
|
|
$dompdf->loadHtml($html);
|
|
|
|
#(可选)设置纸张大小和方向
|
|
|
|
$dompdf->setPaper('A4', 'landscape');
|
|
|
|
|
|
|
|
# 将HTML渲染为PDF
|
|
|
|
$dompdf->render();
|
|
|
|
|
|
|
|
// 获取PDF内容
|
|
|
|
$pdfContent = $dompdf->output();
|
|
|
|
|
|
|
|
// 指定保存路径和文件名
|
|
|
|
$savePath = public_path('PDF/' . $filename . '.pdf');
|
|
|
|
|
|
|
|
// 将PDF内容保存到文件
|
|
|
|
file_put_contents($savePath, $pdfContent);
|
|
|
|
|
|
|
|
// 输出保存成功消息
|
|
|
|
return $savePath;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 根据数据生成 Html
|
|
|
|
* @param $item
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
protected function html($item)
|
|
|
|
{
|
|
|
|
$html = '<html>';
|
|
|
|
$html .= '<body style="font-family:arial">';
|
|
|
|
$html .= '<h1>Hello, World!</h1>';
|
|
|
|
$html .= '<p>中文内容</p>';
|
|
|
|
$html .= '</body>';
|
|
|
|
$html .= '</html>';
|
|
|
|
return $html;
|
|
|
|
}
|
|
|
|
} |
...
|
...
|
|