简体   繁体   中英

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:

半角假名被认为是全角

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/

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:

cid0jp渲染差异示例

Examples with embedded font:

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

嵌入字体的例子

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM