Generating PDF With Another Encoding
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.
HTMLDOC
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.
Example:
a controller (app/controllers/doc_controller.rb
):
-
span style=”color:#008000; font-style:italic;”>#to get @variables
-
"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.
Result
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.
Conclusion
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?