XSL FO从服务器获取图像

时间:2019-01-30 11:07:41

标签: spring xslt apache-fop

我使用的是FOP 2.1版。我有一个xsl fo模板,我想在其中显示图像:

 <xsl:variable name="ImagePath" select="defaultImageUrl"/>
 <fo:external-graphic src="{$ImagePath}" content-width="scale-down-to-fit" width="100%"/>

某些图像的Web地址如下:

https://upload.wikimedia.org/wikipedia/commons/thumb/b/b1/Tulipa_biflora_UME.jpg/800px-Tulipa_biflora_UME.jpg

但是其他图像来自我的网络服务器,其地址来自:

https://localhost:4200/api/download/image/?fixedPrice=true&amp;productId=1329&amp;fileId=1304

这是对端点的响应:

public ResponseEntity<byte[]> getFileAsResponseEntity(@RequestParam boolean fixedPrice, @RequestParam long productId, @RequestParam long fileId) throws IOException, SQLException {
    HttpHeaders headers = new HttpHeaders();
    FileDownload fileDownload = productService.getProductFile(productId, fileId, fixedPrice);
    headers.setCacheControl(CacheControl.noCache().getHeaderValue());
    String n = fileDownload.getFileName().toLowerCase();
    if (fileDownload.getFileTypeEnum().equals(FileTypeEnum.PICTURE) && (n.contains(".jpeg") || n.contains("jpg"))) {
        headers.setContentType(MediaType.IMAGE_JPEG);
    } else if (fileDownload.getFileTypeEnum().equals(FileTypeEnum.PICTURE) && (n.contains(".png"))) {
        headers.setContentType(MediaType.IMAGE_PNG);
    } else if (fileDownload.getFileTypeEnum().equals(FileTypeEnum.PICTURE) && (n.contains(".gif"))) {
        headers.setContentType(MediaType.IMAGE_GIF);
    }
    return new ResponseEntity<>(fileDownload.getByteArray(), headers, HttpStatus.OK);
}

fo:external-graphic是否可以接受这两个不同的URL?还是我需要做一些其他工作才能使它正常工作,因为当前当图像来自网络服务器时,生成的pdf文件中没有图像,只有空白。

编辑: 这是应该将XML转换为XSL到PDF的代码:

 byte[] xsl = IOUtils.toByteArray(this.getClass().getResourceAsStream("/browserDocument.xsl"));
    byte[] xml = getBrowserDocument(filter, clientId, representId, ecatMain, showImage, language);
    InputStream inStr = this.getClass().getResourceAsStream("/fop.xml");
    FopFactory fopFactory = FopFactory.newInstance(new java.net.URI("."), inStr);
    ByteArrayOutputStream out = new ByteArrayOutputStream();

    Fop fop = fopFactory.newFop(MimeConstants.MIME_PDF, out);

    javax.xml.transform.Source xsltSrc = new StreamSource(new ByteArrayInputStream(xsl));

    TransformerFactory transformerFactory = TransformerFactory.newInstance();
    Transformer transformer = transformerFactory.newTransformer(xsltSrc);

    String xmlStr = new String(xml, "UTF-8");
    xmlStr = xmlStr.replaceAll("<", "<");
    xmlStr = xmlStr.replaceAll(">", ">");

    javax.xml.transform.Source src = new StreamSource(new ByteArrayInputStream(xmlStr.getBytes("UTF-8")));
    Result res = new SAXResult(fop.getDefaultHandler());

    transformer.transform(src, res);

    return out.toByteArray();

我在日志文件中不断收到错误消息:

2019-01-30 16:07:48.300 ERROR 8424 --- [https-jsse-nio-8087-exec-3] org.apache.fop.apps.FOUserAgent          : Image not found. URI: https://localhost:4200/api/efront/secure/download/product/image/?fixedPrice=false&productId=2823&fileId=1756. (No context info available)

似乎正在调用URL,但未从中获取实际图像。也许图片标题有问题,或者FOUseragent被阻止了?

1 个答案:

答案 0 :(得分:0)

好吧,实现上述所有可能的逻辑并查看您的代码,我认为URIResolver将有助于摆脱这种情况,如下所示:

将其添加到您的代码中:fopFactory.setURIResolver(new ResolveURIForWebServer());

import javax.xml.transform.Source;
import javax.xml.transform.TransformerException;
import javax.xml.transform.URIResolver;

public class ResolveURIForWebServer implements URIResolver {

@Override
public Source resolve(String href, String baseURI) throws TransformerException {
    Source source = null;
    try {
        // CONVERT IMAGE TO INPUTSTREAM
        source = new StreamSource(InputStream);

    } catch (Exception e) {

    } finally {

    }
    return source;
}
}

希望有帮助。