0
点赞
收藏
分享

微信扫一扫

php合并PDF FPDI

承蒙不弃 2022-02-15 阅读 117

目录

参考资料FPDF   下载网址  http://www.fpdf.org/FPDI     下载网址 FPDI free PDF document importer ▷ setasign.com————————————————

效果图

实现代码


参考资料
FPDF   下载网址  http://www.fpdf.org/
FPDI     下载网址  https://www.setasign.com/products/fpdi/about/
————————————————

效果图

 

实现代码

    /*多个PDF合一
    *@param array  $files  多个PDF的绝对路径
    *@param string  $number  文件名
     * */
    public function pdf_synthetic(array $files,  $number): array
    {
        import('fpdf.fpdf', EXTEND_PATH);
        import('fpdi.fpdi', EXTEND_PATH);
        // define some files to concatenate
        $pdf = new \Fpdi();//Ln
        //增加大pdf合成和 新路径标识

        // iterate through the files
        foreach ($files AS $file) {
            if (!file_exists($file)) {
                $return_info['code'] = 'error';
                $return_info['data'] = '文件不存在:' . $file;
                return $return_info;
            }
            // get the page count
            try {
                $pageCount = $pdf->setSourceFile($file);
            } catch (\Exception $e) {
                $return_info['code'] = 'error';
                $return_info['data'] = '合并失败: ' . $e->getMessage();
                return $return_info;
            }

            // iterate through all pages
            for ($pageNo = 1; $pageNo <= $pageCount; $pageNo++) {
                // import a page
                $templateId = $pdf->importPage($pageNo);
                // get the size of the imported page
                $size = $pdf->getTemplateSize($templateId);

                // create a page (landscape or portrait depending on the imported page size)
                if ($size['w'] > $size['h']) {
                    $pdf->AddPage('L', array($size['w'], $size['h']));
                } else {
                    $pdf->AddPage('P', array($size['w'], $size['h']));
                }
                // use the imported page
                $pdf->useTemplate($templateId);

                $pdf->SetFont('Helvetica');
                $pdf->SetXY(5, 5);
                //$pdf->Write(8, 'A simple concatenation demo with FPDI');
            }
        }

        // Output the new PDF
        $directory=ROOT_PATH.'public';//根目录
        $path     = $directory. '/static/uploads/synthetic/' . date('Ym') . '/';
        //检测并创建文件夹
        if ($this->exists_dir($path) === false) {
            $return_info['code'] = 'error';
            $return_info['data'] = '目录不存在 ';
            return $return_info;
        }

        $new_pdf_name_path = $path . $number . '.pdf';
        $new_pdf_name      = '/static/uploads/synthetic/' . date('Ym') . '/' . $number . '.pdf';
        $pdf->Output($new_pdf_name_path, 'f');
        $return_info['code'] = 'success';
        $return_info['data'] = $new_pdf_name;
        return $return_info;
    }

    /**
     * Notes: 检测文件夹是否存在 不存在自动创建文件夹
     * User: ZHOU WEI YUN
     * Date: 2021/3/23 0023
     * Time: 上午 10:51
     * @param string $path 文件的绝对路径
     * @return bool  false 文件创建失败 其他返回为true
     */
    public function exists_dir(string $path): bool
    {
        if (file_exists($path) === false) {
            $path     = str_replace("\\", '/', $path);
            $path_arr = explode('/', $path);
            $ping     = '';
            //循环逐级检测 不存在则创建
            foreach ($path_arr as $k => $v) {
                $ping .= $v . '/';
                if (file_exists($ping) === false && !mkdir($ping, 755) && is_dir($ping)) {
                    return false;
                }
            }
        }
        return true;
    }
    
    public function demo(){
      PRINT_R($this->pdf_synthetic([ROOT_PATH.'public/static/uploads/preview/202202/demo.pdf',ROOT_PATH.'public/static/uploads/preview/202202/demo.pdf'],'demo'));
    }
举报

相关推荐

0 条评论