批量导出pdf 和导出图片

1.

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

/**
     *  批量导出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);
            }
        }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值