Automatically opening pdf files stored in a bytea field

Automatically opening pdf files stored in a bytea field

am 11.10.2005 12:58:30 von Adam Witney

Hi,

This may be off-topic as regards the database aspect, but im sure people on
this list must come across this problem.

I have PDF files stored in a bytea field in the database and I want to all
the user to click a link on the web page and have the file automatically
opened in acrobat (or whatever they have set to read the pdf). It works for
most browsers except for in Internet Explorer on windows (surprise
surprise!). Here is my code

$sql_data = "SELECT filename, file_data FROM dba_suppl WHERE dba_suppl_id =
".$dba_suppl_id.";";

if($stat2 = execute($sql_data))
{
if($rows = pg_numrows($stat2))
{
$data = pg_fetch_array($stat2, 0);

header("Content-type: application/pdf");
header('Content-Disposition: attachment;
filename="'.$data['filename'].'"');

echo pg_unescape_bytea($data['file_data']);
}
}

If I click it it opens acrobat but acrobat gives an error. If I right click
the link and save to disk I can open the file from there no problem. As I
say, on my mac and on FireFox on windows it does the right thing... Its just
IE on windows!

Is there something I am forgetting to do here? Or is it just IE and there is
no way around it??

Thanks for any help

Adam


--
This message has been scanned for viruses and
dangerous content by MailScanner, and is
believed to be clean.


---------------------------(end of broadcast)---------------------------
TIP 4: Have you searched our list archives?

http://archives.postgresql.org

Re: Automatically opening pdf files stored in a bytea

am 11.10.2005 13:30:30 von Frank Bax

At 06:58 AM 10/11/05, Adam Witney wrote:
>I have PDF files stored in a bytea field in the database and I want to all
>the user to click a link on the web page and have the file automatically
>opened in acrobat (or whatever they have set to read the pdf). It works for
>most browsers except for in Internet Explorer on windows (surprise
>surprise!). Here is my code
>
> header("Content-type: application/pdf");
> header('Content-Disposition: attachment;
> filename="'.$data['filename'].'"');


Here's mine...
if(isset($_SERVER['HTTP_USER_AGENT']) &&
strpos($_SERVER['HTTP_USER_AGENT'],'MSIE'))
header('Content-Type: application/force-download');
else
header('Content-Type: application/octet-stream');
header('Content-Length: '.strlen($this->buffer));
header('Content-disposition: attachment; filename="'.$name.'"');


But on a some systems (both Win98 & WinXP), user must save/open instead of
open directly - haven't figured out why yet.


---------------------------(end of broadcast)---------------------------
TIP 4: Have you searched our list archives?

http://archives.postgresql.org

Re: Automatically opening pdf files stored in a bytea

am 11.10.2005 17:52:37 von operationsengineer1

--- Frank Bax wrote:

> At 06:58 AM 10/11/05, Adam Witney wrote:
> >I have PDF files stored in a bytea field in the
> database and I want to all
> >the user to click a link on the web page and have
> the file automatically
> >opened in acrobat (or whatever they have set to
> read the pdf). It works for
> >most browsers except for in Internet Explorer on
> windows (surprise
> >surprise!). Here is my code
> >
> > header("Content-type: application/pdf");
> > header('Content-Disposition: attachment;=20
> > filename=3D"'.$data['filename'].'"');
>=20
>=20
> Here's mine...
> if(isset($_SERVER['HTTP_USER_AGENT']) &&
> =20
> strpos($_SERVER['HTTP_USER_AGENT'],'MSIE'))
> header('Content-Type:
> application/force-download');
> else
> header('Content-Type:
> application/octet-stream');
> header('Content-Length:
> '.strlen($this->buffer));
> header('Content-disposition: attachment;
> filename=3D"'.$name.'"');
>=20
>=20
> But on a some systems (both Win98 & WinXP), user
> must save/open instead of=20
> open directly - haven't figured out why yet. =20
>=20
>=20
> ---------------------------(end of
> broadcast)---------------------------
> TIP 4: Have you searched our list archives?
>=20
> http://archives.postgresql.org

i have had very good success storing links to pdf
files in the database and storing the actual files in
the locations pointed to by the link.

my application is work instructions - i have a naming
convention that i use so once you know the part
number, you know the name of the pdf if it exists.

i have two types of pdf documents and a name prefix
differentiates them from each other.

both firefox and ie will open these pdfs as expected.

if this approach makes sense for your application, you
may want to try it.


=09
__________________________________=20
Start your day with Yahoo! - Make it your home page!=20
http://www.yahoo.com/r/hs

---------------------------(end of broadcast)---------------------------
TIP 3: Have you checked our extensive FAQ?

http://www.postgresql.org/docs/faq

Re: Automatically opening pdf files stored in a bytea field

am 11.10.2005 19:56:41 von Volkan YAZICI

Hi,

On 10/11/05, Adam Witney wrote:
> I have PDF files stored in a bytea field in the database and I want to all
> the user to click a link on the web page and have the file automatically
> opened in acrobat (or whatever they have set to read the pdf).

Look at header() function's manual page [1] for PDF files. You'll see
lots of IE related problems and their fixes there.

[1] http://tr2.php.net/manual/en/function.header.php

Regards.

---------------------------(end of broadcast)---------------------------
TIP 5: don't forget to increase your free space map settings

Re: Automatically opening pdf files stored in a bytea

am 11.10.2005 21:47:38 von Adam Witney

On 11/10/05 12:30 pm, "Frank Bax" wrote:

> At 06:58 AM 10/11/05, Adam Witney wrote:
>> I have PDF files stored in a bytea field in the database and I want to all
>> the user to click a link on the web page and have the file automatically
>> opened in acrobat (or whatever they have set to read the pdf). It works for
>> most browsers except for in Internet Explorer on windows (surprise
>> surprise!). Here is my code
>>
>> header("Content-type: application/pdf");
>> header('Content-Disposition: attachment;
>> filename="'.$data['filename'].'"');
>
>
> Here's mine...
> if(isset($_SERVER['HTTP_USER_AGENT']) &&
> strpos($_SERVER['HTTP_USER_AGENT'],'MSIE'))
> header('Content-Type: application/force-download');
> else
> header('Content-Type: application/octet-stream');
> header('Content-Length: '.strlen($this->buffer));
> header('Content-disposition: attachment; filename="'.$name.'"');
>
>
> But on a some systems (both Win98 & WinXP), user must save/open instead of
> open directly - haven't figured out why yet.

From the link sent by Volkan, adding these header lines fixes the problem

header("Pragma: public");
header("Expires: 0");
header("Cache-Control: private");

Thanks for the help guys

Adam


--
This message has been scanned for viruses and
dangerous content by MailScanner, and is
believed to be clean.


---------------------------(end of broadcast)---------------------------
TIP 2: Don't 'kill -9' the postmaster

Re: Automatically opening pdf files stored in a bytea field

am 12.10.2005 03:29:04 von Christopher Kings-Lynne

I'm not 100% sure you need the pg_unescape_bytea - I thought that was
already done automatically?

Chris

Adam Witney wrote:
> Hi,
>
> This may be off-topic as regards the database aspect, but im sure people on
> this list must come across this problem.
>
> I have PDF files stored in a bytea field in the database and I want to all
> the user to click a link on the web page and have the file automatically
> opened in acrobat (or whatever they have set to read the pdf). It works for
> most browsers except for in Internet Explorer on windows (surprise
> surprise!). Here is my code
>
> $sql_data = "SELECT filename, file_data FROM dba_suppl WHERE dba_suppl_id =
> ".$dba_suppl_id.";";
>
> if($stat2 = execute($sql_data))
> {
> if($rows = pg_numrows($stat2))
> {
> $data = pg_fetch_array($stat2, 0);
>
> header("Content-type: application/pdf");
> header('Content-Disposition: attachment;
> filename="'.$data['filename'].'"');
>
> echo pg_unescape_bytea($data['file_data']);
> }
> }
>
> If I click it it opens acrobat but acrobat gives an error. If I right click
> the link and save to disk I can open the file from there no problem. As I
> say, on my mac and on FireFox on windows it does the right thing... Its just
> IE on windows!
>
> Is there something I am forgetting to do here? Or is it just IE and there is
> no way around it??
>
> Thanks for any help
>
> Adam
>
>


---------------------------(end of broadcast)---------------------------
TIP 1: if posting/reading through Usenet, please send an appropriate
subscribe-nomail command to majordomo@postgresql.org so that your
message can get through to the mailing list cleanly

Re: Automatically opening pdf files stored in a bytea

am 12.10.2005 14:28:37 von Adam Witney

I sent this but it didn't seem to appear....

> At 06:58 AM 10/11/05, Adam Witney wrote:
>> I have PDF files stored in a bytea field in the database and I want to all
>> the user to click a link on the web page and have the file automatically
>> opened in acrobat (or whatever they have set to read the pdf). It works for
>> most browsers except for in Internet Explorer on windows (surprise
>> surprise!). Here is my code
>>
>> header("Content-type: application/pdf");
>> header('Content-Disposition: attachment;
>> filename="'.$data['filename'].'"');
>
>
> Here's mine...
> if(isset($_SERVER['HTTP_USER_AGENT']) &&
> strpos($_SERVER['HTTP_USER_AGENT'],'MSIE'))
> header('Content-Type: application/force-download');
> else
> header('Content-Type: application/octet-stream');
> header('Content-Length: '.strlen($this->buffer));
> header('Content-disposition: attachment; filename="'.$name.'"');
>
>
> But on a some systems (both Win98 & WinXP), user must save/open instead of
> open directly - haven't figured out why yet.

From the link sent by Volkan, adding these header lines fixes the problem

header("Pragma: public");
header("Expires: 0");
header("Cache-Control: private");

Thanks for the help guys

Adam


--
This message has been scanned for viruses and
dangerous content by MailScanner, and is
believed to be clean.


---------------------------(end of broadcast)---------------------------
TIP 5: don't forget to increase your free space map settings

Re: Automatically opening pdf files stored in a bytea field

am 12.10.2005 19:08:29 von Volkan YAZICI

Hi,

On 10/12/05, Christopher Kings-Lynne wrote:
> I'm not 100% sure you need the pg_unescape_bytea - I thought that was
> already done automatically?

Yep, I agree. Furthermore, (un)escape routines will consume so much
system CPU. If you can, try to use parameters (pg_query_params() and
pg_send_params()) in any bytea/lo storage. This makes you free from
escaping without any potential SQL Injection threats.

Regards.

---------------------------(end of broadcast)---------------------------
TIP 1: if posting/reading through Usenet, please send an appropriate
subscribe-nomail command to majordomo@postgresql.org so that your
message can get through to the mailing list cleanly

Re: Automatically opening pdf files stored in a bytea field

am 13.10.2005 04:13:29 von Christopher Kings-Lynne

> Yep, I agree. Furthermore, (un)escape routines will consume so much
> system CPU. If you can, try to use parameters (pg_query_params() and
> pg_send_params()) in any bytea/lo storage. This makes you free from
> escaping without any potential SQL Injection threats.

PostgreSQL prepared statements don't escape bytea afaik

Chris


---------------------------(end of broadcast)---------------------------
TIP 2: Don't 'kill -9' the postmaster