# Generate fdf then open in browser using php?



## adlamb (Nov 5, 2004)

Ok, so I assume that this is fairly simple but I don't have enough programming experience to get it to work correctly.

I have a html form that I have made to generate a fdf file and save it on the server. This file should then be opened using a pdf also on the server and insert the form details into the correct part of the pdf. It works fine but not in one simple step. I need to add something to my php such that when the form is submitted, the fdf file is saved and then immediately opened and displayed to the user. As it currently stands, the file needs opening manually. I've been messing about with fdf_open but without success!?


```
<?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();

        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{
                $data['Text2']=$_POST['Text2'];
            }

            if(!isset($_POST['Text3'])){
                die('You did not submit the correct form.');
            }

            $data['Text3']=$_POST['Text3'];
            $data['Text4']=$_POST['Text4'];
            $data['Text5']=$_POST['Text5'];

            // Date
            $data['Text1']=date('d/m/Y');

            // FDF file - function definition
            require_once 'createFDF.php';

            // file name .fdf
            $fdf_file=$_POST['Text2'].time().'.fdf';

            // the directory to write the result in
            $fdf_dir=dirname(__FILE__).'/results';

            // pdf file for data to go into
            $pdf_doc='../voucher_online.pdf';

            // generate the file content
            $fdf_data=createFDF($pdf_doc,$data);

        }
    }else{
        echo 'You did not submit a form.';
    }
?>
```


----------



## allnodcoms (Jun 30, 2007)

What do you do after this? Basically you seem to be doing everything right, but your code above doesn't actually do anything with content you've created...

Danny


----------



## adlamb (Nov 5, 2004)

No it doesn't, that's kinda the problem. I want to then display the generated fdf as a pdf to the user to allow them to save or print it. I believe I have to do something to merge the fdf with a pdf first but I have no idea how to achieve this...?


----------



## allnodcoms (Jun 30, 2007)

I think you are merging it with the code you have, but as the original .pdf is a template I think it's unlikely that the merge operation will overwrite it on disk. You probably merging in server side ram, so writing $fdf_data to disk would be my first guess, then echo out a header call with location set to the newly created file.

Give it a go and let me know...

Danny


----------



## allnodcoms (Jun 30, 2007)

adlamb said:


> As it currently stands, the file needs opening manually. I've been messing about with fdf_open but without success!?


There is a file created? What sort of file is it? If it's a .pdf then just 'header(Location: $file_URL);'

Danny


----------



## adlamb (Nov 5, 2004)

I think ive done that havent I? The last line in the code writes the contents of the html form to file as an fdf....?
Having said that, this only works locally, I have no idea how to make this work online as it will need to write to my server.

I also then need to someone make the fdf open-able in the web browser and I don't think this is possible unless it is merged with the pdf?


----------



## adlamb (Nov 5, 2004)

allnodcoms said:


> There is a file created? What sort of file is it? If it's a .pdf then just 'header(Location: $file_URL);'
> 
> Danny


sorry, yes that's right. My last post was written would wouldn't post for some reason so I didn't see your second reply.


----------



## allnodcoms (Jun 30, 2007)

OK, the createFDF() call takes a pdf template and fdf form data and merges them, then writes this to disk as an fdf? That doesn't make a whole lot of sense, have you tried renaming the new fie to .pdf and calling header() to redirect the browser? I have a sneaky suspicion that the new file is probably a properly formatted pdf just waiting to be viewed...

Danny


----------



## adlamb (Nov 5, 2004)

no unfortunately not, the new file is an fdf but as part of this file, it has a link to the correct pdf file location that should be used to merge all the other data. When you open the fdf file locally, it therefore opens the pdf as well and displays the pdf with all the form fields completed using the fdf data that originally came from the html form.
The createFDF command does simply that and unfortunately does not merge with the pdf, just links to it.

thanks


----------



## allnodcoms (Jun 30, 2007)

I think it does a bit more than that mate... I've Googled this and come up with this page. If you scroll down to the bottom (nearly) you get this:


```
// generate the file content
            $fdf_data=createFDF($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);
```
This looks like there is a data structure created by the call which holds a merged copy. 
Worth a try...

Danny


----------



## adlamb (Nov 5, 2004)

Yeah thats the one. I still cant find any evidence of an actual pdf being generated tho. It makes an fdf in the correct folder but nothing else. This fdf can be opened with a text viewer and simply shows the form fields and a relative link to the pdf template. It can therefore be opened as an fdf but it needs the pdf template to be in the correct location as well.
Very frustrating, so nearly doing what I need yet still not!
Thanks for you help tho.


----------

