-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhtml2pdf_converter.cpp
More file actions
77 lines (64 loc) · 3.43 KB
/
Copy pathhtml2pdf_converter.cpp
File metadata and controls
77 lines (64 loc) · 3.43 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
#include "html2pdf_converter.hpp"
#include <string>
#include <memory>
bool HtmlToPdfConverter::convertFile(const std::string& input_html_path,
const std::string& output_pdf_path,
const Options& opts) {
// Создаем глобальные настройки
wkhtmltopdf_global_settings* global_settings = wkhtmltopdf_create_global_settings();
// if (!global_settings) {
// wkhtmltopdf_deinit();
// return false;
// }
// Устанавливаем глобальные настройки
setGlobalSetting(global_settings, "out", output_pdf_path.c_str());
setGlobalSetting(global_settings, "size.pageSize", opts.page_size.c_str());
setGlobalSetting(global_settings, "orientation", opts.orientation.c_str());
setGlobalSetting(global_settings, "dpi", std::to_string(opts.dpi).c_str());
setGlobalSetting(global_settings, "margin.top", (std::to_string(opts.margin_top) + "mm").c_str());
setGlobalSetting(global_settings, "margin.bottom", (std::to_string(opts.margin_bottom) + "mm").c_str());
setGlobalSetting(global_settings, "margin.left", (std::to_string(opts.margin_left) + "mm").c_str());
setGlobalSetting(global_settings, "margin.right", (std::to_string(opts.margin_right) + "mm").c_str());
if (opts.grayscale) {
setGlobalSetting(global_settings, "colorMode", "Grayscale");
}
if (opts.lowquality) {
setGlobalSetting(global_settings, "quality", "Low");
}
// Создаем конвертер
wkhtmltopdf_converter* converter = wkhtmltopdf_create_converter(global_settings);
if (!converter) {
wkhtmltopdf_deinit();
return false;
}
// Создаем настройки объекта (страницы)
wkhtmltopdf_object_settings* object_settings = wkhtmltopdf_create_object_settings();
if (!object_settings) {
wkhtmltopdf_destroy_converter(converter);
wkhtmltopdf_deinit();
return false;
}
// Устанавливаем настройки объекта
setObjectSetting(object_settings, "page", input_html_path.c_str());
setObjectSetting(object_settings, "web.defaultEncoding", "utf-8");
setObjectSetting(object_settings, "load.zoomFactor", std::to_string(opts.zoom).c_str());
setObjectSetting(object_settings, "web.minimumFontSize", std::to_string(opts.minimum_font_size).c_str());
setObjectSetting(object_settings, "load.disableSmartShrinking", opts.disable_smart_shrinking ? "true" : "false");
setObjectSetting(object_settings, "web.enableLocalFileAccess", opts.enable_local_file_access ? "true" : "false");
// Добавляем объект в конвертер
wkhtmltopdf_add_object(converter, object_settings, nullptr);
// Выполняем конвертацию
int conversion_result = wkhtmltopdf_convert(converter);
// Очищаем ресурсы
wkhtmltopdf_destroy_converter(converter);
// wkhtmltopdf_deinit();
return true;
}
void HtmlToPdfConverter::setGlobalSetting(wkhtmltopdf_global_settings* settings,
const char* name, const char* value) {
wkhtmltopdf_set_global_setting(settings, name, value);
}
void HtmlToPdfConverter::setObjectSetting(wkhtmltopdf_object_settings* settings,
const char* name, const char* value) {
wkhtmltopdf_set_object_setting(settings, name, value);
}