Created
November 30, 2010 19:21
-
-
Save mariochavez/722215 to your computer and use it in GitHub Desktop.
Uso de PDFKit
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 | |
| end | |
| end | |
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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