xwmx / pandoc-ruby

Ruby wrapper for Pandoc
MIT License
340 stars 43 forks source link

Syntax for serving converted file with Rails? #11

Closed archonic closed 10 years ago

archonic commented 10 years ago

I'm trying to serve a converted file with Rails and I'm not sure what the syntax should be. I've run the command line and everything is installed correctly.

format.pdf  {
    html = render_to_string(
      :template => 'standards/show.pdf.erb',
      :layout => 'layouts/pdf.html.erb' )
    converter = PandocRuby.new(html, :from => :html, :to => :pdf)
    f = File.new("/tmp/#{@standard.title}_#{Time.now()}.pdf", "w")
    f.write(converter.convert)
    send_file f, :disposition => "attachment"
    f.close
}

It looks like the convert method returns nothing - the resulting pdf is totally empty (as in not even file headers). Changing to converter = PandocRuby.new(html, {:from => :html, :to => :pdf}, :standalone) gives a broken pipe error. Any help is greatly appreciated!

xwmx commented 10 years ago

In order to convert to PDF, pandoc uses pdflatex by default and requires specifying an output file:

http://johnmacfarlane.net/pandoc/demo/example19/Creating-a-PDF.html

So, for example, a command like echo "# Some Markdown" | pandoc -o example.pdf could be called via PandocRuby using PandocRuby.convert("# Some Markdown", :o => "example.pdf"), since you can just pass pandoc options as a hash to PandocRuby. Similarly, you could rewrite the example you posted using something like PandocRuby.convert(html, :from => :html, :o => "/tmp/somefilename.pdf").

One thing to note is that there is no error handling or feedback at all about whether the command succeeds.

Also, in case it helps, there are several other options for generating PDF files in Rails:

https://www.ruby-toolbox.com/categories/pdf_generation

ruinunes commented 8 years ago
filename = 'somefilename.pdf'
html = "<html><body>Hello <span style='background: yellow; color: red'>world</span></body></html>"
PandocRuby.convert(html, from: :html, o: "#{Rails.root}/tmp/#{filename}")
File.open "#{Rails.root}/tmp/#{filename}", 'rb' do |f|
  send_data f.read, filename: filename, type: 'application/pdf', disposition: :inline
end