0

I need to generate a PDF file using some data I get from a POST request. As response to this request, I want to send the generated PDF file, for which the user should get a "save file as"-dialog.

This all should happen on the fly. I don't want to save the PDF to the disk and serve it with an additional request. It should all happen with one single request.

But I don't know how I could achieve such a thing. If I use a XMLHttRequest, I can't make the "save-file-as"-dialog appear. On the other hand, if I use a form, I can't send the POST data without some URL-encryption overhead.

Does anyone have an idea?

Van Coding
  • 24,244
  • 24
  • 88
  • 132
  • Using JS on the client alone to generate the PDF could be option for you then? This may be interesting: http://stackoverflow.com/questions/742271/generating-pdf-files-with-javascript – sajawikio Sep 12 '12 at 15:49
  • "URL-encryption overhead" ... what is that? – hackattack Sep 12 '12 at 15:51
  • If not wanting to use that PDF JS library, then saving it to the server memory temporarily would be other option. That would be done with your server side code in whatever server side language you are using. But there would still be that additional request overhead :(( - I guess you really do want the Client to generate the PDF then, in which case I guess either that JS library, or some Flash thing would work. – sajawikio Sep 12 '12 at 15:51
  • @haackattack I am guessing he means "overhead of the POST request being sent over the wire," rather than encryption in terms of SSL. Probably encryption is not the right word, but I think that's what OP means. – sajawikio Sep 12 '12 at 15:54
  • @hackattack Using a form, I can't completely set the body of the POST request, instead I can define values, which are then urlencoded. An example body: "&myjsondata={title:'hello'}", where "&myjsondata=" is just unneeded overhead. Using xhr I can set the body to just "{title:'hello'}" – Van Coding Sep 12 '12 at 16:06
  • @sajawikio when I am able to generate the PDF at the client side, how can I let the user save it to the disk? – Van Coding Sep 12 '12 at 16:07
  • Not familiar with jsPDF but from a very, very quick glance at the source code, it will actually open PDF in the same window, as a base64 encoded URL (of course, all generated from the jsPDF client) and would look exactly as if someone looked at it on a server (pdf file is right there displayed, is saveable, etc.) . But AFAIK (please let me know if I am mistaken) you cannot force a save dialog box to appear (unless you also use a Flash or Java applet, or the like). Instead, the user sees the PDF , and if he/she chooses, must save it using the "Save As" feature on their browser, voluntarily. – sajawikio Sep 12 '12 at 16:27
  • hmmm... base64 encoded URLs are not supported by IE8 (only 32KB and only for resources)... – Van Coding Sep 12 '12 at 16:41
  • Indeed, that solution probably won't work then - I am not aware of any other one that really generates PDF's without intrusive plugins such as Flash and Java that I probably wouldn't want to use, but am interested if anyone else knows of any. – sajawikio Sep 12 '12 at 16:47

2 Answers2

0

In the end, I solved it with 2 Requests. The first sends the needed data to the server and creates the PDF out of it, and then returns the ID of the temporary file. The second downloads the pdf and deletes the temporary file.

Van Coding
  • 24,244
  • 24
  • 88
  • 132
0

I'm doing this by posting data from a javascript generated form-field to the php.

var form = $('<form method="post" action="svgoutput/convert.php"> <input type="text"  name="incomingSvgData" value="'+base64_encode(svgDataString)+'"><input type="submit" value="Submit Post"> </form> ');
form[0].submit();

For IEs the form has be in the dom while submitting.

On php side I do my generation and stuff (SVG to PNG in this case) and set the following headers:

header("Content-Type: YOURMIMETYPE");
header("Content-Disposition: attachment; filename=YOURFILENAME.EXT");
echo YOURDATA;

Downloads smoothly w/o even showing a save as dialog in just one post. If you need the Save As just play around with the headers, like here How to show 'Save as' dialog box using PHP for text files

Community
  • 1
  • 1
RedRoosterMobile
  • 786
  • 10
  • 21