Can prawn generate PDFs with links? - ruby-on-rails

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"}])

Related

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")

Repeater in Prawn PDF rendering contents twice

I have a pdf being generated by prawn, it contains a header, a table and a footer. Both header and footer are repeatable, with :dynamic => true, the table is not.
The problem I'm having is that all content on the header and footer is displaying as if it's being rendered twice in the same place, and this seems to mess with the anti aliasing function so the text appears to be choppy and bold (see image below). However, when I zoom in or print it, it looks right.
Here's some code of the specific part where I start the repeater blocks:
pdf = Prawn::Document.new :page_size => "A4", :margin => [28, 20, 7, 20]
(...)
pdf.repeat :all, :dynamic => true do
pdf.bounding_box([0, 803], :width => 555, :height => 60) do
pdf.stroke_bounds
(...)
end
end
(...)
pdf.render
# End of file
As far as I know, this is the default way to declare a repeater block, I almost mirrorred it from the manual.
Here's the image: the top container is the header, inside the repeater, it looks like everything is bold, but it's the exact same font and line width as the section below, which is from the table, and looks fine. Sorry I can't post a larger part of the pdf, it's for confidentiality reasons.
If I remove the repeater, the header will look correct, as it should, but when I add it again it looks like this.
Has anyone else encountered this issue before? How do I fix it?
EDIT: Added some more code above. Also added the code below, which is inside a Rails template file, "historico.pdf.prawn", and this is how I call it on the controller to render the PDF and return it to the user:
rendered_pdf = render_to_string :template => "reports/historico.pdf"
send_data rendered_pdf, :filename => "Historico.pdf", :type => "application/pdf"
When you use pdf.repeat :all the pdf is 'reopened' after the document is already created and the data within the repeat block is added. This can cause a myriad of odd behavior, I believe this is what is causing your issue.
There is a related issue with a solution that utilizes Prawn's canvas method. If you adapt this solution to your problem (use canvas to generate your header and footer, rather than repeat :all) , you should no longer have these text-on-top-of-text problems.
Note:
It may be important for you to know that Prawn has officially stated that they are, "not in a good position to support templating features (I bring this up because you appear to be creating a template), " so similar workarounds may be needed if you continue to use this tool for these types of needs. Depending on your dependance on these types of templating features, you may want to look into using a different tool.

Rendering Pagedown Markdown in Rails 4

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 }

rails 3 is printing html tags to screen instead of rendering them

I'm using vhochstein's fork of active_scaffold, which runs quite nicely on rails 3, except for a few small bugs - http://github.com/vhochstein/active_scaffold.
In rails 2.3, the following code disables a link:
return "<a class='disabled'>#{text}</a>" unless authorized
But in Rails 3, it causes the escaped html tags to be printed out instead as in the following photo:
How can I make the content of this return statement render the way it should in rails 3?
The code above, is from the list_column_helpers.rb file in vendor/plugins/active_scaffold/helpers/
UPDATE:
Floatless fixed this by suggesting to add .html_safe to the code.
I have since found that the folowing change also needs to be made as there's more than one bit of code that is respondible for disabling action links in active_Scaffold:
In /plugins/active_scaffold/frontends/default/views/_list_actions.html.erb change:
<%= record.authorized_for?(:crud_type => etc etc etc -%>
By making it use "raw"
i.e.
<%= raw record.authorized_for?(:crud_type => etc etc etc -%>
Anyway, thanks to floatless and hopefully mr hochstein will be able to use this stuff.
Try this:
return "<a class='disabled'>#{text}</a>".html_safe unless authorized

Markdown to text/plain and text/html for multipart email

I’m looking for a solution to send DRY multipart emails in Rails. With DRY I mean that the content for the mail is only defined once.
I’ve thought about some possible solutions but haven’t found any existing implementations.
The solutions I’ve thought about are:
load the text from I18n and apply Markdown for the html mail and apply Markdown with a special output type for the text mail where
links are put in parenthesis after the link text
bold, italic and other formatting that doesn't make sense are removed
ordered and unordered lists are maintained
generate only the html mail and convert that to text according to the above conditions
Is there any available solution out there? Which one is probably the better way to do it?
In Chapter 4 of Crafting Rails Applications, Jóse Valim walks you through how to make a "merb" handler that uses markdown with interspersed erb and can compile to text and html. Then you make a mailer generator that generates a single merb template for each of your mail actions.
You can read an excerpt from that chapter on the page I linked you to. I highly recommend buying the book.
If you're interested in using my sorry version of what he describes in that book, you can slap this in your Gemfile:
gem 'handlers', :git => "git://github.com/chadoh/handlers.git"
Be warned that I barely know what I'm doing, that I'm not versioning that gem, and that I probably won't really even maintain it. Frankly, I wish I could find someone else who was doing a better job, but I've been unsuccessful in doing so. If you want to fork my project and be the person doing that better job, go for it!
This is a PITA, but is the only way to DRY mail such that you can support both HTML (multipart) & plaintext:
Put the html email copy in a partial file in your ActionMailer view directory with the following extension: _action.html.erb
Replace "action" with whatever action name you are using.
Then create 2 more files in the same directory:
action.text.html.erb and
action.text.plain.erb
In the text.html partial:
<%= render "action.html", :locals => {:html => true} %>
In the text.plain partial:
<% content = render "action.html", :locals => {:html => false} %>
<%= strip_tags(content) %>
That works for me, though it certainly makes me want to pay the monthly service for madmimi
Use the maildown gem.
This gems does the heavy lifting of allowing you to use email.md.erb instead of email.html.erb and email.text.erb. Write it once in a sane format and have it automatically display in HTML and in Plain Text. Win.
There are some intricacies here that you'll want to look at based on your use-case, but here's some of what we did to get it working well:
Create a maildown.rb initializer to setup some sane defaults:
Maildown.allow_indentation = true # Prevents code blocks from forming when using indentiation in markdown emails.
Maildown::MarkdownEngine.set_text do |text|
text.gsub( /{:.*}\n?/, "" ) # Removes Kramdown annotations that apply classes, etc. with `{: .class }`.
This allows you to use indents in your blocks, etc. But also precludes the ability to add indents in your Plain Text. It also removes Kramdown-specific annotation from Plain Text.
Then just replace your HTML and Plain Text files with a single .md.erb file and test it out to make sure it looks good in both versions.
Note, until you remove the .html.erb and .text.erb files, it will show those first before looking for a .md.erb file. This may actually be a nice feature if you ever needed to write separate formats for a specific email (maybe a marketing one that requires more complex formatting than Markdown can provide) without having to specify anything anywhere.
Works a treat.

Resources