dalaowang

先说下需求:正常点击免责声明是下载的文件,根据用户需求是点击预览,所以这里利用phpword生成一个静态页面并进行预览。不多说,直接上代码附带类文件。

类文件:https://pan.baidu.com/s/1yhZu5JyrtIfKnGllRblPtg  提取码:fbu3  把类文件放在vendor目录下。

html代码:

<!-- 这里是点击下载的html代码,隐藏域是当前的id -->
<p><input type="hidden" value="{$vo.file_id}" class="file_id"><a href="#" class="btn btn-primary click">点击下载</a></p>

<script type="">
    //触发ajax把id传到控制器
    $(\'.click\').click(function(){
        id = $(this).prev().val();
         $.ajax({
            type: "POST",
            url: "{:url(\'home/Single/ajax\')}",
            data: {id:id},
            dataType: "json",
            success: function (msg) {
              window.open(msg.html)//新页面打开预览
            }
          });
         return false;
    })
</script>

  后台代码:

//首先头部引用类
use PhpOffice\PhpWord\IOFactory;
use PhpOffice\PhpWord\PhpWord;

public function ajax(){
        //根据id查询数据库存的文档路径
        $file_id = $_POST[\'id\'];
        $info = M(\'download_file\')->where([\'file_id\'=>$file_id])->find();
        $IOFactory = new PhpWord();
        $phpWord = \PhpOffice\PhpWord\IOFactory::load(\'./\'.$info[\'file_url\']);
        $xmlWriter = \PhpOffice\PhpWord\IOFactory::createWriter($phpWord, "HTML");
        //生成hello.html文件路径
        $xmlWriter->save(\'./Public/doc/hello.html\');
        //判断文件是否存在,额庵后传到前台
        if(file_exists(\'./Public/doc/hello.html\')){
            $html=\'/Public/doc/hello.html\';
            $dataa = array(\'html\'=>$html,\'url\'=>$url);
            echo json_encode($dataa);
            
        }else{
            $dataa = array(\'html\'=>\'0\');
            echo json_encode($dataa);
        }
    }

  

分类:

技术点:

相关文章:

  • 2022-02-03
  • 2022-01-26
  • 2022-12-23
  • 2021-09-15
  • 2021-09-27
  • 2022-02-14
  • 2022-02-09
猜你喜欢
  • 2021-12-09
  • 2021-09-27
  • 2021-10-15
  • 2021-08-15
  • 2022-02-07
  • 2022-02-26
相关资源
相似解决方案