Package jfix.html

Source Code of jfix.html.Html2Pdf

/*
    Copyright (C) 2010 maik.jablonski@gmail.com

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/
package jfix.html;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FilenameFilter;
import java.net.URL;

import org.xhtmlrenderer.pdf.ITextRenderer;

import com.lowagie.text.DocumentException;

/**
* Converts a XHTML-document into PDF.
*
* Dependencies: flyingsaucer.jar, itext.jar
*
* <code>
* Usage:
*
* new Html2Pdf(xhtmlString).getBytes()
* or
* new Html2Pdf(url).getBytes()
* or
* new Html2Pdf(file).getBytes()
* <code>
*
* Drop additional TTF-fonts in JRE/lib/fonts.
*
* @author Maik Jablonski
*/
public class Html2Pdf {

  public static String PAGE_BREAK = "<!-- pagebreak -->";

  private final static String JRE_FONT_DIR = System.getProperty("java.home")
      + File.separator + "lib" + File.separator + "fonts";

  private final static File[] TTF_FILES = new File(JRE_FONT_DIR)
      .listFiles(new FilenameFilter() {
        public boolean accept(File dir, String name) {
          return name.endsWith(".ttf");
        }
      });

  private String xhtml;
  private URL url;
  private File file;

  public Html2Pdf(String xhtml) {
    this.xhtml = xhtml;
  }

  public Html2Pdf(URL url) {
    this.url = url;
  }

  public Html2Pdf(File file) {
    this.file = file;
  }

  public byte[] getBytes() {
    try {
      ITextRenderer itext = new ITextRenderer();
      if (TTF_FILES != null) {
        for (File file : TTF_FILES) {
          try {
            itext.getFontResolver().addFont(file.getAbsolutePath(),
                true);
          } catch (DocumentException e) {
            // pass non-embeddable fonts
          }
        }
      }
      if (xhtml != null) {
        itext.setDocumentFromString(transform(xhtml));
      }
      if (url != null) {
        itext.setDocument(url.toString());
      }
      if (file != null) {
        itext.setDocument(file);
      }
      itext.layout();
      ByteArrayOutputStream bos = new ByteArrayOutputStream();
      itext.createPDF(bos, true);
      return bos.toByteArray();
    } catch (Exception e) {
      throw new RuntimeException(e);
    }
  }

  protected String transform(String xhtml) {
    return xhtml.replaceAll(PAGE_BREAK,
        "<div style=\"page-break-after:always;\"></div>").trim();
  }
}
TOP

Related Classes of jfix.html.Html2Pdf

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.