Monday, December 23, 2013

[xtopdf] PDFWriter can create PDF from standard input

By Vasudev Ram

I added a small program to my PDF creation toolkit, xtopdf, that shows how to use it to generate PDF from the standard input of a command line. I called the program Here it is:


# Read the contents of stdin (standard input) and write it to a PDF file 
# whose name is specified as a command line argument.
# Author: Vasudev Ram -
# This program is part of the xtopdf toolkit:

import sys
from PDFWriter import PDFWriter

    with PDFWriter(sys.argv[1]) as pw:
        pw.setFont("Courier", 12)
        for lin in sys.stdin:
except Exception, e:
    print "ERROR: Caught exception: " + repr(e)

And, dogfooding it (again :-)), here is the output of when given itself as input, using the command:

python StdinToPDF.pdf <


What this means is that can be used at the end of a Unix command pipeline, after all the other processing (of text) by various other tools is done, to convert the final text to PDF. (It cannot be used at the beginning or in the middle of a pipeline, obviously (as it stands now), because it does not write to standard output.)

Here's a handy online presentation about xtopdf, for those who haven't seen it before.

- Enjoy.

- Vasudev Ram - Python programming and training

Contact Page

No comments: