前往小程序,Get更优阅读体验!
立即前往
发布
社区首页 >专栏 >批量导出pdf 和导出图片

批量导出pdf 和导出图片

作者头像
全栈程序员站长
发布2022-09-05 18:44:32
发布2022-09-05 18:44:32
1.5K00
代码可运行
举报
运行总次数:0
代码可运行

大家好,又见面了,我是你们的朋友全栈君。

1.

基于已经制作好的pdf和上传到服务器上后,前端只需要传递pdf链接 数组的形式

代码语言:javascript
代码运行次数:0
复制
/**
     *  批量导出pdf
     * @access  public
     * @param   id     分类编号  cat_name分类名称
     * @return  array
     */
    public function actionExportpdf(){
        $ids = Yii::$app->request->post('id');
        $dcat_id = Yii::$app->request->post('dcat_id');
        if (!$ids) {
            return ['code' => -2, 'msg' => '参数错误'];
        }
        //商品文件属性
        $dataFlie = new \backend\models\MmcDataFile();
        $goodsDataFiles = $dataFlie->getGoodsDataFile($ids);
        $goodsDataFiles= array_filter($goodsDataFiles,function ($v)use($dcat_id){
            if($v['dcat_id']==$dcat_id){
                return TRUE;
            }
        });
        foreach ($goodsDataFiles as $val){
            $url = $val['file_url'];
        }
        ob_end_clean();
        ob_start();
        $file = file_get_contents($url);
        header("Content-type:application/octet-stream");
        $filename = pathinfo($url,PATHINFO_BASENAME);
        header("Content-Disposition:attachment;filename=" . $filename);
        header("Accept-ranges:bytes");
        ob_end_flush();
        exit($file);
    }

//导出图片    基于前端传递图片二维数组链接   把属于二维数组中的一维数组多个图片合并后并下载
    public function actionExportimg()
    {
        $ids = Yii::$app->request->post('id');
        $goodsThumb= Yii::$app->request->post('goods_thumb');
        if (!$ids || !$goodsThumb) {
            return ['code' => -2, 'msg' => '参数错误'];
        }
        if(is_array($goodsThumb)){
            $root =  $_SERVER['DOCUMENT_ROOT'];
            $num = strrpos($root,'/');
            $rootdir = substr($root,0,$num);
            $num = strrpos($rootdir,'/');
            $rootdir = substr($root,0,$num);
            if(count($goodsThumb)<=0){return json_encode(['code'=>-101,'message'=>'未接收到图片','data'=>'']);}
            $editor = Grafika::createEditor();
            $imageBack = Grafika::createBlankImage(1200, count($goodsThumb)*1200);//创建一个750*750的空白图像
            $backurl =$rootdir . '/backend/web/';
            foreach ($goodsThumb as $k=>$pic_path) {
                $dir_name =$backurl . $pic_path;
                $editor->open($image,$dir_name );//打开1.jpeg并且存放到$image1
                $editor->resizeFill($image, '1200','1200');//居中剪裁。就是把较短的变缩放到200px,然后将长边的大于200px的部分居中剪裁掉,图片不会变形
                $editor->blend($imageBack, $image, 'normal', 1, 'top-center',0,$k*1200);//将两个图像合成在一起,第一个图像作为基础,第二个图像在顶部。支持多种混合模式
                $num = strrpos($dir_name,'/');
                $savedir = substr($dir_name,0,$num);
                $savedir = $savedir . '/all_'.$ids.'.jpg';
                $file = $editor->save($imageBack, $savedir );//imageBack保存为all.jpg
            }
    //        var_dump($savedir);die;
            if($file){
    //        $shorturl = str_replace($backurl,"",$savedir);
            $httpurl = $savedir;
            // var_dump($httpurl);die;
            $files = file_get_contents($httpurl);
            header("Content-type:application/octet-stream");
            $filename = pathinfo($httpurl, PATHINFO_BASENAME);
            header("Content-Disposition:attachment;filename=" . $filename);
            header("Accept-ranges:bytes");
            exit($files);
        }else{
                $file = file_get_contents($goodsThumb);
                header("Content-type:application/octet-stream");
                $filename = pathinfo($goodsThumb, PATHINFO_BASENAME);
                header("Content-Disposition:attachment;filename=" . $filename);
                header("Accept-ranges:bytes");
                exit($file);
            }
        }

发布者:全栈程序员栈长,转载请注明出处:https://javaforall.cn/135367.html原文链接:https://javaforall.cn

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2022年6月5,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 1.
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档