Skip to content

Instantly share code, notes, and snippets.

@mariochavez
Created November 30, 2010 19:21
Show Gist options
  • Save mariochavez/722215 to your computer and use it in GitHub Desktop.
Save mariochavez/722215 to your computer and use it in GitHub Desktop.
Uso de PDFKit
def create
@modelo = Modelo.new(:params)
#generacion de HTML a partir de una vista de rails
html = render_to_string(:layout => false, :action => 'mi_formato.html.haml')
pdfKit = PDFKit.new html
#incluir un css para un mejor formato
pdfKit.stylesheets << 'public/stylesheets/pdf.css'
#enviar el archivo PDF al navegador
send_data pdfKit.to_pdf, :filename => 'Mi Formato.pdf', :type => 'application/pdf'
end
class PDFKit
def to_pdf
append_stylesheets
IO.popen(command.join(' '), 'r+') do |f|
f.write(@source.to_s)
f.close_write
pdf = f.readlines
f.close
pdf
end
end
end
def to_pdf
append_stylesheets
pdf = Kernel.open('|-', "w+")
exec(*command) if pdf.nil?
pdf.puts(@source.to_s) if @source.html?
pdf.close_write
result = pdf.gets(nil)
pdf.close_read
raise "command failed: #{command.join(' ')}" if result.to_s.strip.empty?
return result
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment