Search in sources :

Example 1 with CssAppliers

use of com.itextpdf.tool.xml.html.CssAppliers 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 CssAppliers

use of com.itextpdf.tool.xml.html.CssAppliers 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)

Aggregations

Chapter (com.itextpdf.text.Chapter)2 DocumentException (com.itextpdf.text.DocumentException)2 XMLWorker (com.itextpdf.tool.xml.XMLWorker)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 XMLParser (com.itextpdf.tool.xml.parser.XMLParser)2 CSSResolver (com.itextpdf.tool.xml.pipeline.css.CSSResolver)2 CssResolverPipeline (com.itextpdf.tool.xml.pipeline.css.CssResolverPipeline)2 PdfWriterPipeline (com.itextpdf.tool.xml.pipeline.end.PdfWriterPipeline)2 HtmlPipeline (com.itextpdf.tool.xml.pipeline.html.HtmlPipeline)2 HtmlPipelineContext (com.itextpdf.tool.xml.pipeline.html.HtmlPipelineContext)2 SecaoApostilaDTO (com.tomasio.projects.trainning.dto.SecaoApostilaDTO)2 CoreException (com.tomasio.projects.trainning.exeption.CoreException)2 IOException (java.io.IOException)2 PdfReader (com.itextpdf.text.pdf.PdfReader)1 PdfStamper (com.itextpdf.text.pdf.PdfStamper)1 ApostilaDTO (com.tomasio.projects.trainning.dto.ApostilaDTO)1