how to force long URL/text to wrap in table cell when generating PDF in rails?

790 Views Asked by At

I am using princely(https://github.com/mbleigh/princely) to generate PDF in rails. I have long url in one table cell. It will extend the margin when I generate the PDF. In html,"word-break: break-all;" works well. But this rule "word-break: break-all;" doesn't work in PDF. Any body have any idea to wrap the long text when generating PDF?

2

There are 2 best solutions below

0
On

Since princely is converting the ERB to PDF i believe we can use the truncate helper function of rails to make the link shorter.

= link_to truncate("The anchor you want to place", :length => 5), 'http://yoururl'
0
On

I was confronted with a similar issues when printing. Aside from the technical problem, you need to ask yourself some design questions: If you were generating a web page, a user would click the link. But if you're generating a PDF, I assume the goal is to print it. In which case, someone needs to type in this long URL.

  1. How likely are the users going to be to type in a long url? If it only makes sense in the context of a live webpage, maybe you want to eliminate that content from the PDF.
  2. If they do need to visit the destination, but they don't need to see the exact URL, then it may make more sense to shorten the URL.

If you want to shorten the URL, you can implement a URL redirection service on your Rails app. I like the following bit of code to generate the short URL code because they are all keyboard-friendly (they don't contain confusing characters, and don't require lots of shifting or switching of keyboards):

def generate_short_murl
  a = [('a'..'k'),('m'..'z')].map{|i| i.to_a}.flatten
  n = [('2'..'9')].map{|i| i.to_a}.flatten
  (0...4).map{ a[rand(a.length)] }.join + (0...3).map{ n[rand(n.length)] }.join
end