| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152 |
- from fpdf import FPDF
- from generate_qr import generate_qr_code
- from section import Section
- from get_antiwar import get_antiwar_article
- import re
- class PDF(FPDF):
- def header(self):
- self.set_font("helvetica", style="B", size=15)
- width = self.get_string_width(self.title) + 6
- self.set_x((210 - width) / 2)
- # self.set_draw_color(0, 80, 180)
- # self.set_fill_color(230, 230, 0)
- # self.set_text_color(220, 50, 50)
- self.set_line_width(.1)
- self.cell(
- width,
- 9,
- self.title,
- border=1,
- new_x="LMARGIN",
- new_y="NEXT",
- align="C",
- fill=False,
- )
- self.ln(10)
- def footer(self):
- self.set_y(-15)
- self.set_font("helvetica", style="I", size=8)
- self.set_text_color(128)
- self.cell(0, 10, f"Page {self.page_no()}", align="C")
- pdf = PDF()
- pdf.set_title("Paper News Test")
- pdf.add_page()
- # pdf.set_text_shaping(True)
- awa = get_antiwar_article()
- col_width = 60
- pdf.set_font('helvetica', style="b", size=12)
- top = pdf.get_y()
- left = pdf.get_x()
- pdf.multi_cell(
- w=col_width,
- h=5,
- text=awa.title,
- border=False,
- new_x="LMARGIN",
- new_y="NEXT")
- pdf.set_font("Helvetica", size=8)
- pdf.multi_cell(
- w=col_width,
- h=8,
- text=awa.author,
- border=False,
- new_x="LMARGIN",
- new_y="NEXT")
- pdf.multi_cell(
- w=col_width,
- h=8,
- text=awa.published,
- border=False,
- new_x="LMARGIN",
- new_y="NEXT")
- transchars = str.maketrans({"’": "\'", "“": "\"", "”": "\"", "–": "-"})
- awatext = awa.text.translate(transchars)[:1000]
- awatext = re.sub(r'[^a-zA-Z]+$', '', awatext)
- # while awatext and not awatext[-1].isalpha():
- # awatext = awatext[:-1]
- pdf.multi_cell(
- w=col_width,
- h=None,
- text=awatext + '...',
- border=False,
- new_x="LMARGIN",
- new_y="NEXT")
- pdf.set_y(pdf.get_y() + 10)
- pdf.image(generate_qr_code(awa.link), x=pdf.get_x() + col_width/4, y=None, h=col_width/2)
- pdf.set_y(pdf.get_y() + 3)
- pdf.set_font('helvetica', size=8)
- pdf.multi_cell(
- w=col_width,
- h=None,
- text='Read more at antiwar.com',
- align="C",
- border=False,
- new_x="LMARGIN",
- new_y="TOP")
- col_space=5
- pdf.set_xy(left + col_width + col_space,top)
- pdf.multi_cell(
- w=col_width,
- h=None,
- text=awatext + '...',
- border=False,
- new_x="LMARGIN",
- new_y="NEXT")
- pdf.set_xy(left + col_width*2 + col_space*2,top)
- pdf.multi_cell(
- w=col_width,
- h=None,
- text=awatext + '...',
- border=False,
- new_x="LMARGIN",
- new_y="NEXT")
- # box_section = Section(50,30,55,col_width)
- # pdf.rect(pdf.get_x(), pdf.get_y() + 4, box_section.h, box_section.w, style="D") ##,round_corners=False, corner_radius=0)
- pdf.output("news.pdf")
- # TABLE_DATA = (
- # ("First name", "Last name", "Age", "City"),
- # ("Jules", "Smith", "34", "San Juan"),
- # ("Mary", "Ramos", "45", "Orlando"),
- # ("Carlson", "Banks", "19", "Los Angeles"),
- # ("Lucas", "Cimon", "31", "Angers"),
- # )
- # pdf.set_font("Times", size=16)
- # with pdf.table() as table:
- # for data_row in TABLE_DATA:
- # row = table.row()
- # for datum in data_row:
- # row.cell(datum)
- # pdf.output('table.pdf')
|