Multiline graph in PDF file using ruby on rails - ruby-on-rails

I want to give export option in my application that will export PDF file and it should consist multi-line graph. How can i do this in ruby on rails.

It depends on your situation, and what content you already have (if any) which you want to render your PDF based on. Some existing options are:
PDFKit, which can create PDFs based on your existing HTML documents
Prawn, which is a lightweight Ruby API for creating PDFs
Some options (like PDFKit above, and also Wicked PDF) will rely on an external tool like wkhtmltopdf to render existing HTML pages as PDFs, whereas other options (like Prawn) are more Ruby native, so it really depends on your situation which path your should take in your application.
As for the graph you mentioned, if you already have it being rendered as an image file, it might be easiest to use one of the HTML-to-PDF generation options, but if you're wanting to render the graph custom in the PDF context and don't already have it as a standalone image, something like Prawn may be best.

In my application, i am using Highcharts.
I have found great success in rendering charts to pdf using a combination of 2 gems:
wicked_pdf - https://github.com/mileszs/wicked_pdf
and
wkhtmltopdf-binary - https://github.com/zakird/wkhtmltopdf_binary_gem
The second is the binary dependency for wicked_pdf.
It can render your rails view in pdf format.
Edit:
I have used PDFkit as well, code is given below from my controller method:
Onclick of "Download PDF" button:
if params[:commit]=="Download PDF"
html = render_to_string(:layout => false , :action => "controller_name/pdf_chart_erb_file.html.erb")
kit = PDFKit.new(html)
kit.stylesheets << "#{Rails.root}/app/assets/stylesheets/application.css"
send_data(kit.to_pdf, :filename => "my_chart.pdf", :type => 'application/pdf', :disposition => 'inline')
file = kit.to_file("#{Rails.root}/app/exe.pdf")
end
You can use or render your graphs in pdf_chart_erb_file.html.erb, it simply converts all your html file to PDF file as per code given above.

Related

Linking to pdf in Rails

I have a rails app where I am providing summary information re: various legal opinions. The information about each opinion is called from MySQL. In addition to the information from the database, I would also like to provide a link to a PDF of that particular opinion. Currently the pdfs are all stored in a folder labeled "op_pdf" with in the "assets folder." Each pdf is named using the opinion.id for that opinion (opinion with id = 1 is 1.pdf, 2 is 2.pdf, etc).
I've researched this extensively but keep finding conflicting information.
The code I am currently using (that does not work) is:
'<%= link_to #opinion.name, asset_path "op_pdf/#opinion.id", :format => :pdf) %>'
In initializers/myme_types.rb I have:
Mime::Type.register "application/pdf", :pdf
The 3rd step is my issue. I have to do something in the controller and/or routes. I am working from this - Rails 4 - how to make a link to a PDF file (name.PDF)?
but it seems to be overkill for what I need. Can anyone help me?

render dynamic HTML with wicked pdf

I have a DSL that renders HTML. If I manually generate HTML in the console, and save it as a view (as an experiment, there's no reason to do this), so that it lives in app/views/manifests as show.pdf.erb, and then I simply render the PDF using Wicked's/Rails built in rendering, everything works great and looks great. Now, because this is dynamic HTML, meaning it can change based on the data, what I have to do is generate the HTML as a string, and then use Wicked's pdf_from_string:
html = HtmlGenerator.parse_node node, "root", #manifest
pdf = WickedPdf.new.pdf_from_string(html) # note, this is the same HTML that looks great as a view
and then send it to the browser
send_data(pdf,
:filename => "my_pdf_name.pdf",
:disposition => 'inline')
Now, everything is screwed up. It appears like the fonts are 15% bigger, all of my rows are screwed up. Does the rendering routine in WickedPDF use a different formatting that pdf_from_string?
Thanks for any help,
Kevin

How to create fixed footer/header with PDFKit in rails?

I'm using https://github.com/pdfkit/pdfkit in rails to generate a PDF.
kit = PDFKit.new(html)
kit.stylesheets << 'app/assets/stylesheets/pdf.css'
kit.to_pdf
The PDF renders properly based on the HTML. I'm just curious how to introduce a fixed footer/header. For instance, if I like to add a page number fixed at the bottom of the page along with a logo on every single page.
Thanks in advance.
I just dug into the gem more and saw the :header_center option.
Looks like I'll be able to pass in value (probably a template as well) when creating the PDF like so.
kit = PDFKit.new(html, :header_center => "foo", :footer_center => "bar")

Repeat HTML in every page generated with Wicked PDF and Rails

I'm generating PDF with the Wicked PDF gem on Ruby on Rails, but have to repeat some HTML content on every page.
What I'm trying is to use just part of the page to my main content, and use HTML to add stuff around it, in every page.
Something like this image (Check)
I tried playing with header, but I wasn't able to put the content in front of the HTML (even using z-index), and was only able to position the main content with margin and spacing vertically (didn't find any options to do it horizontally).
Any ideas? Thanks!
Since no one answered, I will post my solution here. It is not beautiful nor brilliant, it is so so so far from these - but since no one answered, maybe we can start a discussion from it.
I was able to generate a PDF with only the text (but with the correct margins), with no background, using Wicked PDF. Like this image. I used it to just save the file.
This was the code for it:
# Save PDF with only the text (with correct margins)
render :pdf => "text#{id}",
:margin => {:top => "1.6in", :left => "4.1in", :right => "1.2in", :bottom => "2.5in"},
:page_size => "Letter",
:template => "careers/job_pdf_text.pdf.erb",
:save_to_file => Rails.root.join('public/job_pdf_tempfiles', "text#{id}.pdf"),
:save_only => true,
:no_background => true
Then I used RMagick to create images from this saved PDF. The important here is that I saved GIFs with transparent background, and Magick creates one image for each page on the PDF. This was the code to save the images:
# Save images from the only-text PDF
#text_images = []
text_images_magick = Magick::Image.read(Rails.root.join('public/job_pdf_tempfiles', "text#{id}.pdf"))
text_images_magick.each_with_index do |image, index|
file_name = Rails.root.join('app/assets/images/careers_pdf', "text#{id}-#{index}.gif")
image.write(file_name)
#text_images << file_name
end
Ok, so at this moment I have images of the text, like this. Now what I did was put those in an HTML page, in the correct place and then used Wicked PDF again to render the final PDF. I rendered the PDF with margin 0, and for each #text_images I created a container, from which I positioned everything else (including the image itself) to achieve what I wanted in the beggining.
Does anyone have a better idea to share?

Can prawn generate PDFs with links?

I need to embed a link into a generated pdf in a ruby on rails app. Is there a way to do this with prawn?
Reading about this it turns out that prawn-format was the answer for awhile, but 0.7.x broke this.
prawn-format uses the link_annotate(rect, options={}) function to create links. What options need to be passed into this to get it to create a link in the PDF?
edit:
I would like to see a code example of this being done if anyone has one.
I know this is an old question, but for those still stumbling upon it, in current versions of Prawn, you can use inline format like this:
pdf.text "Website: <link href='http://www.stackoverflow.com'>stackoverflow</link>", :inline_format => true
If you are attempting to create a link to an external page (http://google.com), for instance you could use the following, to place a link that is 100x100 and placed at 5, 5 from the bottom left of the page, with a 1px border:
pdf.link_annotation([100, 100, 5, 5], :Border => [0,0,1], :A => { :Type => :Action, :S => :URI, :URI => Prawn::LiteralString.new("http://google.com") } )
Prawn Format would parse the text passed to the pdf.text method and find html a tags. It would then use regular expressions to parse out the target and link text and finally create a link like the one above with a bounding box (the first param) that would fit around the text that was within the tags. I'm not sure how you could achieve this without Prawn Format. But that is how you can create a link using link_annotation.
As of Prawn 0.7, prawn-format is
completely unsupported, and will not
work with versions of Prawn 0.7+. Feel
free to fork and fix, of course
- prawn-format's homepage on github
The other option is to use prawn's built in low-level annotation support:
http://prawn.majesticseacreature.com/docs/prawn-core/classes/Prawn/Document/Annotations.html#M000158
Heres the method:
link_annotation(rect, options={})
A convenience method for creating Link
annotations. rect must be an array of
four numbers, describing the bounds of
the annotation. The options hash
should include either :Dest
(describing the target destination,
usually as a string that has been
recorded in the document‘s Dests
tree), or :A (describing an action to
perform on clicking the link), or :PA
(for describing a URL to link to).
I recently did it like this - works great:
formatted_text_box([{:text=>"Google", :link=>"https://google.com", :color=>"0000ee"}])

Resources