help submitting a form which serves a file

0 views
Skip to first unread message

spectrus

unread,
Oct 9, 2008, 3:14:41 PM10/9/08
to Prototype & script.aculo.us
Hello,

I am trying to submit a form that calls PHP which generates a PDF file
and shoots it back to the user. When I do it using GET directly in the
browser address bar it works. However when I submit the form with
Prototype (either POST or GET) it doesn't:

$('form_pdf').request({ parameters: params });

I see the response headers in firebug set correctly as ' application/x-
download' (which are set in the back-end). Furthermore, the response
tab in the firebug console shows the actual contents of the PDF file
(flat-text gibberish).

It must be something trivial, perhaps setting some parameter for the
request method. Does Prototype somehow override the headers when it
receives responses to its AJAX requests?

If anyone has ever dealt with a similar issue, any pointers are
greatly appreciated.

Regards,
Max

lfortin

unread,
Oct 9, 2008, 5:24:34 PM10/9/08
to Prototype & script.aculo.us
Hi Max,


Using Form#request with a form performs an Ajax.Request, using the
inputs of the form as serialized parameters.
This might not bring you the behavior you expect to have.

Maybe if you simply do a flat form submit, using $
('form_pdf').submit(); with all its inputs(hidden, text, ...) pre-
filled:


<form id="form_pdf" action="/any/url/">
<input type="hidden" name="param1" value="anything">
<input type="hidden" name="param2" value="anything">
...
</form>


hope it helps,

-Laurent

Brian Williams

unread,
Oct 9, 2008, 5:34:08 PM10/9/08
to prototype-s...@googlegroups.com
I have been working like mad with PDFs built dynamically with PHP the last week.

One thing to be *absolutely* sure of is that you do not have ANY
output before the PDF download starts.

If you have Apache2 with mod_header loaded, there is a FilesMatch
directive to always force a download for a PDF

<FilesMatch "\.(?i:pdf)$">
ForceType application/octet-stream
Header set Content-Disposition attachment
</FilesMatch>


On a side note, you do not need AJAX for this feature to feed the user
a PDF file for download just set the form action to empty (same page)
and it should leave the page/browser right where it is with the form
still filled out, unless you are trying to feed a PDF to a div... you
aren't are you?

by the by which PDF library did you use to generate with?

spectrus

unread,
Oct 10, 2008, 9:03:48 AM10/10/08
to Prototype & script.aculo.us
Laurent,

Yes, it does indeed work with a flat submit, thank you very much for
the tip. The thing is, I was hoping that somehow I would be able to
get the PDF download starting, with optionally getting some data back
for a callback in case something fails and I do get JSON as a
response, which is impossible if I do a flat submit (am I wrong?). I
guess you can't have your cake and eat it too.

Brian,

Thank you for the Apache directive! Nice tip, although unfortunately I
am in an environment where I can't rely on Apache directives, since I
don't control that. Yes, I don't need AJAX to feed a file to the user,
but as I mentioned above, I would love to have it in case something
fails - in that case I can let the user know that something went
wrong. Perhaps there's a way to do that which I can't see?..

