From b342519e66db42cf38685c3f3738db4889f9d21f Mon Sep 17 00:00:00 2001 From: PJ Beers Date: Sat, 21 Jan 2023 23:02:40 +0100 Subject: [PATCH] Latex feature pages: Continue page numbering The feature pages start with page number one. This patch keeps count of the number of pdf pages written _before_ the feature pages and then starts with the number after that. For most characters, this is 4, but 3 characters without spells. --- dungeonsheets/forms/preamble.tex | 1 + dungeonsheets/latex.py | 2 +- dungeonsheets/make_sheets.py | 35 +++++++++++++++++++++----------- 3 files changed, 25 insertions(+), 13 deletions(-) diff --git a/dungeonsheets/forms/preamble.tex b/dungeonsheets/forms/preamble.tex index 4254095d..07c850c2 100644 --- a/dungeonsheets/forms/preamble.tex +++ b/dungeonsheets/forms/preamble.tex @@ -148,3 +148,4 @@ \begin{document} \chapter*{[[ title ]]} +[% if first_page is defined %]\setcounter{page}{[[ first_page ]]}[% endif %] diff --git a/dungeonsheets/latex.py b/dungeonsheets/latex.py index d2cd30a8..7e5ccb64 100644 --- a/dungeonsheets/latex.py +++ b/dungeonsheets/latex.py @@ -50,7 +50,7 @@ def create_latex_pdf( basename: str, keep_temp_files: bool = False, use_dnd_decorations: bool = False, - comm1: str = "pdflatex" + comm1: str = "pdflatex", ): # Create tex document tex_file = f"{basename}.tex" diff --git a/dungeonsheets/make_sheets.py b/dungeonsheets/make_sheets.py index 7efd56e9..06c03dfb 100644 --- a/dungeonsheets/make_sheets.py +++ b/dungeonsheets/make_sheets.py @@ -12,6 +12,7 @@ from itertools import product from multiprocessing import Pool, cpu_count from pathlib import Path +from pypdf import PdfReader from typing import Union, Sequence, Optional, List from dungeonsheets import ( @@ -398,6 +399,7 @@ def make_character_content( fancy_decorations: bool = False, spell_order: bool = False, feat_order: bool = False, + first_page: int = 1, ) -> List[str]: """Prepare the inner content for a character sheet. @@ -432,6 +434,7 @@ def make_character_content( content = [ jinja_env.get_template(f"preamble.{content_format}").render( use_dnd_decorations=fancy_decorations, + first_page=first_page, title="Features, Magical Items and Spells", ) ] @@ -594,17 +597,8 @@ def make_character_sheet( person_base = basename + "_person" sheets = [char_base + ".pdf"] pages = [] - # Prepare the tex/html content - content_suffix = format_suffixes[output_format] - # Create a list of features and magic items - content = make_character_content( - character=character, - content_format=content_suffix, - fancy_decorations=fancy_decorations, - spell_order=spell_order, - feat_order=feat_order, - ) - # Typeset combined LaTeX file + totalpages = 0 + # Typeset LaTeX character, background and spell-sheet files if output_format == "pdf": if use_tex_template: msavage_sheet( @@ -633,7 +627,24 @@ def make_character_sheet( ) for spell_base in created_basenames: sheets.append(spell_base + ".pdf") - # Combined with additional LaTeX pages with detailed character info + # Check how many pages we've written + for file in sheets: + with open(file, 'rb') as handle: + readpdf = PdfReader(handle) + totalpages = totalpages + len(readpdf.pages) + # Prepare the tex/html content + content_suffix = format_suffixes[output_format] + # Create a list of features and magic items + content = make_character_content( + character=character, + content_format=content_suffix, + fancy_decorations=fancy_decorations, + spell_order=spell_order, + feat_order=feat_order, + first_page=totalpages + 1, + ) + if output_format == "pdf": + # Create and combine additional LaTeX pages with detailed character info features_base = "{:s}_features".format(basename) try: if len(content) > 2: