fkrauthan / wp-mpdf

Print WordPress posts as PDF. Optional with Geshi highlighting.
55 stars 23 forks source link

How to add custom font to PDF? #28

Closed nijatmursali closed 1 year ago

nijatmursali commented 1 year ago

Hello,

I'm currently working on a project and using this WordPress plugin. It works perfectly fine and I'm able to generate PDF with selected theme which is basically PHP code that includes HTML and CSS. I want to change the font family to custom one like:

body {
    padding: 0;
    margin: 0;
    height: 100%;
    width: 100%;
    font-family: "EuclidCircularB Regular";
}

but it does not work. The font is installed and added correctly. Is there any way to add this custom font to PDF?

My PDF generation code looks like this:


if (have_posts()) :
    while (have_posts()) : the_post();
        $relative_url = untrailingslashit(ABSPATH) . wp_make_link_relative(get_the_post_thumbnail_url());
        $pdf_output .= pdf_header();
        $pdf_output .= pdf_images($relative_url);
        // $pdf_output .= pdf_additional_images();
        $pdf_output .= pdf_description(the_title('', '', false), $post);
        $pdf_output .= pdf_header_next();

        $blocks = parse_blocks($post->post_content);

        $specifications = '';
        foreach ($blocks as $block) {
            if ('acf/metratec-specifications' === $block['blockName']) {
                $specifications .= render_block($block);
            }
        }

        $pdf_output .= pdf_specifications($specifications);
        $similar = '';
        foreach ($blocks as $block) {
            if ('acf/metratec-related-products' === $block['blockName']) {
                $similar .= render_block($block);
            }
        }

        $pdf_output .= pdf_similar($similar);
        $pdf_output .= pdf_ending();

    endwhile;

Functions are basically returning HTML content as a string. Is there any way to specify the specific font?

fkrauthan commented 1 year ago

You need to follow the mpdf instructions for adding custom fonts. I also recommend to use the mpdf_adjust_settings filter to modify the mpdf instance and to call the AddFontDirectory method (as if you place the font file inside the plugin folder it would be overwritten during an update).