php url转word
composer require phpoffice/phpword<?php
<p>require_once 'vendor/autoload.php';</p>
<p>use PhpOffice\PhpWord\IOFactory;
use PhpOffice\PhpWord\Shared\Html;
use PhpOffice\PhpWord\PhpWord;</p>
<p>// 创建一个新的Word文档
$phpWord = new PhpWord();</p>
<p>// 添加一个新的空白页
$section = $phpWord->addSection();</p>
<p>// 你想要抓取的URL
$url = 'http://example.com';</p>
<p>// 从URL获取内容
$content = file_get_contents($url);</p>
<p>// 将HTML内容添加到Word文档中
Html::addHtml($section, $content, false, false);</p>
<p>// 将文档保存为Word文件
$writer = IOFactory::createWriter($phpWord, 'Word2007');
$writer->save('document.docx');</p>
<p>echo 'Word文档已创建。';
?>还有一种不需要安装的扩展的,不过对样式的兼容性不太友好
public function worddown(){
$t=input("t")==1 ? 1:"";
$url = _doadmin."admin/login/cpdetails$t?id=".input("id");</p>
<pre><code> // 从URL获取HTML内容
$content = file_get_contents($url);
// 创建一个临时的docx文件
$tempDocx = tempnam(sys_get_temp_dir(), 'Docx');
// 将HTML内容写入临时文件
$handle = fopen($tempDocx, 'wb');
fwrite($handle, $content);
fclose($handle);
$file=uniqid();
// 设置头信息,以便浏览器将文件识别为Word文档并下载
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename="'.$file.'.docx"');
header('Content-Length: ' . filesize($tempDocx));
// 读取临时文件并发送到客户端
readfile($tempDocx);
unlink($tempDocx);
}</code></pre><p style=""></p>
本文是原创文章,采用 CC BY-NC-ND 4.0 协议,完整转载请注明来自 阿牛哥
评论
匿名评论
隐私政策
你无需删除空行,直接评论以获取最佳展示效果