Rails: convert HTML to PDF?

What is the best and easiest way to transfer HTML and convert it to a PDF file, similar to using CFDOCUMENT on ColdFusion?

UPDATE: I really appreciate all the comments and suggestions that people have made so far, however I feel that the people leaving their answers do not make sense.

1) the solution should be free or open. one person suggested using pricexml and another pd4ml. both of these solutions cost money (pricexml costs a hand and a leg), which I’m not talking about.

2) they should be able to accept html (either from a file, url, or a string variable), and then produce pdf. libraries, such as prawn, rprf, rtex, are created using native methods and are not taken in html.

please don’t think that I am ungrateful for the suggestions, it’s just that PDF generation seems really a problem for people like me who use ColdFusion but want to convert to Rails.

+52
ruby-on-rails pdf
Dec 10 '08 at 19:12
source share
15 answers

The WickedPDF gem does just that.

+59
Dec 05 '09 at 20:55
source share

The biggest thing at the moment is PDFKit for this job:

+15
Apr 09 2018-11-11T00:
source share

Try WickedPDF , a PDF generator module (from HTML).

Here is an example

gem 'wicked_pdf' gem 'wkhtmltopdf-binary' #wicked_pdf is a wrapper for wkhtmltopdf, you'll need to install that, too 

In your controller (I assumed that you are in the show method):

  respond_to do |format| format.html format.pdf do pdf = render_to_string :pdf => 'test', layout: 'pdf.html.erb', template: 'show.pdf.slim', header: { :right => '[page] of [topage]'}, margin: {top: 0, bottom: 0, left: 0, right: 0}, outline: {outline: true, outline_depth: 2} end end 

In your view for a PDF link

 = link_to 'Download Pdf', you_show_path(@uour_object, format: :pdf) 

If you want to send a PDF to an email application, first save the pdf file in a public or temporary folder, and then in the mail program class (for example, in mailers / e_mailer.rb)

 attachments["test.pdf"] = File.read(Rails.root.join('public',"test.pdf")) mail(:to => to, :cc => cc , :subject => "subject") 

After sending the email you can delete the file

 File.delete(Rails.root.join("public", "test.pdf")) 
+10
07 Oct '15 at 12:26
source share

I know that this question is almost 5 years old. However, new options are available.

Try the shrimp! https://github.com/adeven/shrimp

+3
Jun 05 '13 at 17:30
source share

After I managed to write a rather simple application for rails after a lot of sweat and blood tears, which allows the user to write letter templates through TinyMce , they are then saved and can be considered as PDF documents.

I decided to limit the parameters as much as possible in the wysiwyg editor, since some of the parameters did not work exactly as expected, but something small gsub-ing could not be solved if necessary.

There is a ruby ​​stone that wraps HTMLDOC that you will need: PDF :: HTMLDoc

Once you get this, register the mime type, then you can do something like:

 @letter_template = LetterTemplate.find(params[:id]) respond_to do |format| format.html format.pdf { send_data render_to_pdf({:action => 'show.rpdf', :layout => 'pdf_report'}), :filename => @letter_template.name + ".pdf", :disposition => 'inline' } end 

In the application controller, I added the render_to_pdf method, for example:

 def render_to_pdf(options =nil) data = render_to_string(options) pdf = PDF::HTMLDoc.new pdf.set_option :bodycolor, :white pdf.set_option :toc, false pdf.set_option :portrait, true pdf.set_option :links, false pdf.set_option :webpage, true pdf.set_option :left, '2cm' pdf.set_option :right, '2cm' pdf.set_option :footer, "../" pdf.set_option :header, "..." pdf.set_option :bottom, '2cm' pdf.set_option :top, '2cm' pdf << data pdf.generate end 

You will find additional documentation on the HTMLDOC website with which Travis Beale is associated. Hope this helps you along the way, and if your documents are really complex, that should be enough. Let us know how you are doing.

+2
May 8 '09 at
source share

