I've been using iTextSharp with VB.NET for quite sometimes. Tonight, I try to instead use JRuby and iText. I've been thinking of a project that I'd embark very soon (freelance job). I've never done any freelance using Ruby on Rails before. This will be my first chance to show to my customer that Ruby and RoR can be extremely productive as well as fun. I expect that some of the reports will need to be converted to PDF on the fly.
To those who might not yet know what's iText and iTextSharp : ITextSharp actually is the .NET port of iText. iText is a free Java-PDF library. It provide API for you to produce PDF file programmatically on the fly. It can be used to produce PDF from normal applications as well as serve via web applications.
Okay, enough introduction. Let see what you'll need to follow through this mini tutorial. I'll assume that you're using Ms Windows. However, if you're using other than windows, most of the steps are still relevant; you just need to adapt some of the steps to your platform.
1. Of course, you'll need Java. Head up to http://java.sun.com/ and download Java SDK and install it.
2. Go to http://jruby.codehaus.org/ and download jruby. As of this writing, the latest version is jruby 1.0. After you've successfully download it, extract the file to c:\. You'll have a new folder c:\jruby-1.0
3. We need one more thing - iText. Go to iText website and download iText (bear in mind that you need to download the Java version i.e. the compiled code itext-2.0.4.jar). Copy it to any location where you store other Java jars file. In my case I have one folder called c:\jars
4. Okay, now we need to tweak some Windows environment variable. Don't worry, it's not rocket science. Click 'Start' button, then right-click on 'My Computer', then choose 'Properties..' and then choose 'Advanced' tab. Click the 'Environment Variables' button. You'll get a new window. Click new under the System variables. You'll get a prompt windows which ask you for variable name and variable value. Put JAVA_HOME in variable name and c:\Program Files\Java\jdk1.6.0_01 in variable value textbox. Bear in mind that your java installation folder may be different from mine. Repeat the process of entering new variable two more times with these values:
JRUBY_HOME c:\jruby-1.0
CLASSPATH .;c:\jars\itext-2.0.4.jar
5. If you reach this far, you've pass the boring part. Now we move on to the interesting part - the coding!
6. As usual, the helloworld example! Let's write the code below using whatever editor you love.
require 'java'
include_class "java.io.FileOutputStream"
include_class "java.io.IOException"
include_class "com.lowagie.text.pdf.PdfWriter"
include_class "com.lowagie.text.Document"
include_class "com.lowagie.text.Paragraph"
document=Document.new()
PdfWriter.getInstance(document, FileOutputStream.new("HelloWorld.pdf"))
document.open()
document.add(Paragraph.new("Hello World"))
document.close
7. Save the file as helloworldpdf.rb. Make sure you know where you save it.
8. Now open your command prompt (i.e. Start | Program Files | Accessories | Command Prompt). Change directory to where you;ve saved the helloworldpdf.rb before. To execute the code, you need to type jruby helloworldpdf.rb
9. Voila! if there is no error, you'll have your first PDF created programmatically using Ruby and iText on the same folder where you save the helloworldpdf.rb.
10. That's all folks.
I plan to write more on the iText library later. Till then, I hope you've enjoyed this mini tutorial.

6 comments:
Nice tutorial. PDF's can also be created via programs using the PDF::Writer library for Ruby.
- Vasudev Ram
Dancing Bison Enterprises
PDF::Writer isn't very good or advanced.
After setting CLASSPATH, at the last step, I got this error but don't know how to fix:
Exception in thread "main" java.lang.NoClassDefFoundError: org/jruby/Main
Caused by: java.lang.ClassNotFoundException: org.jruby.Main
at java.net.URLClassLoader$1.run(URLClassLoader.java:200)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:188)
at java.lang.ClassLoader.loadClass(ClassLoader.java:306)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:276)
at java.lang.ClassLoader.loadClass(ClassLoader.java:251)
at java.lang.ClassLoader.loadClassInternal(ClassLoader.java:319)
Why not Rjb and iText? :D
require 'rubygems'
require 'rjb'
Rjb::load(classpath = '.', jvmargs=[])
FileOutputStream = Rjb::import('java.io.FileOutputStream')
PdfWriter = Rjb::import('com.lowagie.text.pdf.PdfWriter')
Document = Rjb::import('com.lowagie.text.Document')
Paragraph = Rjb::import('com.lowagie.text.Paragraph')
document=Document.new()
PdfWriter.getInstance(document, FileOutputStream.new("HelloWorld.pdf"))
document.open
document.add(Paragraph.new('Hello'))
document.close
Joy in warcraft leveling living comes wow lvl from having wow lvl fine emotions,wow power level trusting them,power leveling giving them power leveling the freedom of wrath of the lich king power leveling a bird in the open.wlk power leveling Joy in living can age of conan gold never be assumed as a pose,or put on from guildwars gold the outside as a mask. People who have this joy don not need maple story mesos to talk about it; they radiate it. They just live out their joy and let it splash its sunlight and glow into other lives as naturally as bird sings.We can never get it by working for it directly.
Weekends to peopleig2tmean that they can have a two-day wowgold4europe good rest. For example, people gameusdcan go out to enjoy themselves or get meinwowgoldtogether with relatives and friends to talk with each storeingameother or watch interesting video tapes with the speebiewhole family.
Everyone spends agamegoldweekends in his ownmmoflyway. Within two days,some people can relax themselves by listening to music, reading novels,or watchingogeworld films. Others perhaps are more active by playing basketball,wimming ormmorpgvipdancing. Different people have different gamesavorrelaxations.
I often spend weekends withoggsalemy family or my friends. Sometimes my parents take me on a visit to their old friends. Sometimesgamersell I go to the library to study or borrow some books tommovirtexgain much knowledge. I also go to see various exhibition to broadenrpg tradermy vision. An excursion to seashore or mountain resorts is my favorite way of spending weekends. Weekends are always enjoyable for me.
igxe swagvaultoforu wowgold-usaignmax wowgoldlivebrogame thsaleGoldRockUbrogameswagvaultgoldsoonoforuigxethsale
Post a Comment