解決iText+freemark導出pdf不支持base64的解決辦法


工具類:

  1 package test;
  2 
  3 import java.io.IOException ;
  4 import org.w3c.dom.Element ;
  5 import org.xhtmlrenderer.extend.FSImage ;
  6 import org.xhtmlrenderer.extend.ReplacedElement ;
  7 import org.xhtmlrenderer.extend.ReplacedElementFactory ;
  8 import org.xhtmlrenderer.extend.UserAgentCallback ;
  9 import org.xhtmlrenderer.layout.LayoutContext ;
 10 import org.xhtmlrenderer.pdf.ITextFSImage ;
 11 import org.xhtmlrenderer.pdf.ITextImageElement ;
 12 import org.xhtmlrenderer.render.BlockBox ;
 13 import org.xhtmlrenderer.simple.extend.FormSubmissionListener ;
 14 import com.lowagie.text.BadElementException ;
 15 import com.lowagie.text.Image ;
 16 import com.lowagie.text.pdf.codec.Base64 ;
 17 
 18 
 19 
 20 public class B64ImgReplacedElementFactory implements ReplacedElementFactory {
 21 
 22     /**
 23      * 實現createReplacedElement 替換html中的Img標簽
 24      * 
 25      * @param c 上下文
 26      * @param box 盒子
 27      * @param uac 回調
 28      * @param cssWidth css寬
 29      * @param cssHeight css高
 30      * @return ReplacedElement
 31      */
 32     public ReplacedElement createReplacedElement(LayoutContext c, BlockBox box, UserAgentCallback uac,
 33             int cssWidth, int cssHeight) {
 34         Element e = box.getElement();
 35         if (e == null) {
 36             return null;
 37         }
 38         String nodeName = e.getNodeName();
 39         // 找到img標簽
 40         if (nodeName.equals("img")) {
 41             String attribute = e.getAttribute("src");
 42             FSImage fsImage;
 43             try {
 44                 // 生成itext圖像
 45                 fsImage = buildImage(attribute, uac);
 46             } catch (BadElementException e1) {
 47                 fsImage = null;
 48             } catch (IOException e1) {
 49                 fsImage = null;
 50             }
 51             if (fsImage != null) {
 52                 // 對圖像進行縮放
 53                 if (cssWidth != -1 || cssHeight != -1) {
 54                     fsImage.scale(cssWidth, cssHeight);
 55                 }
 56                 return new ITextImageElement(fsImage);
 57             }
 58         }
 59 
 60         return null;
 61     }
 62 
 63     /**
 64      * 將base64編碼解碼並生成itext圖像
 65      * 
 66      * @param srcAttr 屬性
 67      * @param uac 回調
 68      * @return FSImage
 69      * @throws IOException io異常
 70      * @throws BadElementException BadElementException
 71      */
 72     protected FSImage buildImage(String srcAttr, UserAgentCallback uac) throws IOException,
 73             BadElementException {
 74         FSImage fsImage;
 75         if (srcAttr.startsWith("data:image/")) {
 76             String b64encoded = srcAttr.substring(srcAttr.indexOf("base64,") + "base64,".length(),
 77                     srcAttr.length());
 78             // 解碼
 79             byte[] decodedBytes = Base64.decode(b64encoded);
 80 
 81             fsImage = new ITextFSImage(Image.getInstance(decodedBytes));
 82         } else {
 83             fsImage = uac.getImageResource(srcAttr).getImage();
 84         }
 85         return fsImage;
 86     }
 87 
 88 
 89     /**
 90      * 實現reset
 91      */
 92     public void reset() {
 93     }
 94 
 95 
 96     @Override
 97     public void remove(Element arg0) {}
 98 
 99     @Override
100     public void setFormSubmissionListener(FormSubmissionListener arg0) {}
101 }

生成pdf時:

1 // 解決base64圖片支持問題  
2 sharedContext.setReplacedElementFactory(new B64ImgReplacedElementFactory());  
3 sharedContext.getTextRenderer().setSmoothingThreshold(0);  
4 renderer.setDocumentFromString(strFileContent);  

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM