Ignore:
Timestamp:
03/18/09 13:23:35 (3 years ago)
Author:
jharrop
Message:

Refactoring:

  • pdf output is now created via one of three implementations of PdfConversion? (only viaHTML is currently implemented).
  • convert.out.html and .xmlPackage implement new Output interface.
File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/docx4j/src/main/java/org/docx4j/openpackaging/packages/WordprocessingMLPackage.java

    r680 r682  
    276276        private Substituter fontSubstituter; 
    277277     
    278         /** Create a pdf version of the document.  
    279          *  
    280          * @param os 
    281          *            The OutputStream to write the pdf to  
    282          *  
    283          * */      
    284     public void pdf(OutputStream os) throws Exception { 
    285          
    286         /* 
    287          * There are 2 broad approaches we could use to render the document 
    288          * as a PDF: 
    289          *  
    290          * 1.  XSL-FO 
    291          * 2.  XHTML to PDF 
    292          *  
    293          * Given that a word2html.xsl is already freely available, we use  
    294          * the second approach. 
    295          *  
    296          * The question then is how the stylesheet is made to work with 
    297          * our main document and style definition parts. 
    298          *  
    299          * it processes pck:package/pck:part 
    300          *  
    301          */ 
    302                                  
    303         // Put the html in result 
    304                 org.w3c.dom.Document xhtmlDoc = org.docx4j.XmlUtils.neww3cDomDocument(); 
    305                 javax.xml.transform.dom.DOMResult result = new javax.xml.transform.dom.DOMResult(xhtmlDoc); 
    306                 org.docx4j.convert.out.html.HtmlExporter.html(this, result, false, 
    307                                 System.getProperty("java.io.tmpdir") ); // false -> don't use HTML fonts. 
    308                                  
    309                 // Now render the XHTML 
    310                 org.xhtmlrenderer.pdf.ITextRenderer renderer = new org.xhtmlrenderer.pdf.ITextRenderer(); 
    311                                  
    312                 // 4.  Use addFont code like that below as necessary for the fonts 
    313                  
    314                         // See https://xhtmlrenderer.dev.java.net/guide/users-guide-r7.html#xil_32 
    315                 org.xhtmlrenderer.extend.FontResolver resolver = renderer.getFontResolver();             
    316                                  
    317                 Map fontMappings = getFontSubstituter().getFontMappings(); 
    318                 Map fontsInUse = this.getMainDocumentPart().fontsInUse(); 
    319                 Iterator fontMappingsIterator = fontsInUse.entrySet().iterator(); 
    320                 while (fontMappingsIterator.hasNext()) { 
    321                 Map.Entry pairs = (Map.Entry)fontMappingsIterator.next(); 
    322                 if(pairs.getKey()==null) { 
    323                         log.info("Skipped null key"); 
    324                         pairs = (Map.Entry)fontMappingsIterator.next(); 
    325                 } 
    326                  
    327                 String fontName = (String)pairs.getKey(); 
    328                 embed(renderer, Substituter.normalise(fontName), fontMappings);          
    329                 // For any font we embed, also embed the bold, italic, and bold italic substitute 
    330                 // .. at present, we can't tell which of these forms are actually used, so add them all 
    331                 embed(renderer, Substituter.normalise(fontName + Substituter.BOLD), fontMappings); 
    332                 embed(renderer, Substituter.normalise(fontName + Substituter.ITALIC), fontMappings); 
    333                 embed(renderer, Substituter.normalise(fontName + Substituter.BOLD_ITALIC), fontMappings); 
    334                  
    335             } 
    336                  
    337             // TESTING 
    338 //          xhtmlDoc = org.docx4j.XmlUtils.neww3cDomDocument(); 
    339 //          try { 
    340 //                      javax.xml.parsers.DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); 
    341 //                      dbf.setNamespaceAware(true); 
    342 //                      dbf.newDocumentBuilder().newDocument(); 
    343 //               
    344 //                      xhtmlDoc = dbf.newDocumentBuilder().parse(new File("C:\\Users\\jharrop\\workspace\\docx4all\\sample-docs\\comic.html")); 
    345 //        } catch (Exception e) { 
    346 //            e.printStackTrace(); 
    347 //        }          
    348              
    349                 renderer.setDocument(xhtmlDoc, null); 
    350                 renderer.layout(); 
    351                  
    352                 renderer.createPDF(os); 
    353                  
    354         } 
    355         /** 
    356          * @param renderer 
    357          * @param fontName 
    358          * @param fm 
    359          */ 
    360         private void embed(org.xhtmlrenderer.pdf.ITextRenderer renderer, 
    361                         String fontName, Map fontMappings) { 
    362                 Substituter.FontMapping fm = (Substituter.FontMapping)fontMappings.get( fontName ); 
    363                  
    364                 if (fm == null) { 
    365                         log.warn("No mapping found for: " + fontName); 
    366                 } else if (fm.getPhysicalFont()!=null) { 
    367                         try { 
    368                                 if (fm.getPhysicalFont().getEmbeddedFile().endsWith(".pfb")) { 
    369                                          
    370 //                                              String afm = fm.getPhysicalFont().getEmbeddedFile().substring(5, fm.getPhysicalFont().getEmbeddedFile().length()-4 ) + ".afm";  // drop the 'file:' 
    371                                         String afm = FontUtils.pathFromURL(fm.getPhysicalFont().getEmbeddedFile()); 
    372                                         afm = afm.substring(0, afm.length()-4 ) + ".afm";  // drop the 'file:' 
    373                                         log.info("Looking for: " + afm); 
    374                                          
    375                                         // Given the check in substituter, we expect to find one or the other. 
    376                                         File f = new File(afm); 
    377                                 if (f.exists()) {                                
    378                                         log.info("Got it"); 
    379 //                                      renderer.getFontResolver().addFont(afm, BaseFont.CP1252, true, FontUtils.pathFromURL(fm.getPhysicalFont().getEmbeddedFile()));  // drop the 'file:'      
    380                                         renderer.getFontResolver().addFont(afm, BaseFont.IDENTITY_H, true, FontUtils.pathFromURL(fm.getPhysicalFont().getEmbeddedFile()));  // drop the 'file:'  
    381                                                 log.info("Substituting " + fontName + " with embedding " + fm.getPhysicalFont().getFamilyName() + " from " + fm.getPhysicalFont().getEmbeddedFile() ); 
    382                                 } else { 
    383                                         // Should we be doing afm first, or pfm? 
    384                                                 String pfm = FontUtils.pathFromURL(fm.getPhysicalFont().getEmbeddedFile()); 
    385                                                 pfm = pfm.substring(0, pfm.length()-4 ) + ".pfm";  // drop the 'file:' 
    386                                                 log.info("Looking for: " + pfm); 
    387                                                 f = new File(pfm); 
    388                                         if (f.exists()) {                                
    389                                                 log.info("Got it"); 
    390 //                                              renderer.getFontResolver().addFont(pfm, BaseFont.CP1252, true, FontUtils.pathFromURL(fm.getPhysicalFont().getEmbeddedFile() ));  // drop the 'file:' 
    391                                                 renderer.getFontResolver().addFont(pfm, BaseFont.IDENTITY_H, true, FontUtils.pathFromURL(fm.getPhysicalFont().getEmbeddedFile() ));  // drop the 'file:' 
    392                                                         log.info("Substituting " + fontName + " with embedding " + fm.getPhysicalFont().getFamilyName() + " from " + fm.getPhysicalFont().getEmbeddedFile() ); 
    393                                         } else { 
    394                                                 // Shouldn't happen. 
    395                                                 log.error("Couldn't find afm or pfm corresponding to " + fm.getPhysicalFont().getEmbeddedFile()); 
    396                                         } 
    397                                 } 
    398                                 } else {                                 
    399                                         renderer.getFontResolver().addFont(FontUtils.pathFromURL(fm.getPhysicalFont().getEmbeddedFile()), true); 
    400                                         log.info("Substituting " + fontName + " with embedding " + fm.getPhysicalFont().getFamilyName() + " from " + fm.getPhysicalFont().getEmbeddedFile() ); 
    401                                 } 
    402                         } catch (java.io.IOException e) { 
    403                          
    404                         /*  
    405                          * [AWT-EventQueue-0] INFO  packages.WordprocessingMLPackage - Substituting symbol with standardsymbolsl from file:/usr/share/fonts/type1/gsfonts/s050000l.pfb  
    406 java.io.IOException: Unsupported font type 
    407 at org.xhtmlrenderer.pdf.ITextFontResolver.addFont(ITextFontResolver.java:199) 
    408  
    409 .pfb not supported, even with iText 2.0.8 
    410  
    411                          */ 
    412                                 e.printStackTrace(); 
    413                                 log.warn("Shouldn't happen - should have been detected upstream ... " +  e.getMessage() + ": " + fm.getPhysicalFont().getEmbeddedFile());  
    414                         } catch (Exception e) { 
    415                                 e.printStackTrace(); 
    416                                 log.error("Shouldn't happen - should have been detected upstream ... " + e.getMessage());  
    417                         } 
    418                 } else { 
    419                         log.warn("Can't addFont for: " + fontName);  
    420                 } 
    421         }        
    422278         
    423279 
Note: See TracChangeset for help on using the changeset viewer.