Image 无法使用xslfo和FOP2.1在PDF中查看base64图像
我有一个用例,我必须在pdf上显示动态图像。我使用的是PDF生成的Fapachefop2.1。我从API调用中获取图像行,然后将该图像转换为Base64格式 请查找要转换图像的java coe:Image 无法使用xslfo和FOP2.1在PDF中查看base64图像,image,pdf,base64,xsl-fo,apache-fop,Image,Pdf,Base64,Xsl Fo,Apache Fop,我有一个用例,我必须在pdf上显示动态图像。我使用的是PDF生成的Fapachefop2.1。我从API调用中获取图像行,然后将该图像转换为Base64格式 请查找要转换图像的java coe: String jpgFileName = ConfigManager.getImageFilePath() + "/jpgImage-"+System.currentTimeMillis()+".jpg"; Blob imageDataBlob = (Blob) faesRow.get("imageDa
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>
数据:图像/jpg;base64,{$!signatureImage}
在模板呈现的输出中,我在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>
数据:图像/jpg;base64,{77+977+977+977+9ABBK…}
现在的问题是,图像并没有按生成的PDF输出定价您能帮我找到一种在这里打印图像的方法吗。
额外资料:
1.我没有得到任何错误生成PDF。
2.PDF能够打印静态图像和条形码。我发现了这种情况下的问题 第一个问题是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>
在那种情况下,我发现了问题所在 第一个问题是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>
内容类型不应该是image/jpeg
而不是image/jpg吗?另外,b64字符串周围的{}对我来说看起来很可疑。我尝试过使用jpeg并删除{}。。但是运气不好,它没有显示图像。看起来问题在于base64转换,但不确定问题出在哪里。当我对“base64”数据77+977+977+977+9ABB进行base64解码时。。。它以0xef 0xbf 0xbd 0xef开头。。。这似乎不是一个jpeg文件,它将以0xff 0xd8开始,然后可能是0xff 0xe0。在我看来是错误的…内容类型不应该是image/jpeg
而不是image/jpg吗?另外,b64字符串周围的{}对我来说看起来很可疑。我尝试过使用jpeg并删除{}。。但是运气不好,它没有显示图像。看起来问题在于base64转换,但不确定问题出在哪里。当我对“base64”数据77+977+977+977+9ABB进行base64解码时。。。它以0xef 0xbf 0xbd 0xef开头。。。这似乎不是一个jpeg文件,它将以0xff 0xd8开始,然后可能是0xff 0xe0。在我看来是错的…最好的问题是OP自己找到答案的问题。最好的问题是OP自己找到答案的问题。