report_teacher_weekly_stats_pdf.php 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. <?php
  2. /**
  3. * 生成「老师组卷 + 学情分析」周报 PDF(与 report_teacher_weekly_stats.php 同源数据)
  4. *
  5. * 用法:
  6. * php scripts/report_teacher_weekly_stats_pdf.php
  7. * php scripts/report_teacher_weekly_stats_pdf.php /path/to/out.pdf
  8. *
  9. * 输出:
  10. * - 带时间戳:teacher-weekly-stats-{Y-m-d}_{His}.pdf(His = 24 小时制的时-分-秒,如 143052)
  11. * - 固定别名:storage/app/reports/teacher-weekly-stats-latest.pdf(覆盖为最近一次生成,供后台一键打开)
  12. */
  13. use League\CommonMark\Environment\Environment;
  14. use League\CommonMark\Extension\CommonMark\CommonMarkCoreExtension;
  15. use League\CommonMark\Extension\GithubFlavoredMarkdownExtension;
  16. use League\CommonMark\MarkdownConverter;
  17. require __DIR__ . '/../vendor/autoload.php';
  18. ob_start();
  19. require __DIR__ . '/report_teacher_weekly_stats.php';
  20. $markdown = ob_get_clean();
  21. $env = new Environment([
  22. 'html_input' => 'allow',
  23. 'allow_unsafe_links' => false,
  24. ]);
  25. $env->addExtension(new CommonMarkCoreExtension());
  26. $env->addExtension(new GithubFlavoredMarkdownExtension());
  27. $converter = new MarkdownConverter($env);
  28. $bodyHtml = $converter->convert($markdown)->getContent();
  29. $css = <<<'CSS'
  30. <style>
  31. body { font-family: 'DejaVu Sans', sans-serif; font-size: 10pt; color: #111; }
  32. h2 { font-size: 14pt; margin: 0 0 8px 0; }
  33. h3 { font-size: 11pt; margin: 16px 0 8px 0; }
  34. blockquote { margin: 0 0 10px 0; padding: 6px 10px; background: #f9fafb; border-left: 3px solid #d1d5db; font-size: 9pt; color: #4b5563; }
  35. table { border-collapse: collapse; width: 100%; margin: 8px 0 12px 0; }
  36. th, td { border: 1px solid #d1d5db; padding: 4px 6px; vertical-align: top; }
  37. th { background: #f3f4f6; font-weight: 600; }
  38. </style>
  39. CSS;
  40. $html = '<!DOCTYPE html><html><head><meta charset="UTF-8">'.$css.'</head><body>'.$bodyHtml.'</body></html>';
  41. $defaultOut = storage_path('app/reports/teacher-weekly-stats-'.date('Y-m-d_His').'.pdf');
  42. $outPath = $argv[1] ?? $defaultOut;
  43. $dir = dirname($outPath);
  44. if (! is_dir($dir)) {
  45. mkdir($dir, 0755, true);
  46. }
  47. $mpdf = new \Mpdf\Mpdf([
  48. 'mode' => 'utf-8',
  49. 'format' => 'A4',
  50. 'margin_top' => 14,
  51. 'margin_bottom' => 14,
  52. 'margin_left' => 12,
  53. 'margin_right' => 12,
  54. 'default_font' => 'dejavusans',
  55. ]);
  56. $mpdf->WriteHTML($html);
  57. $mpdf->Output($outPath, \Mpdf\Output\Destination::FILE);
  58. $mdPath = preg_replace('/\.pdf$/i', '.md', $outPath);
  59. file_put_contents($mdPath, $markdown);
  60. $latestPdf = storage_path('app/reports/teacher-weekly-stats-latest.pdf');
  61. $latestMd = storage_path('app/reports/teacher-weekly-stats-latest.md');
  62. @copy($outPath, $latestPdf);
  63. @copy($mdPath, $latestMd);
  64. fwrite(STDERR, "PDF: {$outPath}\nMD: {$mdPath}\n");
  65. fwrite(STDERR, "最新别名(一键打开): {$latestPdf}\n");