package com.github.jhonnymertz.wkhtmltopdf.wrapper;

import com.github.jhonnymertz.wkhtmltopdf.wrapper.configurations.WrapperConfig;
import com.github.jhonnymertz.wkhtmltopdf.wrapper.params.Param;
import org.apache.pdfbox.pdfparser.PDFParser;
import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.util.PDFTextStripper;
import org.junit.Assert;
import org.junit.Test;

import java.io.ByteArrayInputStream;
import java.io.File;

import static org.hamcrest.core.StringContains.containsString;

public class PdfTest {

    @Test
    public void testCommand() throws Exception {
        Pdf pdf = new Pdf();
        pdf.addToc();
        pdf.addParam(new Param("--enable-javascript"), new Param("--html-header", "file:///example.html"));
        pdf.addPageFromUrl("http://www.google.com");
        Assert.assertThat("command params should contain the --enable-javascript and --html-header", pdf.getCommand(), containsString("--enable-javascript --html-header file:///example.html"));
    }

    @Test
    public void findExecutable() throws Exception {
        WrapperConfig wc = new WrapperConfig();
        Assert.assertThat("executable should be /usr/bin/wkhtmltopdf", wc.findExecutable(), containsString("/usr/bin/wkhtmltopdf"));
    }

    @Test
    public void testPdfFromStringTo() throws Exception {

        // GIVEN a html template containing special characters that java stores in utf-16 internally
        Pdf pdf = new Pdf();
        pdf.addPageFromString("<html><head><meta charset=\"utf-8\"></head><h1>Müller</h1></html>");

        // WHEN
        byte[] pdfBytes = pdf.getPDF();

        PDFParser parser = new PDFParser(new ByteArrayInputStream(pdfBytes));

        // that is a valid PDF (otherwise an IOException occurs)
        parser.parse();
        PDFTextStripper pdfTextStripper = new PDFTextStripper();
        String pdfText = pdfTextStripper.getText(new PDDocument(parser.getDocument()));

        Assert.assertThat("document should contain the creditorName", pdfText, containsString("Müller"));
    }

    @Test
    public void testMultiplePages() throws Exception {
        Pdf pdf = new Pdf();
        pdf.addPageFromString("<html><head><meta charset=\"utf-8\"></head><h1>Page 1</h1></html>");
        pdf.addPageFromString("<html><head><meta charset=\"utf-8\"></head><h1>Page 2</h1></html>");
        pdf.addPageFromUrl("http://www.google.com");
        pdf.addPageFromString("<html><head><meta charset=\"utf-8\"></head><h1>Page 4</h1></html>");

        // WHEN
        byte[] pdfBytes = pdf.getPDF();

        PDFParser parser = new PDFParser(new ByteArrayInputStream(pdfBytes));

        // that is a valid PDF (otherwise an IOException occurs)
        parser.parse();
        PDFTextStripper pdfTextStripper = new PDFTextStripper();
        String pdfText = pdfTextStripper.getText(new PDDocument(parser.getDocument()));

        Assert.assertThat("document should contain the fourth page name", pdfText, containsString("Page 4"));
    }

    @Test
    public void testRemovingGeneratedFile() throws Exception {
        Pdf pdf = new Pdf();
        pdf.addPageFromString("<html><head><meta charset=\"utf-8\"></head><h1>Page 1</h1></html>");
        pdf.addPageFromString("<html><head><meta charset=\"utf-8\"></head><h1>Page 2</h1></html>");
        pdf.addPageFromUrl("http://www.google.com");
        pdf.addPageFromString("<html><head><meta charset=\"utf-8\"></head><h1>Page 4</h1></html>");

        File savedPdf = pdf.saveAs("output.pdf");
        Assert.assertTrue(savedPdf.delete());
    }
}

@@ -2973,32 +2973,880 @@
 ge 4%22));%0A    %7D%0A%0A
+    @Test%0A    public void callingGetCommandFollowedByGetPdfShouldNotInfluenceTheOutput() throws Exception %7B%0A        Pdf pdf = new Pdf();%0A%0A        pdf.addPageFromString(%22%3Chtml%3E%3Chead%3E%3Cmeta charset=%5C%22utf-8%5C%22%3E%3C/head%3E%3Ch1%3ETwice%3C/h1%3E%3C/html%3E%22);%0A%0A        // WHEN%0A        pdf.getCommand();//The command side effect cause the second call to getPDF to go wrong%0A%0A        byte%5B%5D pdfBytes = pdf.getPDF();%0A%0A        PDFParser parser = new PDFParser(new ByteArrayInputStream(pdfBytes));%0A%0A        // that is a valid PDF (otherwise an IOException occurs)%0A        parser.parse();%0A        PDFTextStripper pdfTextStripper = new PDFTextStripper();%0A        String pdfText = pdfTextStripper.getText(new PDDocument(parser.getDocument()));%0A%0A        Assert.assertThat(%22document should contain the string that was originally inserted%22, pdfText, containsString(%22Twice%22));%0A    %7D%0A%0A
     @Test%0A    pu

Add test case to show the bug