【问题标题】:PoDoFo c++ PDF library, polish charactersPoDoFo c++ PDF库,波兰字符
【发布时间】:2015-01-13 17:37:14
【问题描述】:

我在 PoDoFo 库中编码波兰语字符时遇到问题。

此代码生成单词“Łódź”的无效编码

#include <podofo/podofo.h>

using namespace PoDoFo;

int main(int argc, char *argv[], char *env[]) {
    PdfStreamedDocument document("polish.pdf");
    PdfPainter painter;
    PdfPage* pPage;


    pPage = document.CreatePage( PdfPage::CreateStandardPageSize( ePdfPageSize_A4 ) );
    painter.SetPage( pPage );
    PdfFont* pFont = document.CreateFont("Helvetica");
//  PdfFont* pFont = document.CreateFont("Helvetica", new PdfIdentityEncoding(0, 0xffff, true) );
    PdfString pString("Polish word: Łódź");
//  PdfString pString(reinterpret_cast<const pdf_utf8*>("Polish word: Łódź"));

    painter.SetFont( pFont );
    painter.DrawText( 100.0, pPage->GetPageSize().GetHeight()-100.0, pString );
    painter.FinishPage();
    document.Close();

    return 0;
}

在输出pdf中有

波兰语:ņÃ3dÅo

我尝试更改源字符串的编码(使用示例代码中的注释行),但都失败了。

谁能解释我如何使用 PoDoFo 库创建包含非 ascii 字符的 PDF 文档?

【问题讨论】:

  • 两个明显的错误。您期望 PDF 渲染器能够解析 UTF8(无论您是否知道,您都在使用它)。其次,您希望内置的 Helvetica 包含波兰语字符——请参阅 stackoverflow.com/questions/26631815/… 了解更多信息。
  • @Jongware 我已经修改了我的代码,并尝试添加带有波兰语字符的嵌入字体:Liberation-Serif,这是代码:PdfFont* pFont = document.CreateFontSubset("LiberationSerif", false, false , PdfEncodingFactory::GlobalWinAnsiEncodingInstance(), "fonts/LiberationSerif-Regular.ttf");而且没有成功...

标签: c++ pdf encoding utf-8 podofo


【解决方案1】:
#include <podofo/podofo.h>

using namespace PoDoFo;

int main(int argc, char *argv[], char *env[]) {
    PdfStreamedDocument document("polish.pdf");
    PdfPainter painter;
    PdfPage* pPage;


    pPage = document.CreatePage( PdfPage::CreateStandardPageSize( ePdfPageSize_A4 ) );
    painter.SetPage( pPage );
    const PdfEncoding* pEncoding = new PdfIdentityEncoding(); // required for UTF8 characters
    PdfFont *pFont = document.CreateFont("LiberationSerif", false, false, pEncoding ); // LiberationSerif has polish characters 
    PdfString pString(reinterpret_cast<const pdf_utf8*>("Polish word: Łódź")); // Need to cast input string into pdf_utf8
    painter.SetFont( pFont );
    painter.DrawText( 100.0, pPage->GetPageSize().GetHeight()-100.0, pString );
    painter.FinishPage();
    document.Close();

    return 0;
}

这段代码对我有用。

【讨论】:

    【解决方案2】:

    使用PdfIdentityEncoding 修复波兰语字符的显示,但使用这种编码时字符间距是错误的。

    【讨论】:

      【解决方案3】:

      在我的情况下,使用西班牙语波浪号:

      PdfFont* pFont = document.CreateFont("Arial"); PdfString pString(reinterpret_cast("máma mía"); ...

      否则特殊字符可能打印错误..

      【讨论】:

        猜你喜欢
        • 2021-12-31
        • 1970-01-01
        • 1970-01-01
        • 2013-10-10
        • 1970-01-01
        • 2018-07-22
        • 1970-01-01
        • 2016-03-02
        • 1970-01-01
        相关资源
        最近更新 更多