Help with header() redirect and a PDF file

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

me!
Forum Contributor
Posts: 133
Joined: Sat Nov 04, 2006 8:45 pm

Help with header() redirect and a PDF file

Post by me! »

I am trying to get a PDF file (FDF file linked to a PDF file) to display in a SEPARATE striped down window ( no back button etc.) window with with no luck...

I think the problem is with my hearer info but I am having a problem finding the correct info.
This is what I have now:

Code: Select all

// show the new .pdf file
header("Location: http://localhost/QSL/results/$fdf_file");
header('Content-type: application/pdf');
header('Pragma: no-cache');
This does show the new PDF file but not how I want it. The other strange thing is it will not load in FireFox, when I call the page that has this code it opens IE to display the PDF file???
User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

Re: Help with header() redirect and a PDF file

Post by s.dot »

I think what you will want to do is use javascript to open the new window (so you can control the browser window).

Also, are you just trying to redirect to the file or read it in the browser window? You might want to try readfile() with appropriate headers and forget the redirect.
Set Search Time - A google chrome extension. When you search only results from the past year (or set time period) are displayed. Helps tremendously when using new technologies to avoid outdated results.
me!
Forum Contributor
Posts: 133
Joined: Sat Nov 04, 2006 8:45 pm

Re: Help with header() redirect and a PDF file

Post by me! »

I gave the java a shot and it worked the same as the header redirect, opened a new window in FireFox and then opened IE....
This appears to be a problem with FireFox and .fdf files, it just does not know how to open them?

I changed the content type to application/vnd.fdf but noticed no difference.

This morning I thought why not try just opening the .fdf file in the same window. I gave this code a shot, but nothing happens, so I must have something wrong?

Code: Select all

// get the fdf file
			$ourFileName = "http://localhost/QSL/results/$fdf_file";
			$fh = fopen($ourFileName, 'r') or die("Can't open file");
			fclose($fh);
Here is what I am trying to do:
Users info is in our DB, they go to web page and search for there records, then they can click and have a printable .pdf certificate come up with there info in it. I have all this working except the bring up the .pdf part. It works but not well. I think the java pop-up may be the best way to go? but it is all screwed up with FireFox. :?
lunarnet76
Forum Commoner
Posts: 67
Joined: Sun Apr 04, 2010 2:07 pm
Location: Edinburgh

Re: Help with header() redirect and a PDF file

Post by lunarnet76 »

hey,

Code: Select all

<?php
header('Content-type: application/pdf');
readfile('D:/1.pdf');
?>
works in ie/firefox/chrome, open it directly in the window^^
me!
Forum Contributor
Posts: 133
Joined: Sat Nov 04, 2006 8:45 pm

Re: Help with header() redirect and a PDF file

Post by me! »

lunarnet76 wrote:hey,

Code: Select all

<?php
header('Content-type: application/pdf');
readfile('D:/1.pdf');
?>
works in ie/firefox/chrome, open it directly in the window^^
This locks up all my browsers... :roll:
lunarnet76
Forum Commoner
Posts: 67
Joined: Sun Apr 04, 2010 2:07 pm
Location: Edinburgh

Re: Help with header() redirect and a PDF file

Post by lunarnet76 »

arf,
well the script I have given you I have used it for years with a lot of different customers and it has always worked ...
so you might have an error in your script that prevent the render, or your pdf is corrupted or maybe your adobe reader is failing!
Maybe you could give us the whole script and one of the pdf!
After that you will :drunk: lol
ell0bo
Forum Commoner
Posts: 79
Joined: Wed Aug 13, 2008 4:15 pm

Re: Help with header() redirect and a PDF file

Post by ell0bo »

The code above should work at pushing a file out. It's the same way I push out files via a control script.

You can always use javascript to set the src of an iframe. Just direct that to the file you then want to push out. Only problem then becomes IE trying to open up a reader in that frame, so it's not always recommended, but it should work for you.
me!
Forum Contributor
Posts: 133
Joined: Sat Nov 04, 2006 8:45 pm

Re: Help with header() redirect and a PDF file

Post by me! »

Please keep in mind that it is not exactly a .PDF document, it is a FDF file that calls a PDF on it's own The readfile(name.fdf) will thus bring up a linked PDF file. You do not open the PDF directly (it is blank) Got that :lol:

The FDF and PDF file are working just fine my problem is how do I display it when the script runs... :?:
I had some success with:

Code: Select all

			header("Location: http://localhost/QSL/results/$fdf_file");
			header('Content-type: application/pdf');
			exit;
But it would open IE to show the file if I was using FF... :crazy:


This is the code that will pull users info from the DB and make the FDF file. (makepdf.php)

Code: Select all

// db connect info, other functions not used by this file
include('includes/config.php'); 

