FPDF "ne commence pas par '%PDF-' "

127 views
Skip to first unread message

J-Michel Angers

unread,
Jun 21, 2017, 11:18:50 AM6/21/17
to web2py-users
Hi,
A try to write my first PDF report (the user fill a form, manager it, .... at the end another user print this order).
All the test I read in this forum or in the documentation alwyas end with "Le fichier ne commence pas par '%PDF-' Local\EWH-3508-0 "

First test :
from gluon.contrib.fpdf import FPDF
def test():
        pdf = FPDF()
        pdf.add_page()
        pdf.set_font('Arial', 'B', 16)
        pdf.cell(40,10,'Hello World')
        pdf.output('tuto1.pdf','F')
        return response.stream(open('tuto1.pdf'))
==> I get the message

Second one :
from gluon.contrib.fpdf import FPDF
def test():
        pdf = FPDF()
        pdf.add_page()
        pdf.set_font('Arial', 'B', 16)
        pdf.cell(40,10,'Hello World')
        response.headers['Content-Type'] = 'application/pdf'   
        return XML(pdf.output('', 'S'))
==> I get the message

I remark in the source code, that the "%PDF- + version" is written in the _enddoc function, called by the close.
So I try to add "pdf.close()" ... ==> I also get the message.

Other try :
    pdf = FPDF('P','mm','A4')   # A4 Portrait en mm
    pdf.add_page()
    pdf.set_font('Arial', 'I', 14)
    pdf.cell(40, 10, "Hello, ExampleServer World")
    pdf.close()
    stuff = open("stuff.pdf", 'w')
    stuff.write(pdf.output('', 'S'))
    stuff.close()
==> I file is created, named stuff.pdf. When I try to open this file (with Windows Acrobat reader) => "Cette page contient une erreur" (This page contains an error).


Peter

unread,
Jun 26, 2017, 1:12:49 AM6/26/17
to web2py-users

Have been outputting pdfs for a while and this is what works for me ...
 
note, I don't close the pdf    pdf.close? but this allows the user to either open or save the file anywhere on their machine (Linux and Windows)

   
    ....
    pdf.cell( ln=0, h=9.0, align='R', w=178.0, txt="%s %s" % (euro(), '{:10,.2f}'.format(inv_tot)), border=0 )

    import os
    import sys
    if session.inv_print_type == 'ORIGINAL':
        invc_filename = "INVC_%s_%s.pdf" % (session.inv_number,session.inv_company)
    else:
        invc_filename = "INVC_%s_%s_COPY.pdf" % (session.inv_number, session.inv_company)

    invc_filename = invc_filename.replace(' ','_')
    invc_filename = invc_filename.replace( '&', 'and')

    s_io = pdf.output(dest='S')
    response.headers['Content-Type']='application/pdf'
    response.headers['Content-Disposition'] = 'attachment; filename="%s"' % invc_filename
    raise HTTP(200, s_io, **response.headers)


J-Michel Angers

unread,
Jun 26, 2017, 6:01:46 AM6/26/17
to web2py-users
SUPER !
With thiw, it works exactly as I wish.
 => A message appears "Do you want to open/save/saveAs ?", and after the choice "Open", I see my PDF in the "Acrobat" tool.
Thank you very much for this advice :-) :-)


Le lundi 26 juin 2017 07:12:49 UTC+2, Peter a écrit :

Have been outputting pdfs for a while and this is what works for me ...
 
note, I don't close the pdf    pdf.close? but this allows the user to either open or save the file anywhere on their machine (Linux and Windows)

   
    ....

Peter

unread,
Jun 28, 2017, 11:11:35 AM6/28/17
to web2py-users

Usually I'm the one getting the help J-Michel,

So I'm very happy to have helped someone out for a change!

Regards
Peter

Ovidio Marinho

unread,
Jun 28, 2017, 2:18:59 PM6/28/17
to web...@googlegroups.com
Publish this in http://www.web2pyslices.com/home, everyone always asks for this problem.





                   http://itjp.net.br
                     http://itjp.net.br
          Ovidio Marinho Falcao Neto
                 ovid...@gmail.com

                            Brasil
            

--
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+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply all
Reply to author
Forward
0 new messages