Example usage for com.itextpdf.tool.xml XMLWorkerHelper getCSS

List of usage examples for com.itextpdf.tool.xml XMLWorkerHelper getCSS

Introduction

In this page you can find the example usage for com.itextpdf.tool.xml XMLWorkerHelper getCSS.

Prototype

public static synchronized CssFile getCSS(InputStream in) 

Source Link

Usage

From source file:projekt.Class.SaveToPDF.java

/**
 * * Metoda, ktra tworzy plik pdf na podan ciek
 *
 * @param file cieka dostpu do pluku na ktrym ma by zapisaby plik pdf
 * @throws IOException       wyjtek wejcia/ wyjcia
 * @throws DocumentException wyjatek podczas tworzenia dokumentu
 *//*w w w  .  j a v a2 s  .  co m*/
public static void createPdf(File file) throws IOException, DocumentException {
    Document document = new Document();
    String HTML = "src/projekt/HTML/Diagnoza/diagnoza.html";
    String CSS = "src/projekt/HTML/Diagnoza/styl.css";
    PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(file));
    writer.setInitialLeading(12.5f);
    document.open();
    CSSResolver cssResolver = new StyleAttrCSSResolver();
    CssFile cssFile = XMLWorkerHelper.getCSS(new FileInputStream(CSS));
    cssResolver.addCss(cssFile);
    HtmlPipelineContext htmlContext = new HtmlPipelineContext(null);
    htmlContext.setTagFactory(Tags.getHtmlTagProcessorFactory());
    PdfWriterPipeline pdf = new PdfWriterPipeline(document, writer);
    HtmlPipeline html = new HtmlPipeline(htmlContext, pdf);
    CssResolverPipeline css = new CssResolverPipeline(cssResolver, html);
    XMLWorker worker = new XMLWorker(css, true);
    XMLParser p = new XMLParser(worker);
    p.parse(new FileInputStream(HTML));
    document.close();
}

From source file:sandbox.xmlworker.reporting.FillTemplateHelper.java

public static ElementList parseHtml(String content, String style, TagProcessorFactory tagProcessors)
        throws IOException {
    // CSS//from   w  w  w .j  a  va  2  s . co m
    CSSResolver cssResolver = new StyleAttrCSSResolver();
    CssFile cssFile = XMLWorkerHelper.getCSS(new FileInputStream(style));
    cssResolver.addCss(cssFile);
    // HTML
    HtmlPipelineContext htmlContext = new HtmlPipelineContext(null);
    htmlContext.setTagFactory(tagProcessors);
    htmlContext.autoBookmark(false);
    // Pipelines
    ElementList elements = new ElementList();
    ElementHandlerPipeline end = new ElementHandlerPipeline(elements, null);
    HtmlPipeline html = new HtmlPipeline(htmlContext, end);
    CssResolverPipeline css = new CssResolverPipeline(cssResolver, html);
    // XML Worker
    XMLWorker worker = new XMLWorker(css, true);
    XMLParser p = new XMLParser(worker);

    p.parse(new FileInputStream(content), Charset.forName("cp1252"));
    return elements;
}

From source file:scoretracker.beans.EJB.PDFService.java

