Font problem creating pdf file with mPDF

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?

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']);

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…

//  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!

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

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);

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.

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

Well, the Android font is really called " Roboto ". It is actually available from 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

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.

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);

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…

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

Local computer

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

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

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?

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!

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

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.

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.
:innocent: :exploding_head:

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

    require_once ‘dompdf/autoload.inc.php’;
    ob_start();
    include("…/fiber_fiyat_teklifi_tablo_pdf.php");
    $html = ob_get_clean();

    use Dompdf\Dompdf;
    use Dompdf\Options;

    $options = new Options();
    $options->set(‘defaultFont’, ‘DejaVu Sans’);
    $dompdf = new Dompdf($options);

    $dompdf->loadHtml($html);

    // (Optional) Setup the paper size and orientation
    $dompdf->setPaper(‘A4’, ‘landscape’);

    // Render the HTML as PDF
    $dompdf->render();
    $dompdf->set_base_path(DIR . ‘/style.css’);
    $output = $dompdf->output();

    $fh = fopen($pdf_dosya_adi, ‘w’) or die(“PDF Dosya açılmıyor”);
    $veri = base64_encode($output);
    fwrite($fh, $veri);
    fclose($fh);

I couldn’t get codes code tag

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

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

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

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

Sponsor our Newsletter | Privacy Policy | Terms of Service