Welcome, guest | Sign In | My Account | Store | Cart

Python port of a very popular implementation of text2pdf in the C programming language. I ported this to python because it could be used as a script instead of executable which is the case with the original program. Most of the options remain the same.

Python, 645 lines
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
"""
 pyText2Pdf - Python script to convert plain text files into Adobe
 Acrobat PDF files.

 Version 1.2

 Author: Anand B Pillai <abpillai at lycos dot com>
 Keywords: python, tools, converter, pdf, text2pdf, adobe, acrobat,
           processing.

 Copyright (C) 2003-2004 Free Software Foundation, Inc.

 This file is free software; you can redistribute it and/or modify
 it under the terms of the GNU General Public License as published by
 the Free Software Foundation; either version 2, or (at your option)
 any later version.

 This file is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY; without even the implied warranty of
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 GNU General Public License for more details.
    
 You should have received a copy of the GNU General Public License
 along with GNU Emacs; see the file COPYING.  If not, write to
 the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 Boston, MA 02111-1307, USA.
    
 Commentary:

 Modification History:

 Mon Feb 17 12:20:13 2003 Changed option parsing algorithm to use
                          getopt. Use __main__ calling convention.
                          Bug in FF character fixed.
 Thu Apr 10 11:26:58 2003 Modified to use python style strings
                          and function objects.
 July 1 2003              Fixed help string errors. Added the
                          Creator property.
 Feb 25 2004              Rewrote argument parser to remove
                          duplicate code.Use string.join() instead
                          of concatenation. Modified sys.exit()
                          calls to print messages.
    Code:
"""

# http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/189858

import sys, os
import string
import time
import getopt

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 ]
>>
"""


PROG_HELP = """\

%(progname)s  [options] [filename]

%(progname)s  makes a 7-bit clean PDF file from any input file.

It reads from a named file, and writes the PDF file to a file specified by 
the user, otherwise to a file with '.pdf' appended to the input file.

Author: Anand B Pillai.

Copyright (C) 2003-2004 Free Software Foundation, http://www.fsf.org

There are various options as follows:

  -h\t\tshow this message\n
  -o/-O\t\tdirect output to this file
  -f<font>\tuse PostScript <font> (must be in standard 14, default: Courier)
  -I\t\tuse ISOLatin1Encoding
  -s<size>\tuse font at given pointsize (default 10) points\n
  -v<dist>\tuse given line spacing (default 12) points
  -l<lines>\tlines per page (default 60, determined automatically\n\t\tif unspecified)
  -c<chars>\tmaximum characters per line (default 80)
  -t<spaces>\tspaces per tab character (default 4)
  -F\t\tignore formfeed characters (^L)
    \t\t(i.e, accept formfeed characters as pagebreaks)\n
  -A4\t\tuse A4 paper (default Letter)
  -A3\t\tuse A3 paper (default Letter)
  -x<width>\tindependent paper width in points
  -y<height>\tindependent paper height in points
  -2\t\tformat in 2 columns
  -L\t\tlandscape mode

Note that where one variable is implied by two options, the second option
takes precedence for that variable. (e.g. -A4 -y500)

In landscape mode, page width and height are simply swapped over before
formatting, no matter how or when they were defined.
"""


class pyText2Pdf:

    def __init__(self):
        # 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=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)
        
        if title:
            buf = "".join(("/Title (", title, ")\n"))
            ws(buf)

        ws(">>\n")
        ws("endobj\n")
    
        self._locations[2] = self._fpos

        ws("2 0 obj\n")
        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()

PDF is a very useful portable document format. It is platform independent, and a very affordable way to store your documents. Unfortunately python does not have any tools to create/work with PDF documents. This is one of the many attempts at filling the gap.

This solution is the python port of widely used text2pdf utility written in C programming language. It has lots of configurable options. For an alternative implementation try http://www.reportlab.com.

15 comments

Noah Spurrier 21 years ago  # | flag

Nice. This could grow. You should put it on sourceforge.

Anand (author) 21 years ago  # | flag

Project on sourceforge. Hi

Thanks Noah for encouragement.

I have created the project 'pdfutils'

on sourceforge.net. I havent got the

confirmation from sourceforge.net. I will

publish the details here soon.

Regards

Anand Pillai

Anand Pillai

Anand (author) 21 years ago  # | flag

Project on sourceforge. Hi

Thanks Noah for encouragement.

I have created the project 'pdfutils'

on sourceforge.net. I will publish the

details of it here and on my webpage.

Regards

Anand Pillai

Matthew Shomphe 20 years, 10 months ago  # | flag

Bug in code? I got this to successfully work on my machine, but when opening the PDF, Acrobat showed a message: "The file is damaged but is being repaired"

I copied the above code into a file.

I made a text file that said:

"This is a pdf test"

I then excuted the script from a DOS window:

C:\Python>pyText2Pdf.py tmp.txt
[] ['tmp.txt']
Using font Courier  size = 10
Input file =  tmp.txt
Writing pdf file  tmp.txt.pdf ...
Wrote file  tmp.txt.pdf

I'm using Adobe Acrobat v4.05a 1124 on a Windows 2K machine. I'm also using Python 2.3b1

Also, in your help text, you say:

_Fore_ more python scripts ...

I think you mean:

_For_ more python scripts ...
Martin Miller 20 years, 9 months ago  # | flag

Re: Bug in code? Seems to work fine for me on Win XP Pro (SP1) with Acrobat v5.0.5.

Nice work, Anand!

P.S. The program's help says the default tab size is 8, but it's actually initialized to 4 in the code...which I prefer, but the usage info is inaccurate.

Anand (author) 20 years, 9 months ago  # | flag

Fixed documentation errors. Hi Ppl,

Thanks for the feedback. I have fixed the documentation (help-string) errors and uploaded a new version.

Regards,

ABP

Anand (author) 20 years, 9 months ago  # | flag

Very important!. If you find this useful, mail me your suggestions/ideas to improve this. :-)

Anand

Farhad Fouladi 20 years, 1 month ago  # | flag

compatiblity with Python 2.1. If you replace False with 0 and True with 1, it would be downward compatible with Python version 2.1.

Jan Persson 20 years, 1 month ago  # | flag

Backward compability. Now that we have boolean literals in Python, I really cannot see why we should not use them for new code.

If you need to run this code with an older version of Python, then do a find/replace with your favorite text editor.

What's the use of improving Python when the new language features can not be used, because they are imcompatible with the older versions of the language?

Damjan Georgievski 19 years, 7 months ago  # | flag

Unicode support? Is it possible to extend this txt2pdf creator to also work for unicode strings?

I'd like to use it with some cyrillic texts I have. The text is un UTF-8, and its easy to convert to python unicode strings.

Manisha Kode 18 years, 1 month ago  # | flag

Can i append the text file to existing PDF file. hello,

I have a PDF file created from post script file using ps2pdf and i have a text file which has data regarding job success.

I would like to append the text file to the already created pdf file. I can convert text to pdf using this utility.

Now my challenge is to combine those 2 PDFs in one using python or any other utilities which can run on linux. There is a reportlab solutions which is not free.

Can you suggest or can we trick this program to append to existing PDF file?

Thanks in advance, Manisha K Kode.

s g 17 years, 1 month ago  # | flag

joining PDF files. pyPDF (pure Python) can merge PDF files: http://pybrary.net/pyPdf/

A few other (non-Python) utilities for joining PDF files: gs (ghostscript), pdftk and pdfjam/pdfjoin.

Selasie Agbavor 13 years ago  # | flag

awesome!! excellent one there...I wanted to use your code as a module so I did a little bit of editing; commented out the part responsible for passing command line arguments and added a multiple parameter list to the constructor. Such that you pass the names of text files as arguments to pyText2Pdf().

Anand B Pillai 13 years ago  # | flag

Thanks, in fact I have written a rewrite of this recipe which is available as http://code.activestate.com/recipes/532908/ but for some reason this one is still popular though the code in the other one is more recent, better refactored and it supports more cmd line options.

I am the same Anand who wrote both recipes, but for some reason ASPN has screwed up my logins so I think I might be appearing as a different user here.

Anoop 7 years, 2 months ago  # | flag

Hi Anand,

I am new to python and don't know much about it. I downloaded it run it on my local machine and it works perfect. I tried to run this py script in Jenkins. But some how it is not recognizing the arguments I tried all the different way but if gives some kind of error. Main issue is that it gives a Syntax err for the print statement. I am pasting the output statements below for your reference:

  • python pyText2Pdf.py -O partnerships-acct-memo-update-ASR-Report-2017-01-27-14-40-02-581.pdf partnerships-acct-memo-update-ASR-Report-2017-01-27-14-40-02-581.txt File "pyText2Pdf.py", line 272 print self._progname, ': ignoring invalid switch: ', o ^ SyntaxError: invalid syntax

Any help from your side is appreciated.