Attempting to center a line on a PDF not working properly

Hello forum :slight_smile:

I’m having this issue I can’t make sense of. Running this code in debug mode (2022R1.1) works perfectly, howerver when I upload it to a server the line where I print the lineOne variable doesn’t center correctly. Here is the code:

g.FontSize = 10
g.DrawingColor = Color.Black
Var currentFont As String = "Curent Font: " + g.FontName
g.DrawText( currentFont , 30 , 750 , g.Width - 60 )
Var lineOne As String = "This is the text for line 1. Some filler content for testing purposes"
g.DrawText( lineOne , (g.Width / 2) - (g.TextWidth(lineOne) / 2) , 760 ) // This line is not centering correctly
g.Bold = True
Var lineTwo As String = "This is the text for line 2 ( Centers correctly )" 
g.DrawText( lineTwo , (g.Width / 2) - (g.TextWidth(lineTwo) / 2) , 770 )

Any thoughts on what could be causing this weird behavior?

And yes, it is verbouse code, just trying to make my point as understandable as possible.

Do you have the “Helvética” font installed in your server? If not, try to set first the FontName to any font that is already installed.

This is the dafault font used by the PDFDocument, but most importantly: Both lines use the same font so if it was a Font issue it would consistently fail in both lines wouldn’t it?

1 Like

Not exactly.

First, being the default font doesn’t mean that such font is installed on your computer/server or not. So it is better you set the FontName to a font you know is installed.

When you set attributes as Bold in this case, PDFDocument tries to do it’s best to “find” an installed font as fallback if the “base” font is not found. That’s why you’re getting the second line centered.

Video in English: Using Xojo's PDFDocument #XDCAnywhere - YouTube

Video in Spanish: Usando Xojo PDFDocument #XDCAnywhere - YouTube

Blog entry: PDFDocument: Font Handling Improvements – Xojo Programming Blog

Thanks for the links, I’ve watched the videos and read the blog post as soon as they were available days ago.

Doing some additional testing:

g.Bold = True
currentFont = "Curent Font: " + g.FontName
g.DrawText( currentFont , 30 , 760 , g.Width - 60 )
Var lineTwo As String = "This is the text for line 2 ( Centers correctly )" 
g.DrawText( lineTwo , (g.Width / 2) - (g.TextWidth(lineTwo) / 2) , 770 )

Produces this output:

Same font. But I’ll not be obtuse and try setting the Font explicitly and see if that fixes the issue.

What server are you using?
What fonts are installed on the server?

From the blog post:

It is typical for Linux based servers not to have many fonts installed; make sure to install all the font files you intend to use in the creation of your PDF documents

Hello @AlbertoD

I’m using Debian 10.

I understand where you are heading and it makes sense. What doesn’t makes sense is it working on some lines and not working on others :man_shrugging:

My guess is that non-bold font doesn’t find a font for the g.TextWidth calculation so the result is 0/2 and printed that way (starting from the center of the page).

For bold font, it does find a font to do the g.TextWidth calculation, for me it seems that the bold line is not perfectly centered but a little to the left than where it should print, so the calculation is also wrong but closer to what you expect.

If you use one of the installed fonts, then you can see if there is a difference.

Edit: like this (bold not perfectly centered)

Thans for taking the time to offer your input, very much appreciated.

I added a third line for testing:

g.Bold = False
currentFont = "Curent Font: " + g.FontName
g.DrawText( currentFont , 30 , 770 , g.Width - 60 )
Var lineThree As String = "This is line Three"
g.DrawText( lineThree , (g.Width / 2) - (g.TextWidth(lineThree) / 2) , 780 )

Which produced this output:

See my updated post, bold is not centering correctly.

Not-bold font for ‘Helvetica’ fails the g.TextWidth(line) / 2
g.TextWidth(line) = 0
so your ‘Helvetica’ font not-bold will always print from the center of the page.

Unless you assign a font installed on your server you will get wrong results. Some more wrong than others.

If with an installed font you get this, then open a case on Issues with your server, font and sample code to reproduce.

1 Like

You are correct

It fails in the first line because you don’t set any font. Try setting a font. Just that. And if it is one that is in fact installed in your server, best. :slightly_smiling_face:

Setting the font to DejaVu actually fixes the calculations. But now there is no bold text.

This is the result with DejaVu font:

I haven’t used PDFDocument but the fact that Hector’s code says g.FontName and prints on the PDF as ‘Helvetica’ doesn’t mean that the font is set? I guess is set before the code he shows.
Maybe I’m missing something.

Do you have DejaVu bold installed on your server?

That’s exactly what threw me off. I tested what the content of g.FontName was and it was returning “Helvetica”. That made me assume that the font was actually available.

DejaVu is the default font for Debian 10.

And I thought the default font would be used and there was no need for me to explicitly set it. Seems I was wrong.

But there’s still the issue of bold not working with this font.

Maybe DejaVu has different fonts, for example DejaVu Sans, DejaVu Serif, DejaVu and your system, when you set only DejaVu for PDFDocument there is no DejaVu Bold.

Can you check what fonts your server has and the name for them?

I’m not sure how PDFDocument works with Font Names, my guess is that it can’t find the Bold version of DejaVu, maybe you need to define the name as DejaVu Bold or DejaVuSans Bold

Edit: downloaded the DejaVu fonts from DejaVu Fonts — License
According to the Readme, this is the list of fonts included:

DejaVu Sans Mono
DejaVu Sans Mono Bold
DejaVu Sans Mono Bold Oblique
DejaVu Sans Mono Oblique
DejaVu Sans
DejaVu Sans Bold
DejaVu Sans Bold Oblique
DejaVu Sans Oblique
DejaVu Sans ExtraLight (experimental)
DejaVu Serif
DejaVu Serif Bold
DejaVu Serif Bold Italic (experimental)
DejaVu Serif Italic (experimental)
DejaVu Sans Condensed (experimental)
DejaVu Sans Condensed Bold (experimental)
DejaVu Sans Condensed Bold Oblique (experimental)
DejaVu Sans Condensed Oblique (experimental)
DejaVu Serif Condensed (experimental)
DejaVu Serif Condensed Bold (experimental)
DejaVu Serif Condensed Bold Italic (experimental)
DejaVu Serif Condensed Italic (experimental)
DejaVu Math TeX Gyre

Maybe try to use ‘DejaVu Sans Bold’ or ‘DejaVu Serif Bold’ instead of just bold for DejaVu?

That makes sense, let me test it. I’ll be back with results so this can help someone else having this same issue.

Further testing (in debug mode).

Using Zapfino font I get the text rendered but notice that the TextWidth is not being calculated correctly:

g.FontSize = 10
g.DrawingColor = Color.Black
g.FontName = "Zapfino"
Var currentFont As String = "Curent Font: " + g.FontName
g.DrawText( currentFont , 30 , 710 , g.Width - 60 )
Var LineZero As String = "<- TextWidth - Testing what gets rendered with this Font"
LineZero = Str(g.textwidth(LineZero)) + " - " + LineZero
g.DrawText( LineZero , (g.Width / 2) - (g.TextWidth(LineZero) / 2) , 720 ) // This line is not centering correctly