Little caveat when using SSL (HTTPS) and PRAWN on Internet Explorer

Little caveat when using SSL (HTTPS) and PRAWN, which is a PDF creation tool for Rails, on Internet Explorer.

So in your controller you are calling the PDF generation

prawnto :prawn => {:left_margin => 50, :right_margin => 50
    , :top_margin => 25, :bottom_margin => 25}


and all looks great and you can download the PDF (inline or whatever) in your browser.

Except some users can’t. And they are all using Internet Explorer (i.e., IE6…IE8). Grrr.

The solution is to modifier the headers as follows:

response.headers['Content-Disposition'] = "attachment;
    filename=\"#{@some_model_to_params_identifier}.pdf\""
response.headers['Content-Description'] = 'File Transfer'
response.headers['Content-Transfer-Encoding'] = 'binary'
response.headers['Expires'] = '0'
response.headers['Pragma'] = 'public'

Aah, all can resume.

This entry was posted in Pallay's Blog and tagged , . Bookmark the permalink.

2 Responses to Little caveat when using SSL (HTTPS) and PRAWN on Internet Explorer

  1. Ian says:

    How can you DRY this so that any/all controller methods where the Rails application might print PDFs will run this code? I don’t want to put this into every controller method that prints PDFs.

  2. Administrator says:

    Hi Ian,

    The headers hash works in a similar way to the params and session hashes. As it is based on the template, it may need to render in the controller actions so that it has access to your model, unless you hardcode a filename I suppose. To answer your question, it depends on what controllers need this code; you could have a before/after_filter for each method in a particular controller.

    Let me know how you get on.

Leave a Reply