| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778 |
- <?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
- *
- * 输出:
- * - 带时间戳:teacher-weekly-stats-{Y-m-d}_{His}.pdf(His = 24 小时制的时-分-秒,如 143052)
- * - 固定别名:storage/app/reports/teacher-weekly-stats-latest.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>
- body { font-family: 'DejaVu Sans', 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; vertical-align: top; }
- th { background: #f3f4f6; font-weight: 600; }
- </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' => 'dejavusans',
- ]);
- $mpdf->WriteHTML($html);
- $mpdf->Output($outPath, \Mpdf\Output\Destination::FILE);
- $mdPath = preg_replace('/\.pdf$/i', '.md', $outPath);
- file_put_contents($mdPath, $markdown);
- $latestPdf = storage_path('app/reports/teacher-weekly-stats-latest.pdf');
- $latestMd = storage_path('app/reports/teacher-weekly-stats-latest.md');
- @copy($outPath, $latestPdf);
- @copy($mdPath, $latestMd);
- fwrite(STDERR, "PDF: {$outPath}\nMD: {$mdPath}\n");
- fwrite(STDERR, "最新别名(一键打开): {$latestPdf}\n");
|