We are using FPDF (http://www.fpdf.org/), which is free and suits our
needs pretty well (generating two types of rather simple reports, as
well as an invoice). Anything better out there?

Best regards,
Max


On Oct 9, 5:34 pm, "Brian Williams" <brianw1...@gmail.com> wrote:
> I have been working like mad with PDFs built dynamically with PHP the last week.
>
> One thing to be *absolutely* sure of is that you do not have ANY
> output before the PDF download starts.
>
> If you have Apache2 with mod_header loaded, there is a FilesMatch
> directive to always force a download for a PDF
>
> <FilesMatch "\.(?i:pdf)$">
>   ForceType application/octet-stream
>   Header set Content-Disposition attachment
> </FilesMatch>
>
> On a side note, you do not need AJAX for this feature to feed the user
> a PDF file for download just set the form action to empty (same page)
> and it should leave the page/browser right where it is with the form
> still filled out, unless you are trying to feed a PDF to a div... you
> aren't are you?
>
> by the by which PDF library did you use to generate with?
>

Brian Williams

unread,
Oct 10, 2008, 2:12:26 PM10/10/08
to prototype-s...@googlegroups.com
On Fri, Oct 10, 2008 at 9:03 AM, spectrus <spec...@gmail.com> wrote:
>
> Laurent,
>
> Yes, it does indeed work with a flat submit, thank you very much for
> the tip. The thing is, I was hoping that somehow I would be able to
> get the PDF download starting, with optionally getting some data back
> for a callback in case something fails and I do get JSON as a
> response, which is impossible if I do a flat submit (am I wrong?). I
> guess you can't have your cake and eat it too.

unless i am missing something you won't be able to feed to the same
browser instance two different things - application/pdf and text/html
without using a response handler to parse some kind of response from
the server (prolly json would be easiest) with a textual response and
then a hidden iframe that you can redirect to a link provided in the
json.

>
> Brian,
>
> Thank you for the Apache directive! Nice tip, although unfortunately I
> am in an environment where I can't rely on Apache directives, since I
> don't control that. Yes, I don't need AJAX to feed a file to the user,
> but as I mentioned above, I would love to have it in case something
> fails - in that case I can let the user know that something went
> wrong. Perhaps there's a way to do that which I can't see?..

see above

>
> We are using FPDF (http://www.fpdf.org/), which is free and suits our
> needs pretty well (generating two types of rather simple reports, as
> well as an invoice). Anything better out there?
>
> Best regards,
> Max

yes, something that is much more up to date (and actually still in
development) which is based on FPDF and is free
http://www.tecnick.com/public/code/cp_dpage.php?aiocp_dp=tcpdf

spectrus

unread,
Oct 15, 2008, 9:50:43 AM10/15/08
to Prototype & script.aculo.us

> On a side note, you do not need AJAX for this feature to feed the user
> a PDF file for download just set the form action to empty (same page)
> and it should leave the page/browser right where it is with the form
> still filled out,

Brian, I can't seem to get that behaviour to work, it keeps refreshing
the page no matter what. Could you please help me out with a code
snippet that submits the form without refreshing the page? Or did I
misinterpret and the page actually refreshes, WITH the form still
being filled out after refresh?

Thanks in advance,
Max

Walter Lee Davis

unread,
Oct 15, 2008, 10:13:33 AM10/15/08
to prototype-s...@googlegroups.com

It's been my experience that if you leave the action blank, you
submit the page to itself. What that page does in response to a POST
or GET is entirely up to that page. So, if you had "wired" your page
to play back the submitted variables (or if you were using a
framework that did this sort of thing automagically) then you would
see the form completed, as if nothing had changed. But if you did not
have such a thing going on in the background, you would see an empty
form. The browser would not re-fill (excepting any sort of auto-fill
behavior) the fields.

Walter

Brian Williams

unread,
Oct 15, 2008, 9:20:41 PM10/15/08
to prototype-s...@googlegroups.com
TCPDF seems to have some bugs with php4 but it could be my setup.  I haven't had a chance to mess with it on my own server yet, all of my work has been done at well, work.  But you'll get the gist that the form doesn't have to be lost.  I've put the entire page code up there but will take it down in 24 hours for security reasons.  note that I've not done any server side validation -- which can be done as well also.

http://bruuuuuce.com/selfposting.php

spectrus

unread,
Oct 16, 2008, 4:49:28 PM10/16/08
to Prototype & script.aculo.us
Walter,

Yes, you are of course correct. It just didn't occur to me, being
responsible for just that narrow functionality, I wasn't thinking.
And, being submerged into the AJAX world for so long, was starting to
forget the basics.

Brian,

Thank you very much!

I see the catch.. You produce PDF directly inside the page. I had my
PDF generation code in a class that is called by the template engine,
which then immediately produces and feeds an HTML template to the
client, which confuses the hell out of FIrefox (works in IE and Opera
though).

Once again, thanks a lot!

Regards,
Max

Brian Williams

unread,
Oct 16, 2008, 5:02:47 PM10/16/08
to prototype-s...@googlegroups.com
Yeah, I've fallen into that same trap with other bits of things.  Overuse of a "fancy" gadget for no reason.  For me it's a menu and when having to support non-js clients having a JS based menu is a bad idea (i've gotta fix that soon shhhh )

enjoy the code, glad i could help.
Reply all
Reply to author
Forward
0 new messages