PdfTest.java 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. package com.github.jhonnymertz.wkhtmltopdf.wrapper;
  2. import com.github.jhonnymertz.wkhtmltopdf.wrapper.configurations.WrapperConfig;
  3. import com.github.jhonnymertz.wkhtmltopdf.wrapper.params.Param;
  4. import java.io.IOException;
  5. import org.apache.pdfbox.pdfparser.PDFParser;
  6. import org.apache.pdfbox.pdmodel.PDDocument;
  7. import org.apache.pdfbox.util.PDFTextStripper;
  8. import org.junit.Assert;
  9. import org.junit.Test;
  10. import java.io.ByteArrayInputStream;
  11. import java.io.File;
  12. import static org.hamcrest.core.StringContains.containsString;
  13. public class PdfTest {
  14. @Test
  15. public void testCommand() throws Exception {
  16. Pdf pdf = new Pdf();
  17. pdf.addToc();
  18. pdf.addParam(new Param("--enable-javascript"), new Param("--html-header", "file:///example.html"));
  19. pdf.addPageFromUrl("http://www.google.com");
  20. Assert.assertThat("command params should contain the --enable-javascript and --html-header", pdf.getCommand(), containsString("--enable-javascript --html-header file:///example.html"));
  21. }
  22. @Test
  23. public void findExecutable() throws Exception {
  24. WrapperConfig wc = new WrapperConfig();
  25. Assert.assertThat("executable should be /usr/bin/wkhtmltopdf", wc.findExecutable(), containsString("/usr/bin/wkhtmltopdf"));
  26. }
  27. @Test
  28. public void testPdfFromStringTo() throws Exception {
  29. // GIVEN a html template containing special characters that java stores in utf-16 internally
  30. Pdf pdf = new Pdf();
  31. pdf.addPageFromString("<html><head><meta charset=\"utf-8\"></head><h1>Müller</h1></html>");
  32. // WHEN
  33. byte[] pdfBytes = pdf.getPDF();
  34. String pdfText = getPdfTextFromBytes(pdfBytes);
  35. Assert.assertThat("document should contain the creditorName", pdfText, containsString("Müller"));
  36. }
  37. @Test
  38. public void testMultiplePages() throws Exception {
  39. Pdf pdf = new Pdf();
  40. pdf.addPageFromString("<html><head><meta charset=\"utf-8\"></head><h1>Page 1</h1></html>");
  41. pdf.addPageFromString("<html><head><meta charset=\"utf-8\"></head><h1>Page 2</h1></html>");
  42. pdf.addPageFromUrl("http://www.google.com");
  43. pdf.addPageFromString("<html><head><meta charset=\"utf-8\"></head><h1>Page 4</h1></html>");
  44. // WHEN
  45. byte[] pdfBytes = pdf.getPDF();
  46. String pdfText = getPdfTextFromBytes(pdfBytes);
  47. Assert.assertThat("document should contain the fourth page name", pdfText, containsString("Page 4"));
  48. }
  49. @Test
  50. public void callingGetCommandFollowedByGetPdfShouldNotInfluenceTheOutput() throws Exception {
  51. Pdf pdf = new Pdf();
  52. pdf.addPageFromString("<html><head><meta charset=\"utf-8\"></head><h1>Twice</h1></html>");
  53. // WHEN
  54. pdf.getCommand();
  55. //Followed by
  56. byte[] pdfBytes = pdf.getPDF();//Causes the page fromString's content to have become the file path
  57. String pdfText = getPdfTextFromBytes(pdfBytes);
  58. Assert.assertThat("document should contain the string that was originally inserted", pdfText, containsString("Twice"));
  59. }
  60. @Test
  61. public void testRemovingGeneratedFile() throws Exception {
  62. Pdf pdf = new Pdf();
  63. pdf.addPageFromString("<html><head><meta charset=\"utf-8\"></head><h1>Page 1</h1></html>");
  64. pdf.addPageFromString("<html><head><meta charset=\"utf-8\"></head><h1>Page 2</h1></html>");
  65. pdf.addPageFromUrl("http://www.google.com");
  66. pdf.addPageFromString("<html><head><meta charset=\"utf-8\"></head><h1>Page 4</h1></html>");
  67. File savedPdf = pdf.saveAs("output.pdf");
  68. Assert.assertTrue(savedPdf.delete());
  69. }
  70. private String getPdfTextFromBytes(byte[] pdfBytes) throws IOException {
  71. PDFParser parser = new PDFParser(new ByteArrayInputStream(pdfBytes));
  72. // that is a valid PDF (otherwise an IOException occurs)
  73. parser.parse();
  74. PDFTextStripper pdfTextStripper = new PDFTextStripper();
  75. return pdfTextStripper.getText(new PDDocument(parser.getDocument()));
  76. }
  77. }