papernews.py 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. from fpdf import FPDF
  2. from generate_qr import generate_qr_code
  3. from section import Section
  4. from get_antiwar import get_antiwar_article
  5. import re
  6. class PDF(FPDF):
  7. def header(self):
  8. self.set_font("helvetica", style="B", size=15)
  9. width = self.get_string_width(self.title) + 6
  10. self.set_x((210 - width) / 2)
  11. # self.set_draw_color(0, 80, 180)
  12. # self.set_fill_color(230, 230, 0)
  13. # self.set_text_color(220, 50, 50)
  14. self.set_line_width(.1)
  15. self.cell(
  16. width,
  17. 9,
  18. self.title,
  19. border=1,
  20. new_x="LMARGIN",
  21. new_y="NEXT",
  22. align="C",
  23. fill=False,
  24. )
  25. self.ln(10)
  26. def footer(self):
  27. self.set_y(-15)
  28. self.set_font("helvetica", style="I", size=8)
  29. self.set_text_color(128)
  30. self.cell(0, 10, f"Page {self.page_no()}", align="C")
  31. pdf = PDF()
  32. pdf.set_title("Paper News Test")
  33. pdf.add_page()
  34. # pdf.set_text_shaping(True)
  35. awa = get_antiwar_article()
  36. col_width = 60
  37. pdf.set_font('helvetica', style="b", size=12)
  38. top = pdf.get_y()
  39. left = pdf.get_x()
  40. pdf.multi_cell(
  41. w=col_width,
  42. h=5,
  43. text=awa.title,
  44. border=False,
  45. new_x="LMARGIN",
  46. new_y="NEXT")
  47. pdf.set_font("Helvetica", size=8)
  48. pdf.multi_cell(
  49. w=col_width,
  50. h=8,
  51. text=awa.author,
  52. border=False,
  53. new_x="LMARGIN",
  54. new_y="NEXT")
  55. pdf.multi_cell(
  56. w=col_width,
  57. h=8,
  58. text=awa.published,
  59. border=False,
  60. new_x="LMARGIN",
  61. new_y="NEXT")
  62. transchars = str.maketrans({"’": "\'", "“": "\"", "”": "\"", "–": "-"})
  63. awatext = awa.text.translate(transchars)[:1000]
  64. awatext = re.sub(r'[^a-zA-Z]+$', '', awatext)
  65. # while awatext and not awatext[-1].isalpha():
  66. # awatext = awatext[:-1]
  67. pdf.multi_cell(
  68. w=col_width,
  69. h=None,
  70. text=awatext + '...',
  71. border=False,
  72. new_x="LMARGIN",
  73. new_y="NEXT")
  74. pdf.set_y(pdf.get_y() + 10)
  75. pdf.image(generate_qr_code(awa.link), x=pdf.get_x() + col_width/4, y=None, h=col_width/2)
  76. pdf.set_y(pdf.get_y() + 3)
  77. pdf.set_font('helvetica', size=8)
  78. pdf.multi_cell(
  79. w=col_width,
  80. h=None,
  81. text='Read more at antiwar.com',
  82. align="C",
  83. border=False,
  84. new_x="LMARGIN",
  85. new_y="TOP")
  86. col_space=5
  87. pdf.set_xy(left + col_width + col_space,top)
  88. pdf.multi_cell(
  89. w=col_width,
  90. h=None,
  91. text=awatext + '...',
  92. border=False,
  93. new_x="LMARGIN",
  94. new_y="NEXT")
  95. pdf.set_xy(left + col_width*2 + col_space*2,top)
  96. pdf.multi_cell(
  97. w=col_width,
  98. h=None,
  99. text=awatext + '...',
  100. border=False,
  101. new_x="LMARGIN",
  102. new_y="NEXT")
  103. # box_section = Section(50,30,55,col_width)
  104. # pdf.rect(pdf.get_x(), pdf.get_y() + 4, box_section.h, box_section.w, style="D") ##,round_corners=False, corner_radius=0)
  105. pdf.output("news.pdf")
  106. # TABLE_DATA = (
  107. # ("First name", "Last name", "Age", "City"),
  108. # ("Jules", "Smith", "34", "San Juan"),
  109. # ("Mary", "Ramos", "45", "Orlando"),
  110. # ("Carlson", "Banks", "19", "Los Angeles"),
  111. # ("Lucas", "Cimon", "31", "Angers"),
  112. # )
  113. # pdf.set_font("Times", size=16)
  114. # with pdf.table() as table:
  115. # for data_row in TABLE_DATA:
  116. # row = table.row()
  117. # for datum in data_row:
  118. # row.cell(datum)
  119. # pdf.output('table.pdf')