无法使用 xslfo 和 FOP 2.1 在 PDF 中查看 base64 图像

Not able to see base64 image in PDF using xslfo and FOP 2.1

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

请找到 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可以打印静态图片和条码。

我在那种情况下发现了问题。

第一个问题是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>