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.
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?
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.
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
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.
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.
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.
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.
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.
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?