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")
Related
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
I have a properly working pagedown editor in a rails 4 application, but I believe I'm missing something simple to render the pagedown data at a later point in my application.
Here's the gem (and initialization) I'm using: https://github.com/hughevans/pagedown-bootstrap-rails
Any ideas on how to render that data with the already-used gems?
EDIT: I think the root of my problem is it's not storing the data as the HTML version, so it doesn't render it when I display the data again. It's missing the converter step when the form gets saved, but I don't see any specific instruction on how to do that, so I assumed it was default part of these gems.
EDIT2: I've since taken the approach to convert the Markdown on each page load, with the following code:
Unfortunately, it doesn't use all the Pagedown Markdown, but it's at least handling new lines properly:
Any ideas?
Thanks!
So the answer to this question is two fold. Either you can convert the MD to HTML and store it in the database, or leave it as MD in the DB and convert it to HTML every time you want to render it. Note that you'll need to convert it back to MD (I'm not sure if this is entirely easy or not) if you want that field to be editable in the original MD.
Since this app doesn't care about performance, I decided to store it as MD and render it.
The results I was getting above stemmed from HAML's whitespace rendering that it does, so I had to use a little HAML filters to work around that.
The HAML ended up looking like:
.wmd-output><
:preserve
#{#object.attribute}
The second challenge was actually pretty straightforward, just not explicitly stated anywhere in the Markdown documentation. So I just wrote some javascript that automatically converts any .wmd-output class into it's proper Markdown on page load:
$(function() {
$('.wmd-output').each(function(i) {
var converter = new Markdown.Converter();
var content = $(this).html();
$(this).html(converter.makeHtml(content));
});
});
I hope this helps other people in the future.
This line of haml referenced should be what you need to render it:
= f.input :description, :as => :pagedown, :input_html => { :preview => true }
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.
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?
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"}])