|
|
@@ -0,0 +1,68 @@
|
|
|
+<?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
|
|
|
+ */
|
|
|
+
|
|
|
+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);
|
|
|
+
|
|
|
+fwrite(STDERR, "PDF: {$outPath}\nMD: {$mdPath}\n");
|