Skip to content

Commit

Permalink
TOC - add header and footer options (#66)
Browse files Browse the repository at this point in the history
* TOC - add header and footer options

* miss pasted struct

* Update json_test.go

* TestTOCAndCustomFooter
  • Loading branch information
arturwwl authored Jun 9, 2021
1 parent ae3cf72 commit a0f0071
Show file tree
Hide file tree
Showing 7 changed files with 287 additions and 2 deletions.
4 changes: 2 additions & 2 deletions json_test.go

Large diffs are not rendered by default.

57 changes: 57 additions & 0 deletions testfiles/footer-toc.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<script src="js/footer.js"></script>
<style>

body {
margin: 0 10mm;
}

#pageFooter {
padding-top: 3mm;
padding-bottom: 5mm;
font-weight: bold;
border-top: 0.5mm solid grey;
}

#pageFooter .page-number {
position: absolute;
bottom: 6mm;
font-size: 5mm;
color: grey;
text-align: left;
vertical-align: bottom;
font-family: Verdana sans-serif;
font-weight: bold;
}

#footer-data {
font-size: 2.7mm;
line-height: 4mm;
color: blue;
text-align: center;
vertical-align: bottom;
font-family: Verdana sans-serif;
}

#footer-data a {
color: blue;
text-decoration: none;
}
</style>
</head>
<body onload="footer()">

<div id="pageFooter" >
<div class="page-number page"></div>
<div id="footer-data">
<a href="https://example.com">example.com</a>
<br/>
<strong>TOC FOOTER</strong>
</div>
</div>

</body>
</html>
59 changes: 59 additions & 0 deletions testfiles/footer.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<script src="js/footer.js"></script>
<style>

body {
margin: 0 10mm;
}

#pageFooter {
padding-top: 3mm;
padding-bottom: 5mm;
font-weight: bold;
border-top: 0.5mm solid grey;
}

#pageFooter .page-number {
position: absolute;
bottom: 6mm;
font-size: 5mm;
color: blue;
text-align: left;
vertical-align: bottom;
font-family: Verdana sans-serif;
font-weight: bold;
}

#footer-data {
font-size: 2.7mm;
line-height: 4mm;
color: blue;
text-align: center;
vertical-align: bottom;
font-family: Verdana sans-serif;
}

#footer-data a {
color: blue;
text-decoration: none;
}
</style>
</head>
<body onload="footer()">

<div id="pageFooter" >
<div class="page-number page"></div>
<div id="footer-data">
Example:
<a href="https://example.com">example.com</a>
<br/>
tel.: <a href="tel:+00123123123">+00 123 123 123</a>,
<a href="mailto:[email protected]">[email protected]</a>,
</div>
</div>

</body>
</html>
19 changes: 19 additions & 0 deletions testfiles/js/footer.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
function footer() {
var vars = {};
var query_strings_from_url = document.location.search.substring(1).split('&');
for (var query_string in query_strings_from_url) {
if (query_strings_from_url.hasOwnProperty(query_string)) {
var temp_var = query_strings_from_url[query_string].split('=', 2);
vars[temp_var[0]] = decodeURI(temp_var[1]);
}
}
var css_selector_classes = ['page', 'frompage', 'topage', 'webpage', 'section', 'subsection', 'date', 'isodate', 'time', 'title', 'doctitle', 'sitepage', 'sitepages'];
for (var css_class in css_selector_classes) {
if (css_selector_classes.hasOwnProperty(css_class)) {
var element = document.getElementsByClassName(css_selector_classes[css_class]);
for (var j = 0; j < element.length; ++j) {
element[j].textContent = vars[css_selector_classes[css_class]];
}
}
}
}
110 changes: 110 additions & 0 deletions testfiles/toc.xls
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="2.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:outline="http://wkhtmltopdf.org/outline"
xmlns="http://www.w3.org/1999/xhtml">
<xsl:output doctype-public="-//W3C//DTD XHTML 1.0 Strict//EN"
doctype-system="http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitiona
l.dtd"
indent="yes" />
<xsl:template match="outline:outline">
<html>
<head>
<title>TOC</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<style>
.toc-title {
font-size: 22px;
padding-top: 2mm;
padding-bottom: 5mm;
margin-left: 16mm;
color: grey;
}

li {
list-style: none;
}

ul {
margin-right: -7mm;
position: relative;
}

