【问题标题】:Not able to see base64 image in PDF using xslfo and FOP 2.1无法使用 xslfo 和 FOP 2.1 在 PDF 中查看 base64 图像
【发布时间】:2017-04-20 00:24:18
【问题描述】:

我有一个用例,我必须在 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可以打印静态图片和条码。

【问题讨论】:

  • 内容类型不应该是 image/jpeg 而不是 image/jpg 吗?此外,b64 字符串周围的 { } 在我看来很可疑。
  • 我尝试过使用 jpeg 并删除 { } 。但运气不好,它没有显示图像。看起来问题出在 base64 转换上,但不确定是什么问题。
  • 当我对您的“base64”数据 77+977+977+977+9ABB 进行 base64 解码时...它以 0xef 0xbf 0xbd 0xef ... 开头...这似乎不是 jpeg 文件这将从 0xff 0xd8 开始,然后可能是 0xff 0xe0。我觉得不对...

标签: image pdf base64 xsl-fo apache-fop


【解决方案1】:

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

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

【讨论】:

  • 关于 SO 的最佳问题是 OP 自己找到答案的问题。
猜你喜欢
  • 2015-08-16
  • 2018-11-08
  • 2019-08-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-11-23
  • 2013-10-28
相关资源
最近更新 更多