2013-08-29 2 views
0

У меня есть вопрос с вопросом о прослушивании. Есть ли способ прикреплять xdf-файл к существующему файлу pdf с php на лету? Я создал PDF-файл, и мне нужно прикрепить его к файлу PDF сразу после генерации pdf. Не могли бы вы помочь?Приложить файл xdf к pdf с PHP

ответ

1

Это может быть слишком поздно для вас, но вот класс, который я написал много лет назад для CodeIgniter. Он может быть адаптирован к вашим потребностям.

<?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\n%âãÏÓ\n1 0 obj\n<< \n/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.="] \n/F (".$file.") /ID [ <".md5(microtime(true)).">\n] >>". 
     " \n>> \nendobj\ntrailer\n". 
     "<<\n/Root 1 0 R \n\n>>\n%%EOF\n"; 
    log_message('debug', "FDF string is:\n" . $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; 

    } 

} 

} 

данные передаются в виде ассоциативного, ключи которого являются имена полей и значения содержат данные на полях. Метод вывода используется для автоматического удаления файла FDF в браузер. Надеюсь, это поможет кому-то там.

+0

Большое спасибо за ответ. Я решил эту проблему в прошлом. Не помните хорошо, как, но нашли решение на неясном форуме. В любом случае, спасибо. Очень ценю усилия! –

Смежные вопросы