Re: HTML content in Prawn...

1,520 views
Skip to first unread message

Michael Lang

unread,
Mar 19, 2013, 9:03:12 AM3/19/13
to prawn...@googlegroups.com
I've had very good success with pdfkit, https://github.com/pdfkit/pdfkit

I maintain templates in erubis (*.erb) files and render to html and
then use pdfkit to convert the html output to pdf.

To give you an idea:

def generate
us_states.each do |us_state|
puts "Generating for #{us_state}..."
eruby = ERB.new(File.read("template.erubis"))
html = eruby.result(binding)

PDFKit.new(html).to_file("#{us_state}.pdf"))
end
end

To be clear, this isn't using Prawn. Prawn's definitely a part of my
toolchain, but isn't needed for generating HTML => PDF in this
scenario. BTW, this is a great way to render web pages into PDFs for
"snapshots" of the sites under development.

Michael
On Tue, Mar 19, 2013 at 7:10 AM, Pasi <pa...@tolonen.com> wrote:
> Hi.
>
> As this is the first post to this thread I feel that before anything else I
> need to thank you the developers and maintainers of this superb rails gem,
> Prawn. Ablsoutely fantastic job, we just love using it!
>
> So, everything else works like a charm, but I haven't found a proper way to
> convert/add HTML-formatted text into Prawn. We use Kendo UI Editor for our
> front end text editing and it can save it's output as HTML. We need just
> basic formatting like bold, italics and lists, nothing fancy on top of that.
> I have found following gems
>
> - https://github.com/kristianmandrup/Prawn-assist
> - https://github.com/kristianmandrup/prawn_html
>
> Have anyone tried these with the latest Prawn (1.0.0rc2) or is there any
> other path I should follow with this HTML to Prawn text -issue?
>
> Thanks,
>
> P
>
> --
> You received this message because you are subscribed to the Google Groups
> "Prawn" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to prawn-ruby+...@googlegroups.com.
> For more options, visit https://groups.google.com/groups/opt_out.
>
>



--
http://codeconnoisseur.org

Pasi

unread,
Mar 19, 2013, 2:39:38 PM3/19/13
to prawn...@googlegroups.com
Mike,

thank for your suggestion. I've tried pdfkit and it's many of the flavors like wicked-pdf etc, but I didn't feel at home with them. Especially, because of problems with pagination etc. Not to mention that I really need only a minor subset of HTML, bold, italic and lists.

Anyway. I did as follows (consider this as quick and dirty) using Nokogiri and since I don't use Nokogiri that often, there may be more elegant solution.

   paragraphs = Nokogiri::HTML(htmltext)
   paragraphs.search('li').wrap("</p><p>")

Since I need only bold, italics and lists I convert list items as separate paragraphs. Then I loop through all paragraphs

   paragraphs.css('p').each do |p|
      if p.inner_html.match('<li>')
         list(ActionView::Base.full_sanitizer.sanitize(p.inner_html))
      else
         text "#{p.inner_html}", inline_format: true, leading: 3
      end
   end

The method list is as follows and makes bullets in front of list items:

   def list(string)
      float do
         bounding_box [15,cursor], :width => 10 do
            text "•"
         end
      end
      bounding_box [25,cursor], :width => 510 do
         text string, inline_format: true
      end
   end

Not very elegant, but works pretty neatly.

Pasi

Michael Lang

unread,
Mar 19, 2013, 10:16:31 PM3/19/13
to prawn...@googlegroups.com
Pasi,

It looks to me like you have a solid solution. I would take a working
solution over an "elegant" one any day as long as its well-factored
and has a solid set of tests/specs behind it.

I solved my problem with pagination simply by only generating single
page HTML templates. In the use case I extracted the code presented
earlier, I already had the HTML pages rendering as part of a reporting
website solution, so it was faster to render a PDF from a rather
complex HTML layout than to try to re-create the whole shebang in
Prawn.

The solution works well -- and wasn't "elegant" either, but
implementation was fast and we were able to move on to many more
features as a result.

Regards,

Michael
Reply all
Reply to author
Forward
0 new messages