title: OpenHTMLToPDF: Embed a custom font into PDF created out of HTML
body: <p>I create a PDF from HTML with Jsoup and <a href="https://github.com/danfickle/openhtmltopdf" rel="nofollow noreferrer">OpenHTMLToPDF</a>. I have to use a different font in my PDF to have non-latin glyphcs covered (see <a href="https://stackoverflow.com/questions/5425251/using-pdfbox-to-write-utf-8-encoded-strings-to-a-pdf/5427096#5427096">here</a>). How can I embed my font correctly?</p>
  
  <h2>Simplified program reproducing the issue:</h2>
  
  <p><strong>src/main/resources/test.html</strong></p>
  
  <pre><code>&lt;!DOCTYPE html&gt;
  &lt;html&gt;
      &lt;head&gt;
          &lt;meta charset="UTF-8" /&gt;
          &lt;title&gt;Font Test&lt;/title&gt;
          &lt;style&gt;
              @font-face {
                  font-family: 'source-sans';
                  font-style: normal;
                  font-weight: 400;
                  src: url(fonts/SourceSansPro-Regular.ttf);
              }
          &lt;/style&gt;
      &lt;/head&gt;
      &lt;body&gt;    
          &lt;p style="font-family: 'source-sans',serif"&gt;Latin Script&lt;/p&gt;
          &lt;p style="font-family: 'source-sans',serif"&gt;Είμαι ελληνικό κείμενο.&lt;/p&gt;
      &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>
  
  <ul>
  <li>this file shall be written as PDF</li>
  <li>In a browser it looks correctly and uses the Source Sans font.</li>
  </ul>
  
  <p><strong>src/main/java/main.java:</strong></p>
  
  <pre><code>import com.openhtmltopdf.extend.FSSupplier;
  import com.openhtmltopdf.pdfboxout.PdfRendererBuilder;
  import org.jsoup.Jsoup;
  import org.jsoup.helper.W3CDom;
  import org.w3c.dom.Document;
  
  import java.io.*;
  import java.nio.charset.StandardCharsets;
  import java.util.Objects;
  
  public class main {
      public static void main(String[] args) {
          System.out.println("Starting");
  
          try {
  
              final W3CDom w3cDom = new W3CDom();
              final Document w3cDoc = w3cDom.fromJsoup(Jsoup.parse(readFile()));
              final OutputStream outStream = new FileOutputStream("test.pdf");
  
              final PdfRendererBuilder pdfBuilder = new PdfRendererBuilder();
              pdfBuilder.useFastMode();
              pdfBuilder.withW3cDocument(w3cDoc, "/");
              pdfBuilder.useFont(new File(main.class.getClassLoader().getResource("fonts/SourceSansPro-Regular.ttf").getFile()), "source-sans");
              pdfBuilder.toStream(outStream);
  
              pdfBuilder.run();
              outStream.close();
  
          } catch (Exception e) {
              System.out.println("PDF could not be created: " + e.getMessage());
          }
  
          System.out.println("Finish.");
      }
  
  
      private static String readFile() throws IOException {
          final ClassLoader classLoader = main.class.getClassLoader();
          final InputStream inputStream = classLoader.getResourceAsStream("test.html");
          final StringBuilder sb = new StringBuilder();
          final Reader r = new InputStreamReader(Objects.requireNonNull(inputStream), StandardCharsets.UTF_8);
          char[] buf = new char[1024];
          int amt = r.read(buf);
          while(amt &gt; 0) {
              sb.append(buf, 0, amt);
              amt = r.read(buf);
          }
          return sb.toString();
      }
  }
  </code></pre>
  
  <ul>
  <li>Don't bother about the second function, it just reads the HTML file and is only included here, to have a complete program.</li>
  </ul>
  
  <p><strong>src/main/resources/fonts/SourceSansPro-regular.ttf</strong></p>
  
  <ul>
  <li>Download here: <a href="https://www.fontsquirrel.com/fonts/source-sans-pro" rel="nofollow noreferrer">https://www.fontsquirrel.com/fonts/source-sans-pro</a></li>
  </ul>
  
  <p><strong>pom.xml</strong></p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &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/xsd/maven-4.0.0.xsd"&gt;
      &lt;modelVersion&gt;4.0.0&lt;/modelVersion&gt;
  
      &lt;groupId&gt;paf&lt;/groupId&gt;
      &lt;artifactId&gt;test&lt;/artifactId&gt;
      &lt;version&gt;1.0-SNAPSHOT&lt;/version&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;configuration&gt;
                      &lt;source&gt;7&lt;/source&gt;
                      &lt;target&gt;7&lt;/target&gt;
                  &lt;/configuration&gt;
              &lt;/plugin&gt;
          &lt;/plugins&gt;
      &lt;/build&gt;
      &lt;dependencies&gt;
          &lt;dependency&gt;
              &lt;groupId&gt;com.openhtmltopdf&lt;/groupId&gt;
              &lt;artifactId&gt;openhtmltopdf-pdfbox&lt;/artifactId&gt;
              &lt;version&gt;0.0.1-RC18&lt;/version&gt;
          &lt;/dependency&gt;
          &lt;dependency&gt;
              &lt;groupId&gt;org.jsoup&lt;/groupId&gt;
              &lt;artifactId&gt;jsoup&lt;/artifactId&gt;
              &lt;version&gt;1.11.2&lt;/version&gt;
          &lt;/dependency&gt;
      &lt;/dependencies&gt;
  &lt;/project&gt;
  </code></pre>
  
  <h2>Program output:</h2>
  
  <pre><code>Starting
  com.openhtmltopdf.load INFO:: TIME: parse stylesheets  148ms
  com.openhtmltopdf.match INFO:: media = print
  com.openhtmltopdf.match INFO:: Matcher created with 147 selectors
  com.openhtmltopdf.load INFO:: Loading font(source-sans) from InputStream supplier now.
  com.openhtmltopdf.exception WARNING:: bad URL given: /fonts/SourceSansPro-Regular.ttf
  com.openhtmltopdf.exception WARNING:: Could not load @font-face font: /fonts/SourceSansPro-Regular.ttf
  com.openhtmltopdf.exception WARNING:: Font metrics not available. Probably a bug.
  com.openhtmltopdf.exception WARNING:: Font metrics not available. Probably a bug.
  com.openhtmltopdf.render WARNING:: Font is null.
  com.openhtmltopdf.render WARNING:: Font is null.
  com.openhtmltopdf.render WARNING:: Font is null.
  com.openhtmltopdf.render WARNING:: Font is null.
  com.openhtmltopdf.render WARNING:: Font is null.
  com.openhtmltopdf.render WARNING:: Font is null.
  com.openhtmltopdf.render WARNING:: Font is null.
  Finish.
  </code></pre>
  
  <h2>Resulting PDF</h2>
  
  <pre><code>Latin Script
  ##### ######## #######.
  </code></pre>
  
  <ul>
  <li>In Serif-Font.</li>
  </ul>
  
  <hr>
  
  <p>Edit 1: Various changes according to the pages linked in the comments and updated to RC18. new output now, but font in PDF still not right.</p>
  
  <hr>
  
  <p>Edit 2: Tried fast Renderer</p>