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 StdinToPDF.py. Here it is:
# StdinToPDF.py # 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 - http://www.dancingbison.com # This program is part of the xtopdf toolkit: # https://bitbucket.org/vasudevram/xtopdf import sys from PDFWriter import PDFWriter try: with PDFWriter(sys.argv[1]) as pw: pw.setFont("Courier", 12) for lin in sys.stdin: pw.writeLine(lin) except Exception, e: print "ERROR: Caught exception: " + repr(e) sys.exit(1)And, dogfooding it (again :-)), here is the output of StdinToPDF.py when given itself as input, using the command:
python StdinToPDF.py StdinToPDF.pdf < StdinToPDF.py
Output:
What this means is that StdinToPDF.py 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:
Post a Comment