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
...



terça-feira, 7 de Maio de 2019 às 20:19:50 UTC+1, Paul Ellis escreveu:
>
> 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?
>
> On Fri, May 3, 2019 at 2:55 PM <[email protected] <javascript:>> wrote:
>
>> I use xhtml2pdf.
>>
>>
>> quarta-feira, 14 de Novembro de 2018 às 10:52:36 UTC, mostwanted escreveu:
>>>
>>> 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.
>>>
>>> -- 
>> 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 
>> [email protected] <javascript:>.
>> For more options, visit https://groups.google.com/d/optout.
>>
>

-- 
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 [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/web2py/b9bdc8a0-9193-4bf8-821e-2fdd526d477c%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to