List of usage examples for com.itextpdf.tool.xml XMLWorkerFontProvider register
public void register(final String path, final String alias)
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);/*from w w w. j a v a 2 s. c o m*/ 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); } }
From source file:org.smap.sdal.managers.PDFSurveyManager.java
License:Open Source License
private Parser getXMLParser() { Parser parser = new Parser(); // CSS/*from www . j av a 2 s . co m*/ CSSResolver cssResolver = new StyleAttrCSSResolver(); FileInputStream fis = null; try { fis = new FileInputStream(DEFAULT_CSS); CssFile cssFile = XMLWorkerHelper.getCSS(fis); cssResolver.addCss(cssFile); } catch (Exception e) { log.log(Level.SEVERE, "Failed to get CSS file", e); cssResolver = XMLWorkerHelper.getInstance().getDefaultCssResolver(true); } finally { try { fis.close(); } catch (Exception e) { } } // Pipelines parser.elements = new ElementList(); ElementHandlerPipeline end = new ElementHandlerPipeline(parser.elements, null); String os = System.getProperty("os.name"); log.info("Operating System:" + os); XMLWorkerFontProvider fontProvider = new XMLWorkerFontProvider(); if (os.startsWith("Mac")) { fontProvider.register("/Library/Fonts/NotoSansBengali-Regular.ttf", BaseFont.IDENTITY_H); fontProvider.register("/Library/Fonts/NotoNaskhArabic-Regular.ttf", BaseFont.IDENTITY_H); fontProvider.register("/Library/Fonts/NotoSansBengali-Bold.ttf", BaseFont.IDENTITY_H); fontProvider.register("/Library/Fonts/NotoSans-Regular.ttf", BaseFont.IDENTITY_H); fontProvider.register("/Library/Fonts/NotoSans-Bold.ttf", BaseFont.IDENTITY_H); } else if (os.indexOf("nix") >= 0 || os.indexOf("nux") >= 0 || os.indexOf("aix") > 0) { // Linux / Unix fontProvider.register("/usr/share/fonts/truetype/NotoSansBengali-Regular.ttf", BaseFont.IDENTITY_H); fontProvider.register("/usr/share/fonts/truetype/NotoNaskhArabic-Regular.ttf", BaseFont.IDENTITY_H); fontProvider.register("/usr/share/fonts/truetype/NotoNaskhArabic-Regular.ttf", BaseFont.IDENTITY_H); fontProvider.register("/usr/share/fonts/truetype/NotoSans-Regular.ttf", BaseFont.IDENTITY_H); fontProvider.register("/usr/share/fonts/truetype/NotoSans-Bold.ttf", BaseFont.IDENTITY_H); } /* System.out.println("Fonts present in " + fontProvider.getClass().getName()); Set<String> registeredFonts = fontProvider.getRegisteredFonts(); for (String font : registeredFonts) System.out.println(font); */ CssAppliers cssAppliers = new CssAppliersImpl(fontProvider); // HTML HtmlPipelineContext htmlContext = new HtmlPipelineContext(cssAppliers); htmlContext.setTagFactory(Tags.getHtmlTagProcessorFactory()); htmlContext.autoBookmark(false); HtmlPipeline html = new HtmlPipeline(htmlContext, end); CssResolverPipeline css = new CssResolverPipeline(cssResolver, html); XMLWorker worker = new XMLWorker(css, true); parser.xmlParser = new XMLParser(worker); return parser; }