Search in sources :

Example 1 with CssResolverPipeline

use of com.itextpdf.tool.xml.pipeline.css.CssResolverPipeline in project trainning by fernandotomasio.

the class DOC003PDF method makeReport.

@Override
public void makeReport() {
    teachingDocumentsService = (TeachingDocumentsService) services.get("teachingDocumentsService");
    document = documentManager.getDocumentPortrait();
    os = documentManager.prepareDocument(document);
    writer = documentManager.getWritter();
    ChapterSectionTOC eventHandler = new ChapterSectionTOC();
    writer.setPageEvent(eventHandler);
    document.open();
    CSSResolver cssResolver = new StyleAttrCSSResolver();
    CssFile cssFile = null;
    ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
    String cssStylePath = classLoader.getResource("doc3-styles.css").getPath();
    cssFile = XMLWorkerHelper.getCSS(classLoader.getResourceAsStream("doc3-styles.css"));
    cssResolver.addCss(cssFile);
    XMLWorkerFontProvider fontProvider = new XMLWorkerFontProvider(XMLWorkerFontProvider.DONTLOOKFORFONTS);
    String fontFilePath = classLoader.getResource("MyriadPro-Regular.otf").getPath();
    // fontProvider.register(fontFilePath);
    CssAppliers cssAppliers = new CssAppliersImpl(fontProvider);
    HtmlPipelineContext htmlContext = new HtmlPipelineContext(cssAppliers);
    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);
    p = new XMLParser(worker);
    try {
        String apostilaIdParam = (String) params.get("apostilaId");
        Long apostilaId = Long.parseLong(apostilaIdParam);
        ApostilaDTO apostila = teachingDocumentsService.findApostila(apostilaId);
        CurriculoMinimoDTO curriculo = teachingDocumentsService.findCurriculoMinimo(apostila.getDisciplina().getCurriculoMinimo().getId());
        buildCover(document, curriculo, apostila);
        CapituloApostilaDTO[] capitulos = teachingDocumentsService.findAllCapitulosApostila(apostilaId);
        for (CapituloApostilaDTO capitulo : capitulos) {
            Chapter ch = buildCapitulo(capitulo, p, document);
            document.add(ch);
        }
        document.add(buildReferencias(apostilaId));
        document.add(buildSumario(eventHandler.titles));
    } catch (DocumentException e) {
        Logger.getLogger(this.getClass().getName()).log(Level.SEVERE, null, e);
        throw new CoreException(e.getMessage());
    } catch (IOException ex) {
        Logger.getLogger(DOC003PDF.class.getName()).log(Level.SEVERE, null, ex);
    } finally {
        document.close();
    }
    try {
        PdfReader reader = new PdfReader(os.toByteArray());
        int n = reader.getNumberOfPages();
        String pagesInterval = String.format("1-2,%d, 3-%d", n, n - 1);
        reader.selectPages(pagesInterval);
        os = new ByteArrayOutputStream();
        PdfStamper stamper = new PdfStamper(reader, os);
        stamper.close();
        reader.close();
    } catch (IOException | DocumentException ex) {
        Logger.getLogger(DOC003PDF.class.getName()).log(Level.SEVERE, null, ex);
    }
}
Also used : StyleAttrCSSResolver(com.itextpdf.tool.xml.css.StyleAttrCSSResolver) CssFile(com.itextpdf.tool.xml.css.CssFile) PdfReader(com.itextpdf.text.pdf.PdfReader) CssAppliersImpl(com.itextpdf.tool.xml.html.CssAppliersImpl) CSSResolver(com.itextpdf.tool.xml.pipeline.css.CSSResolver) StyleAttrCSSResolver(com.itextpdf.tool.xml.css.StyleAttrCSSResolver) DocumentException(com.itextpdf.text.DocumentException) CapituloApostilaDTO(com.tomasio.projects.trainning.dto.CapituloApostilaDTO) CurriculoMinimoDTO(com.tomasio.projects.trainning.dto.CurriculoMinimoDTO) XMLParser(com.itextpdf.tool.xml.parser.XMLParser) HtmlPipeline(com.itextpdf.tool.xml.pipeline.html.HtmlPipeline) SecaoApostilaDTO(com.tomasio.projects.trainning.dto.SecaoApostilaDTO) ApostilaDTO(com.tomasio.projects.trainning.dto.ApostilaDTO) CapituloApostilaDTO(com.tomasio.projects.trainning.dto.CapituloApostilaDTO) Chapter(com.itextpdf.text.Chapter) IOException(java.io.IOException) ByteArrayOutputStream(java.io.ByteArrayOutputStream) CssResolverPipeline(com.itextpdf.tool.xml.pipeline.css.CssResolverPipeline) XMLWorkerFontProvider(com.itextpdf.tool.xml.XMLWorkerFontProvider) XMLWorker(com.itextpdf.tool.xml.XMLWorker) CoreException(com.tomasio.projects.trainning.exeption.CoreException) CssAppliers(com.itextpdf.tool.xml.html.CssAppliers) PdfWriterPipeline(com.itextpdf.tool.xml.pipeline.end.PdfWriterPipeline) PdfStamper(com.itextpdf.text.pdf.PdfStamper) HtmlPipelineContext(com.itextpdf.tool.xml.pipeline.html.HtmlPipelineContext)

Example 2 with CssResolverPipeline

use of com.itextpdf.tool.xml.pipeline.css.CssResolverPipeline in project trainning by fernandotomasio.

the class DOC005PDF method makeReport.

@Override
public void makeReport() {
    teachingDocumentsService = (TeachingDocumentsService) services.get("teachingDocumentsService");
    document = documentManager.getDocumentPortrait();
    os = documentManager.prepareDocument(document);
    writer = documentManager.getWritter();
    ChapterSectionTOC eventHandler = new ChapterSectionTOC();
    writer.setPageEvent(eventHandler);
    document.open();
    CSSResolver cssResolver = new StyleAttrCSSResolver();
    CssFile cssFile = null;
    ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
    String cssStylePath = classLoader.getResource("doc3-styles.css").getPath();
    cssFile = XMLWorkerHelper.getCSS(classLoader.getResourceAsStream("doc3-styles.css"));
    cssResolver.addCss(cssFile);
    XMLWorkerFontProvider fontProvider = new XMLWorkerFontProvider(XMLWorkerFontProvider.DONTLOOKFORFONTS);
    String fontFilePath = classLoader.getResource("MyriadPro-Regular.otf").getPath();
    // fontProvider.register(fontFilePath);
    CssAppliers cssAppliers = new CssAppliersImpl(fontProvider);
    HtmlPipelineContext htmlContext = new HtmlPipelineContext(cssAppliers);
    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);
    p = new XMLParser(worker);
    try {
        String secaoIdParam = (String) params.get("secaoId");
        document.add(Chunk.NEXTPAGE);
        SecaoApostilaDTO secao = teachingDocumentsService.findSecaoApostila(Long.parseLong(secaoIdParam));
        Chapter ch = new Chapter(0);
        ch.setNumberDepth(0);
        if (secao.getContent() != null) {
            buildSection(ch, secao, p, document);
        }
        document.add(ch);
    } catch (DocumentException e) {
        Logger.getLogger(this.getClass().getName()).log(Level.SEVERE, null, e);
        throw new CoreException(e.getMessage());
    } catch (IOException ex) {
        Logger.getLogger(DOC005PDF.class.getName()).log(Level.SEVERE, null, ex);
    } finally {
        document.close();
    }
}
Also used : StyleAttrCSSResolver(com.itextpdf.tool.xml.css.StyleAttrCSSResolver) HtmlPipeline(com.itextpdf.tool.xml.pipeline.html.HtmlPipeline) Chapter(com.itextpdf.text.Chapter) CssFile(com.itextpdf.tool.xml.css.CssFile) IOException(java.io.IOException) CssResolverPipeline(com.itextpdf.tool.xml.pipeline.css.CssResolverPipeline) CssAppliersImpl(com.itextpdf.tool.xml.html.CssAppliersImpl) SecaoApostilaDTO(com.tomasio.projects.trainning.dto.SecaoApostilaDTO) XMLWorkerFontProvider(com.itextpdf.tool.xml.XMLWorkerFontProvider) XMLWorker(com.itextpdf.tool.xml.XMLWorker) CoreException(com.tomasio.projects.trainning.exeption.CoreException) CSSResolver(com.itextpdf.tool.xml.pipeline.css.CSSResolver) StyleAttrCSSResolver(com.itextpdf.tool.xml.css.StyleAttrCSSResolver) CssAppliers(com.itextpdf.tool.xml.html.CssAppliers) PdfWriterPipeline(com.itextpdf.tool.xml.pipeline.end.PdfWriterPipeline) DocumentException(com.itextpdf.text.DocumentException) HtmlPipelineContext(com.itextpdf.tool.xml.pipeline.html.HtmlPipelineContext) XMLParser(com.itextpdf.tool.xml.parser.XMLParser)

Example 3 with CssResolverPipeline

use of com.itextpdf.tool.xml.pipeline.css.CssResolverPipeline in project trainning by fernandotomasio.

the class DOC004PDF method makeReport.

@Override
public void makeReport() {
    teachingDocumentsService = (TeachingDocumentsService) services.get("teachingDocumentsService");
    document = documentManager.getDocumentPortrait();
    os = documentManager.prepareDocument(document);
    writer = documentManager.getWritter();
    document.open();
    HtmlPipelineContext htmlContext = new HtmlPipelineContext(null);
    htmlContext.setTagFactory(Tags.getHtmlTagProcessorFactory());
    CSSResolver cssResolver = XMLWorkerHelper.getInstance().getDefaultCssResolver(true);
    Pipeline<?> pipeline = new CssResolverPipeline(cssResolver, new HtmlPipeline(htmlContext, new PdfWriterPipeline(document, writer)));
    XMLWorker worker = new XMLWorker(pipeline, true);
    XMLParser p = new XMLParser(worker);
    try {
        String capituloIdParam = (String) params.get("capituloId");
        Long capituloId = Long.parseLong(capituloIdParam);
        CapituloApostilaDTO capitulo = teachingDocumentsService.findCapituloApostila(capituloId);
        // Paragraph prefacioParagraph = new Paragraph(new Phrase("APOSTILA" + apostila.getDisciplina().getDescricao(), fontManager.getBoldFont()));
        // prefacioParagraph.setAlignment(Element.ALIGN_CENTER);
        // document.add(prefacioParagraph);
        SecaoApostilaDTO[] secoes = teachingDocumentsService.findAllSecoesApostila(capitulo.getId());
        for (SecaoApostilaDTO secao : secoes) {
            if (secao.getContent() != null) {
                Paragraph secaoParagraph = new Paragraph(secao.getContent());
                p.parse(new StringReader(secao.getContent()));
                document.add(Chunk.NEXTPAGE);
            }
        }
    } catch (DocumentException e) {
        Logger.getLogger(this.getClass().getName()).log(Level.SEVERE, null, e);
        throw new CoreException(e.getMessage());
    } catch (IOException ex) {
        Logger.getLogger(DOC004PDF.class.getName()).log(Level.SEVERE, null, ex);
    } finally {
        document.close();
    }
}
Also used : HtmlPipeline(com.itextpdf.tool.xml.pipeline.html.HtmlPipeline) IOException(java.io.IOException) CssResolverPipeline(com.itextpdf.tool.xml.pipeline.css.CssResolverPipeline) Paragraph(com.itextpdf.text.Paragraph) SecaoApostilaDTO(com.tomasio.projects.trainning.dto.SecaoApostilaDTO) XMLWorker(com.itextpdf.tool.xml.XMLWorker) CoreException(com.tomasio.projects.trainning.exeption.CoreException) CSSResolver(com.itextpdf.tool.xml.pipeline.css.CSSResolver) PdfWriterPipeline(com.itextpdf.tool.xml.pipeline.end.PdfWriterPipeline) DocumentException(com.itextpdf.text.DocumentException) CapituloApostilaDTO(com.tomasio.projects.trainning.dto.CapituloApostilaDTO) StringReader(java.io.StringReader) HtmlPipelineContext(com.itextpdf.tool.xml.pipeline.html.HtmlPipelineContext) XMLParser(com.itextpdf.tool.xml.parser.XMLParser)

Aggregations

DocumentException (com.itextpdf.text.DocumentException)3 XMLWorker (com.itextpdf.tool.xml.XMLWorker)3 XMLParser (com.itextpdf.tool.xml.parser.XMLParser)3 CSSResolver (com.itextpdf.tool.xml.pipeline.css.CSSResolver)3 CssResolverPipeline (com.itextpdf.tool.xml.pipeline.css.CssResolverPipeline)3 PdfWriterPipeline (com.itextpdf.tool.xml.pipeline.end.PdfWriterPipeline)3 HtmlPipeline (com.itextpdf.tool.xml.pipeline.html.HtmlPipeline)3 HtmlPipelineContext (com.itextpdf.tool.xml.pipeline.html.HtmlPipelineContext)3 SecaoApostilaDTO (com.tomasio.projects.trainning.dto.SecaoApostilaDTO)3 CoreException (com.tomasio.projects.trainning.exeption.CoreException)3 IOException (java.io.IOException)3 Chapter (com.itextpdf.text.Chapter)2 XMLWorkerFontProvider (com.itextpdf.tool.xml.XMLWorkerFontProvider)2 CssFile (com.itextpdf.tool.xml.css.CssFile)2 StyleAttrCSSResolver (com.itextpdf.tool.xml.css.StyleAttrCSSResolver)2 CssAppliers (com.itextpdf.tool.xml.html.CssAppliers)2 CssAppliersImpl (com.itextpdf.tool.xml.html.CssAppliersImpl)2 CapituloApostilaDTO (com.tomasio.projects.trainning.dto.CapituloApostilaDTO)2 Paragraph (com.itextpdf.text.Paragraph)1 PdfReader (com.itextpdf.text.pdf.PdfReader)1