report_teacher_weekly_stats_pdf.php 3.9 KB

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