Question

is there a way to create pdf using wicked pdf after the page is loaded?

i want to create pdf after loaded, because i create the content for pdf after the page is loaded using javascript.

<div id="content">
    <!-- some content -->
</div>

I would like to create a pdf file using wicked pdf, but is there a way to create it after the page is loaded?

Thank you very much.

Était-ce utile?

La solution

First off, you can probably just create the pdf before it loads, since wkhtmltopdf can process and execute javascript. You may need to adjust the :javascript_delay setting if it isn't completely finishing running the JS.

Otherwise, you may want to package up the html in #content#, and send it as a param to a PDF-creating endpoint, with something like this:

$(function(){
  form = $('<form action="/pages/report.pdf"><input id="form_content" name="form_content"></form>');
  html_content = $('#content').html();
  $('#form_content').val(html_content);
  form.submit();
});

And an endpoint something like this:

format.pdf do
  @content = params[:form_content]
  render :pdf => 'report.pdf'
end

and a view that uses @content:

<%= @content %>

This endpoint probably should not do double duty with an html view, since that injected content could open you up to CSRF or XSS issues.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top