Example usage for com.itextpdf.text FontFactory setFontImp

List of usage examples for com.itextpdf.text FontFactory setFontImp

Introduction

In this page you can find the example usage for com.itextpdf.text FontFactory setFontImp.

Prototype

public static void setFontImp(final FontFactoryImp fontImp) 

Source Link

Document

Sets the font factory implementation.

Usage

From source file:com.propelics.pdfcreator.PdfcreatorModule.java

License:Open Source License

/**
 * @method generatePDFWithHTML/* w ww  .ja va 2 s  .c  o  m*/
 * Generates a PDF with the given file name, based on a HTML file
 * @param {String} filename Name of the PDF file
 * @param {String} html String with the HTML
 * @param {String} [author] Author for metadata
 * Fires a "complete" event when the PDF is generated
 * Fires a "error" event when a error is presented
 */
@Kroll.method(runOnUiThread = true)
public void generatePDFWithHTML(final HashMap args) {
    Log.d(PROXY_NAME, "generatePDFWithHTML()");

    String html = "";
    String author = "";
    String filename = "";
    final float marginPt = 28.35f;//1cm == 28.35pt

    try {
        if (args.containsKey("filename")) {
            filename = (String) args.get("filename");
            Log.d(PROXY_NAME, "filename: " + filename);
        } else
            return;

        if (args.containsKey("html")) {
            html = (String) args.get("html");
            Log.d(PROXY_NAME, "html: " + html);
        } else
            return;

        if (args.containsKey("author")) {
            author = (String) args.get("author");
            Log.d(PROXY_NAME, "author: " + author);
        }

        //create a new document
        Document document = new Document(PageSize.LETTER, marginPt, marginPt, marginPt, 0);
        TiBaseFile file = TiFileFactory.createTitaniumFile(filename, true);

        // Parse to XHTML
        StringWriter xhtmlWriter = new StringWriter();
        Tidy tidy = new Tidy();

        // tidy.setXHTML(true);
        tidy.setXmlOut(true);
        tidy.parse(new StringReader(html), xhtmlWriter);
        String xhtml = xhtmlWriter.toString();

        //get Instance of the PDFWriter
        PdfWriter pdfWriter = PdfWriter.getInstance(document, file.getOutputStream());

        //document header attributes
        document.addAuthor(author);
        document.addCreationDate();
        document.setPageSize(PageSize.LETTER);

        //open document
        document.open();

        // From Stack Overflow lol

        MyFontFactory fontFactory = new MyFontFactory();
        FontFactory.setFontImp(fontFactory);

        // HtmlPipelineContext htmlContext = new HtmlPipelineContext(new CssAppliersImpl(fontFactory));
        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, pdfWriter)));
        XMLWorker worker = new XMLWorker(pipeline, true);
        XMLParser p = new XMLParser(worker);
        p.parse(new StringReader(xhtml));

        // Finish SO c&P

        // Older code
        /*
        // Font Provider creation
        MyFontFactory fontProvider = new MyFontFactory();
        // fontProvider.register("/DroidSans.ttf");
                
        //get the XMLWorkerHelper Instance
        XMLWorkerHelper worker = XMLWorkerHelper.getInstance();
        //convert to PDF
        worker.parseXHtml(pdfWriter, document, new ByteArrayInputStream(xhtml.getBytes("UTF-8")), null, fontProvider); //Load xhtml
        */

        //close the document
        document.close();
        //close the writer
        pdfWriter.close();

        sendCompleteEvent(filename);

    } catch (Exception e) {
        sendErrorEvent(e);
    }
}

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 ww  . 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);
    }
}