# Attempting to center a line on a PDF not working properly

**URL:** <https://forum.xojo.com/t/attempting-to-center-a-line-on-a-pdf-not-working-properly/70676>\
**Category:** Web\
**Created:** [June 10, 2022, 4:23pm UTC](https://forum.xojo.com/t/attempting-to-center-a-line-on-a-pdf-not-working-properly/70676 "2022-06-10T16:23:47Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![Hector\_Marroquin](https://forum.xojo.com/letter_avatar_proxy/v4/letter/h/cdc98d/32.png) [@Hector\_Marroquin](https://forum.xojo.com/u/Hector_Marroquin)\
**Post date:** [June 10, 2022, 4:23pm UTC](https://forum.xojo.com/t/attempting-to-center-a-line-on-a-pdf-not-working-properly/70676/1 "2022-06-10T16:23:47Z")

</div>

Hello forum 🙂

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:

```auto
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.

 ![Not working as expected](https://forum.xojo.com/uploads/default/original/2X/1/1c54ad7dba2b8eed09db20127e5aa57d845517e2.png)

---

<div class="post-metadata">

**Author:** ![Javier\_Menendez](https://forum.xojo.com/user_avatar/forum.xojo.com/javier_menendez/32/15692_2.png) [@Javier\_Menendez](https://forum.xojo.com/u/Javier_Menendez)\
**Post date:** [June 10, 2022, 4:36pm UTC](https://forum.xojo.com/t/attempting-to-center-a-line-on-a-pdf-not-working-properly/70676/2 "2022-06-10T16:36:43Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Hector\_Marroquin](https://forum.xojo.com/letter_avatar_proxy/v4/letter/h/cdc98d/32.png) [@Hector\_Marroquin](https://forum.xojo.com/u/Hector_Marroquin)\
**Post date:** [June 10, 2022, 4:39pm UTC](https://forum.xojo.com/t/attempting-to-center-a-line-on-a-pdf-not-working-properly/70676/3 "2022-06-10T16:39:19Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![Javier\_Menendez](https://forum.xojo.com/user_avatar/forum.xojo.com/javier_menendez/32/15692_2.png) [@Javier\_Menendez](https://forum.xojo.com/u/Javier_Menendez)\
**Post date:** [June 10, 2022, 4:44pm UTC](https://forum.xojo.com/t/attempting-to-center-a-line-on-a-pdf-not-working-properly/70676/4 "2022-06-10T16:44:50Z")

</div>

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](https://m.youtube.com/watch?v=aSfff3hQaX4)

Video in Spanish: [Usando Xojo PDFDocument #XDCAnywhere - YouTube](https://m.youtube.com/watch?v=YQFyQLcIbi8)

Blog entry: [PDFDocument: Font Handling Improvements – Xojo Programming Blog](https://blog.xojo.com/2022/04/05/pdfdocument-font-handling-improvements/)

---

<div class="post-metadata">

**Author:** ![Hector\_Marroquin](https://forum.xojo.com/letter_avatar_proxy/v4/letter/h/cdc98d/32.png) [@Hector\_Marroquin](https://forum.xojo.com/u/Hector_Marroquin)\
**Post date:** [June 10, 2022, 5:08pm UTC](https://forum.xojo.com/t/attempting-to-center-a-line-on-a-pdf-not-working-properly/70676/5 "2022-06-10T17:08:02Z")

</div>

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:

```auto
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:

 ![Screen Shot 2022-06-10 at 11.06.15](https://forum.xojo.com/uploads/default/original/2X/3/333c36cc42c0fe69462d98a93f848c08ded3788d.png)

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

---

<div class="post-metadata">

**Author:** ![AlbertoD](https://forum.xojo.com/letter_avatar_proxy/v4/letter/a/dbc845/32.png) [@AlbertoD](https://forum.xojo.com/u/AlbertoD)\
**Post date:** [June 10, 2022, 5:13pm UTC](https://forum.xojo.com/t/attempting-to-center-a-line-on-a-pdf-not-working-properly/70676/6 "2022-06-10T17:13:28Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Hector\_Marroquin](https://forum.xojo.com/letter_avatar_proxy/v4/letter/h/cdc98d/32.png) [@Hector\_Marroquin](https://forum.xojo.com/u/Hector_Marroquin)\
**Post date:** [June 10, 2022, 5:15pm UTC](https://forum.xojo.com/t/attempting-to-center-a-line-on-a-pdf-not-working-properly/70676/7 "2022-06-10T17:15:58Z")

</div>

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 🤷‍♂️

---

<div class="post-metadata">

**Author:** ![AlbertoD](https://forum.xojo.com/letter_avatar_proxy/v4/letter/a/dbc845/32.png) [@AlbertoD](https://forum.xojo.com/u/AlbertoD)\
**Post date:** [June 10, 2022, 5:23pm UTC](https://forum.xojo.com/t/attempting-to-center-a-line-on-a-pdf-not-working-properly/70676/8 "2022-06-10T17:23:20Z")

</div>

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)

 ![image](https://forum.xojo.com/uploads/default/original/2X/b/b52cae10436b8c43e88cf4fb65975bec44e48867.png)

---

<div class="post-metadata">

**Author:** ![Hector\_Marroquin](https://forum.xojo.com/letter_avatar_proxy/v4/letter/h/cdc98d/32.png) [@Hector\_Marroquin](https://forum.xojo.com/u/Hector_Marroquin)\
**Post date:** [June 10, 2022, 5:30pm UTC](https://forum.xojo.com/t/attempting-to-center-a-line-on-a-pdf-not-working-properly/70676/9 "2022-06-10T17:30:07Z")

</div>

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

I added a third line for testing:

```auto
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:

 ![Screen Shot 2022-06-10 at 11.27.12](https://forum.xojo.com/uploads/default/original/2X/2/21c0a23471650a66597eb37e07adadfcfc37902b.png)

---

<div class="post-metadata">

**Author:** ![AlbertoD](https://forum.xojo.com/letter_avatar_proxy/v4/letter/a/dbc845/32.png) [@AlbertoD](https://forum.xojo.com/u/AlbertoD)\
**Post date:** [June 10, 2022, 5:33pm UTC](https://forum.xojo.com/t/attempting-to-center-a-line-on-a-pdf-not-working-properly/70676/10 "2022-06-10T17:33:31Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Hector\_Marroquin](https://forum.xojo.com/letter_avatar_proxy/v4/letter/h/cdc98d/32.png) [@Hector\_Marroquin](https://forum.xojo.com/u/Hector_Marroquin)\
**Post date:** [June 10, 2022, 5:34pm UTC](https://forum.xojo.com/t/attempting-to-center-a-line-on-a-pdf-not-working-properly/70676/11 "2022-06-10T17:34:08Z")

</div>

> [@AlbertoD](#):
>
> See my updated post, bold is not centering correctly.

You are correct

---

<div class="post-metadata">

**Author:** ![Javier\_Menendez](https://forum.xojo.com/user_avatar/forum.xojo.com/javier_menendez/32/15692_2.png) [@Javier\_Menendez](https://forum.xojo.com/u/Javier_Menendez)\
**Post date:** [June 10, 2022, 5:42pm UTC](https://forum.xojo.com/t/attempting-to-center-a-line-on-a-pdf-not-working-properly/70676/12 "2022-06-10T17:42:03Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Hector\_Marroquin](https://forum.xojo.com/letter_avatar_proxy/v4/letter/h/cdc98d/32.png) [@Hector\_Marroquin](https://forum.xojo.com/u/Hector_Marroquin)\
**Post date:** [June 10, 2022, 5:46pm UTC](https://forum.xojo.com/t/attempting-to-center-a-line-on-a-pdf-not-working-properly/70676/13 "2022-06-10T17:46:59Z")

</div>

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

This is the result with DejaVu font:

 ![Screen Shot 2022-06-10 at 11.41.52](https://forum.xojo.com/uploads/default/original/2X/1/1a871f0c770460eaac2d6aebd73e78fe2ae603c2.png)

---

<div class="post-metadata">

**Author:** ![AlbertoD](https://forum.xojo.com/letter_avatar_proxy/v4/letter/a/dbc845/32.png) [@AlbertoD](https://forum.xojo.com/u/AlbertoD)\
**Post date:** [June 10, 2022, 5:47pm UTC](https://forum.xojo.com/t/attempting-to-center-a-line-on-a-pdf-not-working-properly/70676/14 "2022-06-10T17:47:21Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![AlbertoD](https://forum.xojo.com/letter_avatar_proxy/v4/letter/a/dbc845/32.png) [@AlbertoD](https://forum.xojo.com/u/AlbertoD)\
**Post date:** [June 10, 2022, 5:47pm UTC](https://forum.xojo.com/t/attempting-to-center-a-line-on-a-pdf-not-working-properly/70676/15 "2022-06-10T17:47:45Z")

</div>

Do you have DejaVu bold installed on your server?

---

<div class="post-metadata">

**Author:** ![Hector\_Marroquin](https://forum.xojo.com/letter_avatar_proxy/v4/letter/h/cdc98d/32.png) [@Hector\_Marroquin](https://forum.xojo.com/u/Hector_Marroquin)\
**Post date:** [June 10, 2022, 5:48pm UTC](https://forum.xojo.com/t/attempting-to-center-a-line-on-a-pdf-not-working-properly/70676/16 "2022-06-10T17:48:31Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Hector\_Marroquin](https://forum.xojo.com/letter_avatar_proxy/v4/letter/h/cdc98d/32.png) [@Hector\_Marroquin](https://forum.xojo.com/u/Hector_Marroquin)\
**Post date:** [June 10, 2022, 5:52pm UTC](https://forum.xojo.com/t/attempting-to-center-a-line-on-a-pdf-not-working-properly/70676/17 "2022-06-10T17:52:18Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![AlbertoD](https://forum.xojo.com/letter_avatar_proxy/v4/letter/a/dbc845/32.png) [@AlbertoD](https://forum.xojo.com/u/AlbertoD)\
**Post date:** [June 10, 2022, 5:58pm UTC](https://forum.xojo.com/t/attempting-to-center-a-line-on-a-pdf-not-working-properly/70676/18 "2022-06-10T17:58:15Z")

</div>

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](https://dejavu-fonts.github.io/Download.html)  
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?

---

<div class="post-metadata">

**Author:** ![Hector\_Marroquin](https://forum.xojo.com/letter_avatar_proxy/v4/letter/h/cdc98d/32.png) [@Hector\_Marroquin](https://forum.xojo.com/u/Hector_Marroquin)\
**Post date:** [June 10, 2022, 6:03pm UTC](https://forum.xojo.com/t/attempting-to-center-a-line-on-a-pdf-not-working-properly/70676/19 "2022-06-10T18:03:14Z")

</div>

> [@AlbertoD](#):
>
> 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.

---

<div class="post-metadata">

**Author:** ![Hector\_Marroquin](https://forum.xojo.com/letter_avatar_proxy/v4/letter/h/cdc98d/32.png) [@Hector\_Marroquin](https://forum.xojo.com/u/Hector_Marroquin)\
**Post date:** [June 10, 2022, 7:17pm UTC](https://forum.xojo.com/t/attempting-to-center-a-line-on-a-pdf-not-working-properly/70676/20 "2022-06-10T19:17:39Z")

</div>

Further testing (in debug mode).

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

```auto
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

```

 ![Screen Shot 2022-06-10 at 13.15.36](https://forum.xojo.com/uploads/default/original/2X/a/a203f84be579634a037733c57705c3b676e71fc0.png)

[Next page](https://forum.xojo.com/t/attempting-to-center-a-line-on-a-pdf-not-working-properly/70676.md?page=2)
