I am trying to generate a pdf using wicked_pdf although getting an error 'Unable to render template'. I have followed the wicked_pdf guide but I'm getting stuck. Any help is appreciated.
url:
http://localhost:3000/app/letters/1.pdf
app/controllers/app/letters_controller.rb:
def show
respond_to do |format|
format.html
format.pdf do
render pdf: "Letters" # Excluding ".pdf" extension.
end
end
end
Show page link:
<li><a href="<%= app_letter_path(@letter, format: :pdf) %>" target="_blank"> Print</a></li>
app/views/app/letters/show.pdf:
<!doctype html>
<html>
<head>
</head>
<body>
<div>
<p>test</p>
</div>
</body>
</html>
Gemfile:
gem 'wicked_pdf'
gem 'wkhtmltopdf-binary'
Error:
Failed to load PDF document.
Wicked PDF Checkout here for available options
views/layouts/pdf.html.erb
<!DOCTYPE html> <html> <head> <title>My PDF</title> <%#= wicked_pdf_stylesheet_link_tag "style" -%> </head> <body> <div class='my_container'> <%= yield %> </div> </body> </html>
2- In Controller (You must add option as disposition: 'inline'
to open in new tab)
def show
respond_to do |format|
format.pdf do
render pdf: "show",
disposition: 'inline',
stream: false,
layout: 'layouts/pdf.html.erb'
end
end
end
3 - Pdf html doc name should end with .erb
i .e app/views/app/letters/show.pdf.erb
<div>
<p>test</p>
</div>
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.