I have problems trying to generate an image with qr code via barcode4j library. I've read the following guide but that didn't work out. So here is my code:
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();
}
}
}
template.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>
When I try to launch the application with
kirill@kirill:~/work/source/fop$ java -jar target/fopqr-1.0-SNAPSHOT-jar-with-dependencies.jar
I get
Mar 07, 2015 10:42:02 PM org.apache.fop.events.LoggingEventListener processEvent WARNING: Unknown formatting object "{http://barcode4j.krysalis.org/ns}barcode" encountered (a child of fo:instream-foreign-object}. (See position 13:99) Mar 07, 2015 10:42:02 PM org.apache.fop.events.LoggingEventListener processEvent WARNING: Unknown formatting object "{http://barcode4j.krysalis.org/ns}qr" encountered (a child of barcode}. (See position 14:31) Mar 07, 2015 10:42:02 PM org.apache.fop.events.LoggingEventListener processEvent SEVERE: The intrinsic dimensions of an instream-foreign-object could not be determined. (See position 12:47)
What am I doing wrong?
UPDATE: I also want to add that I read FAQ: 4.1. The FOP extension fails. But as you can see I added all required libraries to my pom.xml file.