Custom fonts with Wicked PDF on Rails

Well! We all have had a hard time getting through the custom fonts in our website or web-application. It has been easier with the @font-face implementation through CSS though this requires different formats of file integrated through it.

This implementation was OK until our client wanted to have the custom font implemented in the PDF generated by wicked_pdf in our Rails Application. This was not working right as intended, PDF using the default font rather than the custom font specified. And yes, we couldn’t even inspect element to diagnose the problem as could be done in webpage.

I also tried specifying the path to fonts explicity as mentioned here and below, but it still didn’t solve the problem.

# app/assets/styleshsheets/fonts.css.erb
@font-face {
  font-family: 'ITC Franklin Gothic STD';
  font-style: normal;
  font-weight: 300;
  src: url('<%= Rails.root.join('app', 'assets', 'fonts', 'itcfranklingothicstd-book.eot') %>') format('eot')
}

Searched through Google for some time and went through different articles to then sum up with the solution:

Solution: Base64 Encode @font-face

Yes, we can actually base64 encode the font-face and then use it through CSS @font-face to have it implemented in our website or web-application. This actually solves the problem of using custom fonts with Wicked PDF on Rails.

First, base64 encode ttf or otf of the font-face which can be done with CLI in Linux or Unix:

$ base64 /path/to/Verlag-Black-Italic.ttf > /just/to/save/verlag-black-italic.txt

Then copy the base64 encoded text from /just/to/save/verlag-black-italic.txt to paste it in the @font-face as mentioned below:

@font-face {
  font-family:"font-name";
  src:url(data:font/truetype;base64,[paste-base64-here]);
  font-style:italic;
  font-weight:900;
}

Wohoo!!! Now run the PDF generator with the CSS implemented for the font and see the magic happen with your custom font implemented in the PDF. This should make you and your client happy and smiling! 🙂

Do leave a comment below if this could be done in a better and/or easier way as Learning is endless…

Resources

Is class required while experimenting Ruby codes in file?

Dear Rubyists,

There I was trying to guide a very good friend of mine, Sajin Shakya who was keen to learn a programming language, Ruby On Rails. I suggested him not to jump onto Rails and dig deep into Ruby first. Hence, as he had learnt C programming earlier, I told him to practice developing similar algorithms he’d previously learnt in his academics through Ruby.

So, he created a method which would calculate the factorial of given number as follows:

[code language=”ruby”]
require "benchmark"
puts Benchmark.measure{
def fact(n)
if n == 0
1
else
n * fact(n-1)
end
end
(1..50000).each do |n|
end
puts fact("5")
}
[/code]

The benchmark seen was added later to check the performance of the code snippet in comparison to that I suggested to him which looks like this:

[code language=”ruby”]
require "benchmark"
puts Benchmark.measure{
class Integer
def fact
n=self
if n == 0
1
else
n * (n-1).fact
end
end
end
(1..50000).each do |n|
end
puts "7".fact
}
[/code]

My first thought before suggesting was if the method definition would work without class definition or not. But to my surprise it was working fine. Then he questioned me why we should be defining the method within the Integer class rather than defining it externally as he’d done. Also, his thought was that it would reduce the line of code as defining class would not be required.

I convinced him with how important the class could be to make our coding life easier but he resisted that in this case it was not required to define the class. Now, our focus was on if the particular change was required to experiment ruby codes in file or not. Hence, the question arose “Is class required while experimenting Ruby codes in file?”.

We benchmarked and noted the difference with the performance but it was not that noticeable. Then, we moved on to figuring out what the difference we could get from these two routes.

The difference noted was that the method were defined within specific class meaning that only that particular class would get the defined method and hence, the error would pop-up immediately if the method implementation was made in a wrong class. And since we’d always have integer to calculate the factorial it seemed legible.

In the first case if a “string” was provided to the method then it would pop-up the error after running through the codes in the method. But in second case it would not bother to run through the codes as it is a “string” and the method is defined for the “integer”. The errors observed can be seen below.

checking the difference

Please feel free to comment your thoughts and help us figure out.

Validation on create with Ripple (Riak On Rails)

Previously I mentioned how to achieve Uniqueness Validation With Ripple.

Similarly, Ripple doesn’t provide direct method for adding validation only on create while trying to implement Riak On Rails using Ripple as modeling layer. This is very much necessary for the validation of properties such as password which are to be checked only while being created. Below is the code sample I developed for validation on create manually.
Continue reading

Uniqueness Validation With Ripple (Riak On Rails)

I’m a riak newbie who is trying to implement Riak On Rails using Ripple as modeling layer. But while creating User model, I wanted validation for uniqueness of user. Validation such as presence is available with Ripple but I couldn’t find proper validation method directly provided for the uniqueness. So, below is the sample code of User model I developed for validation.
Continue reading