a {
text-decoration: none;
}

.chapter-link {
font-size: 4mm;
font-weight:bold;
color: blue;
padding: 6mm 6mm 6mm 6mm;
}

li {
position: relative;
background: lightgrey;
padding: 6mm 6mm 6mm 6mm;
margin-bottom: 3mm;
}

li img {
position: absolute;
left: 0mm;
top: 3mm;
width: 10mm;
height: 10mm;
background: red;
border-radius: 50%;
z-index: 1;
}

.page-number {
float: right;
color: blue;
padding-right: 20mm;
font-size: 6mm;
font-weight:bold;
margin-top: -1mm;
}
</style>
</head>
<body>
<h1 class="toc-title">TOC</h1>
<ul>
<xsl:apply-templates select="outline:item/outline:item"/>

<li class="list-decoration"></li></ul>
</body>
</html>
</xsl:template>
<xsl:template match="outline:item">
<xsl:if test="@title!='TOC'">
<li>

<xsl:if test="contains(@title, 'elements')">

<img src="https://picsum.photos/200/200"/>
</xsl:if>

<a class="chapter-link">
<xsl:if test="@link">
<xsl:attribute name="href"><xsl:value-of select="@link"/></xsl:attribute>
</xsl:if>
<xsl:if test="@backLink">
<xsl:attribute name="name"><xsl:value-of select="@backLink"/></xsl:attribute>
</xsl:if>
<xsl:value-of select="@title" />
</a>

<a class="page-number">
<xsl:if test="@link">
<xsl:attribute name="href"><xsl:value-of select="@link"/></xsl:attribute>
</xsl:if>
PAGE <xsl:value-of select="@page" />
</a>

</li>
</xsl:if>
</xsl:template>
</xsl:stylesheet>
3 changes: 3 additions & 0 deletions wkhtmltopdf.go
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,7 @@ type toc struct {
type allTocOptions struct {
pageOptions
tocOptions
headerAndFooterOptions
}

// PDFGenerator is the main wkhtmltopdf struct, always use NewPDFGenerator to obtain a new PDFGenerator struct
Expand Down Expand Up @@ -177,6 +178,7 @@ func (pdfg *PDFGenerator) Args() []string {
args = append(args, "toc")
args = append(args, pdfg.TOC.pageOptions.Args()...)
args = append(args, pdfg.TOC.tocOptions.Args()...)
args = append(args, pdfg.TOC.headerAndFooterOptions.Args()...)
}
for _, page := range pdfg.pages {
args = append(args, "page")
Expand Down Expand Up @@ -361,6 +363,7 @@ func NewPDFPreparer() *PDFGenerator {
allTocOptions: allTocOptions{
tocOptions: newTocOptions(),
pageOptions: newPageOptions(),
headerAndFooterOptions: newHeaderAndFooterOptions(),
},
},
}
Expand Down
37 changes: 37 additions & 0 deletions wkhtmltopdf_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -330,6 +330,43 @@ func TestPDFGenerator_SetStderr(t *testing.T) {
}
}

func TestTOCAndCustomFooter(t *testing.T) {
//Use a new blank PDF generator
pdfg, err := NewPDFGenerator()
if err != nil {
t.Fatal(err)
}

htmlfile, err := os.Open("./testfiles/html5.html")
if err != nil {
t.Fatal(err)
}
defer htmlfile.Close()
page := NewPageReader(htmlfile)
page.EnableLocalFileAccess.Set(true) //needed to include js
pdfg.AddPage(page)

page.FooterHTML.Set("./testfiles/footer.html")
page.FooterSpacing.Set(8)

pdfg.TOC.XslStyleSheet.Set("./testfiles/toc.xls")
pdfg.TOC.Include = true
pdfg.TOC.EnableLocalFileAccess.Set(true) //needed to include js
pdfg.TOC.FooterHTML.Set("./testfiles/footer-toc.html")
pdfg.TOC.FooterSpacing.Set(8)

err = pdfg.Create()
if err != nil {
t.Fatal(err)
}

// Write buffer contents to file on disk
err = pdfg.WriteFile("./testfiles/TestGeneratePdfTOCAndCustomFooter.pdf")
if err != nil {
t.Fatal(err)
}
}

func TestStringOption(t *testing.T) {
opt := stringOption{
option: "stringopt",
Expand Down

0 comments on commit a0f0071

Please sign in to comment.