Convert html to pdf using prawn in ruby on rails - ruby-on-rails

I am working on a Rails project and I need to convert the HTML page to a PDF page but it's writing HTML as it is on a pdf page. PDF page is not showing like a webpage. How can I generate a proper PDF from an HTML file?
Prawn::Document.generate("test.pdf") do
filepath = ${filepath}"file.html"
data = File.read(filepath);
text data
end

prawn is not really an HTML to PDF generator - see https://github.com/prawnpdf/prawn#should-you-use-prawn
You'll need to use another tool, for example wicked_pdf - see https://github.com/mileszs/wicked_pdf#super-advanced-usage
In your case, to quote from the README, you'll need something like
# create a pdf file from a html file without converting it to string
# Path must be absolute path
pdf = WickedPdf.new.pdf_from_html_file('/your/absolute/path/here')

Related

Why naming convention of views files in rails are considered as action.html.erb only?

Why naming convention of views files in rails are considered as action.html.erb only ( instead of action.erb.html ) ?
What will happen if we write views files as action.erb.html ?
What will happen if we write views files as action.erb.html?
The first thing that will happen is that the Ruby syntax highlighting in your editor will stop working as the file extension is now .html instead of .erb. On almost every file system in use the file extension is the rightmost part of the file name.
The second thing that will happen is that Rails will no longer be able to lookup the template and even if it could it would no longer process it through ERB as it no longer has the .erb file extension.
.html is just a segment of the file name that lets the rails template resolver distinguish between templates for different formats when looking up a template for a given request format. Its not really technically part of the extension. For example:
show.html # just HTML - no processing
show.html.erb # a HTML ERB template
show.html.slim # a HTML Slim template
show.html.haml # a HTML Haml template
show.xml.erb # a XML ERB template
show.xml.slim # a XML Slim template
show.xml.haml # a XML Haml template
show.json.erb # a JSON ERB template
show.json.jbuilder # a JSON jBuilder template
TRDL; changing the file extension is a dumb idea. Especially when you consider that Rails actually supports multiple template engines such as jbuilder, Slim and Haml in addition to ERB.

Configure ckeditor in rails to write in Markdown syntax as opposed to HTML

In one part of my application I am successfully using creditor. It properly takes the text typed in and converts it into html format.
User Types in the content:
And it properly saves the content in html syntax:
In another place in my application I want to again use ckeditor. However: this time I want the content to be converted into Markdown syntax as opposed to HTML syntax. Is this possible? If so, how do you specify that configuration for this one place in the application (as opposed to globally specifying this rule because everywhere else I want ckeditor to continue converting the content into html).
I did look within the ruby gems ckeditor docs. I also looked through the ckeditor documentation on options, however looking through that was a bit overwhelming to me.
Seems like you have to add the markdown addon as it is not part of ckeditor, then follow the documentation.
What I ended up doing was creating a script that converted the database fields in my application that were in markdown syntax into html syntax. I did this with the redcarpet Gem and the rdiscount Gem. With those fields now saved in html syntax instead of markdown syntax, I rendered the text just like I do everywhere else where ckeditor is used.

Grails JR report render as HTML

I am using the JasperReports plugin to generate a complex report. It is working fine with pdf file but requirement is that before download the pdf show exact pdf report preview in html format.
I am just trying to use "inline=true" "_format=html".
When It rendering the html there are a big different in PDF and HTML.
The report in html format looks like this:
and in pdf format:
Can you please suggest how I can show exact pdf version in html?
You can use the following line in your controller:
params.IS_USING_IMAGES_TO_ALIGN = false

how to create multi page pdf from html

We need to print thousands of invoices that are in this format -
http://example.com/orders/n
where n = thousands of orders
Going through each order and then clicking on "print" is taking us a loooong time. Is there a way to create a multipage pdf from each of those URLs that we can download as one pdf so we can hit "print" once?
You could try a ruby script using the PDFkit gem (wraps wkhtmltopdf).
I would suggest splitting your pdf's into probably 50 to 100 pages each, don't like the thought of a 1000 page pdf in memory... probably fall over.
Example script, concats pages into one big html string with page break divs and saves to file:
require 'rubygems'
require 'open-uri'
require 'pdfkit'
PDFKit.configure do |config|
config.wkhtmltopdf = '/path/to/wkhtmltopdf'
end
invoice_numbers = (1..1000) #replace with actual numbers
html = ""
invoice_numbers.each do |n|
html << open("http://example.com/orders/#{n}").read + "<div style='page-break-before:always'></div>"
end
pdf = PDFKit.new(html, :page_size => 'Letter')
pdf.to_file('/path/to/invoices.pdf')
Consider to use wkhtmltopdf.
Its a very nice command line util that uses Webkit rendering engine to produce pdf pages.
For invoices I like to use htmldoc - it renders a little nicer than wkhtmltopdf, but the downside is that you can't use a stylesheet.
So for htmldoc you would probably have to re-code your invoice view to use a more tabular layout with inline styles.

Ruby on Rails: Generate HTML for each file in a folder

I currently have a folder with a list of small *.ogg files that I would like to insert into an html.erb page in my /public folder.
The code is within video tags - I want to use Ruby to scan the folder and produce a video snippet for each video it finds in the folder. The code will stay the same between videos, except for the video filename.
The resulting page will be a list of playable videos.
Any advice would be awesome.
# assumes the current dir contains the ogg files
html = ''
Dir.glob("*.ogg") do |file|
html += "<a href=\"#{file}\" />"
end
puts html
# <a href="a.ogg" /><a href="b.ogg" />

Resources