0

I would like to generate PDF on an area I specify (like in a div id tag) I'ved started to use rails gem PDFKit by following the tutorial from railcast and the docs.

So a simple .pdf link did the trick.

 <%= link_to 'Open PDF', invoice_path(@invoice, :format => "pdf"), :class => "btn btn-md btn-primary" %>

Now the issue is how do I capture just the wanted area ? I explored html answers here: Print <div id=printarea></div> only?

I tried something like this and it doesnt work.

@media print
{  
  body * { visibility: hidden; }
  #printableArea * { visibility: visible; }
  #printableArea { position: absolute; top: 40px; left: 30px; }
}

in show.html.erb

<div id="printableArea">
something in PDF
</div>

I found some other resource, like this http://www.sitepoint.com/pdf-generation-rails/ but it just ask to put tags to hide it. My problem is more to hide the rest and make the div i want visible.

Community
  • 1
  • 1
Axil
  • 3,606
  • 10
  • 62
  • 136

2 Answers2

2

Not sure if this is the best way to do it but I put an if statement around all the code I don't want to show in my pdf like this:

<% if params[:format] != "pdf" %>
  All the code I don't want to show in my pdf
<% end %>

or you could separate your pdf content from your html content altogether

<% if params[:format] == "pdf" %>
  All the code I want to show in my pdf only
<% else %>
  All the code I want to show in html only
<% end %>

And you could use a different layout for not showing navbars and footers

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <title></title>
    <%= wicked_pdf_stylesheet_link_tag 'application' %>
  <link href="http://netdna.bootstrapcdn.com/font-awesome/4.0.3/css/font-awesome.css" rel="stylesheet">
  <script src="http://code.jquery.com/jquery-1.10.1.min.js"></script>

  <%= wicked_pdf_javascript_include_tag "application" %>
  <%= csrf_meta_tags %>
  <%= favicon_link_tag 'https://s3.amazonaws.com/url/favicon.ico' %>
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <script src="https://js.braintreegateway.com/v1/braintree.js"></script>
</head>
<body id="body_tag">

    <%= yield %>

</body>
</html>

Controller

respond_to do |format|
  format.html
  format.pdf do
    render :pdf => "show",
    template: 'deliveries/show.html.erb',
    layout: 'pdf.html'
  end
end
avalente1
  • 246
  • 1
  • 5
  • 21
  • but you will still get everything else not in this condition. consider you have navbar, footer, etc in your page. all those will get into your pdf as well. now how to you not have them and just want the content in say your condition or divs into the generated pdf ? – Axil May 18 '14 at 14:03
  • I use a different layout for pdf's – avalente1 May 18 '14 at 14:12
  • ok thanks, thats one answer. Would be nice if css can actually do that (on turning on the part that is visible and NOT the others). possibly is not possible, I dont know ? i'ved decided to change over to prawn, the generation is really fast compared to wkhtmltopdf. – Axil May 18 '14 at 23:16
  • Yes, that would be nice, and maybe someone will point out how to do so, but so far I haven't seen any explanations on the internet in my searching – avalente1 May 19 '14 at 14:38
0

Hi try this in one css file

#mi_id_div{
display: none;
}

WHEN THE #MI_ID_DIV IS all content not visible in pdf

IN your controller

html = render_to_string(:action => "tiempo_atencion.html.erb", :layout => false)
       kit = PDFKit.new(html)
       kit.stylesheets << "#{Rails.root}/vendor/assets/stylesheets/reportes-estilos/tablas.css"

       send_data(kit.to_pdf, :filename => 'test_report_with_table.pdf', :type => 'application/pdf')
Zombie
  • 41
  • 6