2017-03-06 19 views
1

我正在使用Apache FOP 2.1,JAXB和XSLT 1.0生成PDF。該模板有一些嵌入式SVG。 PDF呈現沒有任何問題,但FO正在拋出混亂我的日誌的錯誤。XSL-FO:「無法設置基本URL」用於嵌入式svg

設置:

XslProcessor.java

public XslProcessor(File f) { 
    try { 
     URIResolverAdapter uriResolverAdapter = new URIResolverAdapter(new UserAgentUriResolver()); 
     FopFactoryBuilder builder = new FopFactoryBuilder(URI.create("/"), uriResolverAdapter); 
     DefaultConfigurationBuilder defaultConfigurationBuilder = new DefaultConfigurationBuilder(); 
     builder.setConfiguration(defaultConfigurationBuilder.buildFromFile("config/pdf/fop.xconf")); 
     fopFactory = builder.build(); 
     transformerFactory = TransformerFactory.newInstance(); 
     transformFile = f; 
    } catch (IOException | SAXException | ConfigurationException e) { 
     throw new RuntimeException(e); 
    } 
} 

public synchronized void process(InputStream is, OutputStream os) { 
    try { 
     try(BufferedOutputStream bufferedOut = new BufferedOutputStream(os)) { 
      Fop fop = fopFactory.newFop(MimeConstants.MIME_PDF, bufferedOut); 
      Source xslt = new StreamSource(transformFile); 
      Transformer transformer = transformerFactory.newTransformer(xslt); 
      Source source = new StreamSource(is); 
      Result result = new SAXResult(fop.getDefaultHandler()); 
      transformer.transform(source, result); 
     } 
     os.flush(); 
     os.close(); 
    } catch (IOException | FOPException | TransformerException e) { 
     throw new RuntimeException(e); 
    } 
} 

UserAgentUriResolver.java

public class UserAgentUriResolver implements URIResolver { 

private static final String USER_AGENT = "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/50.0.2661.86 Safari/537.36"; 
private static final String REFERRER = "https://www.google.com"; 

@Override 
public Source resolve(String href, String base) throws TransformerException { 
    try { 
     URL url = new URL(href); 
     URLConnection connection = url.openConnection(); 
     connection.setRequestProperty("User-Agent", USER_AGENT); 
     connection.setRequestProperty("Referrer", REFERRER); 
     return new StreamSource(connection.getInputStream()); 
    } catch (IOException e) { 
     return new StreamSource(new ByteArrayInputStream(new byte[0])); 
    } 
} 
} 

樣本模板:

<?xml version="1.0" encoding="UTF-8" ?> 
<xsl:stylesheet version="1.0" 
       xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
       xmlns:fo="http://www.w3.org/1999/XSL/Format"> 

    <xsl:template match="/"> 
     <fo:root> 
      <fo:layout-master-set> 
       <fo:simple-page-master master-name="page" 
             page-height="29.7cm" 
             page-width="21cm"> 
        <fo:region-body/> 
        <fo:region-after extent="1.25cm"/> 
       </fo:simple-page-master> 
       <fo:page-sequence-master master-name="all"> 
        <fo:repeatable-page-master-alternatives> 
         <fo:conditional-page-master-reference master-reference="page"/> 
        </fo:repeatable-page-master-alternatives> 
       </fo:page-sequence-master> 
      </fo:layout-master-set> 
      <fo:page-sequence master-reference="all"> 
       <fo:static-content flow-name="xsl-region-after"> 
        <fo:block> 
         <fo:instream-foreign-object> 
          <!-- SVG here! --> 
          <svg xmlns="http://www.w3.org/2000/svg" xml:base="http://www.example.com/"> 
           <rect height="20" width="100%" fill="#ababab"></rect> 
          </svg> 
         </fo:instream-foreign-object> 
        </fo:block> 
       </fo:static-content> 
       <fo:flow flow-name="xsl-region-body"> 
        <fo:block> 
         <xsl:apply-templates/> 
        </fo:block> 
       </fo:flow> 
      </fo:page-sequence> 
     </fo:root> 
    </xsl:template> 
</xsl:stylesheet> 

一旦在模板中每一個SVG ,我得到

org.apache.fop.fo.FONode:對SVG無法設置基本URL

java.lang.IllegalArgumentException異常:URI不在java.net.URI.toURL絕對 ( URI.java:1088)〜[na:1.8.0_91] at org.apache.fop.fo.extensions.svg.SVGElement.getDimension(SVGElement.java:77)〜[fop-2.1.jar:na] at org.apache.fop.fo.flow.stream.InstreamForeignObject.getIntrinsicWidth(InstreamForeignObject.java:125)[fop-2.1.jar:na] at org.apache.fop.fo.flow.stream.InstreamForeignObject.getIntrinsicWidth(InstreamForeignObject.java:125 )[fop-2.1.jar:na] at org.apache.fop.layoutmgr.in line.AbstractGraphicsLayoutManager.getInlineArea(AbstractGraphicsLayoutManager.java:60)[FOP-2.1.jar:NA] 在...

+2

不知道這是否是任何解決方案,但這是可怕的代碼。您正在創建對象,然後運行測試以查看是否需要任何內容​​(好)或沒有任何內容(違反標準),爲什麼您的測試不圍繞您是否創建流內異物? –

+0

你能編輯你的問題來包含一個帶有所有屬性*的結果svg *的例子嗎?我只是試着用FOP 2.1來處理從你的代碼派生的一個fo文件,它根本沒有任何警告,所以我懷疑其他屬性有問題。 – lfurini

+0

可能不是(@someValue)返回false,導致像這樣的結構在某些引擎中可能導致問題。不知道上面的錯誤,從來沒有測試過它。 –

回答

0

嘗試添加​​屬性到<svg>元件。

​​(https://www.w3.org/TR/xmlbase/)比XSL 1.1建議書更新。這不應該的問題,因爲你是<svg>元素反正被添加到非XSL和​​是SVG 1.1:https://www.w3.org/TR/SVG/struct.html#Core.attrib

FOP爲什麼不使用基本URI的FO文件爲基礎的對於SVG -uri是一個謎,所以這可能或可能無法正常工作。

+0

這對我無效。我試過'xml:base =「http://www.example.com」'和'xml:base =「/」'。我使用不正確的值嗎? – nmatte