Advertisement
gregwa

FCM 159 - demo3.py

Jul 9th, 2020
1,238
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.24 KB | None | 0 0
  1. #! /usr/bin/env python
  2. #  -*- coding: utf-8 -*-
  3. # ======================================================
  4. #     demo3.py
  5. #  ------------------------------------------------------
  6. # Created for Full Circle Magazine Issue #159 July 2020
  7. # Written by G.D. Walters
  8. # Copyright (c) 2020 by G.D. Walters
  9. # This source code is released under the MIT License
  10. # ======================================================
  11. from fpdf import FPDF
  12. import sys
  13. import os
  14. class PDF(FPDF):
  15.     def header(self):
  16.         # Arial bold 15
  17.         self.set_font('Arial', 'B', 15)
  18.         # Calculate width of title and position
  19.         w = self.get_string_width(title) + 6
  20.         self.set_x((210 - w) / 2)
  21.         # Title
  22.         self.cell(w, 9, title, 0, 1, 'C', 0)
  23.         # Line break
  24.         self.ln(10)
  25.  
  26.     def footer(self):
  27.         # Position at 1.5 cm from bottom
  28.         self.set_y(-15)
  29.         # Arial italic 8
  30.         self.set_font('Arial', 'I', 8)
  31.         # Text color in gray
  32.         self.set_text_color(128)
  33.         # Page number
  34.         self.cell(0, 10, 'Page ' + str(self.page_no()), 0, 0, 'C')
  35.  
  36.     def chapter_body(self, name, fontfamily=None, fontattrib=None, fontsize=None):
  37.         # Read text file
  38.         with open(name, 'rb') as fh:
  39.             txt = fh.read().decode('latin-1')
  40.         if fontfamily==None:
  41.             # Times 12
  42.             self.set_font('Times', '', 12)
  43.         else:
  44.             self.set_font(fontfamily, fontattrib,fontsize)                    
  45.         # Output justified text
  46.         self.multi_cell(0, 5, txt)
  47.         # Line break
  48.         self.ln()
  49.  
  50. title = 'Demo3 for Full Circle Magazine'
  51. pdf = PDF('p', 'mm', 'letter')
  52. pdf.alias_nb_pages()
  53. pdf.set_title(title)
  54. # Properties start here...
  55. pdf.set_author('G.D. Walters')
  56. pdf.set_subject('Demonstration program for Full Circle Magazine Issue #159')
  57. pdf.set_keywords("PDF, Demonstration, Full Circle Magazine")
  58. pdf.set_creator("Python")
  59. # Properties end here...
  60. pdf.add_page()
  61.  
  62. pdf.chapter_body('demotext1.txt')
  63. pdf.chapter_body('demotext2.txt','Arial','B',14)
  64. pdf.chapter_body('birthdays2.py','Courier','B',11)
  65.  
  66. pdf.output('demo3.pdf', 'F')
  67.  
  68.  
  69. if sys.platform.startswith("linux"):
  70.     os.system("xdg-open ./demo3.pdf")
  71. else:
  72.     os.system("./demo3.pdf")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement