Browse Source

Merge pull request #31 from delta11/bug-side-effect-when-multiple-calls

Bug side effect when multiple calls
Jhonny Mertz 7 years ago
parent
commit
2888170e27

+ 4 - 3
src/main/java/com/github/jhonnymertz/wkhtmltopdf/wrapper/Pdf.java

@@ -138,10 +138,11 @@ public class Pdf {
                 File temp = File.createTempFile("java-wkhtmltopdf-wrapper" + UUID.randomUUID().toString(), ".html");
                 FileUtils.writeStringToFile(temp, page.getSource(), "UTF-8");
 
-                page.setSource(temp.getAbsolutePath());
+                commandLine.add(temp.getAbsolutePath());
+            }
+            else {
+                commandLine.add(page.getSource());
             }
-
-            commandLine.add(page.getSource());
         }
         commandLine.add(STDINOUT);
         return commandLine.toArray(new String[commandLine.size()]);

+ 28 - 12
src/test/java/com/github/jhonnymertz/wkhtmltopdf/wrapper/PdfTest.java

@@ -2,6 +2,7 @@ package com.github.jhonnymertz.wkhtmltopdf.wrapper;
 
 import com.github.jhonnymertz.wkhtmltopdf.wrapper.configurations.WrapperConfig;
 import com.github.jhonnymertz.wkhtmltopdf.wrapper.params.Param;
+import java.io.IOException;
 import org.apache.pdfbox.pdfparser.PDFParser;
 import org.apache.pdfbox.pdmodel.PDDocument;
 import org.apache.pdfbox.util.PDFTextStripper;
@@ -40,12 +41,7 @@ public class PdfTest {
         // 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()));
+        String pdfText = getPdfTextFromBytes(pdfBytes);
 
         Assert.assertThat("document should contain the creditorName", pdfText, containsString("Müller"));
     }
@@ -61,16 +57,27 @@ public class PdfTest {
         // 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()));
+        String pdfText = getPdfTextFromBytes(pdfBytes);
 
         Assert.assertThat("document should contain the fourth page name", pdfText, containsString("Page 4"));
     }
 
+    @Test
+    public void callingGetCommandFollowedByGetPdfShouldNotInfluenceTheOutput() throws Exception {
+        Pdf pdf = new Pdf();
+
+        pdf.addPageFromString("<html><head><meta charset=\"utf-8\"></head><h1>Twice</h1></html>");
+
+        // WHEN
+        pdf.getCommand();
+        //Followed by
+        byte[] pdfBytes = pdf.getPDF();//Causes the page fromString's content to have become the file path
+
+        String pdfText = getPdfTextFromBytes(pdfBytes);
+
+        Assert.assertThat("document should contain the string that was originally inserted", pdfText, containsString("Twice"));
+    }
+
     @Test
     public void testRemovingGeneratedFile() throws Exception {
         Pdf pdf = new Pdf();
@@ -82,4 +89,13 @@ public class PdfTest {
         File savedPdf = pdf.saveAs("output.pdf");
         Assert.assertTrue(savedPdf.delete());
     }
+
+    private String getPdfTextFromBytes(byte[] pdfBytes) throws IOException {
+        PDFParser parser = new PDFParser(new ByteArrayInputStream(pdfBytes));
+
+        // that is a valid PDF (otherwise an IOException occurs)
+        parser.parse();
+        PDFTextStripper pdfTextStripper = new PDFTextStripper();
+        return pdfTextStripper.getText(new PDDocument(parser.getDocument()));
+    }
 }