Skip to Main Content

Java EE (Java Enterprise Edition) General Discussion

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

XHTML to PDF with Flying Saucer on server-side

843834Aug 20 2009
Hi ! Flying Saucer looks great in SE , but when using on a server-side (say in servlet) a little problem is with Unicode... Non-latin characters just don't appear in the generated pdf document..... Below is the code for a PDF generation servlet :

public class PDFServlet extends HttpServlet {
 
 public void doGet(HttpServletRequest request,HttpServletResponse response ) throws                      ServletException,IOException{
 
        response.setContentType("application/pdf");        
 
        StringBuffer buf = new StringBuffer();
 
 
        buf.append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>"
+		"<html xmlns=\"http://www.w3.org/1999/xhtml\">"+
        		"<head><meta http-equiv=\"Content-Type\" content=\"text/html; charset=UTF-8\"/></head>");
 
buf.append("<body>");
 
 
buf.append("<h3>No more bottles of beer on the wall, no more bottles of beer. ");
buf.append(" \u10d0\u10d1\u10d2  , &#4336;&#4308;&#4314;&#4314;&#4317; &#4309;&#4317;&#4320;&#4314;&#4307; &#4325;&#4304;&#4320;&#4311;&#4323;&#4314;&#4304;&#4307; .... \u10d0\u10d1\u10d2.... ");
buf.append("</h3>");
buf.append("</body>");
buf.append("</html>");
 
 try {
 
            DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
            DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
            factory.setNamespaceAware(true);
            factory.setAttribute("http://java.sun.com/xml/jaxp/properties/schemaLanguage", "http://www.w3.org/2001/XMLSchema");
 
            Document doc = (Document) builder.parse(new InputSource(new        StringReader(buf.toString())));
 
            ITextRenderer renderer = new ITextRenderer();
            renderer.getFontResolver().addFont("C:/WINDOWS/Fonts/sylfaen.TTF",  BaseFont.IDENTITY_H,BaseFont.EMBEDDED);
 
           / 
*               I've tried also the following combinations*
 
*                1)   renderer.getFontResolver().addFont("WEB-INF/sylfaen.TTF",  BaseFont.IDENTITY_H,BaseFont.EMBEDDED);*
*                2)  renderer.getFontResolver().addFont(getServletContext().getResource("WEB-INF/sylfaen.TTF").toString(),  BaseFont.IDENTITY_H,BaseFont.EMBEDDED);*
 
/ 
 
            renderer.setDocument(doc, null);
 
            renderer.layout();
 
 
 
        ServletOutputStream os = response.getOutputStream();
 
        renderer.createPDF(os);
 
         os.flush();
         os.close();
 
 
 
 
 
 
 
        } catch (Exception ex) {
            ex.printStackTrace();
 
           // writer.print(ex.getMessage());
        }
    }
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Sep 17 2009
Added on Aug 20 2009
0 comments
642 views