Example usage for com.lowagie.text.pdf BaseFont EMBEDDED

List of usage examples for com.lowagie.text.pdf BaseFont EMBEDDED

Introduction

In this page you can find the example usage for com.lowagie.text.pdf BaseFont EMBEDDED.

Prototype

boolean EMBEDDED

To view the source code for com.lowagie.text.pdf BaseFont EMBEDDED.

Click Source Link

Document

if the font has to be embedded

Usage

From source file:com.estate.report.Scenario.java

public void drawLifeExp() {
    PdfContentByte cb = writer.getDirectContent();
    char c = userInfo.getClientFirstName().charAt(0);
    char s = ' ';

    if (!userInfo.isSingle()) {
        s = userInfo.getSpouseFirstName().charAt(0);
    }// w ww  . j  av a 2  s .  c  o  m
    String cText = Character.toString(c) + ":" + userInfo.getClientFirstName() + " actuarial life expectancy - "
            + Integer.toString(userInfo.getClientLifeExpectancy()) + " years";

    String sText = Character.toString(s) + ":" + userInfo.getSpouseFirstName() + " actuarial life expectancy - "
            + Integer.toString(userInfo.getSpouseLifeExpectancy()) + " years";

    try {
        BaseFont font = BaseFont.createFont(Locations.getFontLocation() + "times.ttf", BaseFont.CP1252,
                BaseFont.EMBEDDED);
        cb.beginText();
        cb.setFontAndSize(font, 8);
        cb.setRGBColorFill(255, 0, 0);
        cb.setTextMatrix(72 * .5f, .5f * 72);
        cb.showText(cText);
        cb.endText();
        if (!userInfo.isSingle()) {
            cb.beginText();
            cb.setFontAndSize(font, 8);
            cb.setRGBColorFill(255, 0, 0);
            cb.setTextMatrix(72 * .5f, .35f * 72);
            cb.showText(sText);
            cb.endText();
        }
    } catch (Exception e) {
        System.err.println("Error printing Life Expectancy");
    }
}

From source file:com.fcore.base.fileSystem.utils.FileUtil.java

/**
 * convert txt file to pdf //from   w  w w  .  j  ava 2s  .  c  o  m
 * @param originalPath
 * @param dirPath
 */
public static void txt2pdf(String originalPath, String dirPath) {
    com.lowagie.text.Document document = null;
    BufferedReader read = null;
    long old = System.currentTimeMillis();
    try {
        document = new com.lowagie.text.Document(PageSize.A4, 80, 80, 60, 30);
        PdfWriter.getInstance(document, new FileOutputStream(dirPath));
        document.open();
        BaseFont bfChinese = BaseFont.createFont(ReadCreatePdf.class.getResource("/") + "/simsun.ttc,1",
                BaseFont.IDENTITY_H, BaseFont.EMBEDDED);
        Font FontChinese = new Font(bfChinese, 18, Font.NORMAL);

        FileInputStream fstream = new FileInputStream(originalPath);
        DataInputStream in = new DataInputStream(fstream);
        read = new BufferedReader(new InputStreamReader(in, "gb2312"));
        String line = null;
        while ((line = read.readLine()) != null) {
            Paragraph t = new Paragraph(line, FontChinese);
            t.setAlignment(Element.ALIGN_LEFT);
            t.setLeading(20.0f);
            document.add(t);
        }
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        try {
            read.close();
            document.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    long now = System.currentTimeMillis();
    System.out.println("" + ((now - old) / 1000.0) + "\n\n" + "?:" + dirPath);
}

From source file:com.google.api.ads.adwords.awreporting.exporter.HTMLExporter.java

License:Open Source License

/**
 * Convert a given HTML report {@link InputStream} to a PDF file.<br>
 * //from w w w .  j  av  a  2  s  . c  o m
 * This method does not close the report InputStream. It is the responsibility 
 * of the caller to do so.
 *
 * @param document The HTML Document
 * @param reportWriter the ReportWriter to which HTML should be written
 * @throws DocumentException error creating PDF file
 * @throws IOException error closing file
 */
public static void exportHtmlToPdf(Document document, ReportWriter reportWriter, List<String> fontPaths)
        throws DocumentException, IOException {

    ITextRenderer renderer = new ITextRenderer();

    // Adding fonts for PDF generation
    if (fontPaths != null) {
        for (String font : fontPaths) {
            renderer.getFontResolver().addFont(font, BaseFont.IDENTITY_H, BaseFont.EMBEDDED);
        }
    }

    renderer.getSharedContext().setReplacedElementFactory(
            new MediaReplacedElementFactory(renderer.getSharedContext().getReplacedElementFactory()));
    renderer.setDocument(document, null);
    renderer.layout();

    ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
    renderer.createPDF(outputStream, true);
    ByteArrayInputStream is = new ByteArrayInputStream(outputStream.toByteArray());
    reportWriter.write(is);

    outputStream.flush();
    outputStream.close();
}

From source file:com.gtdfree.test.PDFTestFont.java

License:Open Source License

/**
 * Fonts and encoding./*from   w  w w  . j  a v  a2s  .  c  o m*/
 * @param args no arguments needed
 */
public static void main(String[] args) {

    System.out.println("Encodings");

    String[] names = GraphicsEnvironment.getLocalGraphicsEnvironment().getAvailableFontFamilyNames();

    System.out.println(Arrays.toString(names));

    /*System.out.println("---");
    try {
    System.getProperties().store(System.out, "");
    } catch (IOException e1) {
    e1.printStackTrace();
    }
    System.out.println("---");*/
    //System.out.println(System.getenv());
    //System.out.println("---");

    //String font= System.getProperty("java.home")+"/lib/fonts/LucidaBrightRegular.ttf";
    //String font= "fonts/DejaVuSans.ttf";

    //byte[] ttf= ApplicationHelper.loadResource(font);

    try {
        // step 1
        Document document = new Document(PageSize.A4, 50, 50, 50, 50);
        // step 2
        PdfWriter.getInstance(document, new FileOutputStream("encodingfont.pdf"));
        // step 3
        document.open();
        // step 4
        String all[] = { "Symbol", "ZapfDingbats" };
        Font hex = new Font(Font.HELVETICA, 5);
        for (int z = 0; z < all.length; ++z) {
            String file = all[z];
            document.add(new Paragraph(
                    "Unicode equivalence for the font \"" + file + "\" with the encoding \"" + file + "\"\n"));
            /*char tb[];
            if (z == 0)
               tb = SYMBOL_TABLE;
            else
               tb = DINGBATS_TABLE;*/
            BaseFont bf;
            if (z == 2) {
                bf = BaseFont.createFont(file, BaseFont.IDENTITY_H, BaseFont.EMBEDDED, true, null, null);
                ;
            } else {
                bf = BaseFont.createFont(file, file, true);
            }
            Font f = new Font(bf, 12);
            PdfPTable table = new PdfPTable(16);
            table.setWidthPercentage(100);
            table.getDefaultCell().setBorderWidth(1);
            table.getDefaultCell().setHorizontalAlignment(Element.ALIGN_CENTER);
            for (char k = 0; k < Character.MAX_VALUE; ++k) {
                char c = k;
                if (bf.charExists(c)) {
                    Phrase ph = new Phrase(12, new String(new char[] { c }), f);
                    ph.add(new Phrase(12, "\n" + Integer.toString(c) + "\n" + cst(c), hex));
                    table.addCell(ph);
                } /*
                  else {
                     Phrase ph = new Phrase("\u00a0");
                     ph.add(new Phrase(12, "\n\n" + cst(c), hex));
                     table.addCell(ph);
                  }*/
            }
            document.add(table);
            document.newPage();
        }
        // step 5
        document.close();

        FontFactory.registerDirectories();

        Set<?> s = FontFactory.getRegisteredFonts();

        System.out.println("Fonts: " + s);

        s = FontFactory.getRegisteredFamilies();

        System.out.println("Families: " + s);

        ArrayList<Font> f = new ArrayList<Font>(s.size());

        for (Object name : s) {

            try {
                f.add(FontFactory.getFont(name.toString(), "UTF-8", true, 12, Font.NORMAL, Color.BLACK, true));
            } catch (Exception e) {
                f.add(FontFactory.getFont(name.toString(), "UTF-8", false, 12, Font.NORMAL, Color.BLACK, true));
            }

        }

        Collections.sort(f, new Comparator<Font>() {
            @Override
            public int compare(Font o1, Font o2) {
                return o1.getFamilyname().compareTo(o2.getFamilyname());
            }
        });

        for (Font ff : f) {

            if (ff.getBaseFont() == null) {
                continue;
            }
            System.out.println(ff.getFamilyname() + " " + ff.getBaseFont().isEmbedded());

        }

    } catch (Exception de) {
        de.printStackTrace();
    }
}

From source file:com.jk.framework.pdf.PDFUtil.java

License:Apache License

/**
 * Creates the font./*  www.  j  a  va 2  s  . c om*/
 *
 * @param fontName
 *            the font name
 * @param size
 *            the size
 * @param fontStyle
 *            the font style
 * @return the font
 * @throws DocumentException
 *             the document exception @1.1
 */
public static Font createFont(final String fontName, final int size, final boolean fontStyle)
        throws DocumentException {
    BaseFont baseFont;
    Font font = null;
    try {
        baseFont = BaseFont.createFont("c:/windows/fonts/" + fontName + ".ttf", BaseFont.IDENTITY_H,
                BaseFont.EMBEDDED);
        if (fontStyle == true) {
            font = new Font(baseFont, size, Font.BOLD);
        } else {
            font = new Font(baseFont, size, Font.NORMAL);
        }
        // return new Font(baseFont, size);
        return font;
    } catch (final IOException e) {
        throw new DocumentException(e);
    }
}

From source file:com.moss.check.us.CheckPdfRenderer.java

License:Open Source License

private BaseFont createMicrFont() throws Exception {

    boolean cached = true;
    byte[] ttf;//from ww w .  ja v  a  2s .co  m
    {
        InputStream in = CheckPdfRenderer.class.getResourceAsStream("/com/moss/check/us/GnuMICR.ttf");
        ByteArrayOutputStream out = new ByteArrayOutputStream();

        byte[] buffer = new byte[1024 * 10]; //10k buffer
        for (int numRead = in.read(buffer); numRead != -1; numRead = in.read(buffer)) {
            out.write(buffer, 0, numRead);
        }

        ttf = out.toByteArray();
    }
    byte[] pfb = null;
    boolean noThrow = false;

    BaseFont baseFont = BaseFont.createFont("GnuMICR.ttf", BaseFont.WINANSI, BaseFont.EMBEDDED, cached, ttf,
            pfb, noThrow);

    return baseFont;
}

From source file:com.moss.check.us.CheckPdfRenderer.java

License:Open Source License

private BaseFont createFixedFont() throws Exception {

    boolean cached = true;
    byte[] ttf;/*  ww w .ja v a2s  .  co m*/
    {
        InputStream in = CheckPdfRenderer.class.getResourceAsStream("/com/moss/check/us/VeraMono.ttf");
        ByteArrayOutputStream out = new ByteArrayOutputStream();

        byte[] buffer = new byte[1024 * 10]; //10k buffer
        for (int numRead = in.read(buffer); numRead != -1; numRead = in.read(buffer)) {
            out.write(buffer, 0, numRead);
        }

        ttf = out.toByteArray();
    }
    byte[] pfb = null;
    boolean noThrow = false;

    BaseFont baseFont = BaseFont.createFont("VeraMono.ttf", BaseFont.WINANSI, BaseFont.EMBEDDED, cached, ttf,
            pfb, noThrow);

    return baseFont;
}

From source file:com.nafundi.opendatakit.codebook.logic.CodebookMaker.java

License:Apache License

public Integer doInBackground() throws Exception {

    String errorMsg = "";

    // string writer
    StringWriter writer = new StringWriter();
    new Html(writer) {
        {/*from   w  w  w.  jav a  2 s . co m*/
            body();
            h4().text(inputFilename + " (" + locale + ")").end();
            table().classAttr("table table-bordered table-condensed");
            thead().tr();
            th().h6().text("Variable Name").end().end();
            th().h6().text("Question Text").end().end();
            th().h6().text("Saved Value").end().end();
            end().end();
            tbody();
            for (int i = 0; i < codebookEntries.size(); i++) {
                if (i % 2 == 0) {
                    // fix for no background color in export to pdf
                    tr().classAttr("gray");
                } else {
                    tr();
                }
                CodebookEntry entry = codebookEntries.get(i);
                td().text(entry.getVariable()).end();

                String question = entry.getQuestion();
                String value = entry.getValue();

                // select question
                if (question.contains("|")) {
                    td().text(question.replace("|", "")).end();
                    td();
                    table().classAttr("table table-bordered table-condensed");
                    String values[] = value.split("\n");
                    for (int j = 0; j < values.length; j++) {
                        tr();
                        //value
                        td().text(values[j].split("\t")[1]).end();
                        //label
                        td().text(values[j].split("\t")[0]).end();
                        end();
                    }
                    end();
                    end();
                } else {
                    td().text(question).end();
                    td().text(value).end();
                }

                end();
            }
            done();
        }
    };

    // build html string

    // bootstrap css with only headings, body type, and tables
    // add custom tr.gray tag to fix bug in html to pdf export
    // change font family to be Arial Unicode and to include embedding
    String htmlHeader = "<!DOCTYPE html><html><head><meta charset=\"utf-8\"/>\n"
            + "<style type=\"text/css\">.clearfix{*zoom:1;}.clearfix:before,.clearfix:after{display:table;content:\"\";line-height:0;}.clearfix:after{clear:both;}.hide-text{font:0/0 a;color:transparent;text-shadow:none;background-color:transparent;border:0;}.input-block-level{display:block;width:100%;min-height:30px;-webkit-box-sizing:border-box;-moz-box-sizing:border-box;box-sizing:border-box;}body{margin:0;font-family:\"Arial Unicode MS\",\"Arial Unicode\", Arial, sans-serif;-fs-pdf-font-embed:embed;-fs-pdf-font-encoding: Identity-H;font-size:11px;line-height:20px;color:#333333;background-color:#ffffff;}a{color:#0088cc;text-decoration:none;}a:hover,a:focus{color:#005580;text-decoration:underline;}.img-rounded{-webkit-border-radius:6px;-moz-border-radius:6px;border-radius:6px;}.img-polaroid{padding:4px;background-color:#fff;border:1px solid #ccc;border:1px solid rgba(0, 0, 0, 0.2);-webkit-box-shadow:0 1px 3px rgba(0, 0, 0, 0.1);-moz-box-shadow:0 1px 3px rgba(0, 0, 0, 0.1);box-shadow:0 1px 3px rgba(0, 0, 0, 0.1);}.img-circle{-webkit-border-radius:500px;-moz-border-radius:500px;border-radius:500px;}p{margin:0 0 10px;}.lead{margin-bottom:20px;font-size:21px;font-weight:200;line-height:30px;}small{font-size:85%;}strong{font-weight:bold;}em{font-style:italic;}cite{font-style:normal;}.muted{color:#999999;}a.muted:hover,a.muted:focus{color:#808080;}.text-warning{color:#c09853;}a.text-warning:hover,a.text-warning:focus{color:#a47e3c;}.text-publishError{color:#b94a48;}a.text-publishError:hover,a.text-publishError:focus{color:#953b39;}.text-info{color:#3a87ad;}a.text-info:hover,a.text-info:focus{color:#2d6987;}.text-success{color:#468847;}a.text-success:hover,a.text-success:focus{color:#356635;}.text-left{text-align:left;}.text-right{text-align:right;}.text-center{text-align:center;}h1,h2,h3,h4,h5,h6{margin:10px 0;font-family:inherit;font-weight:bold;line-height:20px;color:inherit;text-rendering:optimizelegibility;}h1 small,h2 small,h3 small,h4 small,h5 small,h6 small{font-weight:normal;line-height:1;color:#999999;}h1,h2,h3{line-height:40px;}h1{font-size:38.5px;}h2{font-size:31.5px;}h3{font-size:24.5px;}h4{font-size:17.5px;}h5{font-size:14px;}h6{font-size:11.9px;}h1 small{font-size:24.5px;}h2 small{font-size:17.5px;}h3 small{font-size:14px;}h4 small{font-size:14px;}.page-header{padding-bottom:9px;margin:20px 0 30px;border-bottom:1px solid #eeeeee;}ul,ol{padding:0;margin:0 0 10px 25px;}ul ul,ul ol,ol ol,ol ul{margin-bottom:0;}li{line-height:20px;}ul.unstyled,ol.unstyled{margin-left:0;list-style:none;}ul.inline,ol.inline{margin-left:0;list-style:none;}ul.inline>li,ol.inline>li{display:inline-block;*display:inline;*zoom:1;padding-left:5px;padding-right:5px;}dl{margin-bottom:20px;}dt,dd{line-height:20px;}dt{font-weight:bold;}dd{margin-left:10px;}.dl-horizontal{*zoom:1;}.dl-horizontal:before,.dl-horizontal:after{display:table;content:\"\";line-height:0;}.dl-horizontal:after{clear:both;}.dl-horizontal dt{float:left;width:160px;clear:left;text-align:right;overflow:hidden;text-overflow:ellipsis;white-space:nowrap;}.dl-horizontal dd{margin-left:180px;}hr{margin:20px 0;border:0;border-top:1px solid #eeeeee;border-bottom:1px solid #ffffff;}abbr[title],abbr[data-original-title]{cursor:help;border-bottom:1px dotted #999999;}abbr.initialism{font-size:90%;text-transform:uppercase;}blockquote{padding:0 0 0 15px;margin:0 0 20px;border-left:5px solid #eeeeee;}blockquote p{margin-bottom:0;font-size:17.5px;font-weight:300;line-height:1.25;}blockquote small{display:block;line-height:20px;color:#999999;}blockquote small:before{content:'\\2014 \\00A0';}blockquote.pull-right{float:right;padding-right:15px;padding-left:0;border-right:5px solid #eeeeee;border-left:0;}blockquote.pull-right p,blockquote.pull-right small{text-align:right;}blockquote.pull-right small:before{content:'';}blockquote.pull-right small:after{content:'\\00A0 \\2014';}q:before,q:after,blockquote:before,blockquote:after{content:\"\";}address{display:block;margin-bottom:20px;font-style:normal;line-height:20px;}table{max-width:100%;background-color:transparent;border-collapse:collapse;border-spacing:0;}.table{width:100%;margin-bottom:20px;}.table th,.table td{padding:8px;line-height:20px;text-align:left;vertical-align:top;border-top:1px solid #dddddd;}.table th{font-weight:bold;}.table thead th{vertical-align:bottom;}.table caption+thead tr:first-child th,.table caption+thead tr:first-child td,.table colgroup+thead tr:first-child th,.table colgroup+thead tr:first-child td,.table thead:first-child tr:first-child th,.table thead:first-child tr:first-child td{border-top:0;}.table tbody+tbody{border-top:2px solid #dddddd;}.table .table{background-color:#ffffff;}.table-condensed th,.table-condensed td{padding:4px 5px;}.table-bordered{border:1px solid #dddddd;border-collapse:separate;*border-collapse:collapse;border-left:0;-webkit-border-radius:4px;-moz-border-radius:4px;border-radius:4px;}.table-bordered th,.table-bordered td{border-left:1px solid #dddddd;}.table-bordered caption+thead tr:first-child th,.table-bordered caption+tbody tr:first-child th,.table-bordered caption+tbody tr:first-child td,.table-bordered colgroup+thead tr:first-child th,.table-bordered colgroup+tbody tr:first-child th,.table-bordered colgroup+tbody tr:first-child td,.table-bordered thead:first-child tr:first-child th,.table-bordered tbody:first-child tr:first-child th,.table-bordered tbody:first-child tr:first-child td{border-top:0;}.table-bordered thead:first-child tr:first-child>th:first-child,.table-bordered tbody:first-child tr:first-child>td:first-child,.table-bordered tbody:first-child tr:first-child>th:first-child{-webkit-border-top-left-radius:4px;-moz-border-radius-topleft:4px;border-top-left-radius:4px;}.table-bordered thead:first-child tr:first-child>th:last-child,.table-bordered tbody:first-child tr:first-child>td:last-child,.table-bordered tbody:first-child tr:first-child>th:last-child{-webkit-border-top-right-radius:4px;-moz-border-radius-topright:4px;border-top-right-radius:4px;}.table-bordered thead:last-child tr:last-child>th:first-child,.table-bordered tbody:last-child tr:last-child>td:first-child,.table-bordered tbody:last-child tr:last-child>th:first-child,.table-bordered tfoot:last-child tr:last-child>td:first-child,.table-bordered tfoot:last-child tr:last-child>th:first-child{-webkit-border-bottom-left-radius:4px;-moz-border-radius-bottomleft:4px;border-bottom-left-radius:4px;}.table-bordered thead:last-child tr:last-child>th:last-child,.table-bordered tbody:last-child tr:last-child>td:last-child,.table-bordered tbody:last-child tr:last-child>th:last-child,.table-bordered tfoot:last-child tr:last-child>td:last-child,.table-bordered tfoot:last-child tr:last-child>th:last-child{-webkit-border-bottom-right-radius:4px;-moz-border-radius-bottomright:4px;border-bottom-right-radius:4px;}.table-bordered tfoot+tbody:last-child tr:last-child td:first-child{-webkit-border-bottom-left-radius:0;-moz-border-radius-bottomleft:0;border-bottom-left-radius:0;}.table-bordered tfoot+tbody:last-child tr:last-child td:last-child{-webkit-border-bottom-right-radius:0;-moz-border-radius-bottomright:0;border-bottom-right-radius:0;}.table-bordered caption+thead tr:first-child th:first-child,.table-bordered caption+tbody tr:first-child td:first-child,.table-bordered colgroup+thead tr:first-child th:first-child,.table-bordered colgroup+tbody tr:first-child td:first-child{-webkit-border-top-left-radius:4px;-moz-border-radius-topleft:4px;border-top-left-radius:4px;}.table-bordered caption+thead tr:first-child th:last-child,.table-bordered caption+tbody tr:first-child td:last-child,.table-bordered colgroup+thead tr:first-child th:last-child,.table-bordered colgroup+tbody tr:first-child td:last-child{-webkit-border-top-right-radius:4px;-moz-border-radius-topright:4px;border-top-right-radius:4px;}.table-striped tbody>tr:nth-child(odd)>td,.table-striped tbody>tr:nth-child(odd)>th{background-color:#f9f9f9;}.table-hover tbody tr:hover>td,.table-hover tbody tr:hover>th{background-color:#f5f5f5;}table td[class*=\"span\"],table th[class*=\"span\"],.row-fluid table td[class*=\"span\"],.row-fluid table th[class*=\"span\"]{display:table-cell;float:none;margin-left:0;}.table td.span1,.table th.span1{float:none;width:44px;margin-left:0;}.table td.span2,.table th.span2{float:none;width:124px;margin-left:0;}.table td.span3,.table th.span3{float:none;width:204px;margin-left:0;}.table td.span4,.table th.span4{float:none;width:284px;margin-left:0;}.table td.span5,.table th.span5{float:none;width:364px;margin-left:0;}.table td.span6,.table th.span6{float:none;width:444px;margin-left:0;}.table td.span7,.table th.span7{float:none;width:524px;margin-left:0;}.table td.span8,.table th.span8{float:none;width:604px;margin-left:0;}.table td.span9,.table th.span9{float:none;width:684px;margin-left:0;}.table td.span10,.table th.span10{float:none;width:764px;margin-left:0;}.table td.span11,.table th.span11{float:none;width:844px;margin-left:0;}.table td.span12,.table th.span12{float:none;width:924px;margin-left:0;}.table tbody tr.success>td{background-color:#dff0d8;}.table tbody tr.publishError>td{background-color:#f2dede;}.table tbody tr.warning>td{background-color:#fcf8e3;}.table tbody tr.info>td{background-color:#d9edf7;}.table-hover tbody tr.success:hover>td{background-color:#d0e9c6;}.table-hover tbody tr.publishError:hover>td{background-color:#ebcccc;}.table-hover tbody tr.warning:hover>td{background-color:#faf2cc;}.table-hover tbody tr.info:hover>td{background-color:#c4e3f3;}.table tbody tr.gray>td{background-color:#f9f9f9;}.hidden {visibility:hidden;}\t</style>\n"
            + "</head>";
    String htmlFooter = "\n</html>";
    String htmlDocument = htmlHeader + writer.getBuffer().toString() + htmlFooter;

    // make sure html entities aren't mangled in document building process
    Tidy tidy = new Tidy();
    tidy.setQuiet(true);
    tidy.setXmlTags(false);
    tidy.setShowWarnings(false);
    tidy.setInputEncoding("UTF-8");
    tidy.setOutputEncoding("UTF-8");
    tidy.setXHTML(true);
    Document document = tidy.parseDOM(new ByteArrayInputStream(htmlDocument.getBytes("UTF-8")), null);

    // create render of document
    // ITextRender is not thread-safe
    synchronized (this) {
        ITextRenderer renderer = new ITextRenderer();

        if (isWindows()) {
            renderer.getFontResolver().addFont("C:\\WINDOWS\\Fonts\\ARIALUNI.TTF", BaseFont.IDENTITY_H,
                    BaseFont.EMBEDDED);
        } else if (isMac()) {
            renderer.getFontResolver().addFont("/Library/Fonts/Arial Unicode.ttf", BaseFont.IDENTITY_H,
                    BaseFont.EMBEDDED);
        } else if (isUnix()) {
            renderer.getFontResolver().addFont("/usr/share/fonts/truetype/ARIALUNI.TTF", BaseFont.IDENTITY_H,
                    BaseFont.EMBEDDED);
        } else {
            publish("Warning: No Arial Unicode found. Non-Latin characters may not display properly.");
        }

        renderer.setDocument(document, null);
        renderer.layout();

        // write out document as pdf
        OutputStream outputStream = null;
        try {
            outputStream = new FileOutputStream(
                    outputFolderPath + File.separator + inputFilename + " (" + locale + ").pdf");
        } catch (FileNotFoundException e) {
            errorMsg = e.getMessage();
            e.printStackTrace();
        }
        try {
            renderer.createPDF(outputStream);
        } catch (DocumentException e) {
            errorMsg = e.getMessage();
            e.printStackTrace();
        }
        try {
            if (outputStream != null) {
                outputStream.close();
            }
        } catch (IOException e) {
            errorMsg = e.getMessage();
            e.printStackTrace();
        }

    }

    if (!"".equals(errorMsg)) {
        publishError(errorMsg);
        return -1;
    }

    publish("Finished making " + getLocale() + " codebook");
    return 0;
}

From source file:com.nafundi.taskforce.codebook.logic.CodebookMaker.java

License:Apache License

public Integer doInBackground() throws Exception {

    String errorMsg = "";

    // string writer
    StringWriter writer = new StringWriter();
    new Html(writer) {
        {/*  w  w w  .  jav  a  2s .co m*/
            body();
            h4().text(inputFilename + " (" + locale + ")").end();
            table().classAttr("table table-bordered table-condensed");
            thead().tr();
            th().h6().text("Variable Name").end().end();
            th().h6().text("Question Text").end().end();
            th().h6().text("Saved Value").end().end();
            end().end();
            tbody();
            for (int i = 0; i < codebookEntries.size(); i++) {
                if (i % 2 == 0) {
                    // fix for no background color in export to pdf
                    tr().classAttr("gray");
                } else {
                    tr();
                }
                CodebookEntry entry = codebookEntries.get(i);
                td().text(entry.getVariable()).end();

                String question = entry.getQuestion();
                String value = entry.getValue();

                // select question
                if (question.contains("|")) {
                    td().text(question.replace("|", "")).end();
                    td();
                    table().classAttr("table table-bordered table-condensed");
                    String values[] = value.split("\n");
                    for (int j = 0; j < values.length; j++) {
                        tr();
                        //value
                        td().text(values[j].split("\t")[1]).end();
                        //label
                        td().text(values[j].split("\t")[0]).end();
                        end();
                    }
                    end();
                    end();
                } else {
                    td().text(question).end();
                    td().text(value).end();
                }

                end();
            }
            done();
        }
    };

    // build html string

    // bootstrap css with only headings, body type, and tables
    // add custom tr.gray tag to fix bug in html to pdf export
    // change font family to be Arial Unicode and to include embedding
    String htmlHeader = "<!DOCTYPE html><html><head><meta charset=\"utf-8\"/>\n"
            + "<style type=\"text/css\">.clearfix{*zoom:1;}.clearfix:before,.clearfix:after{display:table;content:\"\";line-height:0;}.clearfix:after{clear:both;}.hide-text{font:0/0 a;color:transparent;text-shadow:none;background-color:transparent;border:0;}.input-block-level{display:block;width:100%;min-height:30px;-webkit-box-sizing:border-box;-moz-box-sizing:border-box;box-sizing:border-box;}body{margin:0;font-family:\"Arial Unicode MS\",\"Arial Unicode\", Arial, sans-serif;-fs-pdf-font-embed:embed;-fs-pdf-font-encoding: Identity-H;font-size:11px;line-height:20px;color:#333333;background-color:#ffffff;}a{color:#0088cc;text-decoration:none;}a:hover,a:focus{color:#005580;text-decoration:underline;}.img-rounded{-webkit-border-radius:6px;-moz-border-radius:6px;border-radius:6px;}.img-polaroid{padding:4px;background-color:#fff;border:1px solid #ccc;border:1px solid rgba(0, 0, 0, 0.2);-webkit-box-shadow:0 1px 3px rgba(0, 0, 0, 0.1);-moz-box-shadow:0 1px 3px rgba(0, 0, 0, 0.1);box-shadow:0 1px 3px rgba(0, 0, 0, 0.1);}.img-circle{-webkit-border-radius:500px;-moz-border-radius:500px;border-radius:500px;}p{margin:0 0 10px;}.lead{margin-bottom:20px;font-size:21px;font-weight:200;line-height:30px;}small{font-size:85%;}strong{font-weight:bold;}em{font-style:italic;}cite{font-style:normal;}.muted{color:#999999;}a.muted:hover,a.muted:focus{color:#808080;}.text-warning{color:#c09853;}a.text-warning:hover,a.text-warning:focus{color:#a47e3c;}.text-publishError{color:#b94a48;}a.text-publishError:hover,a.text-publishError:focus{color:#953b39;}.text-info{color:#3a87ad;}a.text-info:hover,a.text-info:focus{color:#2d6987;}.text-success{color:#468847;}a.text-success:hover,a.text-success:focus{color:#356635;}.text-left{text-align:left;}.text-right{text-align:right;}.text-center{text-align:center;}h1,h2,h3,h4,h5,h6{margin:10px 0;font-family:inherit;font-weight:bold;line-height:20px;color:inherit;text-rendering:optimizelegibility;}h1 small,h2 small,h3 small,h4 small,h5 small,h6 small{font-weight:normal;line-height:1;color:#999999;}h1,h2,h3{line-height:40px;}h1{font-size:38.5px;}h2{font-size:31.5px;}h3{font-size:24.5px;}h4{font-size:17.5px;}h5{font-size:14px;}h6{font-size:11.9px;}h1 small{font-size:24.5px;}h2 small{font-size:17.5px;}h3 small{font-size:14px;}h4 small{font-size:14px;}.page-header{padding-bottom:9px;margin:20px 0 30px;border-bottom:1px solid #eeeeee;}ul,ol{padding:0;margin:0 0 10px 25px;}ul ul,ul ol,ol ol,ol ul{margin-bottom:0;}li{line-height:20px;}ul.unstyled,ol.unstyled{margin-left:0;list-style:none;}ul.inline,ol.inline{margin-left:0;list-style:none;}ul.inline>li,ol.inline>li{display:inline-block;*display:inline;*zoom:1;padding-left:5px;padding-right:5px;}dl{margin-bottom:20px;}dt,dd{line-height:20px;}dt{font-weight:bold;}dd{margin-left:10px;}.dl-horizontal{*zoom:1;}.dl-horizontal:before,.dl-horizontal:after{display:table;content:\"\";line-height:0;}.dl-horizontal:after{clear:both;}.dl-horizontal dt{float:left;width:160px;clear:left;text-align:right;overflow:hidden;text-overflow:ellipsis;white-space:nowrap;}.dl-horizontal dd{margin-left:180px;}hr{margin:20px 0;border:0;border-top:1px solid #eeeeee;border-bottom:1px solid #ffffff;}abbr[title],abbr[data-original-title]{cursor:help;border-bottom:1px dotted #999999;}abbr.initialism{font-size:90%;text-transform:uppercase;}blockquote{padding:0 0 0 15px;margin:0 0 20px;border-left:5px solid #eeeeee;}blockquote p{margin-bottom:0;font-size:17.5px;font-weight:300;line-height:1.25;}blockquote small{display:block;line-height:20px;color:#999999;}blockquote small:before{content:'\\2014 \\00A0';}blockquote.pull-right{float:right;padding-right:15px;padding-left:0;border-right:5px solid #eeeeee;border-left:0;}blockquote.pull-right p,blockquote.pull-right small{text-align:right;}blockquote.pull-right small:before{content:'';}blockquote.pull-right small:after{content:'\\00A0 \\2014';}q:before,q:after,blockquote:before,blockquote:after{content:\"\";}address{display:block;margin-bottom:20px;font-style:normal;line-height:20px;}table{max-width:100%;background-color:transparent;border-collapse:collapse;border-spacing:0;}.table{width:100%;margin-bottom:20px;}.table th,.table td{padding:8px;line-height:20px;text-align:left;vertical-align:top;border-top:1px solid #dddddd;}.table th{font-weight:bold;}.table thead th{vertical-align:bottom;}.table caption+thead tr:first-child th,.table caption+thead tr:first-child td,.table colgroup+thead tr:first-child th,.table colgroup+thead tr:first-child td,.table thead:first-child tr:first-child th,.table thead:first-child tr:first-child td{border-top:0;}.table tbody+tbody{border-top:2px solid #dddddd;}.table .table{background-color:#ffffff;}.table-condensed th,.table-condensed td{padding:4px 5px;}.table-bordered{border:1px solid #dddddd;border-collapse:separate;*border-collapse:collapse;border-left:0;-webkit-border-radius:4px;-moz-border-radius:4px;border-radius:4px;}.table-bordered th,.table-bordered td{border-left:1px solid #dddddd;}.table-bordered caption+thead tr:first-child th,.table-bordered caption+tbody tr:first-child th,.table-bordered caption+tbody tr:first-child td,.table-bordered colgroup+thead tr:first-child th,.table-bordered colgroup+tbody tr:first-child th,.table-bordered colgroup+tbody tr:first-child td,.table-bordered thead:first-child tr:first-child th,.table-bordered tbody:first-child tr:first-child th,.table-bordered tbody:first-child tr:first-child td{border-top:0;}.table-bordered thead:first-child tr:first-child>th:first-child,.table-bordered tbody:first-child tr:first-child>td:first-child,.table-bordered tbody:first-child tr:first-child>th:first-child{-webkit-border-top-left-radius:4px;-moz-border-radius-topleft:4px;border-top-left-radius:4px;}.table-bordered thead:first-child tr:first-child>th:last-child,.table-bordered tbody:first-child tr:first-child>td:last-child,.table-bordered tbody:first-child tr:first-child>th:last-child{-webkit-border-top-right-radius:4px;-moz-border-radius-topright:4px;border-top-right-radius:4px;}.table-bordered thead:last-child tr:last-child>th:first-child,.table-bordered tbody:last-child tr:last-child>td:first-child,.table-bordered tbody:last-child tr:last-child>th:first-child,.table-bordered tfoot:last-child tr:last-child>td:first-child,.table-bordered tfoot:last-child tr:last-child>th:first-child{-webkit-border-bottom-left-radius:4px;-moz-border-radius-bottomleft:4px;border-bottom-left-radius:4px;}.table-bordered thead:last-child tr:last-child>th:last-child,.table-bordered tbody:last-child tr:last-child>td:last-child,.table-bordered tbody:last-child tr:last-child>th:last-child,.table-bordered tfoot:last-child tr:last-child>td:last-child,.table-bordered tfoot:last-child tr:last-child>th:last-child{-webkit-border-bottom-right-radius:4px;-moz-border-radius-bottomright:4px;border-bottom-right-radius:4px;}.table-bordered tfoot+tbody:last-child tr:last-child td:first-child{-webkit-border-bottom-left-radius:0;-moz-border-radius-bottomleft:0;border-bottom-left-radius:0;}.table-bordered tfoot+tbody:last-child tr:last-child td:last-child{-webkit-border-bottom-right-radius:0;-moz-border-radius-bottomright:0;border-bottom-right-radius:0;}.table-bordered caption+thead tr:first-child th:first-child,.table-bordered caption+tbody tr:first-child td:first-child,.table-bordered colgroup+thead tr:first-child th:first-child,.table-bordered colgroup+tbody tr:first-child td:first-child{-webkit-border-top-left-radius:4px;-moz-border-radius-topleft:4px;border-top-left-radius:4px;}.table-bordered caption+thead tr:first-child th:last-child,.table-bordered caption+tbody tr:first-child td:last-child,.table-bordered colgroup+thead tr:first-child th:last-child,.table-bordered colgroup+tbody tr:first-child td:last-child{-webkit-border-top-right-radius:4px;-moz-border-radius-topright:4px;border-top-right-radius:4px;}.table-striped tbody>tr:nth-child(odd)>td,.table-striped tbody>tr:nth-child(odd)>th{background-color:#f9f9f9;}.table-hover tbody tr:hover>td,.table-hover tbody tr:hover>th{background-color:#f5f5f5;}table td[class*=\"span\"],table th[class*=\"span\"],.row-fluid table td[class*=\"span\"],.row-fluid table th[class*=\"span\"]{display:table-cell;float:none;margin-left:0;}.table td.span1,.table th.span1{float:none;width:44px;margin-left:0;}.table td.span2,.table th.span2{float:none;width:124px;margin-left:0;}.table td.span3,.table th.span3{float:none;width:204px;margin-left:0;}.table td.span4,.table th.span4{float:none;width:284px;margin-left:0;}.table td.span5,.table th.span5{float:none;width:364px;margin-left:0;}.table td.span6,.table th.span6{float:none;width:444px;margin-left:0;}.table td.span7,.table th.span7{float:none;width:524px;margin-left:0;}.table td.span8,.table th.span8{float:none;width:604px;margin-left:0;}.table td.span9,.table th.span9{float:none;width:684px;margin-left:0;}.table td.span10,.table th.span10{float:none;width:764px;margin-left:0;}.table td.span11,.table th.span11{float:none;width:844px;margin-left:0;}.table td.span12,.table th.span12{float:none;width:924px;margin-left:0;}.table tbody tr.success>td{background-color:#dff0d8;}.table tbody tr.publishError>td{background-color:#f2dede;}.table tbody tr.warning>td{background-color:#fcf8e3;}.table tbody tr.info>td{background-color:#d9edf7;}.table-hover tbody tr.success:hover>td{background-color:#d0e9c6;}.table-hover tbody tr.publishError:hover>td{background-color:#ebcccc;}.table-hover tbody tr.warning:hover>td{background-color:#faf2cc;}.table-hover tbody tr.info:hover>td{background-color:#c4e3f3;}.table tbody tr.gray>td{background-color:#f9f9f9;}.hidden {visibility:hidden;}\t</style>\n"
            + "</head>";
    String htmlFooter = "\n</html>";
    String htmlDocument = htmlHeader + writer.getBuffer().toString() + htmlFooter;

    // make sure html entities aren't mangled in document building process
    Tidy tidy = new Tidy();
    tidy.setQuiet(true);
    tidy.setXmlTags(false);
    tidy.setShowWarnings(false);
    tidy.setInputEncoding("UTF-8");
    tidy.setOutputEncoding("UTF-8");
    tidy.setXHTML(true);
    Document document = tidy.parseDOM(new ByteArrayInputStream(htmlDocument.getBytes("UTF-8")), null);

    // create render of document
    // ITextRender is not thread-safe
    synchronized (this) {
        ITextRenderer renderer = new ITextRenderer();

        if (isWindows()) {
            renderer.getFontResolver().addFont("C:\\WINDOWS\\Fonts\\ARIALUNI.TTF", BaseFont.IDENTITY_H,
                    BaseFont.EMBEDDED);
        } else if (isMac()) {
            //renderer.getFontResolver().addFont("/Library/Fonts/Arial Unicode.ttf", BaseFont.IDENTITY_H, BaseFont.EMBEDDED);
            renderer.getFontResolver().addFont("/Library/Fonts/NotoNaskhArabic-Regular.ttf",
                    BaseFont.IDENTITY_H, BaseFont.EMBEDDED);
            renderer.getFontResolver().addFont("/Library/Fonts/NotoSans-Regular.ttf", BaseFont.IDENTITY_H,
                    BaseFont.EMBEDDED);
            renderer.getFontResolver().addFont("/Library/Fonts/NotoSans-Bold.ttf", BaseFont.IDENTITY_H,
                    BaseFont.EMBEDDED);
            renderer.getFontResolver().addFont("/Library/Fonts/NotoSansBengali-Regular.ttf",
                    BaseFont.IDENTITY_H, BaseFont.EMBEDDED);
            renderer.getFontResolver().addFont("/Library/Fonts/NotoSansBengali-Bold.ttf", BaseFont.IDENTITY_H,
                    BaseFont.EMBEDDED);
        } else if (isUnix()) {

            renderer.getFontResolver().addFont("/usr/share/fonts/truetype/fontawesome-webfont.ttf",
                    BaseFont.IDENTITY_H, BaseFont.EMBEDDED);
            renderer.getFontResolver().addFont("/usr/share/fonts/truetype/NotoNaskhArabic-Regular.ttf",
                    BaseFont.IDENTITY_H, BaseFont.EMBEDDED);
            renderer.getFontResolver().addFont("/usr/share/fonts/truetype/NotoSans-Regular.ttf",
                    BaseFont.IDENTITY_H, BaseFont.EMBEDDED);
            renderer.getFontResolver().addFont("/usr/share/fonts/truetype/NotoSans-Bold.ttf",
                    BaseFont.IDENTITY_H, BaseFont.EMBEDDED);
            renderer.getFontResolver().addFont("/usr/share/fonts/truetype/NotoSansBengali-Regular.ttf",
                    BaseFont.IDENTITY_H, BaseFont.EMBEDDED);
            renderer.getFontResolver().addFont("/usr/share/fonts/truetype/NotoSansBengali-Bold.ttf",
                    BaseFont.IDENTITY_H, BaseFont.EMBEDDED);

        } else {
            publish("Warning: No Arial Unicode found. Non-Latin characters may not display properly.");
        }

        renderer.setDocument(document, null);
        renderer.layout();

        // write out document as pdf
        OutputStream outputStream = null;
        try {
            outputStream = new FileOutputStream(
                    outputFolderPath + File.separator + inputFilename + " (" + locale + ").pdf");
        } catch (FileNotFoundException e) {
            errorMsg = e.getMessage();
            e.printStackTrace();
        }
        try {
            renderer.createPDF(outputStream);
        } catch (DocumentException e) {
            errorMsg = e.getMessage();
            e.printStackTrace();
        }
        try {
            if (outputStream != null) {
                outputStream.close();
            }
        } catch (IOException e) {
            errorMsg = e.getMessage();
            e.printStackTrace();
        }

    }

    if (!"".equals(errorMsg)) {
        publishError(errorMsg);
        return -1;
    }

    publish("Finished making " + getLocale() + " codebook");
    return 0;
}

From source file:com.openkm.util.PDFUtils.java

License:Open Source License

/**
 * Obtain base font for PDF processing/*  w  w  w.j  a  va  2s  .  c  om*/
 */
private static BaseFont getBaseFont() throws DocumentException, IOException {
    String fontName = Config.HOME_DIR + "/lib/unicode.ttf";
    String fontEncoding = BaseFont.IDENTITY_H;

    if (!new File(fontName).exists()) {
        log.warn("Unicode TTF font not found: {}", fontName);
        fontName = BaseFont.HELVETICA;
        fontEncoding = BaseFont.WINANSI;
    }

    return BaseFont.createFont(fontName, fontEncoding, BaseFont.EMBEDDED);
}