Python Forum
Command output to pdf (linux)
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Command output to pdf (linux)
#1
I call the following script 'a2pdf'. It sends a text on its standard input to a pdf format on its standard output, using the venerable a2ps utility and the commands iconv and ps2pdf.
#!/usr/bin/env python3
import argparse
import subprocess as sp
import sys

__version__ = '0.0.1'

def main():
    parser = argparse.ArgumentParser(description='Write text input to pdf')
    parser.add_argument('-o', '--output-file', metavar='OUTPUT-FILE', type=str,
                    help='output.pdf', default='-', dest='ofile')

    args = parser.parse_args()


    com = "iconv -c -f utf-8 -t ISO-8859-1 | a2ps --columns=1 -f12 -R -B --borders=no -Xlatin1 -o - | ps2pdf - {ofile}".format(ofile=args.ofile)
    
    proc = sp.Popen(com, shell=True, stdin=sys.stdin, stdout=sys.stdout)
    proc.wait()

if __name__ == '__main__':
    main()
To use it, just pipe the output of some command, for example
Output:
ls -l | a2pdf -o out.pdf
Or pipe directly the ouptut to a pdf viewer such as okular
Output:
ls -l | a2pdf | okular -
It is currently a rudimentary tool. You can develop your own ways of setting the subcommand's numerous options.
snippsat likes this post
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  how to run a command pipeline and get its result output as a piped file Skaperen 0 1,810 Aug-04-2022, 11:59 PM
Last Post: Skaperen
  Getting wanted data from the 'top' command (Linux) rootVIII 7 4,000 Nov-19-2018, 09:41 PM
Last Post: DeaD_EyE

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020