Replies: 11 comments 7 replies
-
Hi, thanks for asking. I'll try to provide a detailed answer asap. Also, ping @gmischler who could probably also help there. |
Beta Was this translation helpful? Give feedback.
-
I have just made a test regarding this and the following code snippet renders exactly the same with PyFPDF & doc = FPDF()
doc.add_page()
doc.set_font("helvetica", size=TEXT_SIZE)
doc.cell(w=45, h=LINE_HEIGHT, border=1, txt="Lorem")
doc.cell(w=45, h=LINE_HEIGHT, border=1, txt="ipsum")
doc.cell(w=45, h=LINE_HEIGHT, border=1, txt="Ut")
doc.cell(w=45, h=LINE_HEIGHT, border=1, txt="nostrud")
doc.output("discussion_411.pdf") Hence, could you provide more details please?
There should not be any, no. |
Beta Was this translation helpful? Give feedback.
-
I tried, still no difference: from fpdf import FPDF, FPDF_VERSION
print(FPDF_VERSION)
pdf = FPDF()
pdf.add_page()
pdf.set_font("helvetica", size=36)
pdf.cell(w=45, h=36, border=1, align="C", txt="Lorem")
pdf.cell(w=45, h=36, border=1, align="C", txt="ipsum")
pdf.cell(w=45, h=36, border=1, align="C", txt="Ut")
pdf.cell(w=45, h=36, border=1, align="C", txt="nostrud")
pdf.output(f"repro-{FPDF_VERSION}.pdf") @gmischler: have you been able to spot this difference yourself? |
Beta Was this translation helpful? Give feedback.
-
@Lucas-C Thanks, I've written a small example that results in different positions for the versions I have installed. from fpdf import FPDF, FPDF_VERSION
p = FPDF(format='A4')
p.add_page()
p.set_font('helvetica', size=24)
p.set_xy(0, 10)
p.cell(.01, txt=str(FPDF_VERSION))
p.line(0, 50, p.w, 50)
p.set_xy(50, 50)
p.cell(.01, txt='Lorem')
p.output('test.pdf') Result: https://i.imgur.com/QqTFRyj.png pyfpdf version: I do have a slightly strange way that I use cells. I give them a negligible width so that I can place them precisely, but still have the option of centering the text around that point, an option which My first thought was to patch in a wrapper method like @gmischler suggested, but since the text center depends on the particular font and size, I wasn't sure if that "center" position could be easily calculated in all cases. Thanks for the help! |
Beta Was this translation helpful? Give feedback.
-
Thank you for the code! From my test, the only thing that differs for me between the PDFs from If so, I think this is related to #210 |
Beta Was this translation helpful? Give feedback.
-
@Lucas-C Yep, the only difference I'm finding is the vertical alignment. I think a vertical alignment option for I'll look into the source some more today to see if I can move the anchor around with an option, though I start to get lost at the |
Beta Was this translation helpful? Give feedback.
-
Ah, so there's the source of confusion: When you simply say center, then most people will expect you to talk about horizontal centering. In reality it was all about vertical alignment. The current version will center text vertically within a cell, but only if the cell height is greater than the font height. As long as you don't use cell borders and background (both default to transparent), a quick and dirty workaround might be to give the cell a height that is garanteed to be larger than the font height, and anchor it half its height higher than your center line. Nonetheless, we may still soon see a horizontal |
Beta Was this translation helpful? Give feedback.
-
Well, I wrote in an if h is None:
h = self.font_size So instead of defaulting to zero like the old pyfpdf, the cell height defaults to text height. Changing my test to Here's the branch if you're interested: https://github.com/gwyker/fpdf2/tree/anchor-text from fpdf import FPDF, FPDF_VERSION
p = FPDF(format='A4')
p.add_page()
p.set_font('helvetica', size=24)
p.set_xy(0, 10)
p.line(0, 70, p.w, 70)
p.rect(50, 50, 50, 40)
p.set_xy(50, 50)
p.cell(10, 40, txt='Lorem', anchor='C')
p.set_xy(50, 50)
p.cell(10, 40, txt='Lorem', anchor='T')
p.set_xy(50, 50)
p.cell(10, 40, txt='Lorem', anchor='B')
p.output('test.pdf') It's got some subjective positioning to make the text align decently within the cell, so I'm not sure if it'll look odd in other situations. |
Beta Was this translation helpful? Give feedback.
-
Well spotted!
I looked at your code and it's well done! |
Beta Was this translation helpful? Give feedback.
-
In HTML/CSS, the terminology chosen is "center" for horizontal alignment, and "middle" vor vertical. This was done very deliberately to help avoid confusion between the two (the above discussion illustrates nicely why that is helpful). I highly recommend to stay as close as reasonably possible to the HTML/CSS terminology, since most users will already be familiar with that. When doing that, your keys would become "T", "B", and "M".
That is pretty much garanteed to fail with other fonts, which may have vastly different vertical proportions. Maybe this is actually a good opportunity to switch to using Fonttools instead of our homebrew solution. That will also be necessary for any chance to fix #365, btw. |
Beta Was this translation helpful? Give feedback.
-
Agree with you @gmischler on the terminology and I think valign is a better name for it. The hardcoded floats are definitely an issue and are why I didn't go any further with a PR, I couldn't figure out how to align different text styles to the top and bottom in a visually consistent way. The vertical alignment is no longer useful for my case, since I can just use a |
Beta Was this translation helpful? Give feedback.
-
Hi, I've written a pretty large codebase over the past year using the old pyfpdf package and just discovered that fpdf2 exists, so I'm eager to make the switch.
Uninstalling pyfpdf and dropping in fpdf2 results in a pretty huge speed increase, and the outputs look nearly identical, but there is one problem: the old module's
cell()
aligns the center of the text to the cell's xy position, while fpdf2 aligns the top left of the text to the xy position. This means that either all of my code's set_xy calls will need to be adjusted, or I'll have to monkey patchcell()
with some sort of re-alignment logic based on fontsize to simulate the old behavior.I have two questions:
cell()
align cell text like pyfpdf did, or to get the same effect through other means?Beta Was this translation helpful? Give feedback.
All reactions