How to generate an editable document using prawn - ruby-on-rails

I'm new to Ruby on rails. I work on Rails 5. I have a requirement of generating PDF document on click of a button with the details in the database, which I achieved using PRAWN gem. Now there is a requirement that On click of a button I should get an option, in which I should be able to select whether to generate a document in PDF or editable format(DOCX or any other format).
Can we achieve this with PRWAN gem in rails 5?
Please suggest me if there are any other options available.

The gem Prawn works only the pdf.
I belive that all you need is the gem htmltoword
Usage:
require 'htmltoword'
# Configure the location of your custom templates
Htmltoword.config.custom_templates_path = 'some_path'
my_html = '<html><head></head><body><p>Hello</p></body></html>'
document = Htmltoword::Document.create(my_html, word_template_file_name)
file = Htmltoword::Document.create_and_save(my_html, file_path, word_template_file_name)

Related

How to fill a fillable PDF Form with Rails?

i would like to fill a pdf with data from a form/database (like this https://www.youtube.com/watch?v=SJNCc2GwREA&t=190s but i didn't see any gem for that is this even possible ?
i dont want to create html page and generate a pdf like prawn or wicked PDF.
Do u have any ideas ?
It looks like this gem would do the trick - https://github.com/tcocca/active_pdftk
Let me know if this helps you

Ruby Mechanize Gem find fields without names

I’m trying to use the mechanize gem to scrape a page that is behind a login. However for the site i am using, they don’t name their username or password fields in the html. I’ve searched the Mechanize documentation, but i cannot fins the code to enter textin a field without using the field name. Is there a way to find the 5th element on the page, or the first text box?
If you have liberty to use other gem then why don't you try using Nokogiri Gem.
It is very flexible where you can use either css selector OR can use XPath selector to search specific elements.
You can use
agent = Mechanize.new
page = agent.get(<yourpage>)
form = page.forms[<index of form>]
form.fields[<index of field>].value = '<your value>' # setting the values
form.fields[<index of field2>].value = '<your value2>'# setting the values
page2 = form.submit # submitting the form

Support Markdown editing within rails app

I allow my users to create assignments and distribute them to their employees. Assignments have a attribute called description. Instead of users filling out a plain text field to create a description I want to give them a editable markdown supported box to fill in. exactly like the one I'm filling out now. I've never built anything like this out before, but I'm wondering if there is a Ruby gem or plugin that will help me out with this?
I can recommend you SimpleMDE javascript markdown editor. In this case you don't need a ruby gem for markdown because SimpleMDE can generate html version for you. You need to just save in your database both versions - markdown and html.
We have table assignments with two fields description to keep a markdown version and description_html to keep a html version of a question. As far you create common rails assignments' form and bind simplemde instance to description textarea. For description_html create hidden field tag:
<%= f.hidden_field :description_html %>
<%= f.textarea :description %>
On any change simplemde will save a html version to description_html hidden field:
var simplemde = new SimpleMDE({ element: $("#MyID")[0] });
simplemde.codemirror.on("change", function(){
# set a html to a hidden field
$('#description_html_id_CHANGE_IT').val(simplemde.getHtmlValue());
});
I suggest you use github's gem for that: https://github.com/github/markup
First install a couple of gems:
github/markup
html-pipeline
nokogiri
nokogiri-diff
Then try the following:
require "github/markup"
require 'html/pipeline'
require 'nokogiri'
require 'nokogiri/diff'
filename = ARGV.first
puts GitHub::Markup.render(filename, File.read(filename)).strip.force_encoding("utf-8")

Multiline graph in PDF file using 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.

How can I insert a ruby form into Redcloth-modified HTML in Rails?

I have a page where I use RedCloth to use markdown for the text and images. I want to be able to place a form somewhere in the text by putting the string [Form] and my application does a substitution to replace [Form] with the ruby code to execute a Rails form.
How do I do this? I currently executve the RedCloth-ed text with a "<%=h %>" so I don't know how to substitute a string with the Rails form code?
Thanks!
For example, in my view, I currently display a mp3 player using a gem and its helper:
5 #landing_page
6 #message
7 = mp3_player #landing_page.mp3.url unless #landing_page.mp3_file_name.blank?
8 = #redcloth_landing_page
But I actually want more flexibility in terms of being able to place this mp3 player somewhere within the redcloth document by using a substitution string {mp3}.
I'd like where the {mp3} is placed to be where the mp3 player shows up.
First of all I think i understood your question correctly :D,
If you want to add a form (with html tags) as string and wish to function it as a normal form you should look in to template language like 'liquid' (https://github.com/tobi/liquid/). and it works with 'RedCloth' too
cheers
sameera

Resources