gpt4 book ai didi

java - 将 .fo 文件转换为 .pdf

转载 作者:行者123 更新时间:2023-11-30 02:49:25 26 4
gpt4 key购买 nike

我正在尝试将 source.fo 文件转换为 Java 中的 result.pdf 文件。

我正在寻找与调用 Apache Fop 命令相同的结果:

fop -fo source.fo -pdf result.pdf

也许对于这个问题来说并不是一个重要的事实,但我已经设法使用 freemarker 模板创建了这些 .fo 文件,并且在我使用 java fop 库在 Google 上找到的所有示例中,他们使用 xml 创建了 pdf和一个 xsl-fo 文件,这不是我的情况。

我的 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 - 示例输出

enter image description here

关于java - 将 .fo 文件转换为 .pdf,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39174167/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com