How to generate pdf files _with_ utf-8 multibyte characters using Zend Framework

后端 未结 4 1216
天命终不由人
天命终不由人 2020-12-16 06:30

I\'ve got a \"little\" problem with Zend Framework Zend_Pdf class. Multibyte characters are stripped from generated pdf files. E.g. when I write aąbcčdeę it becomes abcd wit

相关标签:
4条回答
  • 2020-12-16 07:08

    Zend_Pdf supports UTF-8 in version 1.5 of Zend Framework. However, the standard PDF fonts support only the Latin1 character set. This means you can't use Zend_Pdf_Font::FONT_TIMES_BOLD or any other "built-in" font. To use special characters you must load another TTF font that includes characters from other character sets.

    I use Mac OS X, so I tried the following code and it produces a PDF document with the correct characters.

    $pdfDoc = new Zend_Pdf();
    $pdfPage = $pdfDoc->newPage(Zend_Pdf_Page::SIZE_LETTER);
    
    // load TTF font from Mac system library
    $font = Zend_Pdf_Font::fontWithPath('/Library/Fonts/Times New Roman Bold.ttf');
    $pdfPage->setFont($font, 36);
    
    $unicodeString = 'aąbcčdeę';
    $pdfPage->drawText($unicodeString, 72, 720, 'UTF-8');
    
    $pdfDoc->pages[] = $pdfPage;
    $pdfDoc->save('utf8.pdf');
    

    See also this bug log: http://framework.zend.com/issues/browse/ZF-3649

    0 讨论(0)
  • 2020-12-16 07:09

    Have you made sure that you are setting the character encoding as this example from the manual?

    // Draw the string on the page
    $pdfPage->drawText($unicodeString, 72, 720, 'UTF-8');
    

    If you're stuck into having to use a bold font, maybe try one of the other bold fonts?

    Zend_Pdf_Font::FONT_COURIER_BOLD
    Zend_Pdf_Font::FONT_TIMES_BOLD
    Zend_Pdf_Font::FONT_HELVETICA_BOLD
    
    0 讨论(0)
  • 2020-12-16 07:12

    ZF v. 1.6, TIMES_BOLD (as I understand thats the only way to make text bold?)

    0 讨论(0)
  • 2020-12-16 07:19

    I believe Zend_Pdf got UTF-8 support in 1.5 - What version of Zend Framework are you running?

    Also - what font are you trying to render with? Have you tried alternate fonts?

    0 讨论(0)
提交回复
热议问题