$blocks */ public function write(array $blocks, string $absolutePath): void { $phpWord = new PhpWord; $phpWord->setDefaultFontName('Microsoft YaHei'); $phpWord->setDefaultFontSize(11); $section = $phpWord->addSection([ 'marginTop' => 1200, 'marginBottom' => 1200, 'marginLeft' => 1200, 'marginRight' => 1200, ]); foreach ($blocks as $block) { match ($block['type']) { 'h1' => $section->addTitle($block['text'] ?? '', 1), 'h2' => $section->addTitle($block['text'] ?? '', 2), 'h3' => $section->addTitle($block['text'] ?? '', 3), 'p' => $section->addText($block['text'] ?? '', null, ['spaceAfter' => 120]), 'bullet' => $section->addListItem($block['text'] ?? '', 0, null, null, ['spaceAfter' => 60]), 'link' => $this->addLinkParagraph($section, $block['text'] ?? '来源', $block['url'] ?? ''), 'spacer' => $section->addTextBreak(1), default => null, }; } $dir = dirname($absolutePath); if (! is_dir($dir)) { mkdir($dir, 0755, true); } $writer = IOFactory::createWriter($phpWord, 'Word2007'); $writer->save($absolutePath); } protected function addLinkParagraph($section, string $label, string $url): void { if ($url !== '' && preg_match('~^https?://~i', $url)) { $section->addLink($url, $label, ['color' => '0563C1', 'underline' => 'single']); $section->addTextBreak(1); return; } $section->addText($label, null, ['spaceAfter' => 120]); } }