# Font problem creating pdf file with mPDF

**URL:** <https://www.phphelp.com/t/font-problem-creating-pdf-file-with-mpdf/33773>\
**Category:** Beginners - Learning PHP\
**Created:** [July 30, 2021, 2:28pm UTC](https://www.phphelp.com/t/font-problem-creating-pdf-file-with-mpdf/33773 "2021-07-30T14:28:32Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![Adem](https://www.phphelp.com/letter_avatar_proxy/v4/letter/a/a4c791/32.png) [@Adem](https://www.phphelp.com/u/Adem)\
**Post date:** [July 30, 2021, 2:28pm UTC](https://www.phphelp.com/t/font-problem-creating-pdf-file-with-mpdf/33773/1 "2021-07-30T14:28:32Z")

</div>

Hello,  
There is no problem in local, but while there was no problem in real unix server, now there is a font problem and I can’t fix it  
I tried many things, but it doesn’t get better, where am I going wrong?

 ![Ekran görüntüsü 2021-07-30 172618](https://www.phphelp.com/uploads/default/original/2X/6/60c511ea9d3fa227ad06c7debd834454a1ea043d.png)

```
require_once __DIR__. '/vendor/autoload.php';

ob_start();
include("../html_tablo_pdf.php");
$html = ob_get_clean();

$mpdf = new \Mpdf\Mpdf();

$mpdf = new \Mpdf\Mpdf(['tempDir' => __DIR__. '/tmp']);

```

---

<div class="post-metadata">

**Author:** ![ErnieAlex](https://www.phphelp.com/letter_avatar_proxy/v4/letter/e/bc8723/32.png) [@ErnieAlex](https://www.phphelp.com/u/ErnieAlex)\
**Post date:** [July 30, 2021, 3:23pm UTC](https://www.phphelp.com/t/font-problem-creating-pdf-file-with-mpdf/33773/2 "2021-07-30T15:23:36Z")

</div>

I am not sure what your code does. It makes no sense to create TWO NEW mPDF objects in a row.  
The second would overwrite the first one. Also, I do not know what ‘tempDir’ =\> does. ??? ‘tempDir’ is an object?

Well, here is sample code how to create the PDF from the $html once you have that loaded…

```auto
// Create a PDF
$mpdf = new mPDF();
$mpdf->WriteHTML($html);
// Save the file
$mpdf->Output("some-folder-name/pdf-filename.pdf", 'F');

```

You probably need to use the new \Mpdf\Mpdf(); in your case…  
But, I do not see any issues with FONT’s…

One further thought for you. Fonts can not be used from a CSS file. You need to place them directly into the HTML file itself. mPDF will convert the CSS correctly, but, since the included php file does not contain the CSS itself, it will not copy over to the PDF. That might be your issue. Good luck!

---

<div class="post-metadata">

**Author:** ![Adem](https://www.phphelp.com/letter_avatar_proxy/v4/letter/a/a4c791/32.png) [@Adem](https://www.phphelp.com/u/Adem)\
**Post date:** [July 31, 2021, 7:52am UTC](https://www.phphelp.com/t/font-problem-creating-pdf-file-with-mpdf/33773/3 "2021-07-31T07:52:39Z")

</div>

I am using VPS  
The first website works fine  
The other website has a font problem  
The codes are the same  
No problem locally

I don’t know if this is really necessary but this creates a directory called temp and copies the font files into it  
`$mpdf = new \Mpdf\Mpdf(['tempDir' => __DIR__. '/tmp']);`

No fonts are added in the CSS file and the HTML table

> [@ErnieAlex](#):
>
> It makes no sense to create TWO NEW mPDF objects in a row.

How so?  
I’m not doing this on purpose  
If the offer is one page I want to create a one page pdf.  
If there is more than one page I want to create more than one pdf  
NOTE: I am saving the pdf file as text and without extension to the temporary server, there are reasons for this according to myself

There is also an interesting case that there is no font problem in the watermark text  
I’m doing something wrong but what is it?  
i keep searching

```
require_once __DIR__. '/vendor/autoload.php';

ob_start();
include("../html_tablo_pdf.php");
$html = ob_get_clean();

$mpdf = new \Mpdf\Mpdf();

$mpdf = new \Mpdf\Mpdf(['tempDir' => __DIR__. '/tmp']);

$mpdf->showWatermarkText = true;
$mpdf->watermark_font = 'DejaVuSansCondensed';

if(isset($watermarktext)){
$mpdf->WriteHTML('<watermarktext content="'.$watermarktext.'" alpha="0.1" />');
}

$mpdf->SetTitle($firma.' ('.$adi_soyadi.')');

$mpdf->SetAuthor('Teklifi Çıkarma Programı - '.$siteurl);
$mpdf->SetSubject('MERKEZİ UYDU ANTEN TV SİSTEMİ FİYAT TEKLİFİDİR');

$mpdf->SetDisplayMode('fullpage');

$mpdf->list_indent_first_level = 1;	// 1 or 0 - whether to indent the first level of a list

// LOAD a stylesheet
$stylesheet = file_get_contents('pdfcss.css');

$mpdf->WriteHTML($stylesheet,1);	// The parameter 1 tells that this is css/style only and no body/html/text

$mpdf->autoScriptToLang = true;
$mpdf->baseScript = 1;
$mpdf->autoLangToFont = true;

$mpdf->WriteHTML($html,2);

$data = $mpdf->Output('','S');
        
$fh = fopen($pdfdosyaadi, 'w') or die("PDF Dosya açılmıyor");   

fwrite($fh, $data); 
fclose($fh);

```

---

<div class="post-metadata">

**Author:** ![ErnieAlex](https://www.phphelp.com/letter_avatar_proxy/v4/letter/e/bc8723/32.png) [@ErnieAlex](https://www.phphelp.com/u/ErnieAlex)\
**Post date:** [July 31, 2021, 1:55pm UTC](https://www.phphelp.com/t/font-problem-creating-pdf-file-with-mpdf/33773/4 "2021-07-31T13:55:43Z")

</div>

> [@Adem](#):
>
> ```auto
> $mpdf = new \Mpdf\Mpdf();
> 
> $mpdf = new \Mpdf\Mpdf(['tempDir' => __DIR__. '/tmp']);
> 
> ```

This first line creates an OBJECT named $mpdf. It does nothing more except set up an empty CLASS OBJECT. An object that is ready to use as a Mpdf housing for whatever…  
The second one creates a new OBJECT over the old one, therefore the first one is a waste of code.  
And, you are setting a custom temp directory outside the vendor structure of Mpdf. I do not understand that part, not the code itself. Mpdf creates its own temp area. It autocleans that tmp folder and removes anything that is 1 hour old. This is usually done when you create a new $mpdf variable. Perhaps I do not understand why you need a new temp file instead of the default one. Lastly, you need to make sure that the new temp folder has write permissions set. That is all I was attempting to explain.

---

<div class="post-metadata">

**Author:** ![Adem](https://www.phphelp.com/letter_avatar_proxy/v4/letter/a/a4c791/32.png) [@Adem](https://www.phphelp.com/u/Adem)\
**Post date:** [August 1, 2021, 8:49am UTC](https://www.phphelp.com/t/font-problem-creating-pdf-file-with-mpdf/33773/5 "2021-08-01T08:49:05Z")

</div>

The reason for all the problems I’m having is because I upgraded to the latest version.  
Latest version is installed when I install using “composer require mpdf/mpdf”

I didn’t do any of the above codes on purpose.  
I’m trying to do it all over again, I’m trying to do it in the best way

I think new versions always make the job easier, but I’ve seen that they make it harder.  
css file works but some features do not work.  
style doesn’t work in html.  
I can’t understand where is the problem.  
I’m so confused I don’t know what to do  
I want to choose a font that is easy to read in a small space. I really like the android font on android phones, but it’s not available here.

CSS styling is also a few things I want  
`<th>` headers background color.  
A table is a light padding and just horizontal dividers.  
I want to make some text bold  
I want to make some text bold, big and red color  
I want to align some columns of text to the right

The pdf file I created with the old mPDF version in the picture. Doesn’t work anymore in new mPDF version

 ![Annotate-a-local-image](https://www.phphelp.com/uploads/default/original/2X/0/069afdb545f2d79f058a675bd194ca152801079d.png)

---

<div class="post-metadata">

**Author:** ![ErnieAlex](https://www.phphelp.com/letter_avatar_proxy/v4/letter/e/bc8723/32.png) [@ErnieAlex](https://www.phphelp.com/u/ErnieAlex)\
**Post date:** [August 1, 2021, 12:57pm UTC](https://www.phphelp.com/t/font-problem-creating-pdf-file-with-mpdf/33773/6 "2021-08-01T12:57:22Z")

</div>

Well, the Android font is really called " Roboto ". It is actually available from [Google.com](http://Google.com).  
Here is a link to a site that discusses it and in that page, there is a link to the Roboto font at Google.  
You can download it there or just link to Google to use it. [Roboto Font](https://uxplanet.org/5-universal-fonts-for-web-mobile-design-7b491df0ea16)

I usually do not upgrade to the latest ever on my live sites. I do it on a test site to see what the upgrade does. Quite often the latest versions will make some things better and other things worse. So, I always create a new copy of my site locally on my test system, do the upgrade and see what happens. If it works 100% good, then I do the upgrade on the live site.

Check out the font and test it. Oh, also, to make text bold, just add the \< b \> tags and that should work well.

---

<div class="post-metadata">

**Author:** ![Adem](https://www.phphelp.com/letter_avatar_proxy/v4/letter/a/a4c791/32.png) [@Adem](https://www.phphelp.com/u/Adem)\
**Post date:** [August 1, 2021, 7:08pm UTC](https://www.phphelp.com/t/font-problem-creating-pdf-file-with-mpdf/33773/7 "2021-08-01T19:08:46Z")

</div>

There may be some errors due to language ;)))

I rebuilt css and pdf layouts with the latest version on local computer  
Hassle-free awesome PDF created just the way I wanted it

On remote server same version creates PDF with same files but css file does not work

The below code does not need it on local computer but needs it on remote server

```
$defaultConfig = (new Mpdf\Config\ConfigVariables())->getDefaults();
$fontDirs = $defaultConfig['fontDir'];

$defaultFontConfig = (new Mpdf\Config\FontVariables())->getDefaults();
$fontData = $defaultFontConfig['fontdata'];

$mpdf = new \Mpdf\Mpdf([
    'fontDir' => array_merge($fontDirs, [
        __DIR__. '/ttfonts',
    ]),
    'fontdata' => $fontData + [
        'frutiger' => [
            'R' => 'Roboto-Regular.ttf',
            'I' => 'Roboto-Regular.ttf',
        ]
    ],
    'default_font' => 'frutiger',
    'default_font_size' => 8,
    'tempDir' => __DIR__. '/tmp'
]);

```

I can’t understand the mPDF explanations very much as they are explained with short and technical words for the developer.

There may be unnecessary and wrong things in this css code because it is a beginner’s work.

```
table.customTable {
    width: 793px;
    background-color: #FFFFFF;
    border-collapse: collapse;
    border-color: #77EEAA;
    border-style: solid;
    font-family: Arial; 
    font-size: 7pt;
    font-family: Roboto, sans-serif;
}

table.customTable tr td {
    padding: 5px;
    border-bottom: 1px solid #ddd;
    font-family:'Roboto-thin',sans-serif;
}

table.customTable tr th {
    background-color: #1570cd;
    color: #ffffff;
    padding: 5px;
    font-family:'Roboto-thin',sans-serif;
}

.align-left {
    text-align:left;
}

.align-right {
    text-align:right;
}

.align-center {
    text-align:center;
}

.bold {
    font-weight:bold;
}

.red {
    color:rgb(255, 0, 0);
}

```

I load the CSS file as follows

```
$mpdf->autoScriptToLang = true;
$mpdf->baseScript = 1;	// Use values in classes/ucdn.php 1 = LATIN
$mpdf->autoLangToFont = true;

$stylesheet = file_get_contents('style.css');

$mpdf->WriteHTML($stylesheet,\Mpdf\HTMLParserMode::HEADER_CSS);
$mpdf->WriteHTML($html,\Mpdf\HTMLParserMode::HTML_BODY);
```

---

<div class="post-metadata">

**Author:** ![ErnieAlex](https://www.phphelp.com/letter_avatar_proxy/v4/letter/e/bc8723/32.png) [@ErnieAlex](https://www.phphelp.com/u/ErnieAlex)\
**Post date:** [August 1, 2021, 8:00pm UTC](https://www.phphelp.com/t/font-problem-creating-pdf-file-with-mpdf/33773/8 "2021-08-01T20:00:42Z")

</div>

Hmmm, so a couple questions…

You got the Roboto fonts all set. That was a good find. I found them with just some Googling…

The code works 100% as you want offline. So, that means something is different on the live server.

1 - Is your folder layout 100% the same on the live server as your offline server?  
2 - Did you copy ALL your files up to the live server? (If one is missing it might be that problem.)  
3 - Are ALL of your server files, libraries the same versions? PHP is the first one I would suspect.

I would suggest to display both phpinfo() displays on your local and live servers and compare them!  
This will tell you what is different. It will be a bit tedious to go thru all of the settings, but, will make sure  
they are the same! Let us know…

---

<div class="post-metadata">

**Author:** ![Adem](https://www.phphelp.com/letter_avatar_proxy/v4/letter/a/a4c791/32.png) [@Adem](https://www.phphelp.com/u/Adem)\
**Post date:** [August 1, 2021, 9:10pm UTC](https://www.phphelp.com/t/font-problem-creating-pdf-file-with-mpdf/33773/9 "2021-08-01T21:10:38Z")

</div>

There are already 3 files used here  
I deleted these 3 files on the remote server and uploaded them again via FTP.

1. style.css
2. include HTML Table
3. PDF creation file with mPDF (codes above)

I uploaded mPDF files to server with terminal  
If I delete the mPDF files, the files come back again.

Local computer PHP version: 7.3.12 ve 7.4.21 (windows 10)  
Remote server PHP version: 7.3.29  
I wonder what is required for mPDF in phpinfo

**Remote server**

 ![Ekran görüntüsü 2021-08-01 234422](https://www.phphelp.com/uploads/default/original/2X/1/1a5a48edaf6fb34d68052949d09bb952ee0c7cbe.png)

**Local computer**

 ![Ekran görüntüsü 2021-08-01 234539](https://www.phphelp.com/uploads/default/original/2X/e/e2a4e753ce96a8e6c4aa96f28da3bf810eb7d1c3.png)

---

<div class="post-metadata">

**Author:** ![ErnieAlex](https://www.phphelp.com/letter_avatar_proxy/v4/letter/e/bc8723/32.png) [@ErnieAlex](https://www.phphelp.com/u/ErnieAlex)\
**Post date:** [August 1, 2021, 10:31pm UTC](https://www.phphelp.com/t/font-problem-creating-pdf-file-with-mpdf/33773/10 "2021-08-01T22:31:57Z")

</div>

The versions all seem okay. Both are 7.3 ( the last part is the minor revisions so not as important. )

Did you upload your fonts to the remote servers?

Adem, you do come up with some interesting problems…

---

<div class="post-metadata">

**Author:** ![ErnieAlex](https://www.phphelp.com/letter_avatar_proxy/v4/letter/e/bc8723/32.png) [@ErnieAlex](https://www.phphelp.com/u/ErnieAlex)\
**Post date:** [August 1, 2021, 10:41pm UTC](https://www.phphelp.com/t/font-problem-creating-pdf-file-with-mpdf/33773/11 "2021-08-01T22:41:49Z")

</div>

I was looking over my various hundreds of websites I have created, repaired, updated or just reviewed.  
In one of them, I had used a library called “dompdf”. It basically lets you create an HTML output and  
convert it to a PDF. It makes life easier in some ways because you can just throw the HTML file into  
it and it seems to work for the few sites I used it. (Not for myself, but, a friend and a client.)

It is a free library and easy to use. You might want to look at it to replace mPDF. I had trouble with mPDF in the past although it is used by a lot of programmers. If I remember correctly, it is quite easy to use either with separate files or in-line OBJ creation of the HTML. Link: [domPDF](https://github.com/dompdf/dompdf)

---

<div class="post-metadata">

**Author:** ![Adem](https://www.phphelp.com/letter_avatar_proxy/v4/letter/a/a4c791/32.png) [@Adem](https://www.phphelp.com/u/Adem)\
**Post date:** [August 2, 2021, 8:48am UTC](https://www.phphelp.com/t/font-problem-creating-pdf-file-with-mpdf/33773/12 "2021-08-02T08:48:05Z")

</div>

> [@ErnieAlex](#):
>
> The versions all seem okay. Both are 7.3 ( the last part is the minor revisions so not as important. )
> 
> Did you upload your fonts to the remote servers?
> 
> Adem, you do come up with some interesting problems

I zip all inclusive directory containing all mpdf and other files in local  
I uploaded to remote server  
I opened the zip  
same problem again

> [@ErnieAlex](#):
>
> I was looking over my various hundreds of websites I have created, repaired, updated or just reviewed.  
> In one of them, I had used a library called “dompdf”. It basically lets you create an HTML output and  
> convert it to a PDF. It makes life easier in some ways because you can just throw the HTML file into  
> it and it seems to work for the few sites I used it. (Not for myself, but, a friend and a client.)
> 
> It is a free library and easy to use. You might want to look at it to replace mPDF. I had trouble with mPDF in the past although it is used by a lot of programmers. If I remember correctly, it is quite easy to use either with separate files or in-line OBJ creation of the HTML. Link: [domPDF](https://github.com/dompdf/dompdf)

There is no such thing as mPDF, I just want to create a stable PDF that I want properly.  
I’ll take a look at this.

How to remove mPDF?  
I delete the folder and it comes back

With PuTTY terminal  
directory path composer remove mpdf/mpdf  
enter  
yes  
i do but i can’t  
How can I remove this? can someone help?

 ![Annotate-a-local-image](https://www.phphelp.com/uploads/default/original/2X/8/8e0f9eade3083fa3292fe4f89bebd12dd847584e.png)  
[https://stackoverflow.com/questions/23126562/how-can-i-remove-a-package-from-laravel-using-php-composer](https://stackoverflow.com/questions/23126562/how-can-i-remove-a-package-from-laravel-using-php-composer)

---

<div class="post-metadata">

**Author:** ![ErnieAlex](https://www.phphelp.com/letter_avatar_proxy/v4/letter/e/bc8723/32.png) [@ErnieAlex](https://www.phphelp.com/u/ErnieAlex)\
**Post date:** [August 2, 2021, 12:52pm UTC](https://www.phphelp.com/t/font-problem-creating-pdf-file-with-mpdf/33773/13 "2021-08-02T12:52:29Z")

</div>

Well, just log into your control panel or use FTP. Putty is hard to use for some things.  
I never use it these days. I just use a FTP client most of the time. But, you can log into the control  
panel and use the file-manager in there to handle files.

I have never heard of a server putting files back up when they are removed. Composer is a package  
control system. So, it might not really be removing the actual files. Not sure on that part!

---

<div class="post-metadata">

**Author:** ![Adem](https://www.phphelp.com/letter_avatar_proxy/v4/letter/a/a4c791/32.png) [@Adem](https://www.phphelp.com/u/Adem)\
**Post date:** [August 2, 2021, 1:27pm UTC](https://www.phphelp.com/t/font-problem-creating-pdf-file-with-mpdf/33773/14 "2021-08-02T13:27:03Z")

</div>

I can’t delete the vendor folder with FTP, it says you don’t have permission  
I’m deleting from cPanel and Directory management, When I refresh the page it comes back again  
I am using VPS from MochaHost  
They couldn’t remove the vendor folder even after technical support.

This “Composer” is such a damn thing

---

<div class="post-metadata">

**Author:** ![ErnieAlex](https://www.phphelp.com/letter_avatar_proxy/v4/letter/e/bc8723/32.png) [@ErnieAlex](https://www.phphelp.com/u/ErnieAlex)\
**Post date:** [August 2, 2021, 1:53pm UTC](https://www.phphelp.com/t/font-problem-creating-pdf-file-with-mpdf/33773/15 "2021-08-02T13:53:37Z")

</div>

I never use Composer. I always download the zipped version and install it myself. That way, you can just use FTP to delete it if needed. And, I have heard others complain about using mPDF. I had bad luck with it in the past, too. The domPDF seems to work well for me. Let me know how it works out. I would test domPDF locally first to see if it does what you need.

---

<div class="post-metadata">

**Author:** ![Adem](https://www.phphelp.com/letter_avatar_proxy/v4/letter/a/a4c791/32.png) [@Adem](https://www.phphelp.com/u/Adem)\
**Post date:** [August 3, 2021, 8:59am UTC](https://www.phphelp.com/t/font-problem-creating-pdf-file-with-mpdf/33773/16 "2021-08-03T08:59:01Z")

</div>

I told the server administrator to delete or disable composer. They said we renamed .composer in the root directory, but I still can’t delete the vendor folder, it comes back.  
😇 🤯

---

<div class="post-metadata">

**Author:** ![Adem](https://www.phphelp.com/letter_avatar_proxy/v4/letter/a/a4c791/32.png) [@Adem](https://www.phphelp.com/u/Adem)\
**Post date:** [August 3, 2021, 10:59am UTC](https://www.phphelp.com/t/font-problem-creating-pdf-file-with-mpdf/33773/17 "2021-08-03T10:59:29Z")

</div>

I’m trying to try domPDF  
There are two problems first.

1. How to add UTF-8 for non-latin characters // $options-\>set(‘defaultFont’, ‘DejaVu Sans’); The character issue is gone but I still want to add UTF-8

2. How to include the css file  
I looked on the internet and tried something but no success

I couldn’t get codes code tag

---

<div class="post-metadata">

**Author:** ![ErnieAlex](https://www.phphelp.com/letter_avatar_proxy/v4/letter/e/bc8723/32.png) [@ErnieAlex](https://www.phphelp.com/u/ErnieAlex)\
**Post date:** [August 3, 2021, 1:05pm UTC](https://www.phphelp.com/t/font-problem-creating-pdf-file-with-mpdf/33773/18 "2021-08-03T13:05:33Z")

</div>

Well, actually, UTF-8 is the HTML part, not the domPDF part. Therefore use code like this…  
( Just an example from their site. )

```auto
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<style>
  body { font-family: DejaVu Sans, sans-serif; }
</style>
<title>č s š Š</title>
</head>
<body>
  <p>??????X Ponuka číslo € černý Češký <br></p>
</body>
</html>

```

You basically, just need to change the META for UTF-8 and make sure you insert the CSS for the font.

---

<div class="post-metadata">

**Author:** ![ErnieAlex](https://www.phphelp.com/letter_avatar_proxy/v4/letter/e/bc8723/32.png) [@ErnieAlex](https://www.phphelp.com/u/ErnieAlex)\
**Post date:** [August 3, 2021, 1:09pm UTC](https://www.phphelp.com/t/font-problem-creating-pdf-file-with-mpdf/33773/19 "2021-08-03T13:09:26Z")

</div>

Here it the link about this. There are several other ways people put code in to fix it. One of them might work well for you, too. [UTF-8 for domPDF](https://stackoverflow.com/questions/16384517/dompdf-character-encoding-utf-8)

---

<div class="post-metadata">

**Author:** ![Adem](https://www.phphelp.com/letter_avatar_proxy/v4/letter/a/a4c791/32.png) [@Adem](https://www.phphelp.com/u/Adem)\
**Post date:** [August 4, 2021, 9:18am UTC](https://www.phphelp.com/t/font-problem-creating-pdf-file-with-mpdf/33773/20 "2021-08-04T09:18:33Z")

</div>

The only option for non-latin characters is the following font  
`$options->set('defaultFont', 'DejaVu Sans');`  
For UTF-8, just like the example you gave into the html page  
I couldn’t find an option for CSS file include or the options I found didn’t work  
It works just by adding the css code inside the HTML page by enclosing the style tag, this solution is not right for me as i have multiple pages, because when a change in style code is needed, it will be necessary to go through all the pages

I couldn’t add a logo image  
i tried to try the options as i found and understand but i was not successful,I even tried converting the logo to base64, again without success.  
With the base64 variable, the logo image appears on the normal HTML page, while the PDF page does not have the logo image with the same variable. `"Image not found or type unknown"`

I’m trying to add a 45 degree text watermark without success for now, I’m still searching

[Next page](https://www.phphelp.com/t/font-problem-creating-pdf-file-with-mpdf/33773.md?page=2)
