XvfbTest.java 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. package br.eti.mertz.wkhtmltopdf.wrapper;
  2. import br.eti.mertz.wkhtmltopdf.wrapper.configurations.WrapperConfig;
  3. import br.eti.mertz.wkhtmltopdf.wrapper.configurations.XvfbConfig;
  4. import br.eti.mertz.wkhtmltopdf.wrapper.page.PageType;
  5. import br.eti.mertz.wkhtmltopdf.wrapper.params.Param;
  6. import org.apache.pdfbox.pdfparser.PDFParser;
  7. import org.apache.pdfbox.pdmodel.PDDocument;
  8. import org.apache.pdfbox.util.PDFTextStripper;
  9. import org.junit.Assert;
  10. import org.junit.Test;
  11. import java.io.ByteArrayInputStream;
  12. import static org.hamcrest.core.StringContains.containsString;
  13. public class XvfbTest {
  14. @Test
  15. public void testXvfbCommand() throws Exception {
  16. WrapperConfig wc = new WrapperConfig();
  17. wc.setXvfbConfig(new XvfbConfig());
  18. Pdf pdf = new Pdf(wc);
  19. Assert.assertThat("command should contain xvfb-run config", pdf.getCommand(), containsString("xvfb-run"));
  20. }
  21. @Test
  22. public void testPdfWithXvfb() throws Exception {
  23. XvfbConfig xc = new XvfbConfig();
  24. xc.addParams(new Param("--auto-servernum"), new Param("--server-num=1"));
  25. WrapperConfig wc = new WrapperConfig();
  26. wc.setXvfbConfig(xc);
  27. Pdf pdf = new Pdf(wc);
  28. pdf.addPage("http://www.google.com", PageType.url);
  29. pdf.saveAs("output.pdf");
  30. // WHEN
  31. byte[] pdfBytes = pdf.getPDF();
  32. PDFParser parser = new PDFParser(new ByteArrayInputStream(pdfBytes));
  33. // that is a valid PDF (otherwise an IOException occurs)
  34. parser.parse();
  35. PDFTextStripper pdfTextStripper = new PDFTextStripper();
  36. String pdfText = pdfTextStripper.getText(new PDDocument(parser.getDocument()));
  37. Assert.assertThat("document should be generated", pdfText, containsString("Google"));
  38. }
  39. }