All posts by Jelle
I love Categories
One thing I really love about Objective-C is Categories.
What are they?
Categories make it possible to add a method to a class without subclassing.
For example:
Let’s assume that you want to create a method isEmpty. And you want to find a clean way to ask the NSString instance for it.
Then we’ll create a category on the NSString class in which we define a method isEmpty that returns a BOOL value.
How to create them?
Create a header (NSString+UtilityMethods.h) and an implementation file (NSString+UtilityMethods.m).
You define the category and the method in the header file like this:
@interface NSString (UtilityMethods)
- (BOOL)isEmpty;
@end
And in the implementation file like this:
@implementation NSString (UtilityMethods)
- (BOOL)isEmpty {
return [self count] == 0;
}
@end
Now you can use the isEmpty method on every instance of NSString. (Don’t forget to include the NSString+UtilityMethods.h file)
NSString *text = @"";
if ([text isEmpty]) {
text = @"Categories are awesome";
}
NSLog(text);
The above snippet will print out “Categories are awesome”.
Another example with UIColor
This is how I clean up my code with categories. I create a category on UIColor to add methods that return every different UIColor used in my application. This way I only have to modify the colors here in orde to change them in the app.
Header file:
@interface UIColor (ApplicationColors)
+ (UIColor *)backgroundColor;
+ (UIColor *)textColor;
@end
Implementation file:
@implementation UIColor (ApplicationColors)
+ (UIColor *)backgroundColor {
return [UIColor redColor];
}
+ (UIColor *)textColor {
return [UIColor whiteColor];
}
@end
Nice isn’t it?
PDFKit and Ruby on Rails
I finally found some time to write my first blog post for my awesome company! Here we go…
For a recent project I decided to use PDFKit to generate PDF files in Ruby on Rails. I’m used to generating these with Prawn, but as we all know it can be very hard to get it styled correctly.
Installation
First things first, we have to install the gem and then the wkhtmltopdf binary.
Installing the gem is easy:
gem install pdfkit
Hey, installing the binary is as easy:
sudo pdfkit --install-wkhtmltopdf
Now we can use PDFKit on our system.
Manual generation
To generate a pdf from a HTML web page just create the kit instance:
kit = PDFKit.new "<p>This is an awesome pdf.</p>"
And then convert it to a pdf file:
pdf = kit.to_file file_path
You can also pass an URL of a file to the PDFKit initializer method:
kit = PDFKit.new "http://www.10to1.be"
kit = PDFKit.new File.new(file_path)
Some Rails magic
But here is some real magic. You can just attach the pdf format to your controller actions. When you go to http://test.be/people/jelle.pdf, the PDF will be downloaded (or your browser will open it in a window).
Just include the following lines of code in your environment.rb (for Rails 2.x) or application.rb (for Rails 3.x):
require "pdfkit"
config.middleware.use PDFKit::Middleware
What happens now is that your show.html.erb file is rendered as usual but it is converted to the PDF format afterwards. So the PDf file gets the same style as the HTML page (with CSS styling included).
I used this for a project, and it works great!
Changing the filename
Now another handy way to change the filename for the PDF file. When you go to http://test.be/people/1.pdf, the downloaded filename will be 1.pdf. But what if I want the filename to be jelle-vandebeeck.pdf? Well, just include this line of code somewhere in your action:
def show
@person = Person.find params[:id]
# Change the filename of the downloaded pdf file
headers["Content-Disposition"] =
"attachment; filename=\"#{@person.name}\""
end
For more information visit the Github page.
