-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.cpp
252 lines (230 loc) · 7.93 KB
/
main.cpp
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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
#include <QApplication>
#include <QFile>
#include <QDir>
#include <QStandardPaths>
#include <QtWebEngine/QtWebEngine>
#include <QWebEnginePage>
#include <QPageLayout>
#include <QPageSize>
#include <QMarginsF>
#include <QDebug>
#include <QCommandLineParser>
#include <QPrinterInfo>
#include <QPrinter>
#include <memory>
#include <functional>
#include <iostream>
#ifdef Q_OS_WIN
#include <io.h>
#include <fcntl.h>
#endif
double to_mm(const QString& unit)
{
if (unit == "mm") { return 1.0; }
if (unit == "cm") { return 10.0; }
if (unit == "in") { return 25.4; }
if (unit == "pt") { return 0.352778; }
return 0.0;
}
/**
* @brief cssLengthToMm
* @param s CSS length value
* @return length in mm
*/
double cssLengthToMm(const QString& s)
{
const double value = s.left(s.length() - 2).toDouble();
const QString unit = s.right(2);
return value * to_mm(unit);
}
/**
* @brief getCssPageLayout
* @param page web page to query for css page layout
* @param cb callback invoked after querying page layout
*/
void getCssPageLayout(QWebEnginePage& page, std::function<void(const QPageLayout&)> cb)
{
constexpr auto jscode = R"JSCODE(
(function getPrintPageStyle() {
var pageStyle = {
"size": "a4",
"margin-top": "0mm",
"margin-left": "0mm",
"margin-right": "0mm",
"margin-bottom": "0mm",
};
function applyCSSPageRule(rule) {
for (var property in pageStyle) {
var value = rule.style.getPropertyValue(property);
pageStyle[property] = (value) ? value : pageStyle[property];
}
}
function hasMediaType(cssMediaRule, type) {
for (var i=0; i < cssMediaRule.media.length; i++)
if (cssMediaRule.media[i] === type)
return true;
return false;
}
function loopRules(rules) {
for (var i = 0; i < rules.length; i++) {
var rule = rules[i];
if (rule instanceof CSSMediaRule && hasMediaType(rule, "print")) {
loopRules(rule.cssRules);
} else if (rule instanceof CSSPageRule) {
applyCSSPageRule(rule);
}
}
}
for (var i = 0; i < document.styleSheets.length; i++) {
loopRules(document.styleSheets[i].cssRules);
}
return pageStyle;
})()
)JSCODE";
page.runJavaScript(jscode, [cb=std::move(cb)](const QVariant& result) {
const QVariantMap pageStyle = result.toMap();
const double defaultMargin_mm = cssLengthToMm(pageStyle["margins"].toString());
std::vector<double> margins_mm;
for(const auto& marginName: {"margin-left", "margin-top", "margin-right", "margin-bottom"})
{
const QString marginCss(pageStyle[marginName].toString());
const double margin_mm = (marginCss.isEmpty()) ? defaultMargin_mm
: cssLengthToMm(marginCss);
margins_mm.push_back(margin_mm);
}
const QPageLayout layout(
QPageSize(QPageSize::A4), QPageLayout::Portrait,
QMarginsF(margins_mm[0], margins_mm[1], margins_mm[2], margins_mm[3]),
QPageLayout::Millimeter
);
cb(layout);
});
}
constexpr auto fileno_bin = [](auto fd) {
#ifdef Q_OS_WIN
_setmode(_fileno(fd), _O_BINARY);
return _fileno(fd);
#else
return fileno(fd);
#endif
};
QByteArray readStdin()
{
QFile file;
file.open(fileno_bin(stdin), QIODevice::OpenMode(QFile::ReadOnly));
const QByteArray data = file.readAll();
file.close();
return data;
}
bool writePdf(const QString& outputFilename, const QByteArray& data)
{
// Write to stdout
if (outputFilename == "-") {
QFile file;
file.open(fileno_bin(stdout), QIODevice::OpenMode(QIODevice::OpenModeFlag::WriteOnly));
file.write(data);
file.close();
return true;
}
// Write to file
QFile file(outputFilename);
bool ok = file.open(QIODevice::WriteOnly);
ok &= file.write(data) == data.size();
file.close();
return ok;
}
void printToPDF(QWebEnginePage& page, const QPageLayout& layout, const QString& outputFilename)
{
page.printToPdf([outputFilename](const QByteArray& pdfData) {
if (pdfData.isEmpty()) {
qCritical() << "Error printing page";
QCoreApplication::exit(EXIT_FAILURE);
return;
}
const bool ok = writePdf(outputFilename, pdfData);
QCoreApplication::exit(ok ? EXIT_SUCCESS : EXIT_FAILURE);
}, layout);
}
void printToPrinter(QWebEnginePage& page, const QPageLayout& layout, const QString& printerName)
{
// Validate printer
const auto printerInfo = QPrinterInfo::printerInfo(printerName);
if (printerInfo.isNull()) {
qCritical() << "Printer not found";
QCoreApplication::exit(EXIT_FAILURE);
return;
}
auto printer = std::make_shared<QPrinter>(printerInfo);
qDebug() << "setPageLayout" << printer->setPageLayout(layout);
page.print(printer.get(), [printer](const bool success) {
if (!success) {
qCritical() << "Error printing page";
}
QCoreApplication::exit(success ? EXIT_SUCCESS : EXIT_FAILURE);
});
}
int main(int argc, char *argv[])
{
QtWebEngine::initialize();
QApplication app(argc, argv);
// Delete WebEngine cache
QDir cache_dir(QStandardPaths::writableLocation(QStandardPaths::CacheLocation));
cache_dir.removeRecursively();
QCommandLineParser parser;
parser.setApplicationDescription("Qt WebEngine HTML to PDF");
parser.addHelpOption();
parser.addOptions({
{"printer", "Send output to printer instead of PDF file"},
{"list-printers", "Show list of available printers and exit"},
{"post", "Send HTTP POST request with data from stdin"},
});
parser.addPositionalArgument("url", "URL to HTML document");
parser.addPositionalArgument("output", "PDF filename, printer name or '-' for stdout");
parser.process(app);
// If list-printers is set, show list and exit
if (parser.isSet("list-printers")) {
const QStringList printerNames = QPrinterInfo::availablePrinterNames();
for (const auto& name : printerNames) { qInfo().quote() << name; }
return EXIT_SUCCESS;
}
// Check number of positional args
const QStringList args_url_output = parser.positionalArguments();
if (args_url_output.size() != 2) {
parser.showHelp(1);
}
const QUrl documentUrl(args_url_output[0]);
const QString outputName(args_url_output[1]);
// Create HTTP request for given program arguments
const QWebEngineHttpRequest request = [&]() {
QWebEngineHttpRequest request { documentUrl };
if (parser.isSet("post")) {
request.setMethod(QWebEngineHttpRequest::Post);
request.setHeader("Content-Type", "application/json"); // Fixed type for now
request.setPostData(readStdin());
}
return request;
}();
// Create web page and PDF creation handlers
QWebEnginePage page;
QObject::connect(&page, &QWebEnginePage::loadFinished, &page, [&](const bool ok) {
if (!ok) {
qCritical() << "Error loading page";
QCoreApplication::exit(EXIT_FAILURE);
return;
}
// Queue print request after page finished loading
// TODO: How to ensure that page finished js processing? -> Add some time as workaround
QTimer::singleShot(100, &page, [&]() {
getCssPageLayout(page, [&](const QPageLayout& layout) {
if (!parser.isSet("printer")) {
printToPDF(page, layout, outputName);
} else {
printToPrinter(page, layout, outputName);
}
});
});
});
// Init process by loading the web page
page.load(request);
return QApplication::exec();
}