What is the best way to generate PDF/HTML/DOCX in Ruby/Rails

12,005

Solution 1

For authoring PDF files

I would go with Prawn over the Wicked-PDF.

I know wicked is more convenient, but Prawn is both native and more flexible.

Wicked depends on wkhtmltopdf and uses systems call - and system calls can cause issues with concurrency and are expensive as far as performance goes.

Which ever one your using, I would probably add CombinePDF into the mix. (I'm biased - I'm the author of the gem)

This will allow you to use template PDF files - so you can write your CV using Prawn or Wicked and throw it over a beautifully designed template using the CombinePDF gem.

CombinePDF could also be used to create simple text objects, number pages or write tables...

require 'combine_pdf'
pdf = CombinePDF.new
pdf.new_page.textbox "Hello World!", font_size: 18, opacity: 0.75
pdf.save 'test.pdf' # use pdf.to_pdf to render the PDF into a String object.

...but I think Prawn is a better tool for authoring, while CombinePDF is a great complementing library that can add pre-made content and design:

require 'combine_pdf'
# get a "stamp" or page template.
# the secure copy will attempt to change the PDF data to avoid name conflicts.
template = CombinePDF.load(template_file_path).pages[0].copy(true)
# move the Prawn document into CombinePDF
pdf = CombinePDF.parse prawn_document.render
# add the template to each page, putting the template on the bottom
# (#>> for bottom vs. #<< for top)
pdf.pages.each {|page| page >> template}
# save the pdf file
pdf.save 'final.pdf' # or render to string, for sending over HTTP: pdf.to_pdf

As for docx, I am clueless... frankly, it's a proprietary format that's usually buggy when reverse-engineered. I would avoid it and let people copy and paste off the HTML if they really wanted.

Solution 2

I use those gems at work, and they are pretty fine, if it can help.

For the html, a simple render could work.

Solution 3

I always use Prawn for PDF and Caracal for DOCX.

I believe the creator of Caracal was inspired by the ease of use of Prawn.

Share:
12,005
Théo Capdet
Author by

Théo Capdet

ETL Talend Developer Freelance (Paris, France).

Updated on June 09, 2022

Comments

  • Théo Capdet
    Théo Capdet about 2 years

    I need to create an app which makes auto-generated CVs from fields. I need to convert them in PDF/HTML/DOC, but there are many gems available.

    Which gem do you think is the most appropriate in order to make CV in PDF, HTML and DOC formats?

    I found prawn for PDF, but is it the most appropriate to make CV-like PDF?

    Thank you in advance.

    EDIT : I found a gem similar to Prawn but for docx maybe that could interest you. https://github.com/trade-informatics/caracal/

  • Théo Capdet
    Théo Capdet about 9 years
    Thank you for the info PDF combine i didn't know that gem