6.9.  HTML2PDF - Does the Rendering Tool Work Correct?

Initial Situation

A web application creates dynamic web pages and additionally it provides the possibility to download the current web page as a PDF document. The PDF generation is done by rendering the HTML page with an appropriate tool.


How do you know that the complete content of the HTML page is also present in the generated PDF? Do you know the boundary conditions required by the rendering tool? Does your HTML meets these requirements?

Solution Approach

The test starts by reading the HTML page using Selenium. Text is extracted with Selenium and stored in local variables.

Then the PDF generation is started through the web page and the resulting document is selected again with Selenium. Finally the stored text can be used in PDFUnit test methods.


 * This sample shows how to test an HTML page with Selenium, then let it be rendered 
 * by the server to PDF and verify that content also appears in PDF.
 * @author Carsten Siedentop, February 2013
public class Html2PDFTest {

  private WebDriver driver;

  public void testHtml2PDFRenderer_WikipediaSeleniumEnglish() throws Exception {
    String urlWikipediaSelenium = "http://en.wikipedia.org/wiki/Selenium_%28software%29";

    String section1  = "History";
    String section2  = "Components";
    String section3  = "The Selenium ecosystem";
    String section4  = "References";
    String section5  = "External links";

    String linkName = "Download as PDF";
    URL url = loadPDF(linkName);
              .containing(section1,  WhitespaceProcessing.IGNORE)
              .containing(section2,  WhitespaceProcessing.IGNORE)
              .containing(section3,  WhitespaceProcessing.IGNORE)
              .containing(section4,  WhitespaceProcessing.IGNORE)
              .containing(section5,  WhitespaceProcessing.IGNORE)

  private void assertLinkPresent(String partOfLinkText) {
    driver.findElement(By.xpath("//a[./span = '" + partOfLinkText + "']"));

  private URL loadPDF(String linkName_LoadAsPDF) throws Exception {
    String title = "Rendering finished - Wikipedia, the free encyclopedia";
    assertEquals(title, driver.getTitle());
    WebElement element = driver.findElement(By.linkText("Download the file"));
    String hrefValue = element.getAttribute("href");
    URL url = new URL(hrefValue); 
    return url;

  public void tearDown() throws Exception {

  public void createDriver() throws Exception {
    driver = new HtmlUnitDriver();