Font problem creating pdf file with mPDF

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

I managed to add the css file as below in the HTML file.
But I don’t think it makes sense (amateurish) ;))

<!DOCTYPE html>
<html lang="tr">
  <head>
<?php 
        $protocol = isset($_SERVER["HTTPS"]) ? 'https' : 'http';
        $site_url = $protocol."://".$_SERVER["SERVER_NAME"];
?>
    <meta charset="utf-8">
  <link rel="stylesheet" type="text/css" media="screen" href="<?php echo $site_url; ?>/dompdf/pdf.css" />
    </head>
        <body>
<table class="customTable">

I am sending the logo image in pdf file with the url link

Well, I am not sure what you mean, Adem. I have placed logos in PDF’s using domPDF and have not had any problems. I just place it in a DIV as an IMG tag in the HTML before it is sent to the domPDF and it works fine. Have you tried just inserting the CSS like normal? WITHOUT A LINK?

Your CSS file is something that just has CSS code in it. No STYLE tags. So, just include it after the < BODY > tag like this:

<style>
<?PHP include("your-css-file-name.css"); ?>
</style>

And, that should include it into the HTML as a stylesheet. Quite often using links do not work without pointing correctly to the file. If you use a link, you must use the real servername/foldername/filename. For example; " www.ernie.com/application/css/my-css-filename.css" It has to include a valid server.
You are using an odd protocol variable which may not be getting the correct URL…

1 Like

no style tag in css
It works when added with “<style></style>” tag inside the html page
I ran logo image and css file with url address
I was able to add watermark using code at this url: #1 How to Generate PDF with Watermark in PHP using Dompdf | Tutorialswebsite
watermark text becomes long or short according to user, I want it to be centered vertically and horizontally with -45 degrees, but I couldn’t find a solution for now
With the value here it is possible to set the text position. But if it’s fixed text it’s ok but if text length changes text position changes

$txtHeight = $fontMetrics->getFontHeight($font, 150); 
$textWidth = $fontMetrics->getTextWidth($text, $font, 40); 

The include example you gave is more correct, it never occurred to me :grinning: :grinning:
They say that the mind is superior to the mind. :grinning: :grinning: :grinning:

I created a pdf file with domPDF in local without any problems.
now it’s time let’s see the remote server how it will be

pdf file creation with domPDF on remote server was successful
@ErnieAlex thank you for suggesting domPDF and for your help
:+1: :+1: :+1:

Great! I am glad to help, as always!

One thing, you can make a watermark by adding it as an image to the body tag.
You can take the image you want to use and fade it so it is low quality and then display in on the page.
If you set the body tag to have a background image in CSS it will work. But, sometimes it is tricky to get it at the correct fade values. Try adding it as a background image to the < body > tag.
You need to center it and make it a set size… Should work.

Not possible to watermark with image
The reason is, each member enters text for the watermark in their profile and each member has their own watermark text in their pdf file.
I don’t know if there is a possibility to convert these texts to pictures automatically.

Watermark text fade problem occurred
If the PDF is two pages, the text is fade on each page, but if it is more than two pages, the first and last page watermark text is fade, but the middle pages are in full black.
To fade watermark text $canvas->set_opacity(0.1,"Multiply");
In the source code the color was red I made it null $canvas->page_text($x, $y, $text, $font, 150, $color = array(null,null,null), $word_space = 0.0, $char_space = 0.0, $angle = -55.0);


I don’t know which phrease to search for this problem

https://www.codesenior.com/en/tutorial/Dompdf--Create-Watermark-and-Page-Numbers
I solved this problem with the code in this url

I am leaving for the day and will be gone for many hours. But, I searched quickly for a solution for you.
Here is a sample of adding a text-watermark using the domPDF library.

// Reference the Dompdf namespace 
use Dompdf\Dompdf; 
// Reference the Options namespace 
use Dompdf\Options; 
// Reference the Font Metrics namespace 
use Dompdf\FontMetrics; 
 
// Set options to enable embedded PHP 
$options = new Options(); 
$options->set('isPhpEnabled', 'true'); 
 
// Instantiate dompdf class 
$dompdf = new Dompdf($options); 
 
// Load HTML content 
$dompdf->loadHtml('<h1>Welcome to Adem's site!</h1>'); 
 
// (Optional) Setup the paper size and orientation 
$dompdf->setPaper('A4', 'landscape'); 
 
// Render the HTML as PDF 
$dompdf->render(); 
 
// Instantiate canvas instance 
$canvas = $dompdf->getCanvas(); 
 
// Instantiate font metrics class 
$fontMetrics = new FontMetrics($canvas, $options); 
 
// Get height and width of page 
$w = $canvas->get_width(); 
$h = $canvas->get_height(); 
 
// Get font family file 
$font = $fontMetrics->getFont('times'); 
 
// Specify watermark text 
$text = "CONFIDENTIAL"; 
 
// Get height and width of text 
$txtHeight = $fontMetrics->getFontHeight($font, 75); 
$textWidth = $fontMetrics->getTextWidth($text, $font, 75); 
 
// Set text opacity 
$canvas->set_opacity(.2); 
 
// Specify horizontal and vertical position 
$x = (($w-$textWidth)/2); 
$y = (($h-$txtHeight)/2); 
 
// Writes text at the specified x and y coordinates 
$canvas->text($x, $y, $text, $font, 75); 
 
// Output the generated PDF (1 = download and 0 = preview) 
$dompdf->stream('document.pdf', array("Attachment" => 0));

This is a full sample to test with, so put it into a separate file and try it. It if works, then move it to your live code. Good luck! Will check back in later in my day…

Sponsor our Newsletter | Privacy Policy | Terms of Service