public void createPdfStudent(Student s) throws IOException, DocumentException {

    //The directory in which the PDF will be stord, including a name 
    final String PDFLOCATION = "C:\\created_PDFs\\" + s.getName() + s.getPrename() + "_" + s.getRNr() + "_"
            + s.getClassId().getName() + ".pdf";

    //Collect student's points
    List<Teststudent> tests = dataservice.getDataPPSTS(s);

    File file = new File(PDFLOCATION);
    //Creating the required directory structure
    file.getParentFile().mkdirs();//from  ww  w . j  a v a2 s  . com

    Document document = new Document();

    PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(file));

    document.open();

    //Create the table headers for our table
    StringBuilder sb = new StringBuilder();
    sb.append("<h4>");
    sb.append("Overview of all tests made by: ");
    sb.append(s.getName());
    sb.append(" ");
    sb.append(s.getPrename());
    sb.append("</h4>");
    sb.append("<p>Class: ");
    sb.append(s.getClassId().getName());
    sb.append("</p><br/>");
    sb.append("<table border=\"2\">");
    sb.append("<tr>");
    sb.append("<th>Course</th>");
    sb.append("<th>Score</th>");
    sb.append("</tr>");

    //Fill the data columns with the student's points
    for (Teststudent test : tests) {
        sb.append("<tr>");
        sb.append("<td>");
        sb.append(test.getTestId().getName());
        sb.append(" - " + test.getTestId().getCourseId().getName());
        sb.append("</td>");

        sb.append("<td>");
        sb.append(test.getScore());
        sb.append("/20");
        sb.append("</td>");
        sb.append("</tr>");
    }

    sb.append("</table>");

    //Apply the CSS to our table
    CSSResolver cssResolver = new StyleAttrCSSResolver();
    CssFile cssFile = XMLWorkerHelper.getCSS(new ByteArrayInputStream(CSS.getBytes()));
    cssResolver.addCss(cssFile);

    HtmlPipelineContext htmlContext = new HtmlPipelineContext(null);
    htmlContext.setTagFactory(Tags.getHtmlTagProcessorFactory());

    PdfWriterPipeline pdf = new PdfWriterPipeline(document, writer);
    HtmlPipeline html = new HtmlPipeline(htmlContext, pdf);
    CssResolverPipeline css = new CssResolverPipeline(cssResolver, html);

    XMLWorker worker = new XMLWorker(css, true);
    XMLParser p = new XMLParser(worker);
    p.parse(new ByteArrayInputStream(sb.toString().getBytes()));

    document.close();
}

From source file:scoretracker.beans.EJB.PDFService.java

public void createPdfTest(Teststudent t) throws IOException, DocumentException {

    List<Teststudent> testsInfo = dataservice.getDataPPT(0, 0, t.getTestId().getId());

    //The directory in which the PDF will be stord, including a name 
    final String PDFLOCATION = "C:\\created_PDFs\\" + t.getTestId().getName() + "_"
            + t.getTestId().getClassId().getName() + ".pdf";

    File file = new File(PDFLOCATION);
    //Creating the required directory structure
    file.getParentFile().mkdirs();/*  www  .ja  v a 2  s. co  m*/

    Document document = new Document();

    PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(file));

    document.open();

    //Create the table headers for our table
    StringBuilder sb = new StringBuilder();
    sb.append("<h4>Results for: ");
    sb.append(t.getTestId().getName());
    sb.append(" - ");
    sb.append(t.getTestId().getCourseId().getName());
    sb.append("</h4>");
    sb.append("<p>Voor klas ");
    sb.append(t.getTestId().getClassId().getName());
    sb.append("</p><br/>");
    sb.append("<table border=\"2\">");
    sb.append("<tr>");
    sb.append("<th>Student</th>");
    sb.append("<th>Score</th>");
    sb.append("</tr>");

    //Fill the data columns with the student's name and points
    for (Teststudent test : testsInfo) {
        sb.append("<tr>");
        sb.append("<td>");
        sb.append(test.getStudentId().getName());
        sb.append(" ");
        sb.append(test.getStudentId().getPrename());
        sb.append("</td>");

        sb.append("<td>");
        sb.append(test.getScore());
        sb.append("/20");
        sb.append("</td>");
        sb.append("</tr>");
    }

    sb.append("</table>");

    //Apply the CSS to our table
    CSSResolver cssResolver = new StyleAttrCSSResolver();
    CssFile cssFile = XMLWorkerHelper.getCSS(new ByteArrayInputStream(CSS.getBytes()));
    cssResolver.addCss(cssFile);

    HtmlPipelineContext htmlContext = new HtmlPipelineContext(null);
    htmlContext.setTagFactory(Tags.getHtmlTagProcessorFactory());

    PdfWriterPipeline pdf = new PdfWriterPipeline(document, writer);
    HtmlPipeline html = new HtmlPipeline(htmlContext, pdf);
    CssResolverPipeline css = new CssResolverPipeline(cssResolver, html);

    XMLWorker worker = new XMLWorker(css, true);
    XMLParser p = new XMLParser(worker);
    p.parse(new ByteArrayInputStream(sb.toString().getBytes()));

    document.close();

}