Howto - display PDF file in new window?

My app is creating a PDF in my app’s public directory. When the visitor
clicks a button, I need to be able to display the file in a new window.
I cannot figure out how to do it. Any help is greatly appreciated.

Thanks,
Bill

If it’s a link, just add target="_blank" to the a tag. If it’s
Javascript,
there’s window.open(url, options*) IIRC.

Jason

Jason R. wrote:

If it’s a link, just add target="_blank" to the a tag. If it’s
Javascript,
there’s window.open(url, options*) IIRC.

Jason

Do you define the Content-Type of your page ?

Guest wrote:

Do you define the Content-Type of your page ?

No. Do I need to? This is the first time I’ve tried to display
anything
other than HTML from a Rails app. I’m feeling really lost.

Thanks,
Bill

Quickly,

Here’s an example of some code in a view to set up a link that will open
up a new window to receive a PDF:

<%= link_to(‘Forms and Endorsements’,
{:action => ‘show_company_terms’, :id =>
company.id},
:popup => [ ‘terms_and_conditions’,
‘width=800, height=700, left = 450, top = 100, resizable,
scrollbars=yes’ ]) %>

This makes a popup window named ‘terms_and_conditions’.

If you wanted the content to show up in the window, the action
‘show_company_terms’ would need to use the send_file() helper to send
back a PDF file. In the parameters for send_file, you would specify the
content-type.

Hope this helps.

Let me know if you need more info.

Wes

Bill W. wrote:

Guest wrote:

Do you define the Content-Type of your page ?

No. Do I need to? This is the first time I’ve tried to display
anything
other than HTML from a Rails app. I’m feeling really lost.

Thanks,
Bill

There is no option to that. One possibility is to make a custom button.

Wes G. wrote:

Here’s an example of some code in a view
to set up a link that will open up a new window
to receive a PDF:

Hope this helps.

That was a huge help. Thank you, Wes!

Let me know if you need more info.

Well… since you asked :wink: I really need to (visually) use a button
rather
than a link. The problem is that button_to uses POST and so the :popup
option doesn’t work. Any suggestions?

Thanks again,
Bill

Bill,

My app. displays one of several PDFs depending on what is requested.
The view has a tag with no attributes to allow the submission
attributes to be fully dynamic. Buttons invoke a Javascript function to
set up the form, submit it via an AJAX request that populates the popup
window with the PDF. The onSuccess code for the AJAX request hides the
AJAX wait message and graphic and then evals some Javascript returned
from the action that does whatever you want in the popup. In my case,
it causes the popup to do a redirect to an action which will render the
PDF (I couldn’t figure out a simpler way to do it). Currently, I’m
using it to render 3 different PDFs.

  1. View with button:

<%= button_to_function(‘Save and Print Insured Forms’,
“submit_form_to_popup(this.form, ‘print_application_window’,
‘print_application’)”, :style => “width: 200px;”) %>

  1. Javascript function submit_form_to_popup:

/* Takes a form object, a popup target name, and an action for the form.
AJAX-y wait graphic provided by default. */
function submit_form_to_popup(form, popup_window_name, action) {
form.action = action;
popup = window.open(’’, popup_window_name, ‘width=800, height=700,
left = 450, top = 50, resizable, scrollbars=yes’);
wait_image_url = window.location.protocol + ‘//’ +
window.location.host + ‘/images/indicator_verybig.gif’;

//Create a DIV in the popup window and populate it with the AJAX wait
message and graphic.
newDiv = popup.document.createElement(‘div’);
newDiv.id = ‘pdf’;
newDiv.style.marginTop = ‘33%’;
newDiv.style.textAlign = ‘center’;
newDiv.innerHTML = ‘Please wait while your forms are
generated…

’;
wait_img = popup.document.createElement(‘img’);
wait_img.src = wait_image_url;
newDiv.appendChild(wait_img);
popup.document.body.appendChild(newDiv);

//Submit the form asynchronously and execute Javascript returned from
the action.
new Ajax.Request(form.action, { asynchronous: true,
method: ‘post’,
parameters: Form.serialize(form), evalScripts: true,
onSuccess: function(request) {newDiv.innerHTML = ‘’;
eval(request.responseText);}
});
return false;
}

  1. Rails action that is the target of the AJAX request:

def print_application
#Create pdfgen object here

render :update do |page|
page.assign(‘popup.location.href’, url_for(:action => ‘render_pdf’,
:filename => pdfgen.output_file))
page.replace_html(‘notice’, ‘The application was saved
successfully.’)
end
end

def render_pdf
send_file(params[:filename], :type => ‘application/pdf’, :disposition
=> ‘inline’)
end

Hope this helps,
Wes

1)Viewwith button:

<%= button_to_function(‘Save and Print Insured Forms’,
“submit_form_to_popup(this.form, ‘print_application_window’,
‘print_application’)”, :style => “width: 200px;”) %>

hello, i tried to use the edited above code inside my application, but
when the button is clicked then i get error message “An unknown error
occured.” do you have any idea what’s wrong with this…

Thanks,

Adi

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs