| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107 |
- <?php
- /**
- * 生成「老师组卷 + 学情分析」周报 PDF(与 report_teacher_weekly_stats.php 同源数据)
- *
- * 用法:
- * php scripts/report_teacher_weekly_stats_pdf.php
- * php scripts/report_teacher_weekly_stats_pdf.php /path/to/out.pdf
- * php artisan report:teacher-weekly-pdf [--days=7]
- * 或:TEACHER_WEEKLY_REPORT_DAYS=14 php scripts/...
- *
- * 输出:
- * - teacher-weekly-stats-{Y-m-d}_{His}.pdf(His = 24 小时制六位数字时分秒,如 143052)
- * - 不配 latest 文件名;后台「打开 PDF」会自动选目录内最新的一份带时间戳 PDF。
- */
- use League\CommonMark\Environment\Environment;
- use League\CommonMark\Extension\CommonMark\CommonMarkCoreExtension;
- use League\CommonMark\Extension\GithubFlavoredMarkdownExtension;
- use League\CommonMark\MarkdownConverter;
- require __DIR__ . '/../vendor/autoload.php';
- ob_start();
- require __DIR__ . '/report_teacher_weekly_stats.php';
- $markdown = ob_get_clean();
- $env = new Environment([
- 'html_input' => 'allow',
- 'allow_unsafe_links' => false,
- ]);
- $env->addExtension(new CommonMarkCoreExtension());
- $env->addExtension(new GithubFlavoredMarkdownExtension());
- $converter = new MarkdownConverter($env);
- $bodyHtml = $converter->convert($markdown)->getContent();
- $css = <<<'CSS'
- <style>
- /* mPDF 内置 Sun-ExtA(sun-exta),覆盖中文、日文、韩文;勿用 DejaVu,否则会显示为方块 */
- body { font-family: 'sun-exta', sans-serif; font-size: 10pt; color: #111; }
- h2 { font-size: 14pt; margin: 0 0 8px 0; }
- h3 { font-size: 11pt; margin: 16px 0 8px 0; }
- blockquote { margin: 0 0 10px 0; padding: 6px 10px; background: #f9fafb; border-left: 3px solid #d1d5db; font-size: 9pt; color: #4b5563; }
- table { border-collapse: collapse; width: 100%; margin: 8px 0 12px 0; }
- th, td { border: 1px solid #d1d5db; padding: 4px 6px; text-align: center; vertical-align: middle; }
- th { background: #f3f4f6; font-weight: 600; }
- .weekly-chart { margin: 8px 0 14px 0; page-break-inside: avoid; }
- .weekly-chart p { margin: 0 0 6px 0; }
- /* 每日对比:左学案、右学情,各占 50%,独立纵轴刻度 */
- .weekly-chart-pair { width: 100%; border-collapse: collapse; margin: 0; page-break-inside: avoid; }
- .weekly-chart-pair td,
- .weekly-chart-pair th { border: none !important; padding: 2px 4px !important; vertical-align: top !important; text-align: center !important; background: transparent !important; }
- .weekly-teacher-table { table-layout: fixed; width: 100%; font-size: 9pt; }
- .weekly-teacher-table th,
- .weekly-teacher-table td { text-align: center; vertical-align: middle; }
- .weekly-teacher-table col.col-name { width: 18mm !important; max-width: 18mm !important; }
- .weekly-teacher-table col.col-slash { width: 11%; }
- .weekly-teacher-table th:nth-child(2),
- .weekly-teacher-table td.td-name {
- width: 18mm !important;
- max-width: 18mm !important;
- overflow: hidden;
- text-overflow: ellipsis;
- white-space: nowrap;
- padding: 4px 3px;
- box-sizing: border-box;
- color: #111827;
- font-size: 9pt;
- }
- .weekly-teacher-table .td-slash {
- font-variant-numeric: tabular-nums;
- font-size: 9pt;
- color: #111827;
- }
- .weekly-teacher-table .td-stu { font-size: 8.5pt; }
- .weekly-teacher-table .td-name .teacher-id { color: #6b7280; font-size: 9pt; font-weight: normal; }
- </style>
- CSS;
- $html = '<!DOCTYPE html><html><head><meta charset="UTF-8">'.$css.'</head><body>'.$bodyHtml.'</body></html>';
- $defaultOut = storage_path('app/reports/teacher-weekly-stats-'.date('Y-m-d_His').'.pdf');
- $outPath = $argv[1] ?? $defaultOut;
- $dir = dirname($outPath);
- if (! is_dir($dir)) {
- mkdir($dir, 0755, true);
- }
- $mpdf = new \Mpdf\Mpdf([
- 'mode' => 'utf-8',
- 'format' => 'A4',
- 'margin_top' => 14,
- 'margin_bottom' => 14,
- 'margin_left' => 12,
- 'margin_right' => 12,
- 'default_font' => 'sun-exta',
- 'autoScriptToLang' => true,
- 'autoLangToFont' => true,
- ]);
- $mpdf->WriteHTML($html);
- $mpdf->Output($outPath, \Mpdf\Output\Destination::FILE);
- $mdPath = preg_replace('/\.pdf$/i', '.md', $outPath);
- file_put_contents($mdPath, $markdown);
- fwrite(STDERR, "PDF: {$outPath}\nMD: {$mdPath}\n");
|