The closest I found as a “solution” to this is to use JRuby and then use the Flying Saucer Project . I just want it to be ported to Ruby so that this can be a native solution. God, I wish I were better at Java.

+1
Dec 11 '08 at 13:11
source share
  • An RPDF in which you describe your pdf file as a view.
  • There is also RTEX , which uses (La) TeX to generate PDF files.
+1
Dec 11 '08 at 17:16
source share

The project may stall, but Pisa (aka XHTML2PDF ) is a Python library that does CSS + HTML in PDF. used it in a low-traffic Rails application some time ago, running it as a command line tool - worked very well.

0
Jun 05 2018-10-06T00:
source share

I would like to offer you an HTML solution for PDF https://grabz.it .

They have a very flexible and easy to use screenshot API that can be used by any type of Ruby application.

If you want to try, you must first get an authorization application key + secret and development of a free SDK

Then in your application the implementation steps:

 //initialization require 'grabzit' grabzItClient = GrabzIt::Client.new("APPLICATION KEY", "APPLICATION SECRET") //capturing the url to PDF grabzItClient.url_to_pdf("http://www.google.com") 

Next is conservation. You can use one of two save methods, save , if an accessible callback descriptor is available:

 grabzItClient.save("http://www.example.com/handler/index") 

And save_to , if you need synchronous saving (which will make your application wait while taking a screenshot):

 filepath = "images/result.jpg" grabzItClient.save_to(filepath) 

Check the documentation for details on saving.

It's all. You can also get other screenshots, such as screenshots of images .

0
Jul 01 '17 at 9:27
source share

If you want to have really good quality (think of the correct layout / CSS, etc.), you need to create a process that has a real web browser built in that displays it off-screen and prints it using the PDF printer driver.

-one
Dec 10 '08 at 19:21
source share

Surely there is no direct way to do this in Ruby (at least there wasn’t when I tried to do this a couple of months ago). If you really cannot avoid generating your pdf files from html, you should probably look at some external tools or services for this purpose.

Good luck

-one
Dec 10 '08 at 19:44
source share

I try the library from www.pd4ml.com

here is the snip code ....

I am pulling html into String content. Then I do a couple of things, such as replacing radio buttons with checkboxes, and then running through the pd4ml library to render pdf.
The result is very similar to the original page ....

  String content = nextPage.generateResponse().contentString(); content = content.replace("Print", ""); content = content.replace("Back", ""); content = content.replace("border=\"1\"", "border=\"0\""); content = content.replace("radio", "checkbox"); java.net.InetAddress i = java.net.InetAddress.getLocalHost(); String address = i.getHostAddress()+":53000"; content = content.replace("img src=\"/cgi-bin", "img src=\"http://"+address+"/cgi-bin"); System.out.println(content); PD4ML html = new PD4ML(); html.setPageSize( new java.awt.Dimension(650, 700) ); html.setPageInsets( new java.awt.Insets(30, 30, 30, 30) ); html.setHtmlWidth( 750 ); html.enableImgSplit( false ); html.enableTableBreaks(true); StringReader isr = new StringReader(content); baos = new ByteArrayOutputStream(); html.render( isr, baos); PDFRegForm pdfForm = (PDFRegForm)pageWithName("PDFRegForm"); pdfForm.baos = baos; pdfForm.generateResponse(); 
-one
Dec 17 '08 at 19:15
source share

Have you tried looking at html2pdf? homepage http://html2pdf.seven49.net/Web/

There is also a sourceforge project.

Alan

-one
Dec 17 '08 at 20:09
source share

Not defined for Ruby, but the best solution I've found is open source HTMLDOC .

-one
May 08 '09 at 1:26 p.m.
source share

HTMLDOC ?

  • Converts HTML to PDF
  • Free and open source (commercial support available);
  • can be used as ...
    • ... a standalone application,
    • ... for batch processing of documents,
    • ... or work as a web service;
  • an interface is a CLI or GUI.
-one
Jun 10 2018-10-10
source share



All Articles