2016-12-05 132 views
0

我有一个用例,我必须在pdf上显示动态图像。我正在使用PDF生成的FApacheFOP 2.1。我从API调用中获取图像行,然后将该图像转换为基本64格式。使用xslfo和FOP 2.1无法在PDF中看到base64图像

请找到Java COE转换图像:

String jpgFileName = ConfigManager.getImageFilePath() + "/jpgImage-"+System.currentTimeMillis()+".jpg"; 
Blob imageDataBlob = (Blob) faesRow.get("imageData"); 

     FileUtil.writeToFile(imageDataBlob, jpgFileName); 

     String base64Result = Base64.getEncoder().encodeToString(FileUtil.readFromFile(jpgFileName).getBytes("utf-8")); 

     result = base64Result; 

我正在使用XSLFO的base64数据类型上打印PDF格式的图像,请在下面找到XSLFO,这里$ signatureImage是发送的数据上面的java代码:

<xsl:param name="Name">data:image/jpg;base64,{$!signatureImage}</xsl:param> 

    <fo:block-container absolute-position="absolute" left="3.50in" top="9.25in" width="4.0in" height="2.0in"> 
     <fo:block text-align="left"> 
     <fo:external-graphic content-width="scale-to-fit" 
      content-height="100%" 
      width="100%" 
      scaling="uniform" 
      src="url({$Name})"/> 
     </fo:block> 
    </fo:block-container> 

在模板渲染的输出中,我可以在xslfo文件中获取base64流。请找到下面的输出:

<xsl:param name="Name">data:image/jpg;base64,{77+977+977+977+9ABBK... }</xsl:param> 

<fo:block-container absolute-position="absolute" left="3.50in" top="9.25in" width="4.0in" height="2.0in"> 
    <fo:block text-align="left"> 
    <fo:external-graphic content-width="scale-to-fit" 
     content-height="100%" 
     width="100%" 
     scaling="uniform" 
     src="url({$Name})"/> 
    </fo:block> 
</fo:block-container> 

现在的问题是,图像没有得到定价生成的PDF输出。 您可以帮我找到一种在这里打印图片的方法。

额外信息: 1.我没有得到任何错误生成PDF。 2. PDF能够打印静态图像和条形码。

+0

内容类型不应该是image/jpeg而不是image/jpg?另外,b64字符串周围的{}看起来对我很可疑。 –

+0

我尝试过使用jpeg并删除{}。但是没有运气,它没有显示图像。它看起来像问题是与base64转换,但不知道是什么问题。 –

+0

当我做你的“base64”数据77 + 977 + 977 + 977 + 9ABB的base64解码...它以0xef 0xbf 0xbd 0xef开头......这似乎不是一个jpeg文件,它将以0xff 0xd8开头,可能接着是0xff 0xe0。看起来对我来说是错误的... –

回答

2

我发现这种情况下的问题。

第一个问题是与base64相互转换,我们需要使用如下的转换:

File file= new File(jpgFileName); 
FileInputStream fileInputStream= new FileInputStream(file); 
byte[] b= new byte[(int) file.length()]; 

fileInputStream.read(b); 

String base64Result = new String(Base64.getEncoder().encode(b),"UTF-8"); 

比这还需要在XSLFO模板中的一些变化的其他太多,请看以下变化:

<fo:block-container absolute-position="absolute" left="3.50in" top="9.25in" width="4.0in" height="2.0in"> 
    <fo:block text-align="left"> 
    <fo:external-graphic content-width="scale-to-fit" 
     content-height="100%" 
     width="100%" 
     scaling="uniform" 
     src="url('data:image/jpeg;base64,$!signatureImage')"/> 
    </fo:block> 
</fo:block-container> 
+0

关于SO的最佳问题是OP自己找到答案的那些问题。 –

相关问题