Hi

I found this script from http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/189858

it uses getopt module for managing cmd arguments, but I need it to work on mobile phones. I want to make a Mobile app using this script but I cant figure out how to change the comand Args to simple raw_inputs. can somebody please help me in this(i.e. removing parseArgs() and argsCallBack()?

I know that its not a polite request, but I really appreciate any help.

thanks in advance

class pyText2Pdf:

    def __init__(self):
        # version number
        self._version="1.1.1"
            self._IsoEnc=0
            # formfeeds flag
            self._doFFs=0
            self._progname="PyText2Pdf"
            self._appname = "".join((self._progname, " Version ", str(self._version)))
            # default font
            self._font="/Courier"
            # default font size
            self._ptSize=10
            # default vert space
        # iso encoding flag
            self._IsoEnc=0
            # formfeeds flag
            self._doFFs=0
            self._progname="PyText2Pdf"
            self._appname = "".join((self._progname, " Version ", str(self._version)))
            # default font
            self._font="/Courier"
            # default font size
            self._ptSize=10
            # default vert space
        self._vertSpace=12
        self._lines=0
        # number of characters in a row
        self._cols=80
        self._columns=1
        # page ht
        self._pageHt=792
        # page wd
        self._pageWd=612
        # input file 
        self._ifile=""
        # output file 
        self._ofile=""
        # default tab width
        self._tab=4
        # input file descriptor
        self._ifs=None
        # output file descriptor
        self._ofs=None
        # landscape flag
        self._landscape=0

        # marker objects
        self._curobj = 5
        self._pageObs = [0]
        self._locations = [0,0,0,0,0,0]
        self._pageNo=0

        # file position marker
        self._fpos=0

    def argsCallBack(self, argslist, listoftuples=False):
        """ Callback function called by argument parser.
        Helps to remove duplicate code """

        x = 0
        while x<len(argslist):
            item = argslist[x]

            if listoftuples:
                o, a = item
            else:
                o = item

            if o == '-h':
                self.ShowHelp()
            elif o == '-I':
                self._IsoEnc=1
            elif o == '-F':
                self._doFFs=1
            elif o == '-2':
                self._columns=2
            elif o == '-L':
                self._landscape=1

            if o in ('-f', '-s', '-l', '-x', 'y', '-c', '-v', '-o', '-O'):

                if not listoftuples:
                    x += 1
                    try:
                        a = argslist[x]
                    except:
                        msg = "Argument error for option " + o
                        sys.exit(msg)

                if a == "" or a[0] == "-":
                    msg = "Error: argument error for option " + o
                    sys.exit(msg)
                elif o == '-f':
                    self._font='/' + a
                elif o == '-A':
                    if a == '3':
                        self._pageWd=842
                        self._pageHt=1190
                    elif a =='4':
                        self._pageWd=595
                        self._pageHt=842
                    else:
                        psz=o[1]+a
                        print self._progname, ': ignoring unknown paper size ', psz
                elif o == '-s':
                    self._ptSize=int(a)
                    if self._ptSize<1:
                        self._ptSize=1
                elif o == '-v':
                    self._vertSpace=int(a)
                    if self._vertSpace<1:
                        self._vertSpace=1       
                elif o == '-l':
                    self._lines=int(a)
                    if self._lines<1:
                        self._lines=1
                elif o == '-c':
                    self._cols=int(a)
                    if self._cols<4:
                        self._cols=4
                elif o == '-t':
                    self._tab=int(a)
                    if self._tab<1:
                        self._tab=1
                elif o == '-x':
                    self._pageWd=int(a)
                    if self._pageWd<72:
                        self._pageWd=72
                elif o == '-y':
                    self._pageHt=int(a)
                    if self._pageHt<72:
                        self._pageHt=72
                elif o in ('-o', '-O'):
                    self._ofile=a
                else:
                    print self._progname, ': ignoring invalid switch: ', o

            x += 1


    def parseArgs(self):

        if len(sys.argv) == 1:
            self.ShowHelp()

        arguments=sys.argv[1:]

        optlist, args = getopt.getopt(arguments, 'hIF2Lf:A:s:v:l:c:t:x:y:o:')

        # input file is the first element in arg list
        # or last element in options list (in case of an error!)
        if len(args):
            self._ifile=args[0]
        else:
            l=len(optlist)
            tup=optlist[l-1]

        # parse options list
        if len(optlist):
            self.argsCallBack( optlist, listoftuples=True )
        else:
            self.argsCallBack( args )

        if self._landscape:
            print 'Landscape option on...'
        if self._columns==2:
            print 'Printing in two columns...'
        if self._doFFs:
            print 'Ignoring form feed character...'
        if self._IsoEnc:
            print 'Using ISO Latin Encoding...'
        print 'Using font', self._font[1:], ' size =', self._ptSize


    def writestr(self, str):
        """ Write string to output file descriptor.
        All output operations go through this function.
        We keep the current file position also here"""

        # update current file position
        self._fpos += len(str)
        for x in range(0, len(str)):
            if str[x] == '\n':
                self._fpos += LF_EXTRA
        try:
            self._ofs.write(str)
        except IOError, e:
            print e
            return -1

        return 0

    def Convert(self):
        """ Perform the actual conversion """

        if self._landscape:
            # swap page width & height
            tmp = self._pageHt
            self._pageHt = self._pageWd
            self._pageWd = tmp

        if self._lines==0:
            self._lines = (self._pageHt - 72)/self._vertSpace
        if self._lines < 1:
            self._lines=1

        try:
            self._ifs=open(self._ifile)
        except IOError, (strerror, errno):
            print 'Error: Could not open file to read --->', self._ifile
            sys.exit(3)

        if self._ofile=="":
            self._ofile=self._ifile + '.pdf'

        try:
            self._ofs = open(self._ofile, 'wb')
        except IOError, (strerror, errno):
            print 'Error: Could not open file to write --->', self._ofile
            sys.exit(3)

        print 'Input file =>', self._ifile
        print 'Writing pdf file', self._ofile, '...'
        self.WriteHeader(self._ifile)
        self.WritePages()
        self.WriteRest()

        print 'Wrote file', self._ofile
        self._ifs.close()
        self._ofs.close()
        return 0

    def WriteHeader(self, title):
        """Write the PDF header"""

        ws = self.writestr

        t=time.localtime()
        timestr=str(time.strftime("D:%Y%m%d%H%M%S", t))
        ws("%PDF-1.4\n")
        self._locations[1] = self._fpos
        ws("1 0 obj\n")
        ws("<<\n")

        buf = "".join(("/Creator (", self._appname, " By Anand B Pillai )\n"))
        ws(buf)
        buf = "".join(("/CreationDate (", timestr, ")\n"))
        ws(buf)
        buf = "".join(("/Producer (", self._appname, "(\\251 Free Software Foundation, 2004))\n"))
        ws(buf)

        ws("<<\n")
        ws("/Type /Catalog\n")
        ws("/Pages 3 0 R\n")
        ws(">>\n")
        ws("endobj\n")

        self._locations[4] = self._fpos
        ws("4 0 obj\n")
        ws("<<\n")
        buf = "".join(("/BaseFont ", str(self._font), " /Encoding /WinAnsiEncoding /Name /F1 /Subtype /Type1 /Type /Font >>\n"))
        ws(buf)

        if self._IsoEnc:
            ws(ENCODING_STR)

        ws(">>\n")
        ws("endobj\n")

        self._locations[5] = self._fpos

        ws("5 0 obj\n")
        ws("<<\n")
        ws("  /Font << /F1 4 0 R >>\n")
        ws("  /ProcSet [ /PDF /Text ]\n")
        ws(">>\n")
        ws("endobj\n")

    def StartPage(self):
        """ Start a page of data """

        ws = self.writestr

        self._pageNo += 1
        self._curobj += 1

        self._locations.append(self._fpos)
        self._locations[self._curobj]=self._fpos

        self._pageObs.append(self._curobj)
        self._pageObs[self._pageNo] = self._curobj

        buf = "".join((str(self._curobj), " 0 obj\n"))

        ws(buf)
        ws("<<\n")
        ws("/Type /Page\n")
        ws("/Parent 3 0 R\n")
        ws("/Resources 5 0 R\n")

        self._curobj += 1
        buf = "".join(("/Contents ", str(self._curobj), " 0 R\n"))
        ws(buf)
        ws(">>\n")
        ws("endobj\n")

        self._locations.append(self._fpos)
        self._locations[self._curobj] = self._fpos

        buf = "".join((str(self._curobj), " 0 obj\n"))
        ws(buf)
        ws("<<\n")

        buf = "".join(("/Length ", str(self._curobj + 1), " 0 R\n"))
        ws(buf)
        ws(">>\n")
        ws("stream\n")
        strmPos = self._fpos

        ws("BT\n");
        buf = "".join(("/F1 ", str(self._ptSize), " Tf\n"))
        ws(buf)
        buf = "".join(("1 0 0 1 50 ", str(self._pageHt - 40), " Tm\n"))
        ws(buf)
        buf = "".join((str(self._vertSpace), " TL\n"))
        ws(buf)

        return strmPos

    def EndPage(self, streamStart):
        """End a page of data """

        ws = self.writestr

        ws("ET\n")
        streamEnd = self._fpos
        ws("endstream\n")
        ws("endobj\n")

        self._curobj += 1
        self._locations.append(self._fpos)
        self._locations[self._curobj] = self._fpos

        buf = "".join((str(self._curobj), " 0 obj\n"))
        ws(buf)
        buf = "".join((str(streamEnd - streamStart), '\n'))
        ws(buf)
        ws('endobj\n')

    def WritePages(self):
        """Write pages as PDF"""

        ws = self.writestr

        beginstream=0
        lineNo, charNo=0,0
        ch, column=0,0
        padding,i=0,0
        atEOF=0

        while not atEOF:
            beginstream = self.StartPage()
            column=1

            while column <= self._columns:
                column += 1
                atFF=0
                atBOP=0
                lineNo=0

                while lineNo < self._lines and not atFF and not atEOF:

                    lineNo += 1
                    ws("(")
                    charNo=0

                    while charNo < self._cols:
                        charNo += 1
                        ch = self._ifs.read(1)
                        cond = ((ch != '\n') and not(ch==FF and self._doFFs) and (ch != ''))
                        if not cond:
                            break

                        if ord(ch) >= 32 and ord(ch) <= 127:
                            if ch == '(' or ch == ')' or ch == '\\':
                                ws("\\")
                            ws(ch)
                        else:
                            if ord(ch) == 9:
                                padding =self._tab - ((charNo - 1) % self._tab)
                                for i in range(padding):
                                    ws(" ")
                                charNo += (padding -1)
                            else:
                                if ch != FF:
                                    # write \xxx form for dodgy character
                                    buf = "".join(('\\', ch))
                                    ws(buf)
                                else:
                                    # dont print anything for a FF
                                    charNo -= 1

                    ws(")'\n")
                    if ch == FF:
                        atFF=1
                    if lineNo == self._lines:
                        atBOP=1

                    if atBOP:
                        pos=0
                        ch = self._ifs.read(1)
                        pos= self._ifs.tell()
                        if ch == FF:
                            ch = self._ifs.read(1)
                            pos=self._ifs.tell()
                        # python's EOF signature
                        if ch == '':
                            atEOF=1
                        else:
                            # push position back by one char
                            self._ifs.seek(pos-1)

                    elif atFF:
                        ch = self._ifs.read(1)
                        pos=self._ifs.tell()
                        if ch == '':
                            atEOF=1
                        else:
                            self._ifs.seek(pos-1)

                if column < self._columns:
                    buf = "".join(("1 0 0 1 ",
                                   str((self._pageWd/2 + 25)),
                                   " ",
                                   str(self._pageHt - 40),
                                   " Tm\n"))
                    ws(buf)

            self.EndPage(beginstream)

    def WriteRest(self):
        """Finish the file"""

        ws = self.writestr
        self._locations[3] = self._fpos

        ws("3 0 obj\n")
        ws("<<\n")
        ws("/Type /Pages\n")
        buf = "".join(("/Count ", str(self._pageNo), "\n"))
        ws(buf)
        buf = "".join(("/MediaBox [ 0 0 ", str(self._pageWd), " ", str(self._pageHt), " ]\n"))
        ws(buf)
        ws("/Kids [ ")

        for i in range(1, self._pageNo+1):
            buf = "".join((str(self._pageObs[i]), " 0 R "))
            ws(buf)

        ws("]\n")
        ws(">>\n")
        ws("endobj\n")

        xref = self._fpos
        ws("xref\n")
        buf = "".join(("0 ", str((self._curobj) + 1), "\n"))
        ws(buf)
        buf = "".join(("0000000000 65535 f ", str(LINE_END)))
        ws(buf)

        for i in range(1, self._curobj + 1):
            val = self._locations[i]
            buf = "".join((string.zfill(str(val), 10), " 00000 n ", str(LINE_END)))
            ws(buf)

        ws("trailer\n")
        ws("<<\n")
        buf = "".join(("/Size ", str(self._curobj + 1), "\n"))
        ws(buf)
        ws("/Root 2 0 R\n")
        ws("/Info 1 0 R\n")
        ws(">>\n")

        ws("startxref\n")
        buf = "".join((str(xref), "\n"))
        ws(buf)
        ws("%%EOF\n")

    def ShowHelp(self):
        """Show help on this program"""

        sys.exit( PROG_HELP % {'progname': self._progname} )

def main():

    pdfclass=pyText2Pdf()
    pdfclass.parseArgs()
    pdfclass.Convert()

if __name__ == "__main__":
    main()

Edited 4 Years Ago by M.S.

Line 6 to 26 are strange, missing if/indention?

I would remove all command line option stuff and build printing defaults/editor for single _ variables in the class, either called at end of init or your favorite way for mobile (say key 0)

Edited 4 Years Ago by pyTony

Comments
really thanks dear pyTony

yes line 6 to 26 got an extra tab indentation, its my mistake in pasting the code(those lines have to be unindented one tab)

really thank you dear pyTony for your kind reply.
if you please make all those as a function that asks the user for each one, say:

   def opts(self):
       opt1 = raw_input('enter page size')
       opt2= ...
       optN= raw....
       return opt1, opt2,..., optn

OR of course something that you think is better,
then I can manage them and change them further for a mobile interface like a setting form or read/write from a setting file.

again thank you so much

Edited 4 Years Ago by M.S.

dear pyTony, whould you please do this task for me?, I'ld really appreciate it.
I myself tried many times but messed the whole code up.
I'm actually not familiar with OOP, and all my experience is with functions.

thanks again.

I did remove the option stuff, added raw_input and parameters to init for input and ouput files and made an example to input or get default for columns value. The output seems quite rubbish (see vefication report in end commanet string), as also can be seen from them comments. This code is ancient, so maybe it used to be OK those times.

You had those doubled lines I mentioned and you where also missing some import and variables.

Why not use the pyPdf module?

import time


LF_EXTRA=0
LINE_END='\015'
# form feed character (^L)
FF=chr(12)

ENCODING_STR = """\
/Encoding <<
/Differences [ 0 /.notdef /.notdef /.notdef /.notdef
/.notdef /.notdef /.notdef /.notdef /.notdef /.notdef
/.notdef /.notdef /.notdef /.notdef /.notdef /.notdef
/.notdef /.notdef /.notdef /.notdef /.notdef /.notdef
/.notdef /.notdef /.notdef /.notdef /.notdef /.notdef
/.notdef /.notdef /.notdef /.notdef /space /exclam
/quotedbl /numbersign /dollar /percent /ampersand
/quoteright /parenleft /parenright /asterisk /plus /comma
/hyphen /period /slash /zero /one /two /three /four /five
/six /seven /eight /nine /colon /semicolon /less /equal
/greater /question /at /A /B /C /D /E /F /G /H /I /J /K /L
/M /N /O /P /Q /R /S /T /U /V /W /X /Y /Z /bracketleft
/backslash /bracketright /asciicircum /underscore
/quoteleft /a /b /c /d /e /f /g /h /i /j /k /l /m /n /o /p
/q /r /s /t /u /v /w /x /y /z /braceleft /bar /braceright
/asciitilde /.notdef /.notdef /.notdef /.notdef /.notdef
/.notdef /.notdef /.notdef /.notdef /.notdef /.notdef
/.notdef /.notdef /.notdef /.notdef /.notdef /.notdef
/dotlessi /grave /acute /circumflex /tilde /macron /breve
/dotaccent /dieresis /.notdef /ring /cedilla /.notdef
/hungarumlaut /ogonek /caron /space /exclamdown /cent
/sterling /currency /yen /brokenbar /section /dieresis
/copyright /ordfeminine /guillemotleft /logicalnot /hyphen
/registered /macron /degree /plusminus /twosuperior
/threesuperior /acute /mu /paragraph /periodcentered
/cedilla /onesuperior /ordmasculine /guillemotright
/onequarter /onehalf /threequarters /questiondown /Agrave
/Aacute /Acircumflex /Atilde /Adieresis /Aring /AE
/Ccedilla /Egrave /Eacute /Ecircumflex /Edieresis /Igrave
/Iacute /Icircumflex /Idieresis /Eth /Ntilde /Ograve
/Oacute /Ocircumflex /Otilde /Odieresis /multiply /Oslash
/Ugrave /Uacute /Ucircumflex /Udieresis /Yacute /Thorn
/germandbls /agrave /aacute /acircumflex /atilde /adieresis
/aring /ae /ccedilla /egrave /eacute /ecircumflex
/edieresis /igrave /iacute /icircumflex /idieresis /eth
/ntilde /ograve /oacute /ocircumflex /otilde /odieresis
/divide /oslash /ugrave /uacute /ucircumflex /udieresis
/yacute /thorn /ydieresis ]
>>
"""

class pyText2Pdf:
    def __init__(self,input_file, output_file):
        # version number
        self._version="1.1.1"
        # iso encoding flag
        self._IsoEnc=0
        # formfeeds flag
        self._doFFs=0
        self._progname="PyText2Pdf"
        self._appname = "".join((self._progname, " Version ", str(self._version)))
        # default font
        self._font="/Courier"
        # default font size
        self._ptSize=10
        # default vert space
        self._vertSpace=12
        self._lines=0
        # number of characters in a row
        self._cols= 80
        self._columns= int(raw_input('Give number colums or enter for default: ') or 1)
        # page ht
        self._pageHt=792
        # page wd
        self._pageWd=612
        # input file 
        self._ifile= input_file
        # output file 
        self._ofile= output_file
        # default tab width
        self._tab=4
        # input file descriptor
        self._ifs=None
        # output file descriptor
        self._ofs=None
        # landscape flag
        self._landscape=0

        # marker objects
        self._curobj = 5
        self._pageObs = [0]
        self._locations = [0,0,0,0,0,0]
        self._pageNo=0

        # file position marker
        self._fpos=0


    def writestr(self, str):
        """ Write string to output file descriptor.
        All output operations go through this function.
        We keep the current file position also here"""

        # update current file position
        self._fpos += len(str)
        for x in range(0, len(str)):
            if str[x] == '\n':
                self._fpos += LF_EXTRA
        try:
            self._ofs.write(str)
        except IOError, e:
            print e
            return -1

        return 0

    def Convert(self):
        """ Perform the actual conversion """

        if self._landscape:
            # swap page width & height
            tmp = self._pageHt
            self._pageHt = self._pageWd
            self._pageWd = tmp

        if self._lines==0:
            self._lines = (self._pageHt - 72)/self._vertSpace
        if self._lines < 1:
            self._lines=1

        try:
            self._ifs=open(self._ifile)
        except IOError, (strerror, errno):
            print 'Error: Could not open file to read --->', self._ifile
            sys.exit(3)

        if self._ofile=="":
            self._ofile=self._ifile + '.pdf'

        try:
            self._ofs = open(self._ofile, 'wb')
        except IOError, (strerror, errno):
            print 'Error: Could not open file to write --->', self._ofile
            sys.exit(3)

        print 'Input file =>', self._ifile
        print 'Writing pdf file', self._ofile, '...'
        self.WriteHeader(self._ifile)
        self.WritePages()
        self.WriteRest()

        print 'Wrote file', self._ofile
        self._ifs.close()
        self._ofs.close()
        return 0

    def WriteHeader(self, title):
        """Write the PDF header"""

        ws = self.writestr

        t=time.localtime()
        timestr=str(time.strftime("D:%Y%m%d%H%M%S", t))
        ws("%PDF-1.4\n")
        self._locations[1] = self._fpos
        ws("1 0 obj\n")
        ws("<<\n")

        buf = "".join(("/Creator (", self._appname, " By Anand B Pillai )\n"))
        ws(buf)
        buf = "".join(("/CreationDate (", timestr, ")\n"))
        ws(buf)
        buf = "".join(("/Producer (", self._appname, "(\\251 Free Software Foundation, 2004))\n"))
        ws(buf)

        ws("<<\n")
        ws("/Type /Catalog\n")
        ws("/Pages 3 0 R\n")
        ws(">>\n")
        ws("endobj\n")

        self._locations[4] = self._fpos
        ws("4 0 obj\n")
        ws("<<\n")
        buf = "".join(("/BaseFont ", str(self._font), " /Encoding /WinAnsiEncoding /Name /F1 /Subtype /Type1 /Type /Font >>\n"))
        ws(buf)

        if self._IsoEnc:
            ws(ENCODING_STR)

        ws(">>\n")
        ws("endobj\n")

        self._locations[5] = self._fpos

        ws("5 0 obj\n")
        ws("<<\n")
        ws("  /Font << /F1 4 0 R >>\n")
        ws("  /ProcSet [ /PDF /Text ]\n")
        ws(">>\n")
        ws("endobj\n")

    def StartPage(self):
        """ Start a page of data """

        ws = self.writestr

        self._pageNo += 1
        self._curobj += 1

        self._locations.append(self._fpos)
        self._locations[self._curobj]=self._fpos

        self._pageObs.append(self._curobj)
        self._pageObs[self._pageNo] = self._curobj

        buf = "".join((str(self._curobj), " 0 obj\n"))

        ws(buf)
        ws("<<\n")
        ws("/Type /Page\n")
        ws("/Parent 3 0 R\n")
        ws("/Resources 5 0 R\n")

        self._curobj += 1
        buf = "".join(("/Contents ", str(self._curobj), " 0 R\n"))
        ws(buf)
        ws(">>\n")
        ws("endobj\n")

        self._locations.append(self._fpos)
        self._locations[self._curobj] = self._fpos

        buf = "".join((str(self._curobj), " 0 obj\n"))
        ws(buf)
        ws("<<\n")

        buf = "".join(("/Length ", str(self._curobj + 1), " 0 R\n"))
        ws(buf)
        ws(">>\n")
        ws("stream\n")
        strmPos = self._fpos

        ws("BT\n");
        buf = "".join(("/F1 ", str(self._ptSize), " Tf\n"))
        ws(buf)
        buf = "".join(("1 0 0 1 50 ", str(self._pageHt - 40), " Tm\n"))
        ws(buf)
        buf = "".join((str(self._vertSpace), " TL\n"))
        ws(buf)

        return strmPos

    def EndPage(self, streamStart):
        """End a page of data """

        ws = self.writestr

        ws("ET\n")
        streamEnd = self._fpos
        ws("endstream\n")
        ws("endobj\n")

        self._curobj += 1
        self._locations.append(self._fpos)
        self._locations[self._curobj] = self._fpos

        buf = "".join((str(self._curobj), " 0 obj\n"))
        ws(buf)
        buf = "".join((str(streamEnd - streamStart), '\n'))
        ws(buf)
        ws('endobj\n')

    def WritePages(self):
        """Write pages as PDF"""

        ws = self.writestr

        beginstream=0
        lineNo, charNo=0,0
        ch, column=0,0
        padding,i=0,0
        atEOF=0

        while not atEOF:
            beginstream = self.StartPage()
            column=1

            while column <= self._columns:
                column += 1
                atFF=0
                atBOP=0
                lineNo=0

                while lineNo < self._lines and not atFF and not atEOF:

                    lineNo += 1
                    ws("(")
                    charNo=0

                    while charNo < self._cols:
                        charNo += 1
                        ch = self._ifs.read(1)
                        cond = ((ch != '\n') and not(ch==FF and self._doFFs) and (ch != ''))
                        if not cond:
                            break

                        if ord(ch) >= 32 and ord(ch) <= 127:
                            if ch == '(' or ch == ')' or ch == '\\':
                                ws("\\")
                            ws(ch)
                        else:
                            if ord(ch) == 9:
                                padding =self._tab - ((charNo - 1) % self._tab)
                                for i in range(padding):
                                    ws(" ")
                                charNo += (padding -1)
                            else:
                                if ch != FF:
                                    # write \xxx form for dodgy character
                                    buf = "".join(('\\', ch))
                                    ws(buf)
                                else:
                                    # dont print anything for a FF
                                    charNo -= 1

                    ws(")'\n")
                    if ch == FF:
                        atFF=1
                    if lineNo == self._lines:
                        atBOP=1

                    if atBOP:
                        pos=0
                        ch = self._ifs.read(1)
                        pos= self._ifs.tell()
                        if ch == FF:
                            ch = self._ifs.read(1)
                            pos=self._ifs.tell()
                        # python's EOF signature
                        if ch == '':
                            atEOF=1
                        else:
                            # push position back by one char
                            self._ifs.seek(pos-1)

                    elif atFF:
                        ch = self._ifs.read(1)
                        pos=self._ifs.tell()
                        if ch == '':
                            atEOF=1
                        else:
                            self._ifs.seek(pos-1)

                if column < self._columns:
                    buf = "".join(("1 0 0 1 ",
                                   str((self._pageWd/2 + 25)),
                                   " ",
                                   str(self._pageHt - 40),
                                   " Tm\n"))
                    ws(buf)

            self.EndPage(beginstream)

    def WriteRest(self):
        """Finish the file"""

        ws = self.writestr
        self._locations[3] = self._fpos

        ws("3 0 obj\n")
        ws("<<\n")
        ws("/Type /Pages\n")
        buf = "".join(("/Count ", str(self._pageNo), "\n"))
        ws(buf)
        buf = "".join(("/MediaBox [ 0 0 ", str(self._pageWd), " ", str(self._pageHt), " ]\n"))
        ws(buf)
        ws("/Kids [ ")

        for i in range(1, self._pageNo+1):
            buf = "".join((str(self._pageObs[i]), " 0 R "))
            ws(buf)

        ws("]\n")
        ws(">>\n")
        ws("endobj\n")

        xref = self._fpos
        ws("xref\n")
        buf = "".join(("0 ", str((self._curobj) + 1), "\n"))
        ws(buf)
        buf = "".join(("0000000000 65535 f ", str(LINE_END)))
        ws(buf)

        for i in range(1, self._curobj + 1):
            val = self._locations[i]
            buf = "".join((str(val).zfill(10), " 00000 n ", str(LINE_END)))
            ws(buf)

        ws("trailer\n")
        ws("<<\n")
        buf = "".join(("/Size ", str(self._curobj + 1), "\n"))
        ws(buf)
        ws("/Root 2 0 R\n")
        ws("/Info 1 0 R\n")
        ws(">>\n")

        ws("startxref\n")
        buf = "".join((str(xref), "\n"))
        ws(buf)
        ws("%%EOF\n")

def main():
    pdfclass=pyText2Pdf(input_file=raw_input('Input file: '), output_file=raw_input('Output file: '))
    pdfclass.Convert()

if __name__ == "__main__":
    main()

""" Does not function, given it's own code gives pdf error and in http://www.pdf-tools.com/pdf/validate-pdfa-online.aspx
the result is:
txt2pdf.pdf does not conform to PDF/A.

Validating file "txt2pdf.pdf" for conformance level pdfa-1b
The offset in the xref table is not correct.
The separator before the object number must be an EOL.
The object's identity 1 doesn't match with the object's reference identity 2.
The comment, classifying the file as containing 8-bit binary data, is missing.
The file trailer dictionary must have an id key.
The "endobj" keyword is missing.
The key Metadata is required but missing.
The documents contains no pages.
The document does not conform to the requested standard.
The file format (header, trailer, objects, xref, streams) is corrupted.
The document doesn't conform to the PDF reference (missing required entries, wrong value types, etc.).
The document's meta data is either missing or inconsistent or corrupt.
Done.
"""

Edited 4 Years Ago by pyTony: pypdf module suggestion

before all, thank you so much for your help pyTony,
about pyPDF, unfortunately cant use it on my mobile, because Python-symbian or pys60 is based on python 2.2.2 and pyPDF module works only on python 2.4 and up.
and yes, the output is not what I expected.

any way, this doent

Edited 4 Years Ago by M.S.

I couldnt edit the last post, here is the corrected last line:D
any way I really thank you again and sorry for my clumsy English :)

yes, but pys60 2 is not popular and is rarely used among symbian users, thats the reason why I was trying to implement this code in pys60 1.45.
btw, I succeded in applying pyPDF in pys60 2.

This question has already been answered. Start a new discussion instead.