dongzhijing8202 2013-08-29 07:16
浏览 199

使用PHP将xdf文件附加到pdf

I have a mind bugging question. Is there a way to attach an xdf file to an existing pdf file with php on the fly ? I have generated the pdf and I need to attach it to the pdf file right after the generation of pdf. Can you help please ?

  • 写回答

1条回答 默认 最新

  • dter8514 2014-05-07 18:26
    关注

    This may be too late for you, but here is a class that I wrote years ago for CodeIgniter. It can be adjusted to your needs.

    <?php
    class Fdf {
    
    private $ci;
    private $db;
    
    private $testing = false; // are we testing?
    
    private $fields = array();
    private $pdf_file = '';
    private $save_where = array( 'browser' => 'inline' );
    
    public function __construct( $config = array() ){
    
        $this->ci =& get_instance();
    
        $this->ci->load->database();
    
        $this->db =& $this->ci->db;
    
        if ( $this->testing && $this->CI->config->item('log_threshold') < 2 ){
            $this->ci->config->set_item( 'log_threshold', 2 );
        }
    
        log_message('debug', 'Fdf class loaded');
    
    }
    
    /**
    * This function generates a string in format FDF for use with the indicated pdf form $file.
    * $info is an associative array holding the form fields in the key => value form
    * $file is the URL of the pdf file that holds the form fields we want to populate
    *
    * @param mixed $info associative array of the fields
    * @param mixed $file URL of the pdf file
    *
    * @author Alin Mazilu
    */
    public function create_fdf_string( $info, $file ){
    
        $data="%FDF-1.2
    %âãÏÓ
    1 0 obj
    << 
    /FDF << /Fields [ ";
    
        foreach($info as $field => $val){
            if(is_array($val)){
                $data.='<</T('.$field.')/V[';
                foreach($val as $opt)
                    $data.='('.trim($opt).')';
                $data.=']>>';
            }else{
                $data.='<</T('.$field.')/V('.trim($val).')>>';
            }
        }
    
        $data.="] 
    /F (".$file.") /ID [ <".md5(microtime(true)).">
    ] >>".
            " 
    >> 
    endobj
    trailer
    ".
            "<<
    /Root 1 0 R 
    
    >>
    %%EOF
    ";
        log_message('debug', "FDF string is:
    " . $data);
        return $data;
    
    }
    
    public function set_fields( $data ){
    
        if (is_object($data)){
            $this->fields = get_object_vars($data);
        } elseif ( is_array($data) ){
            $this->fields = $data;
        } else {
            return false;
        }
        return true;
    }
    
    public function set_pdf_file( $file = '' ){
    
        if ( empty($file) ){
            return false;
        }
    
        $this->pdf_file = $file;
        return true;
    
    }
    
    public function set_output( $where ){
    
        if ( empty( $where ) || !is_array( $where ) ){
            return false;
        }
    
        $this->save_where = $where;
    
        return true;
    
    }
    
    public function output( $where = array( 'browser' => 'inline' ) ){
    
        $str = $this->create_fdf_string( $this->fields, $this->pdf_file );
    
        $len = strlen( $str );
    
        if ( empty( $where ) ){
            $where = $this->save_where;
        }
    
        $file_name = explode( '/', $this->pdf_file );
    
        $file_name = end( $file_name );
    
    
    
        if ( isset( $where['browser'] ) ){
    
            header('Content-Type: application/vnd.fdf');
            header('Cache-Control: public, must-revalidate, max-age=0'); // HTTP/1.1
            header('Pragma: public');
            header('Expires: Sat, 26 Jul 1997 05:00:00 GMT'); // Date in the past
            header('Last-Modified: ' . gmdate('D, d M Y H:i:s') . ' GMT');
            header('Content-Disposition: ' . $where['browser'] . '; filename="' . $file_name . '";');
            header('Content-Length: ' . $len);
    
            echo $str;
    
        }
    
    }
    
    }
    

    The the data is passed in as an associative whose keys are the field names and the values contain the fields' data. The output method is used to dump the FDF file to the browser on the fly. Hope this helps someone out there.

    评论

报告相同问题?

悬赏问题

  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题