Skip to content
Advertisement

PHP TCPDF – Half-width kana is being considered as Full-width

I’m having a problem with rendering a string of text that contains half-width kana in a PDF. It considers the half-width kana to be full-width so it turns out something like this:

Half-width kana being considered as full-width

This is my code snippet:

PDF::Cell(15, 6, '商品コード', 1, 0, 'C', 0, '', 0);

I’m also using the cid0jp font provided in TCPDF to display Japanese characters:

PDF::SetFont('cid0jp', 'B', 9);

In the end, I want it to maintain the half-width katakana to fit the cell and remove unnecessary spaces.

TCPDF Library used: https://tcpdf.org/

Advertisement

Answer

When you use the cid0jp font you’re leaving the font rendering up to the PDF reader which can introduce differences in rendering between different readers and operating systems. The spacing differences can be pretty major, but I’m not sure if that’s an issue with TCPDF’s implementation or just a consequence of relying on the reader to provide the font.

Below, I’ve included an example comparing Microsoft Edge and Foxit Reader rendering of that text in cid0jp. I also included the full-width versions on the second line. Edge came a little closer on the spacing for half-widths than Foxit. Google Drive’s PDF preview did the same thing as Foxit did with the additional spacing around the half-widths.

Since the space you’re working with there is so tight, it might be worth embedding a specific font into the document. In my tests that was a lot more reliable as far as rendering went. (I’ve also included screenshots of that test below. Make sure subsetting is on if you don’t want the entire font included in each file.)

Just in case you might not know how to do that:

$embfont = TCPDF_FONTS::addTTFfont('/Path/to/font.ttf', 'TrueTypeUnicode', '', 32);
$pdf->setFont($embfont, '', '9');
$pdf->Cell(15,6,'商品コード',1,0,'C',0,'',0);

Examples with cid0jp:

Example of cid0jp rendering differences

Examples with embedded font:

(Admittedly, this font isn’t very good at small sizes.)

Examples with embedded font

User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement