将 .fo 文件转换为 .pdf
Transform a .fo file to .pdf
我正在尝试将 source.fo 文件转换为 result.pdf 文件 Java.
我正在寻找与调用 Apache Fop 命令相同的结果:
fop -fo source.fo -pdf result.pdf
也许这不是问题的重要事实,但我已经设法使用 freemarker 模板创建了这些 .fo 文件,并且在我在 Google 上使用 java 找到的所有示例中fop 库,他们使用 xml 和 xsl-fo 文件创建 pdf,我的情况不是这样。
我的 B 计划(我试图避免)是调用系统命令。
在此先感谢您的帮助。
A - 示例 Fo 文件
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<fo:root xmlns:fo="http://www.w3.org/1999/XSL/Format" language="EN">
<fo:layout-master-set>
<fo:simple-page-master margin-right="5mm" margin-left="5mm" margin-bottom="5mm" margin-top="5mm" page-width="210mm" page-height="297mm" master-name="A4-portrail">
<fo:region-body margin-bottom="20mm" margin-top="25mm"/>
<fo:region-before precedence="true" display-align="before" extent="25mm" region-name="xsl-region-before"/>
</fo:simple-page-master>
</fo:layout-master-set>
<fo:page-sequence master-reference="A4-portrail">
<fo:static-content flow-name="xsl-region-before">
<fo:table border-style="solid" border-width="0.4mm" border-color="black" font-size="10pt" width="100%" table-layout="fixed">
<fo:table-column column-width="proportional-column-width(20)"/>
<fo:table-column column-width="proportional-column-width(45)"/>
<fo:table-column column-width="proportional-column-width(20)"/>
<fo:table-body>
<fo:table-row>
<fo:table-cell padding-left="2mm" display-align="center" text-align="left">
<fo:block>1299Levent Divilioglu</fo:block>
</fo:table-cell>
<fo:table-cell display-align="center" text-align="center">
<fo:block font-size="150%">
<fo:basic-link external-destination="http://www.example.com">List of Month Records</fo:basic-link>
</fo:block>
<fo:block space-before="3mm"/>
</fo:table-cell>
<fo:table-cell padding-right="2mm" display-align="center" text-align="right">
<fo:block>19. Mayis Mah. No: 6 Kadikoy/Istanbul</fo:block>
<fo:block space-before="6mm" display-align="before">Page <fo:page-number/> . <fo:page-number-citation ref-id="end-of-document"/>
</fo:block>
</fo:table-cell>
</fo:table-row>
</fo:table-body>
</fo:table>
</fo:static-content>
<fo:flow reference-orientation="0" border-collapse="collapse" flow-name="xsl-region-body">
<fo:block>Records of Month</fo:block>
<fo:table space-after="5mm" display-align="center" text-align="center" border-style="solid" border-width="0.35mm" border-color="black" font-size="10pt" width="100%" table-layout="fixed">
<fo:table-column column-width="proportional-column-width(20)"/>
<fo:table-column column-width="proportional-column-width(30)"/>
<fo:table-column column-width="proportional-column-width(25)"/>
<fo:table-column column-width="proportional-column-width(50)"/>
<fo:table-body font-size="95%">
<fo:table-row height="8mm">
<fo:table-cell>
<fo:block>Date</fo:block>
</fo:table-cell>
<fo:table-cell>
<fo:block>Called Number</fo:block>
</fo:table-cell>
<fo:table-cell>
<fo:block>Price</fo:block>
</fo:table-cell>
<fo:table-cell>
<fo:block>Discount Type</fo:block>
</fo:table-cell>
</fo:table-row>
<fo:table-row>
<fo:table-cell>
<fo:block>03-10-2016:19:25:03</fo:block>
</fo:table-cell>
<fo:table-cell>
<fo:block>505-999-88-77</fo:block>
</fo:table-cell>
<fo:table-cell>
<fo:block>12.95</fo:block>
</fo:table-cell>
<fo:table-cell>
<fo:block>4</fo:block>
</fo:table-cell>
</fo:table-row>
<fo:table-row>
<fo:table-cell>
<fo:block>08-10-2016:20:13:35</fo:block>
</fo:table-cell>
<fo:table-cell>
<fo:block>507-666-42-23</fo:block>
</fo:table-cell>
<fo:table-cell>
<fo:block>24.45</fo:block>
</fo:table-cell>
<fo:table-cell>
<fo:block>1</fo:block>
</fo:table-cell>
</fo:table-row>
<fo:table-row>
<fo:table-cell>
<fo:block>15-10-2016:17:47:34</fo:block>
</fo:table-cell>
<fo:table-cell>
<fo:block>535-234-44-54</fo:block>
</fo:table-cell>
<fo:table-cell>
<fo:block>21.34</fo:block>
</fo:table-cell>
<fo:table-cell>
<fo:block>3</fo:block>
</fo:table-cell>
</fo:table-row>
<fo:table-row>
<fo:table-cell>
<fo:block>21-10-2016:21:31:23</fo:block>
</fo:table-cell>
<fo:table-cell>
<fo:block>542-323-43-58</fo:block>
</fo:table-cell>
<fo:table-cell>
<fo:block>51.35</fo:block>
</fo:table-cell>
<fo:table-cell>
<fo:block>2</fo:block>
</fo:table-cell>
</fo:table-row>
</fo:table-body>
</fo:table>
<fo:block id="end-of-document">
<fo:instream-foreign-object>
<svg xmlns="http://www.w3.org/2000/svg" version="1.1" height="150mm" width="200mm">
<path style="fill:yellow;stroke:red;stroke-width:2" d="M153 334 C153 334 151 334 151 334 C151 339 153 344 156 344 C164 344 171 339 171 334 C171 322 164 314 156 314 C142 314 131 322 131 334 C131 350 142 364 156 364 C175 364 191 350 191 334 C191 311 175 294 156 294 C131 294 111 311 111 334 C111 361 131 384 156 384 C186 384 211 361 211 334 C211 300 186 274 156 274"/>
</svg>
</fo:instream-foreign-object>
</fo:block>
</fo:flow>
</fo:page-sequence>
</fo:root>
B - 示例代码
package com.levent.fop;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import javax.xml.transform.Result;
import javax.xml.transform.Source;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.sax.SAXResult;
import javax.xml.transform.stream.StreamSource;
import org.apache.fop.apps.Fop;
import org.apache.fop.apps.FopFactory;
import org.apache.fop.apps.MimeConstants;
import org.xml.sax.SAXException;
public class ConvertFoToPdf {
public static void main(String[] args) throws SAXException, IOException, TransformerException {
hede();
}
public static void hede() throws SAXException, IOException, TransformerException {
// Step 1: Construct a FopFactory by specifying a reference to the configuration file
// (reuse if you plan to render multiple documents!)
// FopFactory fopFactory = FopFactory.newInstance(new File("D:/output/fop.xconf"));
FopFactory fopFactory = FopFactory.newInstance(new File(".").toURI());
// Step 2: Set up output stream.
// Note: Using BufferedOutputStream for performance reasons (helpful with FileOutputStreams).
OutputStream out = new BufferedOutputStream(new FileOutputStream(new File("D:/output/myfile.pdf")));
try {
// Step 3: Construct fop with desired output format
Fop fop = fopFactory.newFop(MimeConstants.MIME_PDF, out);
// Step 4: Setup JAXP using identity transformer
TransformerFactory factory = TransformerFactory.newInstance();
Transformer transformer = factory.newTransformer(); // identity transformer
// Step 5: Setup input and output for XSLT transformation
// Setup input stream
Source src = new StreamSource(new File("D:/output/input.fo"));
// Resulting SAX events (the generated FO) must be piped through to FOP
Result res = new SAXResult(fop.getDefaultHandler());
// Step 6: Start XSLT transformation and FOP processing
transformer.transform(src, res);
} finally {
//Clean-up
out.close();
}
}
}
C - 示例输出
我正在尝试将 source.fo 文件转换为 result.pdf 文件 Java.
我正在寻找与调用 Apache Fop 命令相同的结果:
fop -fo source.fo -pdf result.pdf
也许这不是问题的重要事实,但我已经设法使用 freemarker 模板创建了这些 .fo 文件,并且在我在 Google 上使用 java 找到的所有示例中fop 库,他们使用 xml 和 xsl-fo 文件创建 pdf,我的情况不是这样。
我的 B 计划(我试图避免)是调用系统命令。
在此先感谢您的帮助。
A - 示例 Fo 文件
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<fo:root xmlns:fo="http://www.w3.org/1999/XSL/Format" language="EN">
<fo:layout-master-set>
<fo:simple-page-master margin-right="5mm" margin-left="5mm" margin-bottom="5mm" margin-top="5mm" page-width="210mm" page-height="297mm" master-name="A4-portrail">
<fo:region-body margin-bottom="20mm" margin-top="25mm"/>
<fo:region-before precedence="true" display-align="before" extent="25mm" region-name="xsl-region-before"/>
</fo:simple-page-master>
</fo:layout-master-set>
<fo:page-sequence master-reference="A4-portrail">
<fo:static-content flow-name="xsl-region-before">
<fo:table border-style="solid" border-width="0.4mm" border-color="black" font-size="10pt" width="100%" table-layout="fixed">
<fo:table-column column-width="proportional-column-width(20)"/>
<fo:table-column column-width="proportional-column-width(45)"/>
<fo:table-column column-width="proportional-column-width(20)"/>
<fo:table-body>
<fo:table-row>
<fo:table-cell padding-left="2mm" display-align="center" text-align="left">
<fo:block>1299Levent Divilioglu</fo:block>
</fo:table-cell>
<fo:table-cell display-align="center" text-align="center">
<fo:block font-size="150%">
<fo:basic-link external-destination="http://www.example.com">List of Month Records</fo:basic-link>
</fo:block>
<fo:block space-before="3mm"/>
</fo:table-cell>
<fo:table-cell padding-right="2mm" display-align="center" text-align="right">
<fo:block>19. Mayis Mah. No: 6 Kadikoy/Istanbul</fo:block>
<fo:block space-before="6mm" display-align="before">Page <fo:page-number/> . <fo:page-number-citation ref-id="end-of-document"/>
</fo:block>
</fo:table-cell>
</fo:table-row>
</fo:table-body>
</fo:table>
</fo:static-content>
<fo:flow reference-orientation="0" border-collapse="collapse" flow-name="xsl-region-body">
<fo:block>Records of Month</fo:block>
<fo:table space-after="5mm" display-align="center" text-align="center" border-style="solid" border-width="0.35mm" border-color="black" font-size="10pt" width="100%" table-layout="fixed">
<fo:table-column column-width="proportional-column-width(20)"/>
<fo:table-column column-width="proportional-column-width(30)"/>
<fo:table-column column-width="proportional-column-width(25)"/>
<fo:table-column column-width="proportional-column-width(50)"/>
<fo:table-body font-size="95%">
<fo:table-row height="8mm">
<fo:table-cell>
<fo:block>Date</fo:block>
</fo:table-cell>
<fo:table-cell>
<fo:block>Called Number</fo:block>
</fo:table-cell>
<fo:table-cell>
<fo:block>Price</fo:block>
</fo:table-cell>
<fo:table-cell>
<fo:block>Discount Type</fo:block>
</fo:table-cell>
</fo:table-row>
<fo:table-row>
<fo:table-cell>
<fo:block>03-10-2016:19:25:03</fo:block>
</fo:table-cell>
<fo:table-cell>
<fo:block>505-999-88-77</fo:block>
</fo:table-cell>
<fo:table-cell>
<fo:block>12.95</fo:block>
</fo:table-cell>
<fo:table-cell>
<fo:block>4</fo:block>
</fo:table-cell>
</fo:table-row>
<fo:table-row>
<fo:table-cell>
<fo:block>08-10-2016:20:13:35</fo:block>
</fo:table-cell>
<fo:table-cell>
<fo:block>507-666-42-23</fo:block>
</fo:table-cell>
<fo:table-cell>
<fo:block>24.45</fo:block>
</fo:table-cell>
<fo:table-cell>
<fo:block>1</fo:block>
</fo:table-cell>
</fo:table-row>
<fo:table-row>
<fo:table-cell>
<fo:block>15-10-2016:17:47:34</fo:block>
</fo:table-cell>
<fo:table-cell>
<fo:block>535-234-44-54</fo:block>
</fo:table-cell>
<fo:table-cell>
<fo:block>21.34</fo:block>
</fo:table-cell>
<fo:table-cell>
<fo:block>3</fo:block>
</fo:table-cell>
</fo:table-row>
<fo:table-row>
<fo:table-cell>
<fo:block>21-10-2016:21:31:23</fo:block>
</fo:table-cell>
<fo:table-cell>
<fo:block>542-323-43-58</fo:block>
</fo:table-cell>
<fo:table-cell>
<fo:block>51.35</fo:block>
</fo:table-cell>
<fo:table-cell>
<fo:block>2</fo:block>
</fo:table-cell>
</fo:table-row>
</fo:table-body>
</fo:table>
<fo:block id="end-of-document">
<fo:instream-foreign-object>
<svg xmlns="http://www.w3.org/2000/svg" version="1.1" height="150mm" width="200mm">
<path style="fill:yellow;stroke:red;stroke-width:2" d="M153 334 C153 334 151 334 151 334 C151 339 153 344 156 344 C164 344 171 339 171 334 C171 322 164 314 156 314 C142 314 131 322 131 334 C131 350 142 364 156 364 C175 364 191 350 191 334 C191 311 175 294 156 294 C131 294 111 311 111 334 C111 361 131 384 156 384 C186 384 211 361 211 334 C211 300 186 274 156 274"/>
</svg>
</fo:instream-foreign-object>
</fo:block>
</fo:flow>
</fo:page-sequence>
</fo:root>
B - 示例代码
package com.levent.fop;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import javax.xml.transform.Result;
import javax.xml.transform.Source;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.sax.SAXResult;
import javax.xml.transform.stream.StreamSource;
import org.apache.fop.apps.Fop;
import org.apache.fop.apps.FopFactory;
import org.apache.fop.apps.MimeConstants;
import org.xml.sax.SAXException;
public class ConvertFoToPdf {
public static void main(String[] args) throws SAXException, IOException, TransformerException {
hede();
}
public static void hede() throws SAXException, IOException, TransformerException {
// Step 1: Construct a FopFactory by specifying a reference to the configuration file
// (reuse if you plan to render multiple documents!)
// FopFactory fopFactory = FopFactory.newInstance(new File("D:/output/fop.xconf"));
FopFactory fopFactory = FopFactory.newInstance(new File(".").toURI());
// Step 2: Set up output stream.
// Note: Using BufferedOutputStream for performance reasons (helpful with FileOutputStreams).
OutputStream out = new BufferedOutputStream(new FileOutputStream(new File("D:/output/myfile.pdf")));
try {
// Step 3: Construct fop with desired output format
Fop fop = fopFactory.newFop(MimeConstants.MIME_PDF, out);
// Step 4: Setup JAXP using identity transformer
TransformerFactory factory = TransformerFactory.newInstance();
Transformer transformer = factory.newTransformer(); // identity transformer
// Step 5: Setup input and output for XSLT transformation
// Setup input stream
Source src = new StreamSource(new File("D:/output/input.fo"));
// Resulting SAX events (the generated FO) must be piped through to FOP
Result res = new SAXResult(fop.getDefaultHandler());
// Step 6: Start XSLT transformation and FOP processing
transformer.transform(src, res);
} finally {
//Clean-up
out.close();
}
}
}