Example usage for com.lowagie.text.html.simpleparser HTMLWorker parseToList

List of usage examples for com.lowagie.text.html.simpleparser HTMLWorker parseToList

Introduction

In this page you can find the example usage for com.lowagie.text.html.simpleparser HTMLWorker parseToList.

Prototype

public static ArrayList parseToList(Reader reader, StyleSheet style, HashMap interfaceProps)
            throws IOException 

Source Link

Usage

From source file:classroom.filmfestival_c.Movies25.java

@SuppressWarnings("unchecked")
public static boolean addText(String s, PdfContentByte canvas, float[] f, float size, boolean simulate)
        throws DocumentException, IOException {
    StyleSheet styles = new StyleSheet();
    styles.loadTagStyle("p", "size", size + "px");
    styles.loadTagStyle("p", "align", "justify");
    styles.loadTagStyle("p", "hyphenation", "en_us");
    ArrayList<Element> objects = HTMLWorker.parseToList(new StringReader(s), styles, null);
    ColumnText ct = new ColumnText(canvas);
    ct.setAlignment(Element.ALIGN_JUSTIFIED);
    ct.setLeading(size * 1.2f);//from  ww  w.  j  a  va  2s  .  c  om
    ct.setSimpleColumn(f[1] + 2, f[2] + 2, f[3] - 2, f[4]);
    for (Element element : objects) {
        ct.addElement(element);
    }
    return ColumnText.hasMoreText(ct.go(simulate));
}

From source file:com.shmsoft.dmass.print.Html2Pdf.java

License:Apache License

/**
 * Bad rendering, perhaps used only for Windows
 *//*  ww  w  .  ja v a 2s . c  o m*/
@SuppressWarnings({ "rawtypes", "unchecked" })
private static void convertHtml2Pdf(Reader htmlReader, String outputFile) throws Exception {
    Document pdfDocument = new Document();

    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    PdfWriter.getInstance(pdfDocument, baos);
    pdfDocument.open();
    StyleSheet styles = new StyleSheet();
    styles.loadTagStyle("body", "font", "Times New Roman");

    ImageProvider imageProvider = new ImageProvider() {

        @Override
        public Image getImage(String src, HashMap arg1, ChainedProperties arg2, DocListener arg3) {

            try {
                Image image = Image.getInstance(IOUtils.toByteArray(
                        getClass().getClassLoader().getResourceAsStream(ParameterProcessing.NO_IMAGE_FILE)));
                return image;
            } catch (Exception e) {
                System.out.println("Problem with html->pdf imaging, image provider fault");
            }

            return null;
        }

    };

    HashMap interfaceProps = new HashMap();
    interfaceProps.put("img_provider", imageProvider);

    ArrayList arrayElementList = HTMLWorker.parseToList(htmlReader, styles, interfaceProps);
    for (int i = 0; i < arrayElementList.size(); ++i) {
        Element e = (Element) arrayElementList.get(i);
        pdfDocument.add(e);
    }
    pdfDocument.close();
    byte[] bs = baos.toByteArray();
    File pdfFile = new File(outputFile);
    FileOutputStream out = new FileOutputStream(pdfFile);
    out.write(bs);
    out.close();
}

From source file:org.freeeed.print.Html2Pdf.java

License:Apache License

/**
 * Bad rendering, perhaps used only for Windows
 *//* ww  w.j a va  2  s  .co  m*/
@SuppressWarnings({ "rawtypes", "unchecked" })
private static void convertHtml2Pdf(Reader htmlReader, File outputFile) throws Exception {
    Document pdfDocument = new Document();

    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    PdfWriter.getInstance(pdfDocument, baos);
    pdfDocument.open();
    StyleSheet styles = new StyleSheet();
    styles.loadTagStyle("body", "font", "Times New Roman");

    ImageProvider imageProvider = new ImageProvider() {
        @Override
        public Image getImage(String src, HashMap arg1, ChainedProperties arg2, DocListener arg3) {

            try {
                return Image.getInstance(IOUtils.toByteArray(
                        getClass().getClassLoader().getResourceAsStream(ParameterProcessing.NO_IMAGE_FILE)));
            } catch (IOException | BadElementException e) {
                logger.warn("Problem with html to pdf rendering.", e);
            }

            return null;
        }
    };

    HashMap interfaceProps = new HashMap();
    interfaceProps.put("img_provider", imageProvider);

    ArrayList arrayElementList = HTMLWorker.parseToList(htmlReader, styles, interfaceProps);
    for (int i = 0; i < arrayElementList.size(); ++i) {
        Element e = (Element) arrayElementList.get(i);
        pdfDocument.add(e);
    }
    pdfDocument.close();
    byte[] bs = baos.toByteArray();
    FileOutputStream out = new FileOutputStream(outputFile);
    out.write(bs);
    out.close();
}

From source file:org.nuxeo.ecm.platform.ui.web.component.seam.UIHtmlText.java

License:Open Source License

private void addFromHtml(String html, FacesContext context) {
    HashMap<String, Object> interfaceProps = new HashMap<String, Object>();
    interfaceProps.put("img_provider",
            new NuxeoITextImageProvider((HttpServletRequest) context.getExternalContext().getRequest()));

    try {//from   w w  w.  jav  a  2s. c  o  m
        for (Object o : HTMLWorker.parseToList(new StringReader(html), getStyle(), interfaceProps)) {
            addToITextParent(o);
        }
    } catch (Exception e) {
        // XXX avoid crash when rendering an image with resource not found
        log.error("Error converting HTML to PDF", e);
    }
}

From source file:test.itext.html.AimsPdf.java

License:Open Source License

private static void writeData(Document doc, StringBuffer sb, String headingText)
        throws IOException, DocumentException {
    String data = Utility.replace(sb.toString(), "<p>&nbsp;</p>", "", Utility.REPLACE_ALL);
    data = Utility.replace(data, "</p>", "</p><br/>", Utility.REPLACE_ALL);
    data = Utility.replace(data, "</ul>", "</ul><br/>", Utility.REPLACE_ALL);
    data = Utility.replace(data, "</p><br/></td>", "</p></td>", Utility.REPLACE_ALL);

    System.out.println(data);/*from w w w .  j  a  va2 s . com*/
    Reader reader = new StringReader("<root>" + data + "</root>");

    if (headingText != null && headingText.length() > 0) {
        Paragraph heading = new Paragraph(headingText,
                FontFactory.getFont("arial", 10, Font.UNDERLINE | Font.BOLDITALIC));
        heading.setSpacingAfter(12f);
        doc.add(heading);
    }

    ArrayList objects = HTMLWorker.parseToList(reader, style, null);
    for (int k = 0; k < objects.size(); ++k) {
        Element ele = (Element) objects.get(k);
        doc.add(ele);
    }

    reader.close();
}