SMOLNET PORTAL home about changes

Create nice unicode PDF using Python

Today I started one of the less motivating activities in Python x: encoding.

In Python 3 unicode will be everywhere, but as of the 6 version I’ve on one of the server I have to endure.

Objective: get data from a UTF-8 encoded json and print a nice PDF.

Tools: json, urllib2, fpdf, cgi

What you need:
pyfpdf: https://code.google.com/p/pyfpdf/downloads/list


https://code.google.com/p/pyfpdf/downloads/list (https://code.google.com)


  • Download fpdf-hg.zip or more recent
  • Unzip, enter the directory and python setup.py install
  • locate fpdf
  • cd /usr/lib/python6/site-packages/fpdf (or the directory name you got with locate)

*

  • Unzip and copy the fonts folder in the fpdf directory


Now you have a working FPDF with unicode support and unicode fonts. Start to write your script, I assume you’re using python 6, if not change python6 to your python version (e.g. 7) or remove version number in the heading (just python). As now FPDF works with Python 5 to

Here I write a simple cgi-bin script, so you have to put it in the /var/www/cgi-bin directory (CentOS) or in /usr/lib/cgi-bin (Debian).

 #!/usr/bin/env python6 #-*- coding: utf-8 -*- from fpdf import FPDF import json import urllib2 import os import cgi import sys # set system encoding to unicode import sys reload(sys) sys.setdefaultencoding("utf-8")


Now get some arguments from url. These will be used to compile a query to a external json service.

 # e.g. http://example.com/cgi-bin/myscript.py?lang=en&sid=2 sid = arguments.getlist('sid')[0] lang = arguments.getlist('lang')[0] # compile a request to get a particular element from an external json dataurl = "http://example.com/external-json-source?lang=%s&sid=%s" % (lang, sid) # load json from dataurl and convert into python elements data = json.load(urlliburlopen(dataurl)) # the json has a user attribute: the user attribute has name and surname attributes as strings user = data['user'] # title is a simple string title = data['title']


Now you have to load the json from the external source. Json must be encoded in UTF-8:

 lato_lungo = 297 lato_corto = 210 pdf = FPDF('L','mm','A4') # add unicode font pdf.add_font('DejaVu','','DejaVuSansCondensed.ttf',uni=True) pdf.add_page() pdf.cell(w=lato_lungo,h=9,txt=title,border=0,ln=1,align='L',fill=0) pdf.set_font('DejaVu','',12) # paragraphs rendered as MultiCell # @see https://code.google.com/p/pyfpdf/wiki/MultiCell # print key: values for each user['data'] dictionary attributes for val in user.iteritems(): pdf.multi_cell(w=0,h=5,txt="%s: %s" % val) # finally print pdf print pdf.output(dest='S')


Now:

Open your browser and visit http://example.com/cgi-bin/myscript.py?lang=en&sid=2 The external source http://example.com/external-json-source?lang=en&sid=2 is grabbed and converted into a python data structure. Both source and destination encoding are unicode utf- Data from external source are used to create the pdf.

http://example.com/cgi-bin/myscript.py?lang=en&sid=2 (http://example.com)
http://example.com/external-json-source?lang=en&sid=2 (http://example.com)


You can use as many fonts as you have in the fpdf/font directory, just add those using pdf.add_font().

https://code.google.com/p/pyfpdf/downloads/list (https://code.google.com)


https://web.archive.org/web/20130510000000*/https://code.google.com/p/pyfpdf/downloads/list (https://web.archive.org)
/> </a></p> <ul style= (https://web.archive.org)
https://web.archive.org/web/20130510000000*/https://code.google.com/p/pyfpdf/downloads/detail?name=fpdf-1.7.hg.zip&amp;can=2&amp;q= (https://web.archive.org)
https://web.archive.org/web/20130510000000*/https://code.google.com/p/pyfpdf/downloads/detail?name=fpdf_unicode_font_pack.zip&amp;can=2&amp;q= (https://web.archive.org)
sid = arguments.getlist('sid')[0] lang = arguments.getlist('lang')[0] # compile a request to get a particular element from an external json dataurl = &quot;http://example.com/external-json-source?lang=%s&amp;sid=%s&quot; % (lang, sid) # load json from dataurl and convert into python elements data = json.load(urllib2.urlopen(dataurl)) # the json has a user attribute: the user attribute has name and surname attributes as strings user = data['user'] # title is a simple string title = data['title'] </pre> <p>Now you have to load the json from the external source. Json must be encoded in UTF-8:</p> <pre class= (https://web.archive.org)
# print key: values for each user['data'] dictionary attributes for val in user.iteritems(): pdf.multi_cell(w=0,h=5,txt=&quot;%s: %s&quot; % val) # finally print pdf print pdf.output(dest='S') </pre> <p>Now:</p> <ol> <li><span style= (https://web.archive.org)
https://web.archive.org/web/20130510000000*/http://example.com/cgi-bin/myscript.py?lang=en&amp;sid=2 (https://web.archive.org)
<li><span style= (https://web.archive.org)
https://web.archive.org/web/20130510000000*/http://example.com/external-json-source?lang=en&amp;sid=2 (https://web.archive.org)
is grabbed and converted into a python data structure. Both source and destination encoding are unicode utf-8.</span></li> <li>Data from external source are used to create the pdf.</li> </ol> <p>You can use as many fonts as you have in the <strong>fpdf/font</strong> directory, just add those using pdf.add_font().</p> <p><a href= (https://web.archive.org)
https://web.archive.org/web/20130510000000*/https://code.google.com/p/pyfpdf/downloads/list (https://web.archive.org)
https://web.archive.org/web/20130510000000*/https://code.google.com/p/pyfpdf/downloads/list</a></p> (https://web.archive.org)
Response: 20 (Success), text/gemini
Original URLgemini://chirale.org/2013-05-10_1103.gmi
Status Code20 (Success)
Content-Typetext/gemini; charset=utf-8