report_teacher_weekly_stats_pdf.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  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. * php artisan report:teacher-weekly-pdf [--days=7]
  9. * 或:TEACHER_WEEKLY_REPORT_DAYS=14 php scripts/...
  10. *
  11. * 输出:
  12. * - teacher-weekly-stats-{Y-m-d}_{His}.pdf(His = 24 小时制六位数字时分秒,如 143052)
  13. * - 不配 latest 文件名;后台「打开 PDF」会自动选目录内最新的一份带时间戳 PDF。
  14. */
  15. use League\CommonMark\Environment\Environment;
  16. use League\CommonMark\Extension\CommonMark\CommonMarkCoreExtension;
  17. use League\CommonMark\Extension\GithubFlavoredMarkdownExtension;
  18. use League\CommonMark\MarkdownConverter;
  19. require __DIR__ . '/../vendor/autoload.php';
  20. ob_start();
  21. require __DIR__ . '/report_teacher_weekly_stats.php';
  22. $markdown = ob_get_clean();
  23. $env = new Environment([
  24. 'html_input' => 'allow',
  25. 'allow_unsafe_links' => false,
  26. ]);
  27. $env->addExtension(new CommonMarkCoreExtension());
  28. $env->addExtension(new GithubFlavoredMarkdownExtension());
  29. $converter = new MarkdownConverter($env);
  30. $bodyHtml = $converter->convert($markdown)->getContent();
  31. $css = <<<'CSS'
  32. <style>
  33. /* mPDF 内置 Sun-ExtA(sun-exta),覆盖中文、日文、韩文;勿用 DejaVu,否则会显示为方块 */
  34. body { font-family: 'sun-exta', sans-serif; font-size: 10pt; color: #111; }
  35. h2 { font-size: 14pt; margin: 0 0 8px 0; }
  36. h3 { font-size: 11pt; margin: 16px 0 8px 0; }
  37. blockquote { margin: 0 0 10px 0; padding: 6px 10px; background: #f9fafb; border-left: 3px solid #d1d5db; font-size: 9pt; color: #4b5563; }
  38. table { border-collapse: collapse; width: 100%; margin: 8px 0 12px 0; }
  39. th, td { border: 1px solid #d1d5db; padding: 4px 6px; text-align: center; vertical-align: middle; }
  40. th { background: #f3f4f6; font-weight: 600; }
  41. .weekly-chart { margin: 8px 0 14px 0; page-break-inside: avoid; }
  42. .weekly-chart p { margin: 0 0 6px 0; }
  43. /* 每日对比:左学案、右学情,各占 50%,独立纵轴刻度 */
  44. .weekly-chart-pair { width: 100%; border-collapse: collapse; margin: 0; page-break-inside: avoid; }
  45. .weekly-chart-pair td,
  46. .weekly-chart-pair th { border: none !important; padding: 2px 4px !important; vertical-align: top !important; text-align: center !important; background: transparent !important; }
  47. .weekly-teacher-table { table-layout: fixed; width: 100%; font-size: 9pt; }
  48. .weekly-teacher-table th,
  49. .weekly-teacher-table td { text-align: center; vertical-align: middle; }
  50. .weekly-teacher-table col.col-name { width: 18mm !important; max-width: 18mm !important; }
  51. .weekly-teacher-table col.col-slash { width: 11%; }
  52. .weekly-teacher-table th:nth-child(2),
  53. .weekly-teacher-table td.td-name {
  54. width: 18mm !important;
  55. max-width: 18mm !important;
  56. overflow: hidden;
  57. text-overflow: ellipsis;
  58. white-space: nowrap;
  59. padding: 4px 3px;
  60. box-sizing: border-box;
  61. color: #111827;
  62. font-size: 9pt;
  63. }
  64. .weekly-teacher-table .td-slash {
  65. font-variant-numeric: tabular-nums;
  66. font-size: 9pt;
  67. color: #111827;
  68. }
  69. .weekly-teacher-table .td-stu { font-size: 8.5pt; }
  70. .weekly-teacher-table .td-name .teacher-id { color: #6b7280; font-size: 9pt; font-weight: normal; }
  71. </style>
  72. CSS;
  73. $html = '<!DOCTYPE html><html><head><meta charset="UTF-8">'.$css.'</head><body>'.$bodyHtml.'</body></html>';
  74. $defaultOut = storage_path('app/reports/teacher-weekly-stats-'.date('Y-m-d_His').'.pdf');
  75. $outPath = $argv[1] ?? $defaultOut;
  76. $dir = dirname($outPath);
  77. if (! is_dir($dir)) {
  78. mkdir($dir, 0755, true);
  79. }
  80. $mpdf = new \Mpdf\Mpdf([
  81. 'mode' => 'utf-8',
  82. 'format' => 'A4',
  83. 'margin_top' => 14,
  84. 'margin_bottom' => 14,
  85. 'margin_left' => 12,
  86. 'margin_right' => 12,
  87. 'default_font' => 'sun-exta',
  88. 'autoScriptToLang' => true,
  89. 'autoLangToFont' => true,
  90. ]);
  91. $mpdf->WriteHTML($html);
  92. $mpdf->Output($outPath, \Mpdf\Output\Destination::FILE);
  93. $mdPath = preg_replace('/\.pdf$/i', '.md', $outPath);
  94. file_put_contents($mdPath, $markdown);
  95. fwrite(STDERR, "PDF: {$outPath}\nMD: {$mdPath}\n");