// this will be removed, used for testin with no form
$contact_id = "2";

 
// check that data was submitted
if(isset($contact_id)) {

	// get the log informatiuon from the db
	$results = mysql_query("SELECT * FROM contacts WHERE id = $contact_id ") or die (mysql_error());  
	
	// check to make sure that only one record is found for the contact_id requested
	$numrows = mysql_num_rows($results);
	if($numrows != '1') {
		echo "ERRORO - No or Mutiple records found for your request, please contact our system admin.";
		die;
		}
		
		// set up array to hold form data
		$data=array();
		
		// put db data into array
		while ($mysql = mysql_fetch_array($results)) {
			$data['callsign'] = $mysql['callsign'];
			$data['callsignname'] = ''.$mysql['callsign'].' - '.$mysql['name'].'';
			$data['namecallsign'] = ''.$mysql['name'].' - '.$mysql['callsign'].'';
			$data['datetime'] = 'Date - Time: '.$mysql['date'].' - '.$mysql['time'].'';
			$data['time'] = 'Time: '.$mysql['time'].'';
			$data['band'] = 'Band: '.$mysql['band'].'';
			$data['freq'] = 'Frequency: '.$mysql['freq'].'';
			$data['mode'] = 'Mode: '.$mysql['mode'].'';
			}
		        
            // need the function definition
            require_once 'createFDF.php';
            
            // some variables to use
            
            // file name will be <the current timestamp>.fdf
            $fdf_file=$data['callsign'].'_'.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/QSL/100yrs.pdf';
            
            // 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);
						
      
    }else{
        echo 'You did not submit a form.';
    }

This is the createFDF.php

Code: Select all

/*
KOIVI HTML Form to FDF Parser for PHP (C) 2004 Justin Koivisto
Version 2.1.2
Last Modified: 9/12/2005

    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
    justin.koivisto@gmail.com
    http://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;
}
me!
Forum Contributor
Posts: 133
Joined: Sat Nov 04, 2006 8:45 pm

Re: Help with header() redirect and a PDF file

Post by me! »

This may have something to do with it...

Code: Select all

function createFDF($file,$info){
    $data="%FDF-1.2\n%âãÏÓ\n1 0 obj\n<< \n/FDF << /Fields [ ";
I just noticed it, any ideas what it should be?
lunarnet76
Forum Commoner
Posts: 67
Joined: Sun Apr 04, 2010 2:07 pm
Location: Edinburgh

Re: Help with header() redirect and a PDF file

Post by lunarnet76 »

ok ... could you try that

Code: Select all

function fdf($file,$infos){
    $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.') >>
    >>
    endobj
    trailer
    <<
    /Root 1 0 R

    >>
    %%EOF';
    return $data;
}
it worked with me but I'm not sure that the file is actually a fdf ... I just dowloaded one from internet!
lunarnet76
Forum Commoner
Posts: 67
Joined: Sun Apr 04, 2010 2:07 pm
Location: Edinburgh

Re: Help with header() redirect and a PDF file

Post by lunarnet76 »

ok ... seems that the /ID was not working in fact... but try and tell me we never know^^
me!
Forum Contributor
Posts: 133
Joined: Sat Nov 04, 2006 8:45 pm

Re: Help with header() redirect and a PDF file

Post by me! »

What is this?
%âãÏÓ (see above)
Li0rE
Forum Commoner
Posts: 41
Joined: Wed Jun 07, 2006 6:26 am

Re: Help with header() redirect and a PDF file

Post by Li0rE »

nothing was happening on the page because you were just calling the fopen() function, not echoing any information
fopen opens the file for the script, not for the browser


you should use echo file_get_contents("http://path/to/filename.php"); to display the file as file_get_contents() reads a file into a string that can then be used.
lunarnet76
Forum Commoner
Posts: 67
Joined: Sun Apr 04, 2010 2:07 pm
Location: Edinburgh

Re: Help with header() redirect and a PDF file

Post by lunarnet76 »

I don't know what it means but

Code: Select all

%âãÏÓ
seems a random value they have chosen to represent a fdf ;-)
anyway have you tried without the id tag?
me!
Forum Contributor
Posts: 133
Joined: Sat Nov 04, 2006 8:45 pm

Re: Help with header() redirect and a PDF file

Post by me! »

Li0rE wrote:nothing was happening on the page because you were just calling the fopen() function, not echoing any information
fopen opens the file for the script, not for the browser


you should use echo file_get_contents("http://path/to/filename.php"); to display the file as file_get_contents() reads a file into a string that can then be used.
Ah!
I over looked that... :oops:

Code: Select all

echo file_get_contents("http://localhost/QSL/results/$fdf_file");
Outputs:
%FDF-1.2 %âãÏÓ 1 0 obj << /FDF << /Fields [ <><><><><><><><>] /F (http://localhost/QSL/100yrs.pdf) /ID [ <2b659bbdf7367d7ae488876a332b7b85> ] >> >> endobj trailer << /Root 1 0 R >> %%EOF
Post Reply