title: Generate PDF base64 encode from iText in Java
body: <p>How can I encode a PDF file generated in iText to base64?</p>
  
  <p>This is my (java) code</p>
  
  <p><strong>App.java</strong></p>
  
  <pre><code>public class App {
  
  public static void main(String[] args) throws DocumentException, IOException {
      // step 1
      Document document = new Document();
      // step 2
      PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream("pdf.pdf"));
      // step 3
      document.open();
  
      String str = "&lt;html&gt;&lt;head&gt;&lt;/head&gt;&lt;body style=\"font-size:12.0pt; font-family:Times New Roman\"&gt;"
              + "&lt;a href='http://www.rgagnon.com/howto.html'&gt;&lt;b&gt;Real's HowTo&lt;/b&gt;&lt;/a&gt;" + "&lt;h1&gt;Show your support&lt;/h1&gt;"
              + "&lt;p&gt;It DOES cost a lot to produce this site - in ISP storage and transfer fees&lt;/p&gt;"
              + "&lt;p&gt;TEST POLSKICH ZNAKÓW: \u0104\u0105\u0106\u0107\u00d3\u00f3\u0141\u0142\u0179\u017a\u017b\u017c\u017d\u017e\u0118\u0119&lt;/p&gt;"
              + "&lt;hr/&gt;"
              + "&lt;p&gt;the huge amounts of time it takes for one person to design and write the actual content.&lt;/p&gt;"
              + "&lt;p&gt;If you feel that effort has been useful to you, perhaps you will consider giving something back?&lt;/p&gt;"
              + "&lt;p&gt;Donate using PayPal\u017d&lt;/p&gt;" + "&lt;p&gt;Contributions via PayPal are accepted in any amount&lt;/p&gt;"
              + "&lt;p&gt;&lt;br/&gt;&lt;table border='1'&gt;&lt;tr&gt;&lt;td&gt;Java HowTo&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;"
              + "&lt;td style='background-color:red;'&gt;Javascript HowTo&lt;/td&gt;&lt;/tr&gt;"
              + "&lt;tr&gt;&lt;td&gt;Powerbuilder HowTo&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;&lt;/p&gt;" + "&lt;/body&gt;&lt;/html&gt;";
  
      XMLWorkerHelper worker = XMLWorkerHelper.getInstance();
      InputStream is = new ByteArrayInputStream(str.getBytes(StandardCharsets.UTF_8));
  
      worker.parseXHtml(writer, document, is, Charset.forName("UTF-8"));
  
      // step 5
      document.close();
  
      System.out.println("PDF Created!");
  
  }
  </code></pre>
  
  <p>}</p>
  
  <p>iText library dependencies (xmlworker e itextpdf)</p>
  
  <p><strong>Pom file:</strong></p>
  
  <pre><code>    &lt;dependencies&gt;
      &lt;dependency&gt;
          &lt;groupId&gt;com.itextpdf&lt;/groupId&gt;
          &lt;artifactId&gt;itextpdf&lt;/artifactId&gt;
          &lt;version&gt;5.4.2&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;5.4.1&lt;/version&gt;
      &lt;/dependency&gt;
  &lt;/dependencies&gt;
  </code></pre>
  
  <p>Im stuck, any help is welcome.</p>