We love designing and developing websites, but what really drives us is solving problems and cultivating strong relationships with our clients
Generating a pdf from an image using PIL and django
By : saket
In this post we shall be performing small image manipulation on the server side and allowing the user to download the pdf. This might be found useful if you design a quiz app and want to generate a certificate for the users. The various methods which can be followed to generate the certificate are:
- Have an image file to serve as a template, use css to place the username at the desired location.
- Have a pdf file, which can be edited to take the username of the current user if he has passed certain test.
- Have an image template ready, upon which we could do some image manipulation to add the user's name at a specified location and render it.
You might think why not to render the entire certificate from pure html/css but putting the signature and logo are tough that way, which is a pre-requisite for a certificate, hence choosing any of the above methods might be something that you want to go about.
Initially we went for the first method, we used pisa library, but the results were not satisfactory, when we tried to render html text over png image and then generate the pdf.
If you want to choose the second method the relevant libraries which can be thought of are: pyPDF and ReportLab, but as per this stackoverflow post things can get trivial if we use this method eventually we went with the third approach, which helped us. The libraries which we used were PIL and imagemagick.
The code is pretty much self explanatory, inside your views use the following snippets:
import PIL
from PIL import ImageFont
from PIL import Image
from PIL import ImageDraw
# subprocess will be used to run command line arguments
import subpprocess
def download(request):
# set the path to the font and the font size
font = ImageFont.truetype("/usr/share/fonts/truetype/ttf-dejavu/DejaVuSans-Bold.ttf", 20)
text = "Sample Text which you want to display"
# the rgb value of the color
tcolor = (255, 0, 0)
# the position of the image where you intend to print the stuff
text_pos = (100,500)
'''open the image file in RGB format, this is important if we miss
convert it wont take the color(RGB) parameter and error will be thrown '''
img = Image.open("path_to_file/certificate.png").convert('RGB')
draw = ImageDraw.Draw(img)
draw.text(text_pos, text, fill=tcolor, font=font)
del draw
# save the manipulated image as the next file
img.save("path_to_image/test.png")
# we want to send a pdf
response = HttpResponse(mimetype='application/pdf')
filename = "certificate"
response['Content-Disposition'] = 'attachment; filename=%s.pdf' % filename
# here we use imagemagick to convert png image to certificate
ret = subprocess.call(["convert", "path_to_file/test.png", "path_to_file/certificate.pdf" ],\
stdout=subprocess.PIPE)
certi = open("path_to_file/certificate.pdf")
response.write(certi.read())
certi.close()
return response
This seems to solve our problem, however there can be another better approach, readers are welcome to post their views/suggestions or a different/better methodology.
Comments
Just a suggestion. It would be nice to see before and after (image source and pdf output) examples rendered from the above code.
- Test Driven Development in Python
- Deploying Django apps on Heroku
- Developing android applications from command line
- Deploy Django App in 5 Easy Steps
- Project Management Tools for Start-Ups
- Generating a pdf from an image using PIL and django
- Dynamically attaching SITE_ID to Django Caching
- Screencast: How to deploy Django on Heroku
- Deploying Django apps on Heroku
- How to use pep8.py to write better Django code
- rails
- django
- linkroundup
- django opinion
- opinion
- business
- API
- appengine
- python
- satire
- startup
- Uncategorized
- marketing
- personal
- rambling
- search
- interviews
- seo-interviews
- 5startupideas
- ideas
- seo
- tips
- forms
- paypal
- utilities
- datetime
- web2.0
- Amazon
- algorithms
- presentations
- products
- pinax
- satchmo
- ecommerce
- microsoft
- yahoo
- book
- tutorial
- models
- aggreagtion
- meta
- India
- apps
- about
- CSS
- Design
- wordpress
- test slug
- vim
- urls
- reviews
- javascript
- xmpp
- emacs
- Typography
- Grid Theory
- Color Theory
- iphone
- android
- titanium
- mobile applications
- CSS3
- Browser Compatibility
- mobile
- jobs
- lamson
- django setup
- files
- upload
- jsTree
- hierarchical view
- web page
- Treeview
- coffeescript
- request
- response
- South
- django south
- django migration
- --fake
- screencasts
- django caching
- SITE_ID prefix
- review
- code hosting
- comparison
- unfuddle
- fogbugz
- assembla
- github
- project management
- ticketing system
- gunicorn
- deploy
- nginx
- ubuntu
- vps
- android terminal
- terminal
- programming
- TDD
- Test Driven
- Development
- May 2012
- April 2012
- March 2012
- February 2012
- January 2012
- December 2011
- October 2011
- September 2011
- July 2011
- June 2011
- April 2011
- February 2011
- January 2011
- December 2010
- November 2010
- October 2010
- September 2010
- June 2010
- April 2010
- March 2010
- January 2010
- December 2009
- November 2009
- October 2009
- September 2009
- August 2009
- July 2009
- June 2009
- April 2009
- March 2009
- February 2009
- November 2008
- October 2008
- June 2008
- May 2008
- April 2008
Lovely takethrough!