解決iText2.0.8+freemark導出pdf不支持base64的解決辦法


轉換工具類

public class B64ImgReplacedElementFactory implements ReplacedElementFactory {  
  
    /** 
     * 實現createReplacedElement 替換html中的Img標簽 
     *  
     * @param c 上下文 
     * @param box 盒子 
     * @param uac 回調 
     * @param cssWidth css寬 
     * @param cssHeight css高 
     * @return ReplacedElement 
     */  
    public ReplacedElement createReplacedElement(LayoutContext c, BlockBox box, UserAgentCallback uac,  
            int cssWidth, int cssHeight) {  
        Element e = box.getElement();  
        if (e == null) {  
            return null;  
        }  
        String nodeName = e.getNodeName();  
        // 找到img標簽  
        if (nodeName.equals("img")) {  
            String attribute = e.getAttribute("src");  
            FSImage fsImage;  
            try {  
                // 生成itext圖像  
                fsImage = buildImage(attribute, uac);  
            } catch (BadElementException e1) {  
                fsImage = null;  
            } catch (IOException e1) {  
                fsImage = null;  
            }  
            if (fsImage != null) {  
                // 對圖像進行縮放  
                if (cssWidth != -1 || cssHeight != -1) {  
                    fsImage.scale(cssWidth, cssHeight);  
                }  
                return new ITextImageElement(fsImage);  
            }  
        }  
  
        return null;  
    }  
  
    /** 
     * 將base64編碼解碼並生成itext圖像 
     *  
     * @param srcAttr 屬性 
     * @param uac 回調 
     * @return FSImage 
     * @throws IOException io異常 
     * @throws BadElementException BadElementException 
     */  
    protected FSImage buildImage(String srcAttr, UserAgentCallback uac) throws IOException,  
            BadElementException {  
        FSImage fsImage;  
        if (srcAttr.startsWith("data:image/")) {  
            String b64encoded = srcAttr.substring(srcAttr.indexOf("base64,") + "base64,".length(),  
                    srcAttr.length());  
            // 解碼  
            byte[] decodedBytes = Base64.decode(b64encoded);  
  
            fsImage = new ITextFSImage(Image.getInstance(decodedBytes));  
        } else {  
            fsImage = uac.getImageResource(srcAttr).getImage();  
        }  
        return fsImage;  
    }  
  
    /** 
     * 實現remove 
     *  
     * @param e 元素 
     */  
    public void remove(Element e) {  
    }  
  
    /** 
     * 實現reset 
     */  
    public void reset() {  
    }  
  
    /** 
     * 實現setFormSubmissionListener 
     *  
     * @param formsubmissionlistener 監聽 
     */  
    public void setFormSubmissionListener(FormSubmissionListener formsubmissionlistener) {  
    }  
}  
ITextRenderer 調用轉換工具類
ITextRenderer renderer = new ITextRenderer();  
SharedContext sharedContext = renderer.getSharedContext();  
// 解決base64圖片支持問題  
sharedContext.setReplacedElementFactory(new B64ImgReplacedElementFactory());  
sharedContext.getTextRenderer().setSmoothingThreshold(0);  
renderer.setDocumentFromString(strFileContent);  

 原文地址:http://blog.csdn.net/rangerkriby/article/details/17439705


免責聲明!

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



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