Support » Plugin: WooCommerce PDF Invoices & Packing Slips » Problems with Custom Fonts

Viewing 6 replies - 1 through 6 (of 6 total)
  • Plugin Author Ewout

    (@pomegranate)

    You’re defining “Droid Sans Fallback” but then you’re setting the primary font family to “DejaVu Sans”. This is in fact a font that is included in dompdf (as you probably knew), so it will use that and discard ‘Droid Sans Fallback’. dompdf does not have a concept of fallback fonts, which the name suggests you were trying to achieve?

    Thread Starter 1000camels

    (@1000camels)

    Perhaps I am misunderstanding what is happening in the PDF context. Can I only use 1 font? Why does Chinese render correctly in html?

    Anyway, I removed the “Dejavu Sans” and now every character (except headings) are displaying a square. Clearly something is not right in my environment.

    Plugin Author Ewout

    (@pomegranate)

    Can I only use 1 font?

    You can use multiple fonts, but an element can only have 1 font.

    Why does Chinese render correctly in html?

    Because your operating system can use system fonts (or secondary fonts specified in CSS) as a fallback. PDF is a self-contained format (the “P” stands for “Portable”) that does not use system fonts because that would let the PDF render differently depending on what’s installed on the system. Additionally, dompdf does not support character substitution so even if you embed the fallback font, it is not able to utilize it.

    I don’t know why your Droid Sans Fallback shows squares, normally this is an indication that those characters are not part of the font you have included.

    To save yourself some trouble you could try WooCommerce PDF Invoices & Packing Slips + mPDF, an extension that replaces dompdf with mpdf (you need the CJK release for support for Chinese).

    Thread Starter 1000camels

    (@1000camels)

    I wish that extension had a readme. I am not sure what ‘CJK release’ means.

    I looked further at the html that is printed when you set Output to HTML. The chinese is an entity (黃大仙下邨). Is that normal?

    Plugin Author Ewout

    (@pomegranate)

    I am not sure what ‘CJK release’ means.

    There are 2 downloads on that release, one .CJK.zip and one .zip. The CJK version includes fonts for Chinese Japanese and Korean (and is huge for that reason). There are no settings to the plugin – you just install it and that’s it! Chinese will then be supported out of the box. Custom fonts are much more difficult with mpdf though, so this will only serve as a solution if you need Chinese and character substitution.

    The chinese is an entity (黃大仙下邨). Is that normal?

    I don’t know Chinese, but I ran that through google translator and it tells me that it means “Lower Wong Tai Sin” which seems to be part of an address? What do you mean by ‘entity’?

    Thread Starter 1000camels

    (@1000camels)

    I found a solution which I wanted to share.
    I do not know if this example could be incorporated in your code, or at the least shared for others trying to deal with mixed character encodings in their invoices.

    /**
     * This little piece of code wraps all Chinese characters in a span tag 
     * identifying them, so we can define an alternate font for them
     * 
     * Came from Answer #7 https://www.py4u.net/discuss/27958
     */
    add_filter( 'wpo_wcpdf_before_dompdf_render', function( $dompdf, $html ) {
    	$html = preg_replace("/(\p{Han}+)/u", '<span class="cjk">$0</span>', html_entity_decode($html));
    
    	// reset html
    	$dompdf->loadHtml( $html );
    
    	return $dompdf;
    }, 10, 2 );

    It is worth noting that I had to decode the entities in order to identify the Chinese characters.

Viewing 6 replies - 1 through 6 (of 6 total)
  • You must be logged in to reply to this topic.