Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to call a PDF file using PHP?

Status
Not open for further replies.

Apollo6

Technical User
Jan 27, 2000
418
US
I have built some pages that query against a PostgreSQL database. Depending on selections from a dropdown, the following page displays additional dropdowns. Each page is collecting information on a specific PDF report to display. The report itself lives in a specific directory on the server.

1. How do I combine the variable from Page#1 with the variable on Page#2? From Page#2, I have a submit button that when clicked, it takes the collected information and lauches a PDF file.

Page#1 - $period
Page#2 - $report_name

This gives me the path: //ip_address/$period/$report_name

2. What syntax is used to call/launch a PDF file?

Please ask if this isn't making since.
 
If you have multiple fields of form data that must be carried over from one form to the next, you have two options.

You can have each successive script collect the field data and place the data in hidden fields on the successive forms.

You can also use sessions.



If you are using Apache and a link directly to the file, Apache probably already knows the document type for a PDF. Check the file mime.types to make sure.
Want the best answers? Ask the best questions: TANSTAAFL!
 
I am using Apache... This is my first go around with trying to generate a PDF file from PHP. I found the following snipet but cannot get it to run...

{
$pdf = pdf_new();
pdf_open_file($pdf, "$path_result");
}

The $path_result is generated from a query I do against the database. It is the entire path including the name of the pdf file. So knowing exactly what the file path is, how do I tell PHP to launch Adobe and open the file?

Or if you could point me to a site that would be a good resource.

Thanks for the help.
 
umm, your not trying to create/edit/open a pdf, are you ?
your trying to launch it in the browser ... then you either
that has nothing todo with php really
the browser may do whatever it thinks right with the file its receiving
you just have to direct the browser to the url of the pdf
or open it with php and then send a pdf header and the data to the browser
 
IRZYXEL,

The part you said "open it with php and then send a pdf header and the data to the browser" is exactly what I would like to do.

From my last query, I have the entire url for the desired report in a variable. My problem is that I don't know how to take that and open it with PHP, send a pdf header and data to the browser...
 
Assuming that your filename is "foo.pdf", then you can send the file through a PHP script as follows:

Code:
<?php

header (&quot;Content-type: application/pdf&quot;);
header (&quot;Content-disposition: attachment; filename=foo.pdf&quot;);

readfile (&quot;foo.pdf&quot;);

?>

With Netscape, Opera, IE, and Mozilla, this will cause a dialog box to open, asking what you want to do with the file. If you instruct your browser to save the file, the appropriate filename will be pre-populated in the filesystem dialog box. Want the best answers? Ask the best questions: TANSTAAFL!
 
sleipnir214,

I still can't get it to work correctly. I'm very new at this game so thanks for any help you can give. I've pasted the block of code below. I tried to clean it up as much as possible. I'll understand if you don't take much time to look at it but thanks for the help anyway...


<?
if (!$submit)
{
?>
<form action=&quot;<? echo $_SERVER['PHP_SELF']; ?>&quot; method=&quot;POST&quot;>
<?
}
else
$host = &quot;***.***.***.***&quot;;
$user = &quot;****&quot;;
$pass = &quot;****&quot;;
$db = &quot;****&quot;;

$connection = pg_connect (&quot;host=$host dbname=$db user=$user
password=$pass&quot;);

if (!$connection)
{
die(&quot;Could not open connection to database server&quot;);
}
//Populate dropdown with available report names
$qryName = &quot;SELECT DISTINCT rpt_name FROM tblmain WHERE period='$period'&quot;;
$name_result = pg_query($connection, $qryName) or die(&quot;Error in query: $qryName.
&quot; . pg_last_error($connection));
$rows = pg_num_rows($name_result);
if ($rows > 0)
{
echo &quot;<select name=rpt_name>\n&quot;;
for ($i=0;$i<$rows;$i++)
{
$row = pg_fetch_array($name_result);
echo &quot;<option value=$row[0]>$row[0]</option>\n&quot;;
}
echo &quot;</select>\n&quot;;
}
else
{
?>
<form action=&quot;<? echo $_SERVER['PHP_SELF']; ?>&quot; method=&quot;POST&quot;>
<?
}
?>
<br />
<br />
<hr />
<br />
</div>
//This is the query that returns the exact url
<?
{
$qryPath = &quot;SELECT rpt_path FROM tblmain WHERE rpt_name='$rpt_name'&quot;;
$path_result = pg_query($connection, $qryPath) or die(&quot;Error in query: $qryPath.
&quot; . pg_last_error($connection));
$rows = pg_num_rows($path_result);
if ($rows = 1)
{
header (&quot;Content-type: application/pdf&quot;);
header (&quot;Content-disposition: attachment; filename=$path_result&quot;);

readfile ($path_result);
}
else
{
?>
<form action=&quot;<? echo $_SERVER['PHP_SELF']; ?>&quot; method=&quot;POST&quot;>
<?
}
pg_close($connection);
}
?>
//End of logic that returns url for existing report

<div align=&quot;center&quot;>
<input type=&quot;submit&quot; name=&quot;submit&quot; value=&quot;SUBMIT&quot;>
</div>
 
I surmise from your posts and what little I can make of this code that you want the script to output a general set of report categories in a dropdown. When that is submitted, it should output a more specific list or reports available in a category. When that is submitted, it should return the report in a PDF. Sounds like three operations to me -- but it looks like you're trying to perform the last two in a single step. Remember that you're dealing with HTTP: you don't have a continuous interation with the user. You ouput something, then he reacts, then your code reacts, then he reacts, then your code reacts, etc.

To be honest, I don't know how much of the enormous numbers of errors I'm seeing are because your inexperience or because of your removing blocks of code to protect sensitive data.

You have what are obviously blocks of code that aren't enclosed in blocking tags (e.g. &quot;{&quot;, &quot;}&quot;). [Look immediately after your first &quot;else&quot;.]

You have sections of code mysteriously blocked off, but with no looping or branching constructs before it. [Look immediately after the comment that reads &quot;//This is the query that returns the exact url&quot;.]

You switch contexts between HTML output and PHP code. PHP's print statement can output many lines of text -- you don't have to switch out of PHP mode to output blocks of HTML.

Your indentation wanders quite a bit.

And you haven't provided any error messages, an exact descriptions of what this code should do, or a description of what this code is doing.
Want the best answers? Ask the best questions: TANSTAAFL!
 
sleipnir214,

A lot of inexperience and some removing blocks of code to protect sensitive data.

I'll go back and apply some of what you stated in your last post to the script.

Just learning to crawl at this point.

Thanks for your help...
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top