Pārlūkot izejas kodu

Merge pull request #28 from orencp/orenco_wkhtmltopdf_wrapper_toc_options

Orenco wkhtmltopdf wrapper toc options
Jhonny Mertz 7 gadi atpakaļ
vecāks
revīzija
b51c7e0f41

+ 4 - 1
README.md

@@ -53,7 +53,7 @@ Pdf pdf = new Pdf();
 pdf.addPageFromString("<html><head><meta charset=\"utf-8\"></head><h1>Müller</h1></html>");
 pdf.addPageFromUrl("http://www.google.com");
 
-// Add a Table of contents
+// Add a Table of Contents
 pdf.addToc();
 
 // The `wkhtmltopdf` shell command accepts different types of options such as global, page, headers and footers, and toc. Please see `wkhtmltopdf -H` for a full explanation.
@@ -61,6 +61,9 @@ pdf.addToc();
 pdf.addParam(new Param("--no-footer-line"), new Param("--header-html", "file:///header.html"));
 pdf.addParam(new Param("--enable-javascript"));
 
+// Add styling for Table of Contents
+pdf.addTocParam(new Param("--xsl-style-sheet", "my_toc.xsl"));
+
 // Save the PDF
 pdf.saveAs("output.pdf");
 ```

+ 10 - 1
src/main/java/com/github/jhonnymertz/wkhtmltopdf/wrapper/Pdf.java

@@ -28,6 +28,8 @@ public class Pdf {
 
     private final Params params;
 
+    private final Params tocParams;
+
     private final List<Page> pages;
 
     private boolean hasToc = false;
@@ -39,6 +41,7 @@ public class Pdf {
     public Pdf(WrapperConfig wrapperConfig) {
         this.wrapperConfig = wrapperConfig;
         this.params = new Params();
+        this.tocParams = new Params();
         this.pages = new ArrayList<Page>();
     }
 
@@ -81,6 +84,10 @@ public class Pdf {
         this.params.add(param, params);
     }
 
+    public void addTocParam(Param param, Param... params) {
+        this.tocParams.add(param, params);
+    }
+
     public File saveAs(String path) throws IOException, InterruptedException {
         File file = new File(path);
         FileUtils.writeByteArrayToFile(file, getPDF());
@@ -120,8 +127,10 @@ public class Pdf {
 
         commandLine.addAll(params.getParamsAsStringList());
 
-        if (hasToc)
+        if (hasToc) {
             commandLine.add("toc");
+            commandLine.addAll(tocParams.getParamsAsStringList());
+        }
 
         for (Page page : pages) {
             if (page.getType().equals(PageType.htmlAsString)) {