How to change page size in pdf generated by pisa

Question:

I am creating pdf from my html page using pisa in django framework. Can anyone please tell me how to adjust the pdf page size. I tried to use A5 but its not reflecting. Please find the code below:

       context_dict = {
            'pagesize':'A5',
            'result_detail': obj.result_object(slug, roll_no),
            'roll_no': roll_no
       }
       template = get_template('widgets/result.html')
       context = Context(context_dict)
       html  = template.render(context)
       result = StringIO.StringIO()
       pdf = pisa.pisaDocument(StringIO.StringIO(html.encode("ISO-8859-1")), result)
Asked By: vlad halmer

||

Answers:

You can do it by overloading a default CSS definition.
To get the current default CSS use the command line tool like this:

xhtml2pdf --css-dump > /path-to-your-project/default.css

Then, open the resulting file and insert the following line at the end:

@page { size: A5 }

In your code, you can use the resulting file as follows:

pdf = pisa.pisaDocument(StringIO.StringIO(html.encode("ISO-8859-1")), result,  default_css=open('default.css','r').read())
Answered By: NorthCat

Simply writing the snippet below in the style tag, did the trick for me.

@page { size: A5 }
Answered By: tablebubble
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.