pdffooter.py 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. #!/usr/bin/env python2
  2. # sudo apt-get install python-subprocess32 texlive-latex-base texlive-latex-recommended
  3. import argparse
  4. import os
  5. import subprocess
  6. class bcolors:
  7. HEADER = '\033[95m'
  8. OKBLUE = '\033[94m'
  9. OKGREEN = '\033[92m'
  10. WARNING = '\033[93m'
  11. FAIL = '\033[91m'
  12. ENDC = '\033[0m'
  13. BOLD = '\033[1m'
  14. UNDERLINE = '\033[4m'
  15. import textwrap as _textwrap
  16. class MultilineFormatter(argparse.HelpFormatter):
  17. def _fill_text(self, text, width, indent):
  18. text = self._whitespace_matcher.sub(' ', text).strip()
  19. paragraphs = text.split('|n ')
  20. multiline_text = ''
  21. for paragraph in paragraphs:
  22. formatted_paragraph = _textwrap.fill(paragraph, width, initial_indent=indent, subsequent_indent=indent) + '\n'
  23. multiline_text = multiline_text + formatted_paragraph
  24. return multiline_text
  25. def Main():
  26. parser = argparse.ArgumentParser(
  27. description="""Little tool to add a footer with pagenumbers, image and classification to a pdf file. Outputs file as output.pdf. Uses pdflatex to generate the pdf. |n |n To only add pagenumbers: |n
  28. ./pdffooter.py test.pdf |n
  29. To add pagenumbers and classification:|n
  30. ./pdffooter.py test.pdf -c Confidential
  31. To add pagenumbers, a classification and an image/logo:|n
  32. ./pdffooter.py test.pdf -c Confidential -l logo.pdf
  33. To add pagenumbers and an image/logo:|n
  34. ./pdffooter.py test.pdf -l logo.png
  35. """, formatter_class=MultilineFormatter)
  36. parser.add_argument("DOC", help="The filename of the PDF of which you want to add pagenumbers to. It'll number page 2 and up.")
  37. parser.add_argument("-c", "--classification", help="Sets the classification of the document in the footer, default is no classification.", default=" ")
  38. parser.add_argument("-l", "--logo", help="Sets a logo or image in the right footer of the document, default is no image or logo. (tested with logo.pdf and logo.png, does not work with logo.svg.)", default=" ")
  39. args = parser.parse_args()
  40. document = str(args.DOC)
  41. logo= str(args.logo)
  42. if logo == " ":
  43. logo=""
  44. else:
  45. logo="""\\rfoot{\includegraphics[width=2cm]{"""+str(args.logo)+"""}}% sets logo or image on the right"""
  46. print bcolors.HEADER + "The input pdf file is " + str(args.DOC)+ "." + bcolors.ENDC
  47. print bcolors.HEADER + "The classification of the file is set to "+ str(args.classification)+ "." + bcolors.ENDC
  48. print bcolors.HEADER + "The header includes the following logo or image: "+ str(args.logo)+ "." + bcolors.ENDC
  49. content = r"""\documentclass[10pt]{article}
  50. \usepackage[final]{pdfpages}
  51. \usepackage{fancyhdr}
  52. \renewcommand{\familydefault}{\sfdefault}
  53. \topmargin 70pt
  54. \oddsidemargin 70pt
  55. \pagestyle{fancy}
  56. \cfoot{\textsf\thepage}%sets pagenumber in the center
  57. \lfoot{"""+str(args.classification)+"""}% sets classification on the left
  58. """+(logo)+"""
  59. \\renewcommand {\\footrulewidth}{0pt}% makes sure there is no line above the footer
  60. \\renewcommand {\headrulewidth}{0pt}% makes sure there is no line below the header
  61. \\begin{document}
  62. \includepdfset{pagecommand=\\thispagestyle{fancy}}
  63. \includepdf[pages=1-]{"""+str(document)+"""}% adds footer to second page and beyond
  64. \end{document}
  65. """
  66. print bcolors.HEADER + "The generated en used template for pdflatex is: \n"+ bcolors.ENDC + bcolors.OKBLUE + content + bcolors.ENDC
  67. with open('output.tex','w') as f:
  68. f.write(content)
  69. cmd = ['pdflatex', '-interaction', 'nonstopmode', 'output.tex']
  70. proc = subprocess.Popen(cmd)
  71. proc.communicate()
  72. retcode = proc.returncode
  73. if not retcode == 0:
  74. os.unlink('output.pdf')
  75. raise ValueError('Error {} executing command: {}'.format(retcode, ' '.join(cmd)))
  76. os.unlink('output.tex')
  77. os.unlink('output.log')
  78. os.unlink('output.aux')
  79. if __name__ == '__main__':
  80. Main()