Example #1
0
def init_pdf():
    pdf = FPDF()
    pdf.set_creation_date(EPOCH)
    pdf.set_font("helvetica", size=24)
    pdf.add_page()
    pdf.cell(w=pdf.epw, h=10, txt="Hello fpdf2!", align="C")
    return pdf
Example #2
0
def gen_pdf(font_path: str, pdf_path: str, pe_ost_list: list[pe_T],
            pt_ost_list: list[pt_T]) -> None:
    """Generate PDF file with per-episode and per-track listings from per-episode OST list."""

    font_family = "ext_font"
    page_margin = 10
    font_size = 10
    cell_h = 5
    cell_padding = 10

    # compute cells and page size

    pdf = FPDF()
    pdf.add_font(font_family, fname=font_path, uni=True)
    pdf.set_font(font_family, size=font_size)

    page_h = 0
    max_col_w = {
        key: pdf.get_string_width(col_name_dict[key])
        for key in col_name_dict
    }  # init with column name width

    for ep_dict in pe_ost_list:
        page_h += cell_h * len(ep_dict[pe_list_key])
        first_col_key = pe_col_list[0]
        max_col_w[first_col_key] = max(
            max_col_w[first_col_key],
            pdf.get_string_width(ep_dict[first_col_key]))

        for track_dict in ep_dict[pe_list_key]:
            for key in track_dict:
                max_col_w[key] = max(max_col_w[key],
                                     pdf.get_string_width(track_dict[key]))

    col_w = {key: max_col_w[key] + cell_padding for key in max_col_w}

    page_w = sum(col_w.values()) + 2 * page_margin
    page_h += 3 * page_margin + font_size

    # init PDF

    pdf = FPDF(format=(page_w, page_h))
    pdf.set_author("DSAureli")
    pdf.set_creation_date(datetime.date.today())
    pdf.set_title(proj_title)
    pdf.set_margins(page_margin, page_margin)
    pdf.add_font(font_family, fname=font_path, uni=True)
    pdf.set_font(font_family, size=font_size)
    pdf.set_auto_page_break(False)

    # define table generating function

    def gen_table(col_list: list[str], ost_list: list[TypeVar("T", pe_T,
                                                              pt_T)],
                  list_key: str) -> None:
        """
		Generate table with columns as in 'col_list' and data as in 'ost_list'.
		'list_key' is the key for the list inside each dictionary in 'ost_list'.
		"""

        # column titles
        for key in col_list:
            pdf.cell(w=col_w[key],
                     h=2 * cell_h,
                     border=1,
                     align="C",
                     txt=col_name_dict[key])
        pdf.ln()

        for head_dict in ost_list:
            # header cell, spanning multiple rows
            pdf.cell(w=col_w[col_list[0]],
                     h=len(head_dict[list_key]) * cell_h,
                     border=1,
                     align="C",
                     txt=head_dict[col_list[0]])
            pdf.ln(h=0)  # line feed, but don't increase y coord

            for row_dict in head_dict[list_key]:
                pdf.set_x(pdf.get_x() +
                          col_w[col_list[0]])  # place cursor after header cell

                for key in row_dict:
                    pdf.cell(w=col_w[key],
                             h=cell_h,
                             border=1,
                             align="C",
                             txt=row_dict[key])

                pdf.ln()

    # generate per-episode page

    pdf.add_page()

    pdf.cell(w=0, align="C", txt=f"{proj_title}  ~  per-episode")
    pdf.ln(h=page_margin)

    gen_table(pe_col_list, pe_ost_list, pe_list_key)

    # generate per-track page

    pdf.add_page()

    pdf.cell(w=0, align="C", txt=f"{proj_title}  ~  per-track")
    pdf.ln(h=page_margin)

    gen_table(pt_col_list, pt_ost_list, pt_list_key)

    # write PDF to file

    pdf.output(pdf_path)