Skip to content

Instantly share code, notes, and snippets.

@acapilleri
Last active December 20, 2015 19:19
Show Gist options
  • Save acapilleri/6182210 to your computer and use it in GitHub Desktop.
Save acapilleri/6182210 to your computer and use it in GitHub Desktop.
Generate PDF with wkhtmltopdf without any required gems. In this case I use rails, but it's flexible for any ruby frameworks or just for simply ruby app
def show
@pet = Pet.find(params[:id])
tempfile = Tempfile.new(@pet.id.to_s)
render_file = Tempfile.new(@pet.id.to_s + '.html')
buffer = render 'show.pdf.erb', layout: false
render_file.write(buffer.first)
html = "#{render_file.path}.html"
system "mv #{render_file.path} #{html} && wkhtmltopdf #{html} #{tempfile.path}"
send_file tempfile.path, type: 'application/pdf'
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment