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

This recipe shows how to create PDF output at the end of a Unix or Linux pipeline, after all the text processing required, is done by previous components of the pipeline (which can use any of the standard tools of Unix such as sed, grep, awk, etc., as well as custom programs that act as filters).

Python, 19 lines
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
# 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)

The program StdinToPDF.py can be used at the end of a normal Unix or Linux pipeline. All the previous components of the pipeline can process or filter the original input text in different ways. The last component, StdinToPDF.py, can then convert the final text to PDF. It requires the xtopdf toolkit and the Reportlab toolkit.

Blog post with more details:

[xtopdf] PDFWriter can create PDF from standard input:

http://jugad2.blogspot.in/2013/12/xtopdf-pdfwriter-can-create-pdf-from.html

xtopdf: http://slid.es/vasudevram/xtopdf

Reportlab: http://www.reportlab.com - get v1.21.

Python v2.5 or higher required because of the use of the with statement.