Example usage for com.itextpdf.tool.xml XMLWorkerFontProvider XMLWorkerFontProvider

List of usage examples for com.itextpdf.tool.xml XMLWorkerFontProvider XMLWorkerFontProvider

Introduction

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

Prototype

public XMLWorkerFontProvider(String fontsPath) 

Source Link

Usage

From source file:net.algem.edition.PdfHandler.java

License:Open Source License

public XMLParser createParser(Document document, PdfWriter writer) {
    CSSResolver cssResolver = new StyleAttrCSSResolver();
    XMLWorkerFontProvider fontProvider = new XMLWorkerFontProvider(XMLWorkerFontProvider.DONTLOOKFORFONTS);
    //    fontProvider.register("resources/fonts/Cardo-Regular.ttf");
    //    fontProvider.register("resources/fonts/Cardo-Bold.ttf");
    //    fontProvider.register("resources/fonts/Cardo-Italic.ttf");
    //    fontProvider.addFontSubstitute("lowagie", "cardo");
    CssAppliers cssAppliers = new CssAppliersImpl(fontProvider);
    HtmlPipelineContext htmlContext = new HtmlPipelineContext(cssAppliers);
    htmlContext.setTagFactory(Tags.getHtmlTagProcessorFactory());
    // Pipelines/*from www .j  a v  a 2 s.  c  o  m*/
    PdfWriterPipeline pdf = new PdfWriterPipeline(document, writer);
    HtmlPipeline html = new HtmlPipeline(htmlContext, pdf);
    CssResolverPipeline css = new CssResolverPipeline(cssResolver, html);
    // XML Worker
    XMLWorker worker = new XMLWorker(css, true);
    return new XMLParser(worker);
}

From source file:net.duckling.ddl.web.controller.LynxDDocController.java

License:Apache License

@RequestMapping(params = "func=exportPdf")
public void exportPdf(HttpServletRequest request, HttpServletResponse response,
        @PathVariable("rid") Integer rid) {
    VWBContext context = VWBContext.createContext(request, UrlPatterns.T_PAGE, rid, LynxConstants.TYPE_PAGE);
    int tid = VWBContext.getCurrentTid();
    Team team = teamService.getTeamByID(tid);
    Resource r = resourceService.getResource(rid);
    response.setHeader("Content-disposition",
            Browser.encodeFileName(request.getHeader("USER-AGENT"), r.getTitle() + ".pdf"));
    response.setContentType("application/pdf");

    PageRender render = resourceOperateService.getPageRender(tid, rid);
    String html = renderingService.getHTML(context, render);
    StringBuffer buf = new StringBuffer();
    List<String> imagePathList = new ArrayList<String>();
    buf.append("<html><head><title>" + r.getTitle() + "</title>");
    buf.append("<link type=\"text/css\" rel=\"stylesheet\" href=\"" + context.getBaseURL()
            + "/jsp/aone/css/css.css\" />");
    buf.append("<style type=\"text/css\"> *{ font-family: simsun; } </style>");
    buf.append("</head>");
    buf.append("<body style=\"margin:0;\">");
    buf.append("<div id=\"DCT_viewcontent\">");
    html = processImagePath(html, team.getName(), imagePathList, request);
    buf.append(html);//  w ww .ja va 2  s .  c om
    buf.append("</div>");
    buf.append("</body></html>");

    Document document = new Document();
    PdfWriter writer;
    try {
        String fontPath = getFontPath("simsun.ttf");
        XMLWorkerFontProvider fontImp = new XMLWorkerFontProvider(fontPath);
        fontImp.register(fontPath, "simsun");
        FontFactory.setFontImp(fontImp);

        writer = PdfWriter.getInstance(document, response.getOutputStream());
        document.open();
        InputStreamReader isr = new InputStreamReader(
                new ByteArrayInputStream(buf.toString().getBytes(LynxConstants.UTF8)), LynxConstants.UTF8);
        XMLWorkerHelper.getInstance().parseXHtml(writer, document, isr);
    } catch (DocumentException e) {
        LOG.error("export pdf error. {rid:" + r.getRid() + "} : " + e.getMessage());
    } catch (IOException e) {
        LOG.error("export pdf Error. {rid:" + r.getRid() + "} : " + e.getMessage());
    } finally {
        try {
            document.close();
        } catch (Exception e) {
            LOG.warn(e.getMessage());
        }
        ;
    }

    //
    for (String path : imagePathList) {
        FileUtil.deleteFile(path);
    }
}