Font problem creating pdf file with mPDF

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…

Thank you very much for your attention
I did a 5 page experiment and it only added a watermark to the last page
I added a perfect watermark with the source code whose URL I gave above. Also, I placed a logo at the bottom of the page, added page numbers per page.
So I think the code I gave the url is perfect.


Thank you very much again

Sponsor our Newsletter | Privacy Policy | Terms of Service