Jump to content

How to use Use google font in dompdf


vikaspa

Recommended Posts

Dear All

Code generated from 

https://github.com/dompdf/dompdf/issues/2481

 

I am using dompdf 2.0

Main program to create pdf

https://prajaktasoftware.com/onlineexam/vendor-special-message.php <- input html file forms input to generated PDF

https://prajaktasoftware.com/onlineexam/testfont-1.php <- program that generates pdf using above program

I am using dompdf (testfont-1.php)

the output generated shows ??? characters pls help

<?php
ob_start();
include 'vendor-special-message.php'; // pdf generatef from this file
$_template = ob_get_clean();
 
 require( 'dompdf/autoload.inc.php'); 
 

use Dompdf\Dompdf;

 $dompdf=new Dompdf();

 //$dompdf->loadHtml($_template, "UTF-8");

 $dompdf->loadHtml($_template, "UTF-8");

 

 $dompdf->setPaper("A4", "portrait");

 $dompdf->set_option('isFontSubsettingEnabled', true);
 $dompdf->set_option('defaultMediaType', 'all');
$dompdf->set_option('isRemoteEnabled', true);
 $dompdf->render();
$dompdf->stream( time() . ".pdf", array("Attachment" => false));
// $dompdf->stream(); // stream the pdf in browser
 
exit;
 ?>

 

 

vendor-special-message.php

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; UTF-8">
    <style>
@import url('https://fonts.googleapis.com/css2?family=Hind:wght@300&display=swap');


@import url('https://fonts.googleapis.com/css2?family=Oswald:wght@200&display=swap');

div {font-family: 'Hind', sans-serif;}  
 
    body {
           font-family: 'Oswald', sans-serif
        }
    </style>
 
</head>
<body>
    <p>Happy Birthday</p> <p>Happy Birthday</p> <p>Happy Birthday</p>
    <div>सभ मानव जन्मतः स्वतन्त्र अछि तथा गरिमा आʼ अधिकारमे समान अछि। सभकेँ अपन–अपन बुद्धि आʼ विवेक छैक आओर सभकेँ एक दोसराक प्रति सौहार्दपूर्ण व्यवहार करबाक चाही।</div>
 
</body>
</html>

 

Edited by vikaspa
Link to comment
Share on other sites

In the documentation about external fonts, it specifically provides an example using google fonts.

Try adding this to your initialization of dompdf:

$tmp = sys_get_temp_dir();

$dompdf = new Dompdf([
    'logOutputFile' => '',
    // authorize DomPdf to download fonts and other Internet assets
    'isRemoteEnabled' => true,
    'isFontSubsettingEnabled' => true,
    'defaultMediaType' => 'all',
    // all directories must exist and not end with /
    'fontDir' => $tmp,
    'fontCache' => $tmp,
    'tempDir' => $tmp,
    'chroot' => $tmp,
]);

$dompdf->setPaper("A4", "portrait");
$dompdf->loadHtml($_template, "UTF-8");
$dompdf->render();
$dompdf->stream(time() . ".pdf", array("Attachment" => false));

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.