我在尝试通过barcode4j 库生成带有二维码的图像时遇到问题。我已阅读以下指南,但没有成功。所以这是我的代码:
pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>fopqr</groupId>
<artifactId>fopqr</artifactId>
<version>1.0-SNAPSHOT</version>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.6</source>
<target>1.6</target>
<encoding>UTF-8</encoding>
</configuration>
</plugin>
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<configuration>
<archive>
<manifest>
<mainClass>Main</mainClass>
</manifest>
</archive>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
</configuration>
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
<groupId>org.apache.xmlgraphics</groupId>
<artifactId>fop</artifactId>
<version>1.0</version>
</dependency>
<dependency>
<groupId>net.sf.barcode4j</groupId>
<artifactId>barcode4j</artifactId>
<version>2.1</version>
</dependency>
<dependency>
<groupId>net.sf.barcode4j</groupId>
<artifactId>barcode4j-fop-ext</artifactId>
<version>2.1</version>
</dependency>
<dependency>
<groupId>com.google.zxing</groupId>
<artifactId>core</artifactId>
<version>3.2.0</version>
</dependency>
</dependencies>
</project>
2.main.java
import org.apache.fop.apps.Fop;
import org.apache.fop.apps.FopFactory;
import org.apache.xmlgraphics.util.MimeConstants;
import javax.xml.transform.Result;
import javax.xml.transform.Source;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.sax.SAXResult;
import javax.xml.transform.stream.StreamSource;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStream;
public class Main {
public static void main(String[] args) throws Exception{
FopFactory fopFactory = FopFactory.newInstance();
OutputStream out = new BufferedOutputStream(new FileOutputStream(new File("/home/user/fop.pdf")));
try {
Fop fop = fopFactory.newFop(MimeConstants.MIME_PDF, out);
TransformerFactory factory = TransformerFactory.newInstance();
Transformer transformer = factory.newTransformer(); // identity transformer
Source src = new StreamSource(new File("/home/user/template.xsl"));
Result res = new SAXResult(fop.getDefaultHandler());
transformer.transform(src, res);
}
finally {
out.close();
}
}
}
模板.xsl
<?xml version="1.0" encoding="UTF-8"?>
<fo:root xmlns:fo="http://www.w3.org/1999/XSL/Format" font-family="sans-serif" font-size="10pt">
<fo:layout-master-set>
<fo:simple-page-master master-name="A4" page-height="29.7cm" page-width="21cm" margin-top="2cm" margin-bottom="0.4cm" margin-left="2cm" margin-right="2cm">
<fo:region-body margin-bottom="2.3cm"/>
<fo:region-after extent="2.2cm"/>
</fo:simple-page-master>
</fo:layout-master-set>
<fo:page-sequence master-reference="A4" language="en">
<fo:flow flow-name="xsl-region-body">
<fo:block>
<fo:instream-foreign-object>
<bc:barcode xmlns:bc="http://barcode4j.krysalis.org/ns" message="hello world">
<bc:qr/>
</bc:barcode>
</fo:instream-foreign-object>
</fo:block>
</fo:flow>
</fo:page-sequence>
</fo:root>
当我尝试启动应用程序时
kirill@kirill:~/work/source/fop$ java -jar target/fopqr-1.0-SNAPSHOT-jar-with-dependencies.jar
我明白了
2015 年 3 月 7 日晚上 10:42:02 org.apache.fop.events.LoggingEventListener processEvent 警告:遇到未知格式对象“{http://barcode4j.krysalis.org/ns}barcode”(fo:instream-foreign-object} 的子项。(参见位置 13:99) 2015 年 3 月 7 日晚上 10:42:02 org.apache.fop.events.LoggingEventListener processEvent 警告:遇到未知格式对象“{http://barcode4j.krysalis.org/ns}qr”(barcode 的子项}。(参见位置 14:31) 2015 年 3 月 7 日晚上 10:42:02 org.apache.fop.events.LoggingEventListener processEvent 严重:无法确定 instream-foreign-object 的内在尺寸。(见位置 12:47)
我究竟做错了什么?
更新:我还想补充一点,我阅读了常见问题解答:4.1。FOP 扩展失败。但正如您所见,我将所有必需的库添加到我的 pom.xml 文件中。