Brak polskich znaków w pliku FDF

0

Witam Was serdecznie! Nie wiem czy tutaj znajdzie się ktoś, kto mi pomoże, ale warto spróbować. Otóż od wielu wielu dni próbuję zrobić coś takiego: na stronie mam formularz HTMl. Po wypełnieniu i kliknięciu Generuj, powinien się wygenerować plik PDF z wypełnionymi - pobranymi z formularza HTML - danymi. Tworzę to na tej zasadzie, że konstruuję w PHP plik FDF, który później łączony jest z szablonem PDF (szablon PDF zawiera nazwane pola - puste oczywiście). Posiłkuję się kodem ze strony http://koivi.com/fill-pdf-form-fields/
Chciałbym stworzyć plik FDF lub XFDF który w sobie ma polskie znaki - a kod, który pokażę poniżej, nie generuje polskich znaków. W rezultacie po otwarciu pliku FDF lub XFDF otwiera się Adobe Reader/Acrobat z wypełnionymi polami wwziętymi z pliku FDF/XFDF ALE Z KRZACZKAMI! Naprawdę wypiję za zdrowie tego, kto rozwikła tę zagadkę Wshechświata:

example_form.php:

<?php
header("Content-Type: text/html; charset=utf-8"); 
?>



<form method="post" action="http://localhost/generator/submit_form.php">
 <fieldset>
  <table>
   <tr><td>Enter Your Name</td><td><input type="text" name="Text2"></td></tr>
   <tr><td>Favorite Color</td><td><input type="text" name="Text3"></td></tr>
   <tr><td>Age</td><td><input type="text" name="Text4"></td></tr>
  </table>
  <p>
   <b>Comments:</b><br />
   <textarea name="Text5"></textarea>
  </p>
  <input type="submit" value="Generate FDF Data" />
 </fieldset>
</form>
 

submit_form.php

 <?php
    // check that a form was submitted
    if(isset($_POST) && is_array($_POST) && count($_POST)){
        // we will use this array to pass to the createFDF function
        $data=array();
        
        // This displays all the data that was submitted. You can
        // remove this without effecting how the FDF data is generated.
        echo'<pre>POST '; print_r($_POST);echo '</pre>';

        if(isset($_POST['Text2'])){
            // the name field was submitted
            $pat='`[^a-z0-9\s]+$`i';
            if(empty($_POST['Text2']) || preg_match($pat,$_POST['Text2'])){
                // no value was submitted or something other than a
                // number, letter or space was included
                die('Invalid input for Text2 field.');
            }else{
                // if this passed our tests, this is safe
                $data['Text2']=$_POST['Text2'];
            }
            
            if(!isset($_POST['Text3'])){
                // Why this? What if someone is spoofing form submissions
                // to see how your script works? Only allow the script to
                // continue with expected data, don't be lazy and insecure ;)
                die('You did not submit the correct form.');
            }
            
            // Check your data for ALL FIELDS that you expect, ignore ones you
            // don't care about. This is just an example to illustrate, so I
            // won't check anymore, but I will add them blindly (you don't want
            // to do this in a production environment).
            $data['Text3']=$_POST['Text3'];
            $data['Text4']=$_POST['Text4'];
            $data['Text5']=$_POST['Text5'];
            
            // I wanted to add the date to the submissions
            $data['Text1']=date('Y-m-d H:i:s');
            
            // if we got here, the data should be valid,
            // time to create our FDF file contents
            
            // need the function definition
            require_once 'createXFDF.php';
            
            // some variables to use
            
            // file name will be <the current timestamp>.fdf
            $fdf_file=time().'.fdf';
            
            // the directory to write the result in
            $fdf_dir=dirname(__FILE__).'/results';
            
            // need to know what file the data will go into
            $pdf_doc='http://localhost/generator/Project2.pdf';
            
            // generate the file content
            $fdf_data=createXFDF($pdf_doc,$data);

            // this is where you'd do any custom handling of the data
            // if you wanted to put it in a database, email the
            // FDF data, push ti back to the user with a header() call, etc.

            // write the file out
            if($fp=fopen($fdf_dir.'/'.$fdf_file,'w')){
                fwrite($fp,$fdf_data,strlen($fdf_data));
                echo $fdf_file,' written successfully.';
            }else{
                die('Unable to create file: '.$fdf_dir.'/'.$fdf_file);
            }
            fclose($fp);
        }
    }else{
        echo 'You did not submit a form.';
    }
?>

I tutaj do wyboru createFDF.php lub createXFDF.php:

createFDF.php

<?php
/*
KOIVI HTML Form to FDF Parser for PHP (C) 2004 Justin Koivisto
Version 2.1.1
Last Modified: 2005-04-22

    This library is free software; you can redistribute it and/or modify it
    under the terms of the GNU Lesser General Public License as published by
    the Free Software Foundation; either version 2.1 of the License, or (at
    your option) any later version.

    This library is distributed in the hope that it will be useful, but
    WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
    or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
    License for more details.

    You should have received a copy of the GNU Lesser General Public License
    along with this library; if not, write to the Free Software Foundation,
    Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 

    Full license agreement notice can be found in the LICENSE file contained
    within this distribution package.

    Justin Koivisto
    [email protected]
    http://www.koivi.com
*/

/*
*   createFDF
*
*   Takes values submitted via an HTML form and fills in the corresponding
*   fields into an FDF file for use with a PDF file with form fields.
*
*   @param  $file   The pdf file that this form is meant for. Can be either
*                   a url or a file path.
*   @param  $info   The submitted values in key/value pairs. (eg. $_POST)
*   @result Returns the FDF file contents for further processing.
*/
function createFDF($file,$info){
    $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(time()).">\n] >>".
        " \n>> \nendobj\ntrailer\n".
        "<<\n/Root 1 0 R \n\n>>\n%%EOF\n";
    return $data;
}
?>
 

createXFDF.php:

 <?php
/*
KOIVI HTML Form to FDF Parser for PHP (C) 2004 Justin Koivisto
Version 1.1
Last Modified: 2010-02-17

    This library is free software; you can redistribute it and/or modify it
    under the terms of the GNU Lesser General Public License as published by
    the Free Software Foundation; either version 2.1 of the License, or (at
    your option) any later version.

    This library is distributed in the hope that it will be useful, but
    WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
    or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
    License for more details.

    You should have received a copy of the GNU Lesser General Public License
    along with this library; if not, write to the Free Software Foundation,
    Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 

    Full license agreement notice can be found in the LICENSE file contained
    within this distribution package.

    Justin Koivisto
    [email protected]
    http://koivi.com
*/

/**
 * createXFDF
 * 
 * Tales values passed via associative array and generates XFDF file format
 * with that data for the pdf address sullpiled.
 * 
 * @param string $file The pdf file - url or file path accepted
 * @param array $info data to use in key/value pairs no more than 2 dimensions
 * @param string $enc default UTF-8, match server output: default_charset in php.ini
 * @return string The XFDF data for acrobat reader to use in the pdf form file
 */
function createXFDF($file,$info,$enc='UTF-8'){
    $data='<?xml version="1.0" encoding="'.$enc.'"?>'."\n".
    	'<xfdf xmlns="http://ns.adobe.com/xfdf/" xml:space="preserve">'."\n".
		'<fields>'."\n";
    foreach($info as $field => $val){
    	$data.='<field name="'.$field.'">'."\n";
        if(is_array($val)){
            foreach($val as $opt)
                $data.='<value>'.htmlentities($opt).'</value>'."\n";
        }else{
            $data.='<value>'.htmlentities($val).'</value>'."\n";
        }
        $data.='</field>'."\n";
    }
    $data.='</fields>'."\n".
    	'<ids original="'.md5($file).'" modified="'.time().'" />'."\n".
    	'<f href="'.$file.'" />'."\n".
    	'</xfdf>'."\n";
    return $data;
}
?>

Ja już szukałem i pytałem wszedzie - bez skutku. Może tutaj znajdę odpowiedź.

0

Tam jest taki problem że trzeba wgrać czcionke z polskimi znakami. Ja ci radzę, żebyś użył http://mpdf.bpm1.com/ <- tutaj jest wszystko gotowe, i nie bedziesz mieć dziwnych rzeczy z pdf'ami

1 użytkowników online, w tym zalogowanych: 0, gości: 1