开源wkhtmltopdf使用心得 (四)
修改wkhtmltox源码,使得转PDF、Image时支持中文路径和中文名称的转换
1.VS2010配置QT5.5.0开发环境或者VS2010配置QT5.5.0开发环境
2.找到pdf_c_binding.cc将下列语句
a.修改
CAPI(int) wkhtmltopdf_set_global_setting(wkhtmltopdf_global_settings * settings, const char * name, const char * value) {
return reinterpret_cast<settings::PdfGlobal *>(settings)->set(name, QString::fromUtf8(value));
}
改为
CAPI(int) wkhtmltopdf_set_global_setting(wkhtmltopdf_global_settings * settings, const char * name, const char * value) {
return reinterpret_cast<settings::PdfGlobal *>(settings)->set(name, QString::fromLocal8Bit(value));
}
修改后重新编译生成wkhtmltox.lib和wkhtmltox.dll,支持保存到本地的中文路径和中文名称
b.修改
CAPI(int) wkhtmltopdf_set_object_setting(wkhtmltopdf_object_settings * settings, const char * name, const char * value) {
return reinterpret_cast<settings::PdfObject *>(settings)->set(name, QString::fromUtf8(value));
}
为
CAPI(int) wkhtmltopdf_set_object_setting(wkhtmltopdf_object_settings * settings, const char * name, const char * value) {
return reinterpret_cast<settings::PdfObject *>(settings)->set(name, QString::fromLocal8Bit(value));
}
修改后重新编译生成wkhtmltox.lib和wkhtmltox.dll,支持中文名称的本地html和网上html保存到本地
3. 找到image_c_binding.cc 将下列语句
a.修改
CAPI(int) wkhtmltoimage_set_global_setting(wkhtmltoimage_global_settings * settings, const char * name, const char * value) {
return reinterpret_cast<settings::ImageGlobal *>(settings)->set(name, QString::fromUtf8(value));
}
为
CAPI(int) wkhtmltoimage_set_global_setting(wkhtmltoimage_global_settings * settings, const char * name, const char * value) {
return reinterpret_cast<settings::ImageGlobal *>(settings)->set(name, QString::fromLocal8Bit(value));
}
修改后重新编译生成wkhtmltox.lib和wkhtmltox.dll,支持保存到本地的中文路径和中文名称
4. 关于网页转为image 输出内容为乱码问题
找到image_c_binding.cc与 image_c_binding_p.hh将下列语句,在头文件中添加
#include <QHash>
MyImageConverter类中添加
QHash<QString, QByteArray> utf8StringCache;
a.修改
CAPI(const char *) wkhtmltoimage_phase_description(wkhtmltoimage_converter * converter, int phase) {
return reinterpret_cast<MyImageConverter *>(converter)->converter.phaseDescription(phase).toUtf8().constData();
}
为
CAPI(const char *) wkhtmltoimage_phase_description(wkhtmltoimage_converter * converter, int phase) {
MyImageConverter* conv = reinterpret_cast<MyImageConverter *>(converter);
QString pd = conv->converter.phaseDescription(phase);
if (!conv->utf8StringCache.contains(pd))
{
return conv->utf8StringCache.insert(pd, pd.toUtf8()).value().constData();
}
else
return conv->utf8StringCache[pd].constData();
}
b.修改
CAPI(const char *) wkhtmltoimage_progress_string(wkhtmltoimage_converter * converter) {
return reinterpret_cast<MyImageConverter *>(converter)->converter.progressString().toUtf8().constData();
}
为
CAPI(const char *) wkhtmltoimage_progress_string(wkhtmltoimage_converter * converter) {
MyImageConverter* conv = reinterpret_cast<MyImageConverter *>(converter);
QString pd = conv->converter.phaseDescription(phase);
if (!conv->utf8StringCache.contains(pd))
{
return conv->utf8StringCache.insert(pd, pd.toUtf8()).value().constData();
}
else
return conv->utf8StringCache[pd].constData();
}
修改后重新编译生成wkhtmltox.lib和wkhtmltox.dll,即可以解决该问题