Thursday 9 July 2015

Converting rml file to pdf

RML->PDF

PDF generation using reportlab's rml file


What is RML?
RML is the Report Markup Language - a member of the XML family of languages, and the XML dialect used by rml2pdf to produce documents in Adobe's Portable Document Format (PDF).RML documents can be written automatically by a program or manually using any word processor that can output text files (e.g. using a "Save as Text" option from the save menu). Since RML documents are basic text files, they can be created on the fly by scripts in Python, Perl, or almost any other language. RML makes creating documents in PDF as simple as creating a basic web page - RML is as easy to write as HTML, and uses "tags" just like HTML. It is much easier than trying to write PDF programmatically.
User guide for rml can be found here.


Installation:
 You will have to download and install reportlab plus. You can find the installation steps here.


(Assuming that you have already created a django app)

Now consider the case that I have an xml file which I need to convert to pdf. For that first you will have to create an rml file depending upon your need and save with .rml extension.

RML sample files can be found here.

In views.py import the required modules.


from rlextra.rml2pdf import rml2pdf
import xml.etree.ElementTree as ElementTree
from xml.dom import minidom



Next we write a function to obtain the rml file. The function at first extracts data from the xml file (as dict) the pass it on to the rml through context. Finally the function returns an rml in the specified encoded format  .


def getRML():
    xmldict = {}
    tree = ElementTree.parse('path to your xml file')
    root = tree.getroot()
    xmldict = XmlDictConfig(root)
    t = Template(open('path your rml file').read())
    c = Context({"xmldata": xmldict}) 
    rml = t.render(c)
    #django templates are unicode, and so need to be encoded to utf-8
    return rml.encode('utf16')


Last step is the pdf generation process.


class ConvertView(View):
   

    def get(self, request, *args, **kwargs):
        rml = getRML()
        buf = cStringIO.StringIO()
        
        #create the pdf
        rml2pdf.go(rml, outputFileName='output.pdf')
        buf.reset()
        pdfData = buf.read()
        
        #send the response
        response = HttpResponse(mimetype='application/pdf')
        response.write(pdfData)
        response['Content-Disposition'] = 'attachment; filename=output.pdf'
        return response


Now in urls.py :

from app.views import ConvertView

urlpatterns = patterns('',
    url(r'^admin/', include(admin.site.urls)),
    url(r'^convert/$', ConvertView.as_view(), name="convert2pdf"),
      
)

Finally call the 'convert' url to obtain the pdf document.

2 comments:

  1. Thanks for this useful information, I'm sure lot of people in this industry is gonna praise the effort you put in to write this post.
    XSL-FO to PDF

    ReplyDelete