title: Using iText, how can I apply css to my PDF document using CssResolver and CssFile?
body: <p>Thanks for your help...</p>
  
  <h1>Problem</h1>
  
  <p>While converting an html table tag/snippet (which I have converted to string) into a PDF document...</p>
  
  <p>I am able to <strong>successfully</strong>  apply css styles to the PDF document using this technique...</p>
  
  <pre><code>CSSResolver cssResolver = XMLWorkerHelper.getInstance().getDefaultCssResolver(false);  
  cssResolver.addCss("td {border-right: white .1px solid;}", true);
  </code></pre>
  
  <p>But, I am <strong>unsuccessful</strong> applying css to the PDF document using existing a css file (i.e., using CssFile object),  like this...</p>
  
  <pre><code>CSSResolver cssResolver = new StyleAttrCSSResolver();
  InputStream csspathtest = Thread.currentThread().getContextClassLoader().getResourceAsStream("styles/itextweb.css");            
  CssFile cssfiletest = XMLWorkerHelper.getCSS(csspathtest);
  cssResolver.addCss(cssfiletest);             
  </code></pre>
  
  <p>...I have not been able to get this working, and dont know what the issue is...I'm getting...</p>
  
  <pre><code>Java.io.IOException The document has no pages
  </code></pre>
  
  <h1>Question:</h1>
  
  <p>How do I properly use CssFile with CssResolver to apply css styles -- i.e., from existing ".css" files -- to my PDF document?   (What is wrong with the way I am using iText to accomplish this?)</p>
  
  <p>(Again, thank you for any help/guidance on this)</p>
  
  <p>================= BELOW IS MORE DETAILED INFOMATION ===================</p>
  
  <p>Java 6, JSF (Mojarra) 2.1.11, Primefaces v3.4.2, itextpdf v5.3.4, xmlworker v1.2.1</p>
  
  <h1>This is the "printPDF" function in question...</h1>
  
  <pre><code>public void createPDF() throws DocumentException, CssResolverException
  {
      FacesContext context = FacesContext.getCurrentInstance();
      ExternalContext econtext = context.getExternalContext();
  
      try
      {
          String htmlstring   = context.getExternalContext().getRequestParameterMap().get("testForm:htmlstring");
  
          InputStream is = new ByteArrayInputStream(htmlstring.getBytes());             
          ByteArrayOutputStream baos = new ByteArrayOutputStream(); 
  
          // step 1
          Document document = new Document();
  
          // step 2
          PdfWriter writer = PdfWriter.getInstance(document, baos);
  
          writer.setInitialLeading(12.5f);
  
          // step 3
          document.open();
  
          HtmlPipelineContext htmlContext = new HtmlPipelineContext(null);
  
          htmlContext.setTagFactory(Tags.getHtmlTagProcessorFactory());
  
          // CSS
          CSSResolver cssResolver = new StyleAttrCSSResolver();
          InputStream csspathtest = Thread.currentThread().getContextClassLoader().getResourceAsStream("styles/itextweb.css");            
          CssFile cssfiletest = XMLWorkerHelper.getCSS(csspathtest);
          cssResolver.addCss(cssfiletest);             
  
          Pipeline&lt;?&gt; pipeline =  new CssResolverPipeline(cssResolver, new HtmlPipeline(htmlContext, new PdfWriterPipeline(document, writer)));
  
          XMLWorker worker = new XMLWorker(pipeline, true);
          XMLParser p = new XMLParser(worker);
          p.parse(is); //new FileInputStream("results/demo2/walden.html"));
  
          // step     
          document.close();
  
          //post back...
          HttpServletResponse response = (HttpServletResponse) context.getExternalContext().getResponse();
          response.setContentType("application/pdf");
          response.setHeader("Expires", "0");
          response.setHeader("Cache-Control", "must-revalidate, post-check=0, pre-check=0");           
          response.setHeader("Content-Type", "application/pdf");
          response.setHeader("Content-disposition","attachment;filename=file.pdf");
          response.setContentLength(baos.size());
          OutputStream os = response.getOutputStream();
          baos.writeTo(os);
          os.flush();
          os.close();
          context.responseComplete();
      }
      catch (FileNotFoundException e)
      {
          e.printStackTrace();
      }
      catch (IOException e)
      {
          e.printStackTrace();
      }
      catch (DocumentException e)
      {
          e.printStackTrace();
      }
  }
  </code></pre>
  
  <h1>This is the page containing the html table snippet to be parsed to pdf (i.e., id="table1")...</h1>
  
  <pre><code>&lt;?xml version='1.0' encoding='UTF-8' ?&gt;
  &lt;!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"&gt;
  &lt;html xmlns="http://www.w3.org/1999/xhtml"
        xmlns:ui="http://java.sun.com/jsf/facelets"
        xmlns:h="http://java.sun.com/jsf/html"
        xmlns:f="http://java.sun.com/jsf/core"
        xmlns:c="http://java.sun.com/jsp/jstl/core"
        xmlns:p="http://primefaces.org/ui"&gt;
      &lt;f:view contentType="text/html"&gt;
          &lt;h:head&gt;
              &lt;title&gt;test html-to-pdf with itext...&lt;/title&gt;
              &lt;meta charset="utf-8" /&gt;
          &lt;/h:head&gt;
          &lt;h:body&gt;
              &lt;h:form id="testForm"&gt;
  
                  &lt;p:panel id="queryPanel"  header="...test itext html-to-pdf conversion..." style="width:100%;"&gt;
  
                      &lt;table id='table1'&gt;
                          &lt;thead class="dt-thd"&gt;
                              &lt;tr id="table1-h-hdr-row"&gt;
                                  &lt;th style="width: 120px" class="dt-hhdr-c " &gt;Last name&lt;/th&gt;
                                  &lt;th style="width: 120px" class="dt-hhdr-c " &gt;First Name&lt;/th&gt;
                                  &lt;th style="width: 120px" class="dt-hhdr-c " &gt;Middle Name&lt;/th&gt;
                                  &lt;th style="width: 180px" class="dt-hhdr-c " &gt;Date Of Birth&lt;/th&gt;
  
                              &lt;/tr&gt;
                          &lt;/thead&gt;
                          &lt;tbody&gt;
                              &lt;tr class="dt-r-even" onclick="uiOnRowClick('table1', 14, this)"&gt;
                                  &lt;td style="width: 120px" class="dt-c  row-selected-left" &gt;&lt;a class="column-link " onfocus="uiOnRowClick('table1', 14, this.parentNode.parentNode)" onclick="storeFilters()" href="#"&gt;lastnameAAA&lt;/a&gt;&lt;/td&gt;
                                  &lt;td style="width: 120px" class="dt-c  row-selected" &gt;firstnameAAA&lt;/td&gt;
                                  &lt;td style="width: 120px" class="dt-c  row-selected" &gt;A&lt;/td&gt;
                                  &lt;td style="width: 180px" class="dt-c  row-selected" &gt;11/27/1971&lt;/td&gt;
                              &lt;/tr&gt;
                              &lt;tr class="dt-r-odd" onclick="uiOnRowClick('table1', 14, this)"&gt;
                                  &lt;td style="width: 120px" class="dt-c " &gt;&lt;a class="column-link " onfocus="uiOnRowClick('table1', 14, this.parentNode.parentNode)" onclick="storeFilters()" href="#"&gt;lastnameBBB&lt;/a&gt;&lt;/td&gt;
                                  &lt;td style="width: 120px" class="dt-c " &gt;firstnameBBB&lt;/td&gt;
                                  &lt;td style="width: 120px" class="dt-c " &gt;B&lt;/td&gt;
                                  &lt;td style="width: 180px" class="dt-c " &gt;01/15/1951&lt;/td&gt;
                              &lt;/tr&gt;
                              &lt;tr class="dt-r-even" onclick="uiOnRowClick('table1', 14, this)"&gt;
                                  &lt;td style="width: 120px" class="dt-c " &gt;&lt;a class="column-link " onfocus="uiOnRowClick('table1', 14, this.parentNode.parentNode)" onclick="storeFilters()" href="#"&gt;lastnameCCC&lt;/a&gt;&lt;/td&gt;
                                  &lt;td style="width: 120px" class="dt-c " &gt;firstnameCCC&lt;/td&gt;
                                  &lt;td style="width: 120px" class="dt-c " &gt;C&lt;/td&gt;
                                  &lt;td style="width: 180px" class="dt-c " &gt;02/16/1962&lt;/td&gt;
                              &lt;/tr&gt;
                              &lt;tr class="dt-r-odd" onclick="uiOnRowClick('table1', 14, this)"&gt;
                                  &lt;td style="width: 120px" class="dt-c " &gt;&lt;a class="column-link " onfocus="uiOnRowClick('table1', 14, this.parentNode.parentNode)" onclick="storeFilters()" href="#"&gt;lastnameDDD&lt;/a&gt;&lt;/td&gt;
                                  &lt;td style="width: 120px" class="dt-c " &gt;firstnameDDD&lt;/td&gt;
                                  &lt;td style="width: 120px" class="dt-c " &gt;D&lt;/td&gt;
                                  &lt;td style="width: 180px" class="dt-c " &gt;03/17/1973&lt;/td&gt;
                              &lt;/tr&gt;
                              &lt;tr class="dt-r-even" onclick="uiOnRowClick('table1', 14, this)"&gt;
                                  &lt;td style="width: 120px" class="dt-c " &gt;&lt;a class="column-link " onfocus="uiOnRowClick('table1', 14, this.parentNode.parentNode)" onclick="storeFilters()" href="#"&gt;lastnameEEE&lt;/a&gt;&lt;/td&gt;
                                  &lt;td style="width: 120px" class="dt-c " &gt;firstnameEEE&lt;/td&gt;
                                  &lt;td style="width: 120px" class="dt-c " &gt;E&lt;/td&gt;
                                  &lt;td style="width: 180px" class="dt-c " &gt;04/18/1984&lt;/td&gt;
                              &lt;/tr&gt;
                              &lt;tr class="dt-r-odd" onclick="uiOnRowClick('table1', 14, this)"&gt;
                                  &lt;td style="width: 120px" class="dt-c " &gt;&lt;a class="column-link " onfocus="uiOnRowClick('table1', 14, this.parentNode.parentNode)" onclick="storeFilters()" href="#"&gt;lastnameFFF&lt;/a&gt;&lt;/td&gt;
                                  &lt;td style="width: 120px" class="dt-c " &gt;firstnameFFF&lt;/td&gt;
                                  &lt;td style="width: 120px" class="dt-c " &gt;F&lt;/td&gt;
                                  &lt;td style="width: 180px" class="dt-c " &gt;05/19/1995&lt;/td&gt;
                              &lt;/tr&gt;
                          &lt;/tbody&gt;
                      &lt;/table&gt;
  
                      &lt;p:commandButton
                          id="printPdf"
                          value="Print"
                          action="#{testBean.createPDF2}"
                          ajax="false"
                          onclick="printPreview(this);this.form.target='_blank'"/&gt;
  
                      &lt;h:inputHidden id="htmlstring" value="no value"/&gt;
  
                  &lt;/p:panel&gt;
  
              &lt;/h:form&gt;
  
              &lt;h:outputStylesheet  library="styles"    name="itextweb.css"      /&gt;
              &lt;h:outputScript      library="primefaces" name="/jquery/jquery.js" /&gt;
              &lt;h:outputScript      library="primefaces" name="/jquery/plugins/ui/jquery-ui.custom.js" /&gt;
              &lt;h:outputScript      library="primefaces" name="/jquery/plugins/inputmask/maskedinput.js" /&gt;
              &lt;h:outputScript      library="js"         name="itextweb.js" /&gt;
          &lt;/h:body&gt;
      &lt;/f:view&gt;
  &lt;/html&gt;
  </code></pre>
  
  <h1>Here is the javascript used...</h1>
  
  <pre><code>function uiOnRowClick(a, b, c)
  {
      alert("uiOnRowClick(a,b,c) function called...blah...");
  }
  
  function storeFilters()
  {
      alert("storeFilters() function called...bleah...");
  }
  
  function printPreview(e)
  {
      var t = document.getElementById("table1");
      var htmlstring = "&lt;table id='table1-hdr' class='dt' style='width:2416px;position:absolute'&gt;" + t.innerHTML + "&lt;/table&gt;";
      document.getElementById('testForm:htmlstring').value = htmlstring;
  }
  </code></pre>
  
  <h1>Here is the CSS stylesheet for this example...</h1>
  
  <pre><code>.text1
  {
      background-color: transparent !important;
      font-weight: bold;
      font-size: 2em;
      color: blue;
      text-align:center;
  }
  
  .ui-inputfield {
      background: white !important;
      height: 10px !important;
      vertical-align: middle;
      display:inline-block;
      white-space: nowrap;
  }
  
  
  .ui-button
  {
      margin-top: .5px !important;
      vertical-align: middle !important;
      display:inline-block !important;
      white-space: nowrap !important;
      text-align: center !important;
  }
  
  .ui-message-error
  {
      background: transparent !important;
      border: none !important;
      font-size: .9em !important;
      font-weight: normal !important;
      font-family: Arial, sans-serif !important;
  }
  
  .ui-message-error-icon {
      display: none;
  }
  
  .ui-messages-error
  {
      background: transparent !important;
      border: none !important;
      font-size: .9em !important;
      font-weight: normal !important;
      font-family: Arial, sans-serif !important;
  }
  
  .ui-messages-error-icon {
      display: none;
  }
  
  .ui-inputfield.ui-state-error
  {
      background: pink !important;
  }
  
  form *
  {
  box-sizing: content-box !important;
  -moz-box-sizing: content-box !important;
  -ms-box-sizing: content-box !important;
  }
  
  
  .ui-widget, .ui-widget .ui-widget
  {
  font-size: 90% !important;
  }
  
  
  .dt-thd
  {
  
  }
  
  .table1-h-hdr-row
  {
  
  }
  
  .dt-hhdr-c
  {
      color: blue;
      background-color:  lightgray;
  }
  
  .dt-r-odd
  {
      background-color: aliceblue;
  }
  
  .dt-r-even
  {
      background-color:  lightskyblue;
  }
  
  .dt-c
  {
      font-size: 8px;
      font-weight: normal;
  }
  </code></pre>
  
  <h1>Below is the pom.xml used with this example (which illustrates dependenies/versions/etc)...</h1>
  
  <pre><code>&lt;project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
           xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"&gt;
      &lt;modelVersion&gt;4.0.0&lt;/modelVersion&gt;
      &lt;groupId&gt;aaa.bbb.ccc&lt;/groupId&gt;
      &lt;artifactId&gt;itextweb-war&lt;/artifactId&gt;
      &lt;packaging&gt;war&lt;/packaging&gt;
      &lt;version&gt;1&lt;/version&gt;
      &lt;name&gt;itextweb-war&lt;/name&gt;
      &lt;url&gt;http://maven.apache.org&lt;/url&gt;
  
      &lt;dependencies&gt;
  
          &lt;dependency&gt;
              &lt;groupId&gt;javax.servlet&lt;/groupId&gt;
              &lt;artifactId&gt;servlet-api&lt;/artifactId&gt;
              &lt;version&gt;2.5&lt;/version&gt;
              &lt;scope&gt;provided&lt;/scope&gt;
          &lt;/dependency&gt;
  
          &lt;dependency&gt;
              &lt;groupId&gt;javax.servlet&lt;/groupId&gt;
              &lt;artifactId&gt;jstl&lt;/artifactId&gt;
              &lt;version&gt;1.2&lt;/version&gt;
          &lt;/dependency&gt;
  
          &lt;dependency&gt;
              &lt;groupId&gt;com.sun.faces&lt;/groupId&gt;
              &lt;artifactId&gt;jsf-api&lt;/artifactId&gt;
              &lt;version&gt;2.1.11&lt;/version&gt;
          &lt;/dependency&gt;
  
          &lt;dependency&gt;
              &lt;groupId&gt;com.sun.faces&lt;/groupId&gt;
              &lt;artifactId&gt;jsf-impl&lt;/artifactId&gt;
              &lt;version&gt;2.1.11&lt;/version&gt;
          &lt;/dependency&gt;
  
          &lt;dependency&gt;
              &lt;groupId&gt;javax.el&lt;/groupId&gt;
              &lt;artifactId&gt;el-api&lt;/artifactId&gt;
              &lt;version&gt;2.2&lt;/version&gt;
          &lt;/dependency&gt;
          &lt;dependency&gt;
              &lt;groupId&gt;org.glassfish.web&lt;/groupId&gt;
              &lt;artifactId&gt;el-impl&lt;/artifactId&gt;
              &lt;version&gt;2.2&lt;/version&gt;
          &lt;/dependency&gt;
  
          &lt;dependency&gt;
              &lt;groupId&gt;org.springframework&lt;/groupId&gt;
              &lt;artifactId&gt;spring-context&lt;/artifactId&gt;
              &lt;version&gt;3.1.1.RELEASE&lt;/version&gt;
          &lt;/dependency&gt;
  
          &lt;dependency&gt;
              &lt;groupId&gt;org.springframework&lt;/groupId&gt;
              &lt;artifactId&gt;spring-webmvc&lt;/artifactId&gt;
              &lt;version&gt;3.1.1.RELEASE&lt;/version&gt;
          &lt;/dependency&gt;
  
          &lt;dependency&gt;
              &lt;groupId&gt;commons-lang&lt;/groupId&gt;
              &lt;artifactId&gt;commons-lang&lt;/artifactId&gt;
              &lt;version&gt;2.6&lt;/version&gt;
          &lt;/dependency&gt;
  
          &lt;dependency&gt;
              &lt;groupId&gt;commons-io&lt;/groupId&gt;
              &lt;artifactId&gt;commons-io&lt;/artifactId&gt;
              &lt;version&gt;2.4&lt;/version&gt;
          &lt;/dependency&gt;
  
          &lt;dependency&gt;
              &lt;groupId&gt;org.primefaces&lt;/groupId&gt;
              &lt;artifactId&gt;primefaces&lt;/artifactId&gt;
              &lt;version&gt;3.4.2&lt;/version&gt;
          &lt;/dependency&gt;
  
          &lt;dependency&gt;
              &lt;groupId&gt;org.primefaces.themes&lt;/groupId&gt;
              &lt;artifactId&gt;aristo&lt;/artifactId&gt;
              &lt;version&gt;1.0.1&lt;/version&gt;
          &lt;/dependency&gt;
  
          &lt;dependency&gt;
              &lt;groupId&gt;com.itextpdf&lt;/groupId&gt;
              &lt;artifactId&gt;itextpdf&lt;/artifactId&gt;
              &lt;version&gt;5.3.4&lt;/version&gt;
          &lt;/dependency&gt;
  
          &lt;dependency&gt;
              &lt;groupId&gt;com.itextpdf.tool&lt;/groupId&gt;
              &lt;artifactId&gt;xmlworker&lt;/artifactId&gt;
              &lt;version&gt;1.2.1&lt;/version&gt;
          &lt;/dependency&gt;
  
      &lt;/dependencies&gt;
      &lt;build&gt;
          &lt;plugins&gt;
              &lt;plugin&gt;
                  &lt;groupId&gt;org.apache.maven.plugins&lt;/groupId&gt;
                  &lt;artifactId&gt;maven-compiler-plugin&lt;/artifactId&gt;
                  &lt;version&gt;2.3.2&lt;/version&gt;
                  &lt;configuration&gt;
                      &lt;source&gt;1.6&lt;/source&gt;
                      &lt;target&gt;1.6&lt;/target&gt;
                  &lt;/configuration&gt;
              &lt;/plugin&gt;
              &lt;plugin&gt;
                  &lt;groupId&gt;org.apache.maven.plugins&lt;/groupId&gt;
                  &lt;artifactId&gt;maven-war-plugin&lt;/artifactId&gt;
                  &lt;version&gt;2.3&lt;/version&gt;
              &lt;/plugin&gt;
  
          &lt;/plugins&gt;
          &lt;finalName&gt;${project.name}-${project.version}&lt;/finalName&gt;
      &lt;/build&gt;
  &lt;/project&gt;
  </code></pre>