PDF view - set printing page size

103 views
Skip to first unread message

Pavel

unread,
Feb 22, 2015, 3:50:55 AM2/22/15
to web...@googlegroups.com

Hallo,
I am making simple app for registrations and printing ID cards. I have simple pdf view. For instance:

{{
import os
from gluon.contrib.generics import pdf_from_html
html
= 'Today is: ' + str(request.now)
=pdf_from_html(html)
}}


How to specify PDF page size for instance like credit card size?

Thank you very much for any advice.

José Luis Redrejo

unread,
Feb 23, 2015, 4:32:19 AM2/23/15
to web...@googlegroups.com
I think this could be useful for you:
https://github.com/jredrejo/Labels-for-pyfpdf

--
Resources:
- http://web2py.com
- http://web2py.com/book (Documentation)
- http://github.com/web2py/web2py (Source code)
- https://code.google.com/p/web2py/issues/list (Report Issues)
---
You received this message because you are subscribed to the Google Groups "web2py-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to web2py+un...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Pavel

unread,
Feb 24, 2015, 8:36:49 AM2/24/15
to
Really thanks José,
your "labels" app help me with understanding fpdf. My apps is working now.
"Little" problem is unicode characters. I tried examples from fpdf wiki pages, but without success.
Fortunately all dynamic fields on our ID cards are numbers and characters without accent.

Thanks

José Luis Redrejo

unread,
Feb 25, 2015, 10:46:06 AM2/25/15
to web...@googlegroups.com
2015-02-24 14:36 GMT+01:00 Pavel <pave...@gmail.com>:
Really thanks José,
your "labels" app help me with understanding fpdf. My apps is working now.


You're welcome
"Little" problem is unicode characters. I tried examples from fpdf wiki pages, but without success.
Fortunately all dynamic fields on our ID cars is numbers and characters without accent.

Yes, all of us who don't live in english spoken countries share the same problem.
There are two solutions for this issue, depending on the charset you need.

The easy one is executing:

    import sys
    reload(sys)
    sys.setdefaultencoding("latin-1")

In the function you are generating the pdf. This works for me with accents or spanish ñ.


But, if there are some chars not available in latin-1 encoding (like the european currency €) the second solution is loading a true type font:

As an example:
    import sys

    reload(sys)
    sys.setdefaultencoding("utf-8")

    pdf.add_font('DejaVu', '',os.path.join(request.folder,'static', 'DejaVuSansCondensed.ttf'), uni=True)

This should work like a charm (if you leave the file DejaVuSansCondensed.ttf  in the static folder of your application)


Regards
José L.

 

Thanks

Pavel

unread,
Mar 5, 2015, 4:31:05 AM3/5/15
to
I am from Central Europe region with a lot of accented characters. I am using your second solution:

import sys

reload
(sys)
sys
.setdefaultencoding("utf-8")

pdf
.add_font('DejaVu', '',os.path.join(request.folder,'static','DejaVuSansCondensed.ttf'), uni=True)



... and now it is all working completely.

Thanks again.

Pavel
Reply all
Reply to author
Forward
0 new messages