我看了所有这些内容,没有看到关于如何使用"--replace"的内容。所以,接下来是关于如何在wkhtmltopdf中使用"--replace"的方法。
请给我一个示例,谢谢:)
我看了所有这些内容,没有看到关于如何使用"--replace"的内容。所以,接下来是关于如何在wkhtmltopdf中使用"--replace"的方法。
请给我一个示例,谢谢:)
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<script>
function substitutePdfVariables() {
function getParameterByName(name) {
var match = RegExp('[?&]' + name + '=([^&]*)').exec(window.location.search);
return match && decodeURIComponent(match[1].replace(/\+/g, ' '));
}
function substitute(name) {
var value = getParameterByName(name);
var elements = document.getElementsByClassName(name);
for (var i = 0; elements && i < elements.length; i++) {
elements[i].textContent = value;
}
}
['username'/*, 'topage', 'page',.. etc and any variable you would want to replace */]
.forEach(function(param) {
substitute(param);
});
}
</script>
</head>
<body onload="substitutePdfVariables()">
<span class="username"></span>
</body>
</html>
您可以将“username”替换为某个值:
wkhtmltopdf my_super_html.html --footer-html my_footer.html --replace "username" "Veaer"
.md
转换为.html
,并在pandoc调用中包含js作为头文件,这样就可以正常工作。但是,使用这些变量将HTML转换为PDF时,变量不再被转换,它们仍然保持为键。 - Timo<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<script charset="utf-8">
// For WKHTMLTOPDF Substitutions
window.onload = function()
{
function substitute(key, value)
{
var elements = document.getElementsByClassName(key);
for (var i = 0; elements && i < elements.length; i++)
{
elements[i].textContent = value;
}
}
var url = window.location.href.replace(/#$/, ""); // Remove last # if exist
// default params
//['page', 'section','sitepage','title','subsection','frompage','subsubsection','isodate','topage','doctitle','sitepages','webpage','time','date'];
var params = (url.split("?")[1] || "").split("&");
for (var i = 0; i < params.length; i++)
{
var param = params[i].split("=");
var key = param[0];
var value = param[1] || '';
var regex = new RegExp('{' + key + '}', 'g');
substitute(key, value);
}
}
</script>
</head>
<body onload="substitutePdfVariables()">
<span class="username"></span>
</body>
</html>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<script charset="utf-8">
function replaceParams() {
var url = window.location.href
.replace(/#$/, ""); // Remove last # if exist
// default params
//['page', 'section','sitepage','title','subsection','frompage','subsubsection','isodate','topage','doctitle','sitepages','webpage','time','date'];
var params = (url.split("?")[1] || "").split("&");
for (var i = 0; i < params.length; i++) {
var param = params[i].split("=");
var key = param[0];
var value = param[1] || '';
var regex = new RegExp('{' + key + '}', 'g');
document.body.innerText = document.body.innerText.replace(regex, value);
}
}
</script>
</head>
<body onload="replaceParams()">
name: {username}
age: {age}
url: {url}
page {page}
</body>
</html>
然后您可以添加多个参数以动态替换
wkhtmltopdf page.html --footer-html footer.html --replace "username" "Veaer" --replace "age" "20" --replace "url" "google.com"