A place for opinions and notes of a valibuk.

Generating PDF With Another Encoding

Tagged with: — ondrej at 12:42 am on Thursday, March 8, 2007

Ruby on Rails A PDF document is a favourite way how to display and store a view on data, sometimes called as a report. Of course, your Ruby On Rails application can display data in a very nice form, but definitively a PDF document is a way how to store a report. Actually, it is one of the most wanted ways.

There is a nice page How to Generate PDF in Ruby on Rails that describes different frameworks for generating a PDF document in RoR.

Everything seems be fine, shiny… But did you try to generate PDF with a different encoding then ISO-8859-1? Well, maybe you know, maybe not, but there are also another languages and some of them use also special small funny waves or lines under or above (sometimes also on the sides of) a letter. Experts may call them acute, wedge or umlaut. But in the computer world, we can simply call them: complications.

So, are you a lucky person that should to put to a report words like čučoriedka?

Tested Frameworks

I tried three different frameworks: PDF::Writer, Ruby FPDF and.. not very Ruby… HTMLDOC.

The first two generated a PDF document, but I did not manage to change their encoding ISO-8859-1 to something different.

Only the last one was able to support different encodings such as ISO-8859-2 — of course, it cannot be perfect — the conversion process (the HTMLDOC converts an HTML page to a PDF document) produces only a roughly similar PDF document to your HTML page. It ignored div borders, div width, etc. in my tests.


So how does HTMLDOC work? You will create a report action, that will return a nice HTML page — a report. Then you will create a print action, that will call HTMLDOC to convert the report action HTML result to a PDF document.

To support different encodings it is necessary to convert a report to the wanted encoding and set the corresponding value of the --charset parameter for the HTMLDOC tool.

a controller (app/controllers/doc_controller.rb):

  1. span style=”color:#008000; font-style:italic;”>#to get @variables
  2. "htmldoc -t pdf –path \".;http://#{@request.env[""]}\" –webpage –charset iso-8859-2 -", "w+""doc/report""test.pdf""application/pdf"

the report view (app/views/doc/report.rhtml):


Check the HTMLDOC man page for more command line possibilities such as author name, document title, etc.


If you would like to test, how does your HTML report look like, use the following link for your local server: http://localhost:3000/doc/report.
To see the final PDF document, use the following link: http://localhost:3000/doc.

Or you can take a look at my PDF file.


The results are definitively not very optimistic. Ruby frameworks does not support UTF-8 or ISO-8859-2. The HTMLDOC provides a compromise: working encoding, but the result is not 100% copied.

Or do you know a better solution?

These icons link to social bookmarking sites where readers can share and discover new web pages.
  • DZone
  • Digg
  • Reddit
  • Technorati
  • Furl
  • NewsVine
  • Slashdot
  • Ma.gnolia
  • StumbleUpon

1 Comment »

Pingback by personal weblog » Blog Archive » PDF reporing tools

November 26, 2007 @ 12:37 pm

[…] Generating PDF With Another Encoding (Ondrej Jaura) – the author is concerned about encoding issue.. he has similar problems as I had. […]

RSS feed for comments on this post. TrackBack URI

Leave a comment

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>

Comment Preview

commercial break :)

Make an account on -- a really good hosting where you have your own virtual machine. I installed Gentoo there = I like it very much ;)