wkhtmltopdf 0.12.6版本--toc-header-text非英文字符截断问题求助
解决wkhtmltopdf 0.12.6生成TOC时非英文字符表头截断问题
针对你遇到的--toc-header-text传入日文等非英文字符被截断的问题,可尝试以下几种解决方案:
1. 确保终端/命令行环境的UTF-8编码设置
wkhtmltopdf的命令行参数依赖终端的编码环境,如果终端默认编码不是UTF-8,非英文字符会被转码损坏:
- Linux/macOS:执行命令前先设置编码环境变量
export LANG=ja_JP.UTF-8 export LC_ALL=ja_JP.UTF-8 wkhtmltopdf --encoding "UTF-8" toc --toc-header-text "目次" --xsl-style-sheet toc-style.xsl input.html output.pdf - Windows:先切换命令行到UTF-8编码(代码页65001)再执行命令
chcp 65001 wkhtmltopdf --encoding "UTF-8" toc --toc-header-text "目次" --xsl-style-sheet toc-style.xsl input.html output.pdf
2. 通过XSL参数动态传递表头文本(推荐)
绕过--toc-header-text的编码bug,改用XSL接收动态参数来渲染表头,同时满足多语言动态传入的需求:
修改你的toc-style.xsl:
添加参数定义并在表头位置引用该参数:
<?xml version="1.0" encoding="UTF-8"?> <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <!-- 定义接收表头文本的参数 --> <xsl:param name="toc-header-text" select="'目次'"/> <xsl:template match="/"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/> <!-- 保留你的其他样式定义 --> </head> <body> <!-- 使用参数值作为表头 --> <h1 style="margin-bottom: 20px;"><xsl:value-of select="$toc-header-text"/></h1> <!-- 保留原有的TOC生成逻辑 --> <xsl:apply-templates/> </body> </html> </xsl:template> <!-- 保留原有的其他XSL模板 --> </xsl:stylesheet>
修改执行命令:
用--toc-param传递动态表头文本:
wkhtmltopdf --encoding "UTF-8" toc --toc-param "toc-header-text=目次" --xsl-style-sheet toc-style.xsl input.html output.pdf
这种方式完全避开了原参数的编码问题,且能灵活传入任意语言的表头文本。
3. 尝试升级wkhtmltopdf版本
0.12.6版本存在不少编码相关的已知问题,若项目允许,升级到最新稳定版(如0.12.6.1或更高)可能直接解决该截断问题。
内容的提问来源于stack exchange,提问作者user5005768-hd
相关产品推荐
相关产品推荐

