Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Number of pages appears to not function correctly #71

Closed
slightlynybbled opened this issue Jan 19, 2021 · 13 comments
Closed

Number of pages appears to not function correctly #71

slightlynybbled opened this issue Jan 19, 2021 · 13 comments
Assignees
Labels

Comments

@slightlynybbled
Copy link

slightlynybbled commented Jan 19, 2021

This worked in fpdf.

I have an object that I created called _Report that subclasses fpdf:

from pathlib import Path
from fpdf import FPDF


class _Report(FPDF):
    def __init__(self, title: str):
        super().__init__()

        self._title = title
        self._user_documents = Path('.').absolute()

        ypos = 50
        xpos = 10
        self.set_x(xpos)
        self.set_y(ypos)
        self.set_font('Arial', 'B', 12)

        self.add_page()
        self.alias_nb_pages()

        self.saved_as = None

    def create_report(self, save_path=None):
        if save_path is None:
            save_as = self._user_documents
        else:
            save_as = save_path

        save_as = save_as / 'report.pdf'

        self.output(save_as, 'F')
        self.saved_as = save_as

    def header(self):
        pos = 30
        self.set_y(pos)
        self.cell(0, 0, 'Organization Name')

        self.set_y(pos + 8)
        self.cell(0, 0, self._title)

    def footer(self):
        self.set_y(-15)
        # Page number
        self.cell(0, 10, f'Page {self.page_no()} of ' + '{nb}', 0, 0, 'C')


if __name__ == '__main__':
    rp = _Report(title='test title')
    rp.create_report()

I have tried several items for that last line in the footer to no avail. I have also tried to move the self.alias_nb_pages() to a couple of different locations, but it always prints "{nb}" on to the PDF.

@Lucas-C
Copy link
Member

Lucas-C commented Jan 19, 2021

Your snippet does include how to initialize & use your custom class.
After trying to use it, I realized that _user_documents refers to an undefined attribute, and that the call to set_font in __init_ must be done before add_page

Could you please provide code that is directly usable to reproduce your issue ?

@slightlynybbled
Copy link
Author

You are correct. I tried to strip things a bit to the bone and ended up making my example un-runnable. My bad. I have updated the snipped to represent a minimal example.

Thank you for your assistance,

j

@Lucas-C
Copy link
Member

Lucas-C commented Jan 19, 2021

I had to slightly modify your create_report method:

    def create_report(self, save_path=None):
        if save_path is None:
            save_as = self._user_documents
        with (save_as / 'report.pdf').open('wb') as report_file:
            self.output(report_file)

But it works:
2021-01-19 15_01_11-report pdf - SumatraPDF

@slightlynybbled
Copy link
Author

When you say "it works", that looks exactly like it doesn't work. I'm expecting to see "Page 1 of 1".

@Lucas-C
Copy link
Member

Lucas-C commented Jan 19, 2021

This is the text string used in the footer method: f'Page {self.page_no()} of ' + '{nb}'

This is where your issue comes from.

Try this instead:

self.cell(0, 10, f'Page {self.page_no()} of {len(self.pages)}', 0, 0, 'C')

@Lucas-C
Copy link
Member

Lucas-C commented Jan 19, 2021

The workaround I mentioned above should work for you,
but this is indeed a bug with the old alias_nb_pages feature.

I'm going to take a look at it soon in order to replicate the original functionality.

Thank you for reporting this!

@Lucas-C Lucas-C self-assigned this Jan 19, 2021
@slightlynybbled
Copy link
Author

Perfect workaround. Thank you!

I don't want to hit "close" b/c it appears that you are tracking this as a bug, but feel free to close when you feel that the issue is resolved.

@Lucas-C
Copy link
Member

Lucas-C commented Jan 19, 2021

Thanks. This will be automatically closed when #72 will be merged.

Also, a side note: I noticed your "trick" to avoid f-string substitution of the {nb} alias.
It's totally valid, just note that an alternative is to double the curly braces: f"{{nb}}" 😉

@slightlynybbled
Copy link
Author

I agree. I had initially done that, but in my attempts to correct the issue, I tried a few things. I prefer f-strings, but I was at my wit's end.

@Lucas-C
Copy link
Member

Lucas-C commented Jan 19, 2021

Closed by #72

@Lucas-C Lucas-C closed this as completed Jan 19, 2021
@msmannan00
Copy link

Hi Lucas,

I am speaking from Globaleaks. I am having an issue with the footer:
def footer(self):
self.set_y(-15)
self.set_font(self.report_default_font, '', 12)
self.set_text_shaping(use_shaping_engine=True, direction="ltr")
self.cell(0, 10, f"{self.page_no()}/{{nb}}", align="C")
self.set_text_shaping(use_shaping_engine=True, direction=self.report_direction)

I have a lot of custom fonts. All work perfectly while generating the PDF, but {{nb}} doesn't get replaced if I use custom fonts and is printed as 1/{nb}. However, if I use your default fonts like Courier, it works as expected, replacing {nb} with total pages.

strange thing is while using custom fonts font of 1/{nb} is also changed its just that the {nb} is not replaced with total page

@andersonhc
Copy link
Collaborator

Hi Lucas,

I am speaking from Globaleaks. I am having an issue with the footer: def footer(self): self.set_y(-15) self.set_font(self.report_default_font, '', 12) self.set_text_shaping(use_shaping_engine=True, direction="ltr") self.cell(0, 10, f"{self.page_no()}/{{nb}}", align="C") self.set_text_shaping(use_shaping_engine=True, direction=self.report_direction)

I have a lot of custom fonts. All work perfectly while generating the PDF, but {{nb}} doesn't get replaced if I use custom fonts and is printed as 1/{nb}. However, if I use your default fonts like Courier, it works as expected, replacing {nb} with total pages.

strange thing is while using custom fonts font of 1/{nb} is also changed its just that the {nb} is not replaced with total page

It is a known issue, reported on #1090 because text shaping with render the {nb} differently with some fonts depending specially on kerning.

I am going to work on this issue soon and we will hopefully have a final solution before the next release.

In the meantime I can suggest 2 workarounds:

  • Using a different font on your footer
  • Using a different page number alias - i.e. calling self.alias_nb_pages(alias="####") and writing f"{self.page_no()}/####" on your trailer

@evilaliv3
Copy link

Thank you @andersonhc , we have solved using 'courier' for the header and footers actually works for us since it adds a nice touch to result we need in @globaleaks.

This is actually the issue i was encountering with @msmannan00 while working on #1175 (comment)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

5 participants