You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用PDF::API2生成日文PDF时,外部数据引发乱码求助

解决PDF::API2生成日文PDF时外部数据乱码问题

核心原因

脚本内直接声明的变量因use utf8被Perl自动解析为内部Unicode字符串,但从MySQL/外部文件读取的是UTF-8字节流,未转换为Perl认可的Unicode格式,导致PDF::API2无法正确渲染日文。

分场景解决方案

1. 处理MySQL数据库数据

  • 连接数据库时启用UTF-8解码,让DBI直接返回Unicode字符串:
    my $dbh = DBI->connect(
        "DBI:mysql:database=your_db;host=your_host",
        "user", "password",
        { 
            mysql_enable_utf8mb4 => 1,  # 支持全日文(含扩展字符)
            RaiseError => 1,
            PrintError => 0
        }
    );
    
  • 若已获取到字节流数据,手动解码为Unicode:
    use Encode qw(decode);
    my $japanese_str = decode('utf8', $raw_data_from_db);
    

2. 处理外部data.lib文件数据

  • 读取文件时指定UTF-8编码,直接得到Unicode字符串:
    open my $fh, '<:encoding(utf8)', 'data.lib' or die "无法打开文件: $!";
    while (<$fh>) {
        # 处理每行数据,此时$_已是Unicode格式
    }
    close $fh;
    
  • 若使用require/do加载lib文件,加载后对变量统一解码:
    do 'data.lib';
    $customer_name = decode('utf8', $customer_name);  # 对lib内的变量做解码处理
    

3. 通用验证与字体检查

  • 用Encode::is_utf8()验证变量是否为Perl内部Unicode:
    use Encode qw(is_utf8);
    print is_utf8($japanese_str) ? "是Unicode字符串" : "是UTF-8字节流";
    
  • 确保PDF使用支持日文的TrueType字体(如IPAMincho、VL Gothic),加载方式:
    my $pdf = PDF::API2->new();
    my $font = $pdf->ttfont('/usr/share/fonts/truetype/ipam/ipam.ttf');  # 替换为实际字体路径
    my $page = $pdf->page();
    my $text = $page->text();
    $text->font($font, 12);
    $text->translate(100, 700);
    $text->text($japanese_str);  # 传入Unicode字符串
    

注意事项

  • 禁止将UTF-8字节流直接传给PDF::API2,必须先转为Perl内部Unicode字符串;
  • 若使用旧版PDF::API2,建议升级到最新版以获得更好的Unicode支持;
  • 数据库表的字符集需设为utf8mb4,确保存储日文无字符丢失。

内容的提问来源于stack exchange,提问作者moring

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.02 15:32:19