How to convert my view to pdf

160 views
Skip to first unread message

mostwanted

unread,
Nov 14, 2018, 5:52:36 AM11/14/18
to web2py-users
I have been researching this topic alot because i am trying to achieve this, i want to convert my view exactly as it to PDF, i came across some information on web2py-appreport but I don't understand exactly how it works, maybe its my slow mind, i followed the examples in https://github.com/lucasdavila/web2py-appreport/wiki/Docs-and-examples but the results are not what i want.

I wanna have a button on my view which when i click converts that view in to pdf straight away and have it saved. Is this possible and if so how can i achieve it?

(Smiles :)

Mostwanted.

Paul Ellis

unread,
Apr 29, 2019, 7:44:41 PM4/29/19
to web2py-users
This is really hard to answer because a PDF has a set page size and a HTML file (view) does not.

Plus getting the css used in a HTML file to translate into css is also a challenge. Just print the page you are talking about with a pdf printer driver to see. You probably won't be happy with the results, and if you are. The next person to print this way won't be.

I have PDF output from my webapp but I have put a lot of work into the formatting of the output and it is not a direct swap from a html. I actually have no HTML input into my PDF view.

jcrm...@gmail.com

unread,
May 3, 2019, 8:55:44 AM5/3/19
to web2py-users
I use xhtml2pdf.

Paul Ellis

unread,
May 7, 2019, 3:19:50 PM5/7/19
to web2py-users
I had a look at xhtml2pdf and I am not sure how to implement it in my web2py application.

Do you save the PDF as a file or output it to the browser?

Do you use the html generated by the web2py view and have the function calls in a view or do you generate the HTML another way in a controller function?

--
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 a topic in the Google Groups "web2py-users" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/web2py/y73g4sctURs/unsubscribe.
To unsubscribe from this group and all its topics, send an email to web2py+un...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

jcrm...@gmail.com

unread,
May 7, 2019, 3:37:22 PM5/7/19
to web2py-users
I have both ways in different apps.

To save to a file use like this
from xhtml2pdf.pisa import CreatePDF


...
# Type is _io.BufferedWriter.
with open(pdf_pn, 'wb') as f_out:  # type: BinaryIO
    created_ok = CreatePDF(html, dest=f_out)

To show the PDF on the browser use like this
from io import BytesIO

from xhtml2pdf.pisa import CreatePDF


def show_pdf():
    ...
    response
.headers['Content-Type'] = 'application/pdf'

    # Type is _io.BytesIO
    in_mem_stream
= BytesIO()  # type: BytesIO
    CreatePDF(html, dest=in_mem_stream)
    pdf
= in_mem_stream.getvalue()  # type: bytes
    in_mem_stream
.close()

    return pdf

I generate the HTML in my controller using a template.
Like this

from string import Template


...

    html_part2
+= Template(template).safe_substitute(fields_dic)

    html
= html_part1 + html_part2 + html_part3  # type: str
...
To unsubscribe from this group and all its topics, send an email to web...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages