Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to change font? #1208

Closed
asratnani opened this issue Jun 3, 2016 · 32 comments
Closed

How to change font? #1208

asratnani opened this issue Jun 3, 2016 · 32 comments
Labels

Comments

@asratnani
Copy link

i am trouble to change to font when i export data as a PDF.
please let me know as soon as possible how to fix this issues.

@bsweeney
Copy link
Member

bsweeney commented Jun 3, 2016

What have you tried? Some sample HTML/CSS would help determine the source of your problem.

@asratnani
Copy link
Author

asratnani commented Jun 3, 2016

I had put this code in dompdf/lib/php-font-lib/www/font_info.php

@font-face {
  font-family: 'Helvetica';
  font-weight: normal;
  font-style: normal;
  font-variant: normal;
  src: url('<?php echo $fontfile; ?>'); 
}

this is enough for ref?

@bsweeney
Copy link
Member

bsweeney commented Jun 3, 2016

$fontfile is the path to the TTF? In addition you need to add a format attribute to the source:

@font-face {
  font-family: 'Helvetica';
  font-weight: normal;
  font-style: normal;
  font-variant: normal;
  src: url('<?php echo $fontfile; ?>') format("truetype");
}

@asratnani
Copy link
Author

Yes $fontfile is path of the ttf font file..
Can you please give me full html ?
Because of i cant getting you..

Thanks bsweeney.

@bsweeney
Copy link
Member

bsweeney commented Jun 3, 2016

Something like this should work


<html>
<head>
  <style>
  @font-face {
    font-family: 'Elegance';
    font-weight: normal;
    font-style: normal;
    font-variant: normal;
    src: url("http://eclecticgeek.com/dompdf/fonts/Elegance.ttf") format("truetype");
  }
  body {
    font-family: Elegance, sans-serif;
  }
  </style>
</head>
<body>
  <p>hello world</p>
</body>
</html>

@asratnani
Copy link
Author

Thanks .. Let me try this .. i will know you..

@asratnani
Copy link
Author

i try this but now working yet..
can you join team viewer or skype?
its is good or me if yes..

@bsweeney
Copy link
Member

bsweeney commented Jun 4, 2016

A few other things to check:

  1. Are you using the latest version of dompdf (currently 0.7.0)?
  2. Does dompdf have read/write access to the temporary directory (DOMPDF_TEMP_DIR in 0.6.2 or earlier, $dompdf->set_option('temp_dir', '') in 0.7.0 or newer)?
  3. Are you calling both $dompdf->output() and $dompdf->stream() in the same script? This can break font rendering.

Might help to see your dompdf-related code.

@st-aleksandar
Copy link

Hi, I have the same problem with fonts. Also, <strong> and <b> do not work in pdf.

$dompdf = new \Dompdf\Dompdf();
    $dompdf->setPaper("A4");

    //settings
    $options = new Dompdf\Options();
    $options->setIsRemoteEnabled(true);
    $options->setDpi(100);
    $options->setIsHtml5ParserEnabled(true);
    $options->setIsJavascriptEnabled(true);
    $options->setIsPhpEnabled(true);
    $dompdf->setBasePath(get_template_directory() . '/style.css');

    $options->setIsFontSubsettingEnabled(true);
    $dompdf->setOptions($options);

    //$dompdf->output();



    // load the html content
    $content = '<!DOCTYPE html><html><head>';
    $content .= '<link rel="stylesheet" src="lib/bootstrap/css/bootstrap.css" type="text/css"/>';
    //$content .= '<link rel="stylesheet" href="'. get_template_directory_uri() .'/style.css" type="text/css" />';
    $content .= '<link rel="stylesheet" src="style.css" type="text/css" />';
    $content .= '<style>';
    $content .= '
    @font-face {
        font-family: "fontbold";
        font-style: normal;
        font-weight: bold;
        src: url("DejaVuSerif-Bold.ttf") format("truetype");
    }';
    $content .= '<div id="header">'. $subtitle . '</div>' . $html . '</body></html>';

    //write to file
//    $myfile = fopen("newfile.html", "w") or die("Unable to open file!");
//    $txt = $content;
//    fwrite($myfile, $txt);
//    fclose($myfile);

    $dompdf->loadHtml($content);
    $dompdf->render();
    $dompdf->setBasePath(get_template_directory_uri() . '/lib/bootstrap/css/bootstrap.csss');
    $canvas = $dompdf->getCanvas();

    $canvas->page_text(28, 10, $title,'', 18, array(0,0,0)); #header
    //$canvas->page_text(28, 35, $solution . ' ' . $feature, '', 10, array(0,0,0)); #header
    $canvas->page_text(28, 800, "Copyright ©2016 GPS Insight", '', 8, array(0,0,0)); #copyright

    $canvas->page_script('
        $pdf->image(get_stylesheet_directory()."/helpcenterlogo.png",487,10,80,35);#logo
        $pdf->line(28,60,567,60,array(0,0,0),1); #header hr
        $pdf->line(28,795,567,795,array(0,0,0),1); #footer hr
    ');

    $canvas->page_text(500, 800, "Page: {PAGE_NUM} of {PAGE_COUNT}", '', 8, array(0,0,0)); #footer

    //$dompdf->setOptions('defaultFont', 'Courier');

    $dompdf->stream($title);

@bsweeney
Copy link
Member

bsweeney commented Jun 5, 2016

@aleksandar-irvas perhaps your sample was just for demonstration, if that's the case maybe my comments are not on point. If so follow up with a bit more information. First, a few things I noticed about your sample code:

  1. You don't need to enable javascript support. That's actually for inserting JS into the rendered PDF.
  2. You don't need to enable PHP support. That's only needed if you're running embedded script (e.g. in the body of the HTML you have some php surrounded by <script type="text/php">...</script>).
  3. The base path works a bit differently from the HTML meta tag. Pass in only a the directory path.

Second, since you're using a DejaVu font you don't need to use an @font-face declaration. The @font-face declaration is only to load a font that dompdf can then use when rendering your document. To actually use the font you would specify it in a font-family declaration (e.g. body { font-family: DejaVu Serif, serif; }).

@st-aleksandar
Copy link

st-aleksandar commented Jun 5, 2016

@bsweeney Thanks for replay, this was just a sample. But I noticed that same code in my localhost bolds <strong> attributes, but on the server strong do not work.

@bsweeney
Copy link
Member

bsweeney commented Jun 6, 2016

@aleksandar-irvas I would check that dompdf has read/write access to the temp directory, font directory, and font cache directory.

@cocomo
Copy link

cocomo commented Jun 7, 2016

Same problem over here trying to change Helvetica to Avenir:

  1. Copied Avenir-Roman.ttf and the rest of the family to dompdf/fonts/. I used TransType to convert the original .ttc to .ttf

  2. Added
    `,

    'Avenir' =>
    array(
    'bold' => $distFontDir . 'Avenir-Medium',
    'bold_italic' => $distFontDir . 'Avenir-MediumOblique',
    'italic' => $distFontDir . 'Avenir-BookOblique',
    'normal' => $distFontDir . 'Avenir-Roman'
    )`
    to dompdf_font_family_cache.dist.php

  3. Changed css style definition for body to Avenir or Avenir-Roman.

Found some advice on StackOverflow, but could not get it to work:

http://stackoverflow.com/questions/12581156/custom-fonts-for-dompdf

2013 is probably outdated? Or do I still need to create the .afm files as advised on stackoverflow?

I want to go after file privileges in temp and font cache directory but dont know where to set these. DejaVu renders fine, I'm using dompdf 0.7.0

@bsweeney
Copy link
Member

bsweeney commented Jun 7, 2016

@cocomo just FYI instead of manually setting up the font you can use an @font-face declaration or the load_font.php script from the utils projects.

That being said, You do need a font metrics file. AFM is for fonts that use Windows ANSI encoding, UFM is for fonts that use Unicode encoding. For all intents and purposes Dompdf uses only UFM. You can get the font metrics using php-font-lib or some other font processing tool, but probably the easiest way to get everything set up is to use one of the methods I mentioned above.

@cocomo
Copy link

cocomo commented Jun 7, 2016

@bsweeney Thank you for the clarification, i finally got Avenir showing up with help of the load_font command. Since Avenir has 6 different weights i'm wondering how to set these up für dompdf? Should I create 3 custom font names, each with custom b/i/bi relations, or is there a way to keep the family bundled and to target individual weights in a css style, like font-weight: 600?

@bsweeney
Copy link
Member

bsweeney commented Jun 7, 2016

Unfortunately Dompdf doesn't currently support numeric weights (see #675). The work around would be, as you stated, to define each weight as a separate family in your stylesheet.

@lippoliv
Copy link

Hey all,
today I added "GosmickSans" via the load_font.php Script. I checked the dompdf_font_family_cache.dist.php and it was added:

'gosmick-sans' => array(
    'normal' => DOMPDF_FONT_DIR . 'GosmickSans',
    'bold' => DOMPDF_FONT_DIR . 'GosmickSansBold',
    'italic' => DOMPDF_FONT_DIR . 'GosmickSansBoldOblique',
    'bold_italic' => DOMPDF_FONT_DIR . 'GosmickSansOblique',
  ),

While returning PDFs Content via an view as HTML, all Looks good, the Font is used. But as soon as I render PDF and open it, the font won't be used?

It's an Laravel-based Project if that is an usefull Information. It's dompdf 0.6.*

@bsweeney
Copy link
Member

@lippoliv do you have a sample of the HTML?

@lippoliv
Copy link

@bsweeney here's the HTML (not really pretty, I know)

<!doctype html>
<html lang="de">
    <head>
        <meta charset="UTF-8">
        <meta name="viewport"
              content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
        <meta http-equiv="X-UA-Compatible" content="ie=edge">
        <title>Document</title>

        <style>
            @page  {
                margin: 0;
                padding: 0;
            }

            td {
                padding: 0;
                spacing: 0;
            }
        </style>
    </head>

    <body style="width: 100%;">

        <table>
            <tr>


                    <td>
                        <div style="
    position: relative;
    float: left;
    width: 259px;
    height: 156px;
    margin: 1px;
    padding: 0;
    font-family: gosmick-sans;
">
            <div style="
        font-size: 7pt;
        position: absolute;
        top: 9px;
        left: 15px;
        heigth: 8pt;
        border-bottom: 1px solid #000;
    ">abc</div>

    <div style="
        font-size: 11pt;
        position: absolute;
        top: 30px;
        left: 15px;
        margin: 0;
        padding: 0;
    ">
        xyz
    </div>
    <div style="
        font-size: 6pt;
        position: absolute;
        right: 15px;
        top: 30px;
        color: #CCC;
    ">200</div>
</div>                    </td>

                                                </tr>
        </table>
    </body>
</html>

@bsweeney
Copy link
Member

Not pretty is ok ... certainly better for tracking down possible issues ;)

@lippoliv
Copy link

Here's the font if it helps you analysing (just normal font, not bold etc) GosmickSans.zip

@bsweeney
Copy link
Member

I don't see anything out of the ordinary, and a quick test seems to work. If you remove absolute positioning does that help? What if you set gosmick-sans globally in your stylesheet, e.g. * { font-family: gosmick-sans, sans-serif; }?

@lippoliv
Copy link

OK @bsweeney you answered it years ago #782

Regarding Laravel I only had to create folder storage/fonts and drop in all font-files and the dompdf_font_family_cache.php because of vendor/barryvdh/laravel-dompdf/config/dompdf.php rewrites the DOMPDF_FONT_DIR

👍

@bsweeney
Copy link
Member

Nice. I haven't used laravel-dompdf much but it seems to be quite popular. Glad to know it makes font installation easy.

@kasra-appscore
Copy link

kasra-appscore commented Jul 11, 2017

Non of them worked for me :-(
Dose anyone have any other solution?

@bsweeney
Copy link
Member

@kasra-appscore we can discuss your issue over on the support forum. Remember to provide details on what you've tried and the results.

@MauricioGO
Copy link

MauricioGO commented Jul 20, 2017

This work for me.

table {
	border-collapse: collapse;
	border-spacing: 0;
	font-family:Arial!important;
}

try to put > !important

@biancaro
Copy link

biancaro commented Dec 27, 2017

@MauricioGO, you're a lifesaver. That did the trick. Thanks!

@thewebdevelopment
Copy link

ہزمح ہزمح ہزمح font get loaded but this is how it is showing every letter is separated in urdu
font I am using https://urdufonts.net/fonts/jameel-noori-nastaleeq-kasheeda

Why every letter is separated?

@bsweeney
Copy link
Member

Dompdf does not currently perform any complext text layout (such as shaping). Follow #2619 for updates. For now you will need to pre-process your text in order for it to

@asifrashid09
Copy link

i want to filter pdf of filtered data fro my table so i used this query
$this->applicantawardsheets = User_Details::where('advertisement_no',$this->advertisement_no)
->where('post_applied',$this->post_applied)
->get();
but is not working . because $this->advertisement_no return null.

i watched all tutorials on dompdf but all used ::all() query .

@bsweeney
Copy link
Member

@asifrashid09 your question appears unrelated and not necessarily a bug. You should start a new discussion, though the doesn't really seem specific to Dompdf so it may be better to use a site like StackOverflow.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